#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+10;
long long mainres,n,p[maxn],h[maxn],c[maxn];
vector<long long>adj[maxn];
set<pair<long long,long long>>dp[maxn];
void vorod(){
cin>>n;
for(long long i=1;i<=n;i++){
cin>>p[i]>>h[i]>>c[i];
mainres+=c[i];
if(i!=p[i]){
adj[p[i]].push_back(i);
}
}
}
void merge(long long u,long long v){
long long fu=u;
if((long long)dp[u].size()<(long long)dp[v].size()){
swap(u,v);
}
for(auto x:dp[v]){
auto y=*dp[u].lower_bound(make_pair(x.first,-1));
if(y.first==x.first){
dp[u].erase(y);
y.second+=x.second;
dp[u].insert(y);
}else{
dp[u].insert(x);
}
}
if(fu!=u){
swap(dp[u],dp[fu]);
}
}
void ins(long long u){
long long moonde=c[u];
while(moonde>0&&(long long)dp[u].size()>0&&(*dp[u].begin()).first<h[u]){
auto xx=dp[u].lower_bound(make_pair(h[u],-1));
xx--;
auto x=*xx;
if(x.second>=moonde){
dp[u].erase(x);
x.second-=moonde;
dp[u].insert(x);
break;
}else{
dp[u].erase(x);
moonde-=x.second;
}
}
if((long long)dp[u].size()==0||(*dp[u].begin()).first>h[u]){
dp[u].insert(make_pair(h[u],c[u]));
return ;
}
auto xx=dp[u].lower_bound(make_pair(h[u],-1));
auto x=*xx;
if(x.first==h[u]){
dp[u].erase(x);
x.second+=c[u];
dp[u].insert(x);
}else{
dp[u].insert(make_pair(h[u],c[u]));
}
}
void solved(long long u){
for(auto x:adj[u]){
solved(x);
merge(u,x);
}
// cout<<u<<" hainjy:"<<endl;
// for(auto x:dp[u]){
// cout<<x.first<<" "<<x.second<<endl;;
// }
ins(u);
// cout<<u<<" injy:"<<endl;
// for(auto x:dp[u]){
// cout<<x.first<<" "<<x.second<<endl;;
// }
}
void pre(){
//felan hichy
}
void solve(){
solved(1);
for(auto x:dp[1]){
mainres-=x.second;
}
}
void khor(){
cout<<mainres<<"\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("inp.txt","r",stdin);
vorod();
pre();
solve();
khor();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
19032 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
3 ms |
19036 KB |
Output is correct |
4 |
Correct |
4 ms |
19036 KB |
Output is correct |
5 |
Correct |
9 ms |
20140 KB |
Output is correct |
6 |
Correct |
7 ms |
19548 KB |
Output is correct |
7 |
Correct |
7 ms |
19292 KB |
Output is correct |
8 |
Correct |
8 ms |
20092 KB |
Output is correct |
9 |
Correct |
8 ms |
19548 KB |
Output is correct |
10 |
Correct |
8 ms |
19292 KB |
Output is correct |
11 |
Correct |
6 ms |
19288 KB |
Output is correct |
12 |
Correct |
8 ms |
19356 KB |
Output is correct |
13 |
Correct |
6 ms |
19288 KB |
Output is correct |
14 |
Correct |
7 ms |
19288 KB |
Output is correct |
15 |
Correct |
7 ms |
19292 KB |
Output is correct |
16 |
Correct |
9 ms |
20316 KB |
Output is correct |
17 |
Correct |
8 ms |
19832 KB |
Output is correct |
18 |
Correct |
6 ms |
19312 KB |
Output is correct |
19 |
Correct |
7 ms |
19548 KB |
Output is correct |
20 |
Correct |
9 ms |
19372 KB |
Output is correct |
21 |
Correct |
6 ms |
19292 KB |
Output is correct |
22 |
Correct |
8 ms |
19548 KB |
Output is correct |
23 |
Correct |
6 ms |
19472 KB |
Output is correct |
24 |
Correct |
8 ms |
19600 KB |
Output is correct |
25 |
Correct |
7 ms |
19292 KB |
Output is correct |
26 |
Correct |
8 ms |
19548 KB |
Output is correct |
27 |
Correct |
7 ms |
19548 KB |
Output is correct |
28 |
Correct |
6 ms |
19548 KB |
Output is correct |
29 |
Correct |
7 ms |
19548 KB |
Output is correct |
30 |
Correct |
7 ms |
19548 KB |
Output is correct |
31 |
Correct |
7 ms |
19548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
19032 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
3 ms |
19036 KB |
Output is correct |
4 |
Correct |
4 ms |
19036 KB |
Output is correct |
5 |
Correct |
9 ms |
20140 KB |
Output is correct |
6 |
Correct |
7 ms |
19548 KB |
Output is correct |
7 |
Correct |
7 ms |
19292 KB |
Output is correct |
8 |
Correct |
8 ms |
20092 KB |
Output is correct |
9 |
Correct |
8 ms |
19548 KB |
Output is correct |
10 |
Correct |
8 ms |
19292 KB |
Output is correct |
11 |
Correct |
6 ms |
19288 KB |
Output is correct |
12 |
Correct |
8 ms |
19356 KB |
Output is correct |
13 |
Correct |
6 ms |
19288 KB |
Output is correct |
14 |
Correct |
7 ms |
19288 KB |
Output is correct |
15 |
Correct |
7 ms |
19292 KB |
Output is correct |
16 |
Correct |
9 ms |
20316 KB |
Output is correct |
17 |
Correct |
8 ms |
19832 KB |
Output is correct |
18 |
Correct |
6 ms |
19312 KB |
Output is correct |
19 |
Correct |
7 ms |
19548 KB |
Output is correct |
20 |
Correct |
9 ms |
19372 KB |
Output is correct |
21 |
Correct |
6 ms |
19292 KB |
Output is correct |
22 |
Correct |
8 ms |
19548 KB |
Output is correct |
23 |
Correct |
6 ms |
19472 KB |
Output is correct |
24 |
Correct |
8 ms |
19600 KB |
Output is correct |
25 |
Correct |
7 ms |
19292 KB |
Output is correct |
26 |
Correct |
8 ms |
19548 KB |
Output is correct |
27 |
Correct |
7 ms |
19548 KB |
Output is correct |
28 |
Correct |
6 ms |
19548 KB |
Output is correct |
29 |
Correct |
7 ms |
19548 KB |
Output is correct |
30 |
Correct |
7 ms |
19548 KB |
Output is correct |
31 |
Correct |
7 ms |
19548 KB |
Output is correct |
32 |
Correct |
8 ms |
20056 KB |
Output is correct |
33 |
Correct |
301 ms |
77580 KB |
Output is correct |
34 |
Correct |
235 ms |
52280 KB |
Output is correct |
35 |
Correct |
285 ms |
74320 KB |
Output is correct |
36 |
Correct |
284 ms |
52464 KB |
Output is correct |
37 |
Correct |
137 ms |
33364 KB |
Output is correct |
38 |
Correct |
121 ms |
29192 KB |
Output is correct |
39 |
Correct |
164 ms |
31316 KB |
Output is correct |
40 |
Correct |
153 ms |
31056 KB |
Output is correct |
41 |
Correct |
82 ms |
31064 KB |
Output is correct |
42 |
Correct |
156 ms |
33428 KB |
Output is correct |
43 |
Correct |
149 ms |
33356 KB |
Output is correct |
44 |
Correct |
314 ms |
106816 KB |
Output is correct |
45 |
Correct |
226 ms |
71448 KB |
Output is correct |
46 |
Correct |
69 ms |
33588 KB |
Output is correct |
47 |
Correct |
221 ms |
43272 KB |
Output is correct |
48 |
Correct |
152 ms |
36480 KB |
Output is correct |
49 |
Correct |
80 ms |
36432 KB |
Output is correct |
50 |
Correct |
214 ms |
49996 KB |
Output is correct |
51 |
Correct |
108 ms |
37480 KB |
Output is correct |
52 |
Correct |
232 ms |
44588 KB |
Output is correct |
53 |
Correct |
140 ms |
37272 KB |
Output is correct |
54 |
Correct |
176 ms |
48920 KB |
Output is correct |
55 |
Correct |
180 ms |
45800 KB |
Output is correct |
56 |
Correct |
143 ms |
45392 KB |
Output is correct |
57 |
Correct |
156 ms |
45848 KB |
Output is correct |
58 |
Correct |
188 ms |
51584 KB |
Output is correct |
59 |
Correct |
184 ms |
51316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
19032 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
3 ms |
19036 KB |
Output is correct |
4 |
Correct |
4 ms |
19036 KB |
Output is correct |
5 |
Correct |
9 ms |
20140 KB |
Output is correct |
6 |
Correct |
7 ms |
19548 KB |
Output is correct |
7 |
Correct |
7 ms |
19292 KB |
Output is correct |
8 |
Correct |
8 ms |
20092 KB |
Output is correct |
9 |
Correct |
8 ms |
19548 KB |
Output is correct |
10 |
Correct |
8 ms |
19292 KB |
Output is correct |
11 |
Correct |
6 ms |
19288 KB |
Output is correct |
12 |
Correct |
8 ms |
19356 KB |
Output is correct |
13 |
Correct |
6 ms |
19288 KB |
Output is correct |
14 |
Correct |
7 ms |
19288 KB |
Output is correct |
15 |
Correct |
7 ms |
19292 KB |
Output is correct |
16 |
Correct |
9 ms |
20316 KB |
Output is correct |
17 |
Correct |
8 ms |
19832 KB |
Output is correct |
18 |
Correct |
6 ms |
19312 KB |
Output is correct |
19 |
Correct |
7 ms |
19548 KB |
Output is correct |
20 |
Correct |
9 ms |
19372 KB |
Output is correct |
21 |
Correct |
6 ms |
19292 KB |
Output is correct |
22 |
Correct |
8 ms |
19548 KB |
Output is correct |
23 |
Correct |
6 ms |
19472 KB |
Output is correct |
24 |
Correct |
8 ms |
19600 KB |
Output is correct |
25 |
Correct |
7 ms |
19292 KB |
Output is correct |
26 |
Correct |
8 ms |
19548 KB |
Output is correct |
27 |
Correct |
7 ms |
19548 KB |
Output is correct |
28 |
Correct |
6 ms |
19548 KB |
Output is correct |
29 |
Correct |
7 ms |
19548 KB |
Output is correct |
30 |
Correct |
7 ms |
19548 KB |
Output is correct |
31 |
Correct |
7 ms |
19548 KB |
Output is correct |
32 |
Correct |
8 ms |
20056 KB |
Output is correct |
33 |
Correct |
301 ms |
77580 KB |
Output is correct |
34 |
Correct |
235 ms |
52280 KB |
Output is correct |
35 |
Correct |
285 ms |
74320 KB |
Output is correct |
36 |
Correct |
284 ms |
52464 KB |
Output is correct |
37 |
Correct |
137 ms |
33364 KB |
Output is correct |
38 |
Correct |
121 ms |
29192 KB |
Output is correct |
39 |
Correct |
164 ms |
31316 KB |
Output is correct |
40 |
Correct |
153 ms |
31056 KB |
Output is correct |
41 |
Correct |
82 ms |
31064 KB |
Output is correct |
42 |
Correct |
156 ms |
33428 KB |
Output is correct |
43 |
Correct |
149 ms |
33356 KB |
Output is correct |
44 |
Correct |
314 ms |
106816 KB |
Output is correct |
45 |
Correct |
226 ms |
71448 KB |
Output is correct |
46 |
Correct |
69 ms |
33588 KB |
Output is correct |
47 |
Correct |
221 ms |
43272 KB |
Output is correct |
48 |
Correct |
152 ms |
36480 KB |
Output is correct |
49 |
Correct |
80 ms |
36432 KB |
Output is correct |
50 |
Correct |
214 ms |
49996 KB |
Output is correct |
51 |
Correct |
108 ms |
37480 KB |
Output is correct |
52 |
Correct |
232 ms |
44588 KB |
Output is correct |
53 |
Correct |
140 ms |
37272 KB |
Output is correct |
54 |
Correct |
176 ms |
48920 KB |
Output is correct |
55 |
Correct |
180 ms |
45800 KB |
Output is correct |
56 |
Correct |
143 ms |
45392 KB |
Output is correct |
57 |
Correct |
156 ms |
45848 KB |
Output is correct |
58 |
Correct |
188 ms |
51584 KB |
Output is correct |
59 |
Correct |
184 ms |
51316 KB |
Output is correct |
60 |
Runtime error |
433 ms |
524288 KB |
Execution killed with signal 9 |
61 |
Halted |
0 ms |
0 KB |
- |