#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3uHasSr ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
const int inf=1e18;
signed main(){
_3uHasSr;
int n;
cin>>n;
vec(pii) a(n);
vi g(n);
vec(vi) rg(n);
rep(i,n){
int u,h,c;
cin>>u>>h>>c;
u-=1;
g[i]=u;
rg[u].pb(i);
a[i]={h,c};
}
vec(std::map<int,int>) dp(n);
vi center(n);
rep(v,n){
center[v]=v;
}
per(v,n){
auto [h,c]=a[v];
dp[center[v]][h]+=c;
auto it=dp[center[v]].find(h);
if(it!=dp[center[v]].begin()){
it=prev(it);
while(1){
auto nit=it;
bool lok=0;
if(it==dp[center[v]].begin()){
lok=1;
}else{
it=prev(it);
}
pii _p=*nit;
if(c>=_p.se){
c-=_p.se;
dp[center[v]].erase(nit);
}else{
_p.se-=c;
dp[center[v]].erase(nit);
dp[center[v]][_p.fi]+=_p.se;
break;
}
if(lok){
break;
}
}
}
if(!v){
continue;
}
int up=g[v];
int u0=center[up],u1=center[v];
if(sz(dp[u0])<sz(dp[u1])){
swap(u0,u1);
center[up]=center[v];
}
for(auto p:dp[u1]){
dp[u0][p.fi]+=p.se;
}
}
int res=0;
rep(v,n){
res+=a[v].se;
}
for(auto p:dp[center[0]]){
// print(p.fi,p.se);
res-=p.se;
}
print(res);
//
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
6 ms |
2132 KB |
Output is correct |
6 |
Correct |
5 ms |
1620 KB |
Output is correct |
7 |
Correct |
3 ms |
1352 KB |
Output is correct |
8 |
Correct |
5 ms |
2132 KB |
Output is correct |
9 |
Correct |
4 ms |
1648 KB |
Output is correct |
10 |
Correct |
3 ms |
1364 KB |
Output is correct |
11 |
Correct |
3 ms |
1108 KB |
Output is correct |
12 |
Correct |
4 ms |
1108 KB |
Output is correct |
13 |
Correct |
3 ms |
1108 KB |
Output is correct |
14 |
Correct |
5 ms |
1108 KB |
Output is correct |
15 |
Correct |
4 ms |
1104 KB |
Output is correct |
16 |
Correct |
7 ms |
2380 KB |
Output is correct |
17 |
Correct |
5 ms |
1748 KB |
Output is correct |
18 |
Correct |
3 ms |
1104 KB |
Output is correct |
19 |
Correct |
4 ms |
1320 KB |
Output is correct |
20 |
Correct |
3 ms |
1136 KB |
Output is correct |
21 |
Correct |
3 ms |
1136 KB |
Output is correct |
22 |
Correct |
4 ms |
1492 KB |
Output is correct |
23 |
Correct |
2 ms |
1232 KB |
Output is correct |
24 |
Correct |
4 ms |
1364 KB |
Output is correct |
25 |
Correct |
4 ms |
1100 KB |
Output is correct |
26 |
Correct |
3 ms |
1100 KB |
Output is correct |
27 |
Correct |
4 ms |
1492 KB |
Output is correct |
28 |
Correct |
4 ms |
1364 KB |
Output is correct |
29 |
Correct |
4 ms |
1360 KB |
Output is correct |
30 |
Correct |
4 ms |
1492 KB |
Output is correct |
31 |
Correct |
5 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
6 ms |
2132 KB |
Output is correct |
6 |
Correct |
5 ms |
1620 KB |
Output is correct |
7 |
Correct |
3 ms |
1352 KB |
Output is correct |
8 |
Correct |
5 ms |
2132 KB |
Output is correct |
9 |
Correct |
4 ms |
1648 KB |
Output is correct |
10 |
Correct |
3 ms |
1364 KB |
Output is correct |
11 |
Correct |
3 ms |
1108 KB |
Output is correct |
12 |
Correct |
4 ms |
1108 KB |
Output is correct |
13 |
Correct |
3 ms |
1108 KB |
Output is correct |
14 |
Correct |
5 ms |
1108 KB |
Output is correct |
15 |
Correct |
4 ms |
1104 KB |
Output is correct |
16 |
Correct |
7 ms |
2380 KB |
Output is correct |
17 |
Correct |
5 ms |
1748 KB |
Output is correct |
18 |
Correct |
3 ms |
1104 KB |
Output is correct |
19 |
Correct |
4 ms |
1320 KB |
Output is correct |
20 |
Correct |
3 ms |
1136 KB |
Output is correct |
21 |
Correct |
3 ms |
1136 KB |
Output is correct |
22 |
Correct |
4 ms |
1492 KB |
Output is correct |
23 |
Correct |
2 ms |
1232 KB |
Output is correct |
24 |
Correct |
4 ms |
1364 KB |
Output is correct |
25 |
Correct |
4 ms |
1100 KB |
Output is correct |
26 |
Correct |
3 ms |
1100 KB |
Output is correct |
27 |
Correct |
4 ms |
1492 KB |
Output is correct |
28 |
Correct |
4 ms |
1364 KB |
Output is correct |
29 |
Correct |
4 ms |
1360 KB |
Output is correct |
30 |
Correct |
4 ms |
1492 KB |
Output is correct |
31 |
Correct |
5 ms |
1492 KB |
Output is correct |
32 |
Correct |
6 ms |
2132 KB |
Output is correct |
33 |
Correct |
363 ms |
95488 KB |
Output is correct |
34 |
Correct |
279 ms |
65544 KB |
Output is correct |
35 |
Correct |
361 ms |
94024 KB |
Output is correct |
36 |
Correct |
273 ms |
65516 KB |
Output is correct |
37 |
Correct |
156 ms |
40812 KB |
Output is correct |
38 |
Correct |
120 ms |
35496 KB |
Output is correct |
39 |
Correct |
141 ms |
32148 KB |
Output is correct |
40 |
Correct |
135 ms |
32092 KB |
Output is correct |
41 |
Correct |
91 ms |
32052 KB |
Output is correct |
42 |
Correct |
144 ms |
32204 KB |
Output is correct |
43 |
Correct |
131 ms |
32036 KB |
Output is correct |
44 |
Correct |
292 ms |
108232 KB |
Output is correct |
45 |
Correct |
205 ms |
72704 KB |
Output is correct |
46 |
Correct |
87 ms |
35048 KB |
Output is correct |
47 |
Correct |
181 ms |
42040 KB |
Output is correct |
48 |
Correct |
116 ms |
35152 KB |
Output is correct |
49 |
Correct |
89 ms |
35000 KB |
Output is correct |
50 |
Correct |
228 ms |
51524 KB |
Output is correct |
51 |
Correct |
88 ms |
39068 KB |
Output is correct |
52 |
Correct |
235 ms |
43072 KB |
Output is correct |
53 |
Correct |
115 ms |
36092 KB |
Output is correct |
54 |
Correct |
91 ms |
32288 KB |
Output is correct |
55 |
Correct |
175 ms |
47148 KB |
Output is correct |
56 |
Correct |
144 ms |
42872 KB |
Output is correct |
57 |
Correct |
152 ms |
42900 KB |
Output is correct |
58 |
Correct |
179 ms |
50116 KB |
Output is correct |
59 |
Correct |
171 ms |
50144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
6 ms |
2132 KB |
Output is correct |
6 |
Correct |
5 ms |
1620 KB |
Output is correct |
7 |
Correct |
3 ms |
1352 KB |
Output is correct |
8 |
Correct |
5 ms |
2132 KB |
Output is correct |
9 |
Correct |
4 ms |
1648 KB |
Output is correct |
10 |
Correct |
3 ms |
1364 KB |
Output is correct |
11 |
Correct |
3 ms |
1108 KB |
Output is correct |
12 |
Correct |
4 ms |
1108 KB |
Output is correct |
13 |
Correct |
3 ms |
1108 KB |
Output is correct |
14 |
Correct |
5 ms |
1108 KB |
Output is correct |
15 |
Correct |
4 ms |
1104 KB |
Output is correct |
16 |
Correct |
7 ms |
2380 KB |
Output is correct |
17 |
Correct |
5 ms |
1748 KB |
Output is correct |
18 |
Correct |
3 ms |
1104 KB |
Output is correct |
19 |
Correct |
4 ms |
1320 KB |
Output is correct |
20 |
Correct |
3 ms |
1136 KB |
Output is correct |
21 |
Correct |
3 ms |
1136 KB |
Output is correct |
22 |
Correct |
4 ms |
1492 KB |
Output is correct |
23 |
Correct |
2 ms |
1232 KB |
Output is correct |
24 |
Correct |
4 ms |
1364 KB |
Output is correct |
25 |
Correct |
4 ms |
1100 KB |
Output is correct |
26 |
Correct |
3 ms |
1100 KB |
Output is correct |
27 |
Correct |
4 ms |
1492 KB |
Output is correct |
28 |
Correct |
4 ms |
1364 KB |
Output is correct |
29 |
Correct |
4 ms |
1360 KB |
Output is correct |
30 |
Correct |
4 ms |
1492 KB |
Output is correct |
31 |
Correct |
5 ms |
1492 KB |
Output is correct |
32 |
Correct |
6 ms |
2132 KB |
Output is correct |
33 |
Correct |
363 ms |
95488 KB |
Output is correct |
34 |
Correct |
279 ms |
65544 KB |
Output is correct |
35 |
Correct |
361 ms |
94024 KB |
Output is correct |
36 |
Correct |
273 ms |
65516 KB |
Output is correct |
37 |
Correct |
156 ms |
40812 KB |
Output is correct |
38 |
Correct |
120 ms |
35496 KB |
Output is correct |
39 |
Correct |
141 ms |
32148 KB |
Output is correct |
40 |
Correct |
135 ms |
32092 KB |
Output is correct |
41 |
Correct |
91 ms |
32052 KB |
Output is correct |
42 |
Correct |
144 ms |
32204 KB |
Output is correct |
43 |
Correct |
131 ms |
32036 KB |
Output is correct |
44 |
Correct |
292 ms |
108232 KB |
Output is correct |
45 |
Correct |
205 ms |
72704 KB |
Output is correct |
46 |
Correct |
87 ms |
35048 KB |
Output is correct |
47 |
Correct |
181 ms |
42040 KB |
Output is correct |
48 |
Correct |
116 ms |
35152 KB |
Output is correct |
49 |
Correct |
89 ms |
35000 KB |
Output is correct |
50 |
Correct |
228 ms |
51524 KB |
Output is correct |
51 |
Correct |
88 ms |
39068 KB |
Output is correct |
52 |
Correct |
235 ms |
43072 KB |
Output is correct |
53 |
Correct |
115 ms |
36092 KB |
Output is correct |
54 |
Correct |
91 ms |
32288 KB |
Output is correct |
55 |
Correct |
175 ms |
47148 KB |
Output is correct |
56 |
Correct |
144 ms |
42872 KB |
Output is correct |
57 |
Correct |
152 ms |
42900 KB |
Output is correct |
58 |
Correct |
179 ms |
50116 KB |
Output is correct |
59 |
Correct |
171 ms |
50144 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |