//tree subtasks: slope trick haha...
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pii pair<int,int>
#define f first
#define s second
int h[200005],c[200005];
vector<int> adjl[200005];
int delt[200005];
map<int,int> ss[200005];
map<int,int>::iterator it;
int nd;
void dfs(int node,int p=-1,bool r=false){
ss[node].clear();delt[node]=0;
for(int x:adjl[node]){
if(x==p)continue;
dfs(x,node);
}
nd=node;
for(int x:adjl[node]){
if(x==p)continue;
if(ss[x].size()>=ss[nd].size())nd=x;
}
if(nd!=node){
swap(ss[nd],ss[node]);
swap(delt[nd],delt[node]);
}
for(int x:adjl[node]){
if(x==p)continue;
for(pii hm:ss[x]){
ss[node][hm.f]+=hm.s;
}
ss[x].clear();
delt[node]+=delt[x];
}
//add node value...
delt[node]+=c[node];
if(r){
ss[node][h[node]]+=c[node];
ss[node][h[node]-1]-=c[node];
}else{
it=ss[node].upper_bound(h[node]-1);
int cur=0;
while(true){
if(it==ss[node].begin())break;
it--;
if(cur+it->s<=c[node]){
cur+=it->s;
it=ss[node].erase(it);
}else{
ss[node][it->f]=cur+it->s-c[node];
break;
}
}
ss[node][h[node]]+=c[node];
}
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(0);
int n;
cin>>n;
int a;
for(int i=1;i<=n;i++){
cin>>a>>h[i]>>c[i];
if(i>1)adjl[a].push_back(i);
}
dfs(1,-1,true);
int ans=delt[1],cur=delt[1];
for(it=ss[1].end();it!=ss[1].begin();){
it--;
cur-=it->s;
ans=min(ans,cur);
}
cout<<ans;
return 0;
}
/*
6
1 6 5
1 3 6
1 8 4
3 4 9
2 2 5
2 5 6
20
1 7 381792936
1 89 964898447
1 27 797240712
3 4 299745243
2 18 113181438
2 20 952129455
4 34 124298446
4 89 33466733
7 40 109601410
5 81 902931267
2 4 669879699
8 23 785166502
8 1 601717183
8 26 747624379
1 17 504589209
9 24 909134233
16 56 236448090
8 94 605526613
5 90 481898834
9 34 183442771
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14332 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14412 KB |
Output is correct |
5 |
Correct |
15 ms |
14820 KB |
Output is correct |
6 |
Correct |
13 ms |
14588 KB |
Output is correct |
7 |
Correct |
12 ms |
14764 KB |
Output is correct |
8 |
Correct |
17 ms |
15012 KB |
Output is correct |
9 |
Correct |
14 ms |
14780 KB |
Output is correct |
10 |
Correct |
13 ms |
14660 KB |
Output is correct |
11 |
Correct |
11 ms |
14664 KB |
Output is correct |
12 |
Correct |
13 ms |
15584 KB |
Output is correct |
13 |
Correct |
12 ms |
15580 KB |
Output is correct |
14 |
Correct |
13 ms |
15180 KB |
Output is correct |
15 |
Correct |
12 ms |
15120 KB |
Output is correct |
16 |
Correct |
14 ms |
15056 KB |
Output is correct |
17 |
Correct |
14 ms |
14740 KB |
Output is correct |
18 |
Correct |
11 ms |
14672 KB |
Output is correct |
19 |
Correct |
13 ms |
15272 KB |
Output is correct |
20 |
Correct |
13 ms |
15136 KB |
Output is correct |
21 |
Correct |
12 ms |
15128 KB |
Output is correct |
22 |
Correct |
12 ms |
14944 KB |
Output is correct |
23 |
Correct |
12 ms |
14884 KB |
Output is correct |
24 |
Correct |
14 ms |
15228 KB |
Output is correct |
25 |
Correct |
14 ms |
15300 KB |
Output is correct |
26 |
Correct |
12 ms |
15564 KB |
Output is correct |
27 |
Correct |
12 ms |
15256 KB |
Output is correct |
28 |
Correct |
13 ms |
15324 KB |
Output is correct |
29 |
Correct |
12 ms |
15588 KB |
Output is correct |
30 |
Correct |
13 ms |
15584 KB |
Output is correct |
31 |
Correct |
14 ms |
15584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14332 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14412 KB |
Output is correct |
5 |
Correct |
15 ms |
14820 KB |
Output is correct |
6 |
Correct |
13 ms |
14588 KB |
Output is correct |
7 |
Correct |
12 ms |
14764 KB |
Output is correct |
8 |
Correct |
17 ms |
15012 KB |
Output is correct |
9 |
Correct |
14 ms |
14780 KB |
Output is correct |
10 |
Correct |
13 ms |
14660 KB |
Output is correct |
11 |
Correct |
11 ms |
14664 KB |
Output is correct |
12 |
Correct |
13 ms |
15584 KB |
Output is correct |
13 |
Correct |
12 ms |
15580 KB |
Output is correct |
14 |
Correct |
13 ms |
15180 KB |
Output is correct |
15 |
Correct |
12 ms |
15120 KB |
Output is correct |
16 |
Correct |
14 ms |
15056 KB |
Output is correct |
17 |
Correct |
14 ms |
14740 KB |
Output is correct |
18 |
Correct |
11 ms |
14672 KB |
Output is correct |
19 |
Correct |
13 ms |
15272 KB |
Output is correct |
20 |
Correct |
13 ms |
15136 KB |
Output is correct |
21 |
Correct |
12 ms |
15128 KB |
Output is correct |
22 |
Correct |
12 ms |
14944 KB |
Output is correct |
23 |
Correct |
12 ms |
14884 KB |
Output is correct |
24 |
Correct |
14 ms |
15228 KB |
Output is correct |
25 |
Correct |
14 ms |
15300 KB |
Output is correct |
26 |
Correct |
12 ms |
15564 KB |
Output is correct |
27 |
Correct |
12 ms |
15256 KB |
Output is correct |
28 |
Correct |
13 ms |
15324 KB |
Output is correct |
29 |
Correct |
12 ms |
15588 KB |
Output is correct |
30 |
Correct |
13 ms |
15584 KB |
Output is correct |
31 |
Correct |
14 ms |
15584 KB |
Output is correct |
32 |
Correct |
14 ms |
14932 KB |
Output is correct |
33 |
Correct |
376 ms |
37004 KB |
Output is correct |
34 |
Correct |
264 ms |
25036 KB |
Output is correct |
35 |
Correct |
369 ms |
35740 KB |
Output is correct |
36 |
Correct |
275 ms |
24832 KB |
Output is correct |
37 |
Correct |
194 ms |
24376 KB |
Output is correct |
38 |
Correct |
184 ms |
24360 KB |
Output is correct |
39 |
Correct |
200 ms |
58492 KB |
Output is correct |
40 |
Correct |
184 ms |
58332 KB |
Output is correct |
41 |
Correct |
134 ms |
58276 KB |
Output is correct |
42 |
Correct |
192 ms |
42804 KB |
Output is correct |
43 |
Correct |
177 ms |
42692 KB |
Output is correct |
44 |
Correct |
337 ms |
36144 KB |
Output is correct |
45 |
Correct |
228 ms |
24260 KB |
Output is correct |
46 |
Correct |
112 ms |
23808 KB |
Output is correct |
47 |
Correct |
296 ms |
46404 KB |
Output is correct |
48 |
Correct |
145 ms |
39512 KB |
Output is correct |
49 |
Correct |
117 ms |
39492 KB |
Output is correct |
50 |
Correct |
243 ms |
34824 KB |
Output is correct |
51 |
Correct |
120 ms |
34696 KB |
Output is correct |
52 |
Correct |
309 ms |
47456 KB |
Output is correct |
53 |
Correct |
181 ms |
46556 KB |
Output is correct |
54 |
Correct |
153 ms |
58204 KB |
Output is correct |
55 |
Correct |
228 ms |
49472 KB |
Output is correct |
56 |
Correct |
220 ms |
58928 KB |
Output is correct |
57 |
Correct |
193 ms |
63300 KB |
Output is correct |
58 |
Correct |
232 ms |
61376 KB |
Output is correct |
59 |
Correct |
231 ms |
61384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14332 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14412 KB |
Output is correct |
5 |
Correct |
15 ms |
14820 KB |
Output is correct |
6 |
Correct |
13 ms |
14588 KB |
Output is correct |
7 |
Correct |
12 ms |
14764 KB |
Output is correct |
8 |
Correct |
17 ms |
15012 KB |
Output is correct |
9 |
Correct |
14 ms |
14780 KB |
Output is correct |
10 |
Correct |
13 ms |
14660 KB |
Output is correct |
11 |
Correct |
11 ms |
14664 KB |
Output is correct |
12 |
Correct |
13 ms |
15584 KB |
Output is correct |
13 |
Correct |
12 ms |
15580 KB |
Output is correct |
14 |
Correct |
13 ms |
15180 KB |
Output is correct |
15 |
Correct |
12 ms |
15120 KB |
Output is correct |
16 |
Correct |
14 ms |
15056 KB |
Output is correct |
17 |
Correct |
14 ms |
14740 KB |
Output is correct |
18 |
Correct |
11 ms |
14672 KB |
Output is correct |
19 |
Correct |
13 ms |
15272 KB |
Output is correct |
20 |
Correct |
13 ms |
15136 KB |
Output is correct |
21 |
Correct |
12 ms |
15128 KB |
Output is correct |
22 |
Correct |
12 ms |
14944 KB |
Output is correct |
23 |
Correct |
12 ms |
14884 KB |
Output is correct |
24 |
Correct |
14 ms |
15228 KB |
Output is correct |
25 |
Correct |
14 ms |
15300 KB |
Output is correct |
26 |
Correct |
12 ms |
15564 KB |
Output is correct |
27 |
Correct |
12 ms |
15256 KB |
Output is correct |
28 |
Correct |
13 ms |
15324 KB |
Output is correct |
29 |
Correct |
12 ms |
15588 KB |
Output is correct |
30 |
Correct |
13 ms |
15584 KB |
Output is correct |
31 |
Correct |
14 ms |
15584 KB |
Output is correct |
32 |
Correct |
14 ms |
14932 KB |
Output is correct |
33 |
Correct |
376 ms |
37004 KB |
Output is correct |
34 |
Correct |
264 ms |
25036 KB |
Output is correct |
35 |
Correct |
369 ms |
35740 KB |
Output is correct |
36 |
Correct |
275 ms |
24832 KB |
Output is correct |
37 |
Correct |
194 ms |
24376 KB |
Output is correct |
38 |
Correct |
184 ms |
24360 KB |
Output is correct |
39 |
Correct |
200 ms |
58492 KB |
Output is correct |
40 |
Correct |
184 ms |
58332 KB |
Output is correct |
41 |
Correct |
134 ms |
58276 KB |
Output is correct |
42 |
Correct |
192 ms |
42804 KB |
Output is correct |
43 |
Correct |
177 ms |
42692 KB |
Output is correct |
44 |
Correct |
337 ms |
36144 KB |
Output is correct |
45 |
Correct |
228 ms |
24260 KB |
Output is correct |
46 |
Correct |
112 ms |
23808 KB |
Output is correct |
47 |
Correct |
296 ms |
46404 KB |
Output is correct |
48 |
Correct |
145 ms |
39512 KB |
Output is correct |
49 |
Correct |
117 ms |
39492 KB |
Output is correct |
50 |
Correct |
243 ms |
34824 KB |
Output is correct |
51 |
Correct |
120 ms |
34696 KB |
Output is correct |
52 |
Correct |
309 ms |
47456 KB |
Output is correct |
53 |
Correct |
181 ms |
46556 KB |
Output is correct |
54 |
Correct |
153 ms |
58204 KB |
Output is correct |
55 |
Correct |
228 ms |
49472 KB |
Output is correct |
56 |
Correct |
220 ms |
58928 KB |
Output is correct |
57 |
Correct |
193 ms |
63300 KB |
Output is correct |
58 |
Correct |
232 ms |
61376 KB |
Output is correct |
59 |
Correct |
231 ms |
61384 KB |
Output is correct |
60 |
Correct |
9 ms |
14412 KB |
Output is correct |
61 |
Incorrect |
10 ms |
14412 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |