시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB102373241.558%

문제

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_i$ euros. The profit may also be negative ($x_i < 0$).

Some jobs depend on another job. That is, there may be a job numbered $p_i$ 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_i = 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.

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

입력

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_i$ and $p_i$ – the profit and the number of the prerequisite job for the $i$-th job, respectively. If $p_i = 0$, the $i$-th job does not have a job dependency.

출력

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

제한

  • $1 ≤ N ≤ 3 \cdot 10^5$
  • $0 ≤ s ≤ 10^{18}$
  • $-10^9 ≤ x_i ≤ 10^9$ (for all $1 ≤ i ≤ N$)
  • $0 ≤ p_i < i$ (for all $1 ≤ i ≤ N$)

서브태스크

번호배점제한
111

$s = 10^{18}$.

214

$N ≤ 2000$ and for all jobs, either $p_i = 0$, or $p_i = i - 1$.

315

For all jobs, either $p_i = 0$, or $p_i = i - 1$.

429

$N ≤ 2000$.

531

No additional constraints.

예제 입력 1

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

예제 출력 1

6

To maximize profit, you should pick jobs $1$, $4$, $3$ and $5$ in the following order:

  • Job $1$: money $1 \rightarrow 4$,
  • Job $4$ (prerequisite $1$ complete): money $4 \rightarrow 6$,
  • Job $3$: money $6 \rightarrow 1$,
  • Job $5$ (prerequisite $3$ complete): money $1 \rightarrow 7$.

Overall, the total profit is $7 - 1 = 6$ (current money minus starting money).

채점 및 기타 정보

  • 예제는 채점하지 않는다.
  • 이 문제의 채점 우선 순위는 2이다.