#include <bits/stdc++.h>
using namespace std;
vector<pair<int,long long> > adj[300005];
set<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-i.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();
}
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
247 ms |
39764 KB |
Output is correct |
2 |
Correct |
190 ms |
39504 KB |
Output is correct |
3 |
Correct |
168 ms |
38608 KB |
Output is correct |
4 |
Correct |
112 ms |
51284 KB |
Output is correct |
5 |
Correct |
119 ms |
60756 KB |
Output is correct |
6 |
Incorrect |
123 ms |
30804 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21336 KB |
Output is correct |
2 |
Correct |
3 ms |
21340 KB |
Output is correct |
3 |
Correct |
3 ms |
21340 KB |
Output is correct |
4 |
Correct |
4 ms |
21548 KB |
Output is correct |
5 |
Incorrect |
4 ms |
21852 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21336 KB |
Output is correct |
2 |
Correct |
3 ms |
21340 KB |
Output is correct |
3 |
Correct |
3 ms |
21340 KB |
Output is correct |
4 |
Correct |
4 ms |
21548 KB |
Output is correct |
5 |
Incorrect |
4 ms |
21852 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21336 KB |
Output is correct |
2 |
Correct |
3 ms |
21340 KB |
Output is correct |
3 |
Correct |
3 ms |
21340 KB |
Output is correct |
4 |
Correct |
4 ms |
21548 KB |
Output is correct |
5 |
Incorrect |
4 ms |
21852 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
247 ms |
39764 KB |
Output is correct |
2 |
Correct |
190 ms |
39504 KB |
Output is correct |
3 |
Correct |
168 ms |
38608 KB |
Output is correct |
4 |
Correct |
112 ms |
51284 KB |
Output is correct |
5 |
Correct |
119 ms |
60756 KB |
Output is correct |
6 |
Incorrect |
123 ms |
30804 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |