Jobs

시간 제한2초메모리 제한1024 MB

요약
각 작업에는 선행 작업이 있고 이익이 음수일 수도 있으며, 잔액이 음수가 되지 않도록 작업을 골라 최대 이익을 구한다.
난이도

어려움10점 중 8점

유형
그리디, 트리, 힙, 정렬
정답자
아직 제출이 없습니다

문제

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 11 to NN.

Completing job ii will make you a profit of x_ix\_i euros. The profit may also be negative (x_i<0x\_i < 0).

Some jobs depend on another job. That is, there may be a job numbered p_ip\_i that must be completed before the ii-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=0p\_i = 0, the ii-th job has no dependency.

You currently have ss 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 NN jobs in a selected order.

입력

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

Then, NN lines follow. The ii-th of them contains two integers x_ix\_i and p_ip\_i – the profit and the number of the prerequisite job for the ii-th job, respectively. If p_i=0p\_i = 0, the ii-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⋅1051 ≤ N ≤ 3 \cdot 10^5
  • 0≤s≤10180 ≤ s ≤ 10^{18}
  • −109≤x_i≤109-10^9 ≤ x\_i ≤ 10^9 (for all 1≤i≤N1 ≤ i ≤ N)
  • 0≤p_i<i0 ≤ p\_i < i (for all 1≤i≤N1 ≤ i ≤ N)

예제1

  1. 예제 1

    입력
    6 1
    3 0
    -3 1
    -5 0
    2 1
    6 3
    -4 5
    
    예상 출력
    6