QOJ.ac

QOJ

実行時間制限: 1 s メモリ制限: 1024 MB 満点: 100

#8707. 工作

統計

你经营着一家成功的企业,通过为客户完成工作来赚取利润。目前,你可以从 $N$ 个一次性工作中进行选择,这些工作编号为 $1$ 到 $N$。

完成工作 $i$ 将为你带来 $x_i$ 欧元的利润。利润也可能是负数($x_i < 0$)。

有些工作依赖于其他工作。也就是说,可能存在编号为 $p_i$ 的工作,必须在开始工作 $i$ 之前完成。因此,如果一项高利润的工作依赖于一项负利润的工作,那么它可能看起来就没有那么诱人了。如果 $p_i = 0$,则说明第 $i$ 项工作没有前置依赖。

你目前拥有 $s$ 欧元,可以决定完成哪些工作以及完成它们的顺序,前提是必须遵守依赖关系。此外,你拥有的资金在任何时候都不能变为负数。

任务

计算通过选择完成部分(可能为零)$N$ 项工作并按选定顺序执行所能获得的最大利润。

输入格式

第一行包含两个整数 $N$ 和 $s$,分别表示工作的数量和你初始拥有的资金金额。

接下来有 $N$ 行。第 $i$ 行包含两个整数 $x_i$ 和 $p_i$,分别表示第 $i$ 项工作的利润和其前置工作的编号。如果 $p_i = 0$,则说明第 $i$ 项工作没有前置依赖。

输出格式

你的程序应输出一个整数,即你所能获得的最大利润。

样例

输入格式 1

6 1
3 0
-3 1
-5 0
2 1
6 3
-4 5

输出格式 1

6

说明

为了最大化利润,你应该按以下顺序选择工作 1、4、3 和 5: 工作 1:资金 1 → 4 工作 4(工作 1 已完成):资金 4 → 6 工作 3:资金 6 → 1 工作 5(工作 3 已完成):资金 1 → 7

总利润为 7 - 1 = 6(当前资金减去初始资金)。

数据范围

  • $1 \le N \le 3 \cdot 10^5$
  • $0 \le s \le 10^{18}$
  • $-10^9 \le x_i \le 10^9$(对于所有 $1 \le i \le N$)
  • $0 \le p_i < i$(对于所有 $1 \le i \le N$)

子任务

编号 分值 其他约束
1 11 $s = 10^{18}$
2 14 $N \le 2000$ 且对于所有工作,满足 $p_i = 0$ 或 $p_i = i - 1$
3 15 对于所有工作,满足 $p_i = 0$ 或 $p_i = i - 1$
4 29 $N \le 2000$
5 31 无其他约束

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.