#ifdef local
#define debug(x) cout<<#x<<" "<<x<<endl;
#else
#define debug(x)
#endif
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pl;
const ll mod=1e9+7,lim=2e5+5,bitlim=15,inf=1e9;
vector<ll>adjl[lim];
ll pwr[lim],cost[lim];
map<ll,ll> dp(ll pos){
map<ll,ll>ret;
if(adjl[pos].size() == 0){
ret[0] = 0;
ret[pwr[pos] + 1] = cost[pos];
}
else{
vector<map<ll,ll> >vv;
for(auto u:adjl[pos]){
vv.push_back(dp(u));
}
sort(vv.begin(),vv.end(),[](map<ll,ll> a,map<ll,ll> b){
return a.size()<b.size();
});
ret = vv.back();
vv.pop_back();
for(auto x:vv){
for(auto p:x){
ll k,v;
tie(k,v)=p;
ret[k]+=v;
}
}
ret[0] += cost[pos];
ret[pwr[pos] + 1] += cost[pos];
auto it=ret.upper_bound(pwr[pos]);
ll rem=cost[pos];
while(rem>0){
it--;
if(rem<=it->second)it->second-=rem,rem=0;
else rem-=it->second,it=ret.erase(it);
}
}
return ret;
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(NULL);
ll n;
cin>>n;
for(int i=1;i<=n;i++){
ll a;
cin>>a>>pwr[i]>>cost[i];
if(i!=a)adjl[a].push_back(i);
}
cout<<dp(1).begin()->second<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
18 ms |
5716 KB |
Output is correct |
6 |
Correct |
12 ms |
5324 KB |
Output is correct |
7 |
Correct |
9 ms |
5324 KB |
Output is correct |
8 |
Correct |
16 ms |
5744 KB |
Output is correct |
9 |
Correct |
12 ms |
5284 KB |
Output is correct |
10 |
Correct |
10 ms |
5328 KB |
Output is correct |
11 |
Correct |
8 ms |
5196 KB |
Output is correct |
12 |
Correct |
34 ms |
6348 KB |
Output is correct |
13 |
Correct |
9 ms |
6308 KB |
Output is correct |
14 |
Correct |
32 ms |
5888 KB |
Output is correct |
15 |
Correct |
20 ms |
5792 KB |
Output is correct |
16 |
Correct |
15 ms |
5708 KB |
Output is correct |
17 |
Correct |
12 ms |
5284 KB |
Output is correct |
18 |
Correct |
8 ms |
5324 KB |
Output is correct |
19 |
Correct |
372 ms |
6036 KB |
Output is correct |
20 |
Correct |
24 ms |
5708 KB |
Output is correct |
21 |
Correct |
8 ms |
5708 KB |
Output is correct |
22 |
Correct |
16 ms |
6396 KB |
Output is correct |
23 |
Correct |
14 ms |
6124 KB |
Output is correct |
24 |
Correct |
358 ms |
6304 KB |
Output is correct |
25 |
Correct |
20 ms |
6220 KB |
Output is correct |
26 |
Correct |
632 ms |
7000 KB |
Output is correct |
27 |
Correct |
411 ms |
6076 KB |
Output is correct |
28 |
Correct |
438 ms |
6272 KB |
Output is correct |
29 |
Correct |
495 ms |
6592 KB |
Output is correct |
30 |
Correct |
297 ms |
6500 KB |
Output is correct |
31 |
Correct |
290 ms |
6460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
18 ms |
5716 KB |
Output is correct |
6 |
Correct |
12 ms |
5324 KB |
Output is correct |
7 |
Correct |
9 ms |
5324 KB |
Output is correct |
8 |
Correct |
16 ms |
5744 KB |
Output is correct |
9 |
Correct |
12 ms |
5284 KB |
Output is correct |
10 |
Correct |
10 ms |
5328 KB |
Output is correct |
11 |
Correct |
8 ms |
5196 KB |
Output is correct |
12 |
Correct |
34 ms |
6348 KB |
Output is correct |
13 |
Correct |
9 ms |
6308 KB |
Output is correct |
14 |
Correct |
32 ms |
5888 KB |
Output is correct |
15 |
Correct |
20 ms |
5792 KB |
Output is correct |
16 |
Correct |
15 ms |
5708 KB |
Output is correct |
17 |
Correct |
12 ms |
5284 KB |
Output is correct |
18 |
Correct |
8 ms |
5324 KB |
Output is correct |
19 |
Correct |
372 ms |
6036 KB |
Output is correct |
20 |
Correct |
24 ms |
5708 KB |
Output is correct |
21 |
Correct |
8 ms |
5708 KB |
Output is correct |
22 |
Correct |
16 ms |
6396 KB |
Output is correct |
23 |
Correct |
14 ms |
6124 KB |
Output is correct |
24 |
Correct |
358 ms |
6304 KB |
Output is correct |
25 |
Correct |
20 ms |
6220 KB |
Output is correct |
26 |
Correct |
632 ms |
7000 KB |
Output is correct |
27 |
Correct |
411 ms |
6076 KB |
Output is correct |
28 |
Correct |
438 ms |
6272 KB |
Output is correct |
29 |
Correct |
495 ms |
6592 KB |
Output is correct |
30 |
Correct |
297 ms |
6500 KB |
Output is correct |
31 |
Correct |
290 ms |
6460 KB |
Output is correct |
32 |
Correct |
18 ms |
5720 KB |
Output is correct |
33 |
Correct |
1123 ms |
34876 KB |
Output is correct |
34 |
Correct |
600 ms |
17568 KB |
Output is correct |
35 |
Correct |
1052 ms |
33668 KB |
Output is correct |
36 |
Correct |
556 ms |
17352 KB |
Output is correct |
37 |
Correct |
264 ms |
16916 KB |
Output is correct |
38 |
Correct |
255 ms |
16724 KB |
Output is correct |
39 |
Execution timed out |
2071 ms |
60444 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
18 ms |
5716 KB |
Output is correct |
6 |
Correct |
12 ms |
5324 KB |
Output is correct |
7 |
Correct |
9 ms |
5324 KB |
Output is correct |
8 |
Correct |
16 ms |
5744 KB |
Output is correct |
9 |
Correct |
12 ms |
5284 KB |
Output is correct |
10 |
Correct |
10 ms |
5328 KB |
Output is correct |
11 |
Correct |
8 ms |
5196 KB |
Output is correct |
12 |
Correct |
34 ms |
6348 KB |
Output is correct |
13 |
Correct |
9 ms |
6308 KB |
Output is correct |
14 |
Correct |
32 ms |
5888 KB |
Output is correct |
15 |
Correct |
20 ms |
5792 KB |
Output is correct |
16 |
Correct |
15 ms |
5708 KB |
Output is correct |
17 |
Correct |
12 ms |
5284 KB |
Output is correct |
18 |
Correct |
8 ms |
5324 KB |
Output is correct |
19 |
Correct |
372 ms |
6036 KB |
Output is correct |
20 |
Correct |
24 ms |
5708 KB |
Output is correct |
21 |
Correct |
8 ms |
5708 KB |
Output is correct |
22 |
Correct |
16 ms |
6396 KB |
Output is correct |
23 |
Correct |
14 ms |
6124 KB |
Output is correct |
24 |
Correct |
358 ms |
6304 KB |
Output is correct |
25 |
Correct |
20 ms |
6220 KB |
Output is correct |
26 |
Correct |
632 ms |
7000 KB |
Output is correct |
27 |
Correct |
411 ms |
6076 KB |
Output is correct |
28 |
Correct |
438 ms |
6272 KB |
Output is correct |
29 |
Correct |
495 ms |
6592 KB |
Output is correct |
30 |
Correct |
297 ms |
6500 KB |
Output is correct |
31 |
Correct |
290 ms |
6460 KB |
Output is correct |
32 |
Correct |
18 ms |
5720 KB |
Output is correct |
33 |
Correct |
1123 ms |
34876 KB |
Output is correct |
34 |
Correct |
600 ms |
17568 KB |
Output is correct |
35 |
Correct |
1052 ms |
33668 KB |
Output is correct |
36 |
Correct |
556 ms |
17352 KB |
Output is correct |
37 |
Correct |
264 ms |
16916 KB |
Output is correct |
38 |
Correct |
255 ms |
16724 KB |
Output is correct |
39 |
Execution timed out |
2071 ms |
60444 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |