QOJ.ac

QOJ

Time Limit: 1.0 s Memory Limit: 960 MB Total points: 100 Hackable ✓

#15838. Max Cut Min Flow

统计

糟糕,洪水季节到了!山顶的水流向下游,淹没了山脚下的城镇。我们必须采取行动!

洪水的路径被建模为一条包含 $n$ 个检查点的直线路径,编号从 $1$ 到 $n$,其中检查点 $1$ 是山顶,检查点 $n$ 是城镇。所有水流都源自检查点 $1$,并按顺序经过检查点 $2$、$3$、$4 \dots$ 直至最终到达位于检查点 $n$ 的城镇。

你可以选择执行 $n - 1$ 个政府项目,编号从 $1$ 到 $n - 1$。项目 $i$ 涉及在检查点 $i$ 和 $i + 1$ 之间建造一道屏障。如果完成了该项目,那么原本会流经这些检查点的水流将被阻挡。

你的钱包初始有 $b$ 比索。项目的成本由一个整数数组 $x_1, x_2, x_3, \dots, x_{n-1}$ 表示。注意,这些整数可能是负数!为什么呢?嗯……

正如你所料,这些大型政府项目通常需要花钱。如果 $x_i \le 0$,那么完成项目 $i$ 会导致你的钱包减少 $|x_i|$ 比索(除非你的钱包里至少有 $|x_i|$ 比索,否则你不能选择执行该项目)。

另一方面,利用魔法,政府项目实际上可能为你赚钱!如果 $x_i > 0$,那么完成项目 $i$ 实际上会使你的钱包增加 $x_i$ 比索!太神奇了!

你可以选择执行任意数量的项目,顺序不限,但每个项目最多只能执行一次。你必须确保水无法从检查点 $1$ 流向检查点 $n$。在所有能达成此目标的方案中,你的钱包最终可能拥有的最大金额是多少?

如果任务无法完成,请说明。

输入格式

第一行包含两个空格分隔的整数 $n$ 和 $b$,分别表示检查点的数量和钱包中的初始金额。

第二行包含 $n - 1$ 个空格分隔的整数 $x_1, x_2, x_3, \dots, x_{n-1}$。

输出格式

如果任务可以完成,输出一个非负整数,表示在所有能达成目标的方案中,钱包最终可能拥有的最大金额。

如果任务无法完成,则输出 $-1$。

数据范围

$2 \le n \le 10^5$ $0 \le b \le 10^9$ 对于每个 $i$,$-10^4 \le x_i \le 10^4$。

样例

样例输入 1

6 7
3 -1 4 1 -5

样例输出 1

15

样例输入 2

5 4
-6 -7 -6 -7

样例输出 2

-1

说明

在第一个样例输入中,我们选择执行项目 $1$、$3$ 和 $4$,因此我们的钱包最终金额为 $7 + 3 + 4 + 1 = 15$。

在第二个样例输入中,我们的预算不足以执行任何政府项目。糟糕!希望镇上的人们能理解 :(

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.