#P10759. [BalticOI 2024] Jobs

[BalticOI 2024] Jobs

Background

Translated from BalticOI 2024 Day1 T1

Problem Description

Currently, you can choose from NN one-time jobs numbered from 11 to NN.

Completing job ii will bring you a profit of xx euros, and of course, the profit may also be negative.

Some jobs depend on another job. That is, there may be a job with index pp that must be completed before job ii can be started. Therefore, if a high-profit job depends on a job with negative profit, the overall gain may look smaller.

If p=0p = 0, then job ii has no dependency.

You currently have ss euros. You can decide which jobs to do and in what order, as long as you respect the dependencies. In addition, the amount of money you have must never become negative at any time.

Please compute the maximum profit you can obtain by choosing and completing these NN jobs in some order (and possibly leaving some jobs undone)。

Input Format

The first line contains two integers NN and ss, representing the number of jobs and the amount of money you initially have.

The next NN lines each contain two integers xx and pp, representing the profit of job ii and the index of its prerequisite job. If p=0p = 0, then job ii has no dependency。

Output Format

Your program should output a single integer: the maximum profit you can obtain。

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

Hint

Choose jobs 1,4,3,51,4,3,5 respectively, and the total profit is 3+25+6=63+2-5+6 = 6.

Subtask ID Special Property Score
11 s=1018s = 10^{18} 1111
22 N2000N \leq 2000 and satisfies Property A 1414
33 Satisfies Property A 1515
44 N2000N \leq 2000 2929
55 No special property 3131
  • Property A: each pip_i is either 00 or i1i-1.

For all testdata, 1N3×1051 \leq N \leq 3 \times 10^5, 0s10180 \leq s \leq 10^{18}, 109xi109-10^9 \leq x_i \leq 10^9, 0pi<i0 \leq p_i < i.

Translated by ChatGPT 5