이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
vector<pair<int,long long> > adj[300005];
multiset<pair<long long,long long> > s[300005];
void greed(int x){
	for(auto i:adj[x]){
		greed(i.first);
		if(i.second>=0){
			s[i.first].insert({0,i.second});
		}
		else{
			pair<long long,long long> ins={-i.second,i.second};
			while(ins.second<0||(!s[i.first].empty()&&s[i.first].begin()->first<=ins.first)){
				if(s[i.first].empty()) break;
				ins.second+=s[i.first].begin()->second;
				ins.first=max(ins.first,s[i.first].begin()->first-ins.second);
				s[i.first].erase(s[i.first].begin());
			}
			if(ins.second>0) s[i.first].insert(ins);
			
		}
		if(s[i.first].size()>s[x].size()) swap(s[i.first],s[x]);
		for(auto j:s[i.first]) s[x].insert(j);
		s[i.first].clear();
	}
	//cout << x << ":\n";
	//for(auto i:s[x]) cout << i.first << ' ' << i.second << '\n';
}
int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	long long n,m;
	cin >> n >> m;
	for(int i=1; i<=n; i++){
		long long a,b;
		cin >> a >> b;
		adj[b].push_back({i,a});
	}
	greed(0);
	long long org=m;
	for(auto i:s[0]){
		if(m>=i.first) m+=i.second;
	}
	cout << m-org;
}
/*7 0
-100 0
-20 1
30 2
101 1
100 0
-101 5
1000 6*/
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |