jobs (BOI contest)
Time limit: 1 seconds | Memory limit: 256 MB | Task type: batch
Algorithms: greedy, data structures, small to large, priority queue, tree

![](_page_0_Picture_16.jpeg)

# Jobs

You have a successful business where you make money by completing jobs for your clients. Currently, you can choose from *N* one-time jobs, numbered from 1 to *N*.

Completing job *i* will make you a profit of *x<sub>i</sub>* euros. The profit may also be negative (*x<sub>i</sub>* < 0).

Some jobs depend on another job. That is, there may be a job numbered *p<sub>i</sub>* that must be completed before the *i*-th job can be started. Hence, a job with a large profit may be less attractive than it seems if it depends on a job with a negative profit. If *p<sub>i</sub>* = 0, the *i*-th job has no dependency.

You currently have *s* euros and can decide which jobs to do and in which order to do them, as long as the dependencies are respected. Moreover, the amount of money you own may not become negative at any point.

## Task

Calculate the maximum profit you can make by choosing to complete some (possibly none) of the *N* jobs in a selected order.

## Input

The first line contains two integers *N* and *s* – the number of jobs and the amount of money you initially own respectively.

Then, *N* lines follow. The *i*-th of them contains two integers *x<sub>i</sub>* and *p<sub>i</sub>* – the profit and the number of the prerequisite job for the *i*-th job, respectively. If *p<sub>i</sub>* = 0, the *i*-th job does not have a job dependency.

## Output

Your program should output a single integer – the maximum profit that you can make.

## Examples

| Input | Output | Explanation                                                                                                                                                                                                 |
|-------|--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 6 1   | 6      | To maximize profit, you should pick jobs 1, 4, 3 and 5 in the following order:                                                                                                                            |
| 3 0   |        |                                                                                                                                                                                                             |
| -3 1  |        | • Job 1: money 1 → 4,                                                                                                                                                                                       |
| -5 0  |        | • Job 4 (prerequisite 1 complete): money 4 → 6,                                                                                                                                                           |
| 2 1   |        | • Job 3: money 6 → 1,                                                                                                                                                                                       |
| 6 3   |        | • Job 5 (prerequisite 3 complete): money 1 → 7.                                                                                                                                                           |
| -4 5  |        | Overall, the total profit is 7 - 1 = 6 (current money minus starting money).                                                                                                                               |

## Constraints

- 1 ≤ *N* ≤ 3 ⋅ 10<sup>5</sup>
- 0 ≤ *s* ≤ 10<sup>18</sup>
- -10<sup>9</sup> ≤ *x<sub>i</sub>* ≤ 10<sup>9</sup> (for all 1 ≤ *i* ≤ *N*)
- 0 ≤ *p<sub>i</sub>* < *i* (for all 1 ≤ *i* ≤ *N*)

## Subtasks

| No. | Points | Additional constraints                                                |
|-----|--------|-----------------------------------------------------------------------|
| 1   | 11     | *s* = 10<sup>18</sup>.                                                       |
| 2   | 14     | *N* ≤ 2000 and for all jobs, either *p<sub>i</sub>* = 0, or *p<sub>i</sub>* = *i* - 1. |
| 3   | 15     | For all jobs, either *p<sub>i</sub>* = 0, or *p<sub>i</sub>* = *i* - 1.                   |
| 4   | 29     | *N* ≤ 2000.                                                       |
| 5   | 31     | No additional constraints.                                            |

Starter code:

Grader code:

Setup script:

Evaluation script: