This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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.first=max(ins.first,s[i.first].begin()->first-ins.second);
ins.second+=s[i.first].begin()->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... |