#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
const int N =2e5+5;
int H[N];
int C[N];
ll sz[N];
ll sum[N];
vector<int> side[N];
bool vis[N];
void getsize(int cur){
sz[cur] =1;
sum[cur] = C[cur];
for(auto i : side[cur]){
getsize(i);
sz[cur]+=sz[i];
sum[cur]+=sum[i];
}
return ;
}
int n;
//map -H[i], easier
void get(map<int,ll> &s,int cur){
vis[cur]=1;
pair<int,int> maxchild = {-1,-1};
for(int i=0;i<side[cur].size();i++){
maxchild = max(maxchild,{sz[side[cur][i]],i});
}
if(maxchild.second!=-1) get(s,side[cur][maxchild.second]);
map<int,ll> s2;
for(int i=0;i<side[cur].size();i++){
if(i==maxchild.second) continue;
map<int,ll> os;
get(os,side[cur][i]);
for(auto it : os){
s2[it.first]+=it.second;
}
}
for(auto it : s2){
s[it.first]+=it.second;
s[it.first] = min(0LL,s[it.first]);
}
s[-H[cur]]-=C[cur];
ll erasesum = 0;
while(true){
auto it = s.upper_bound(-H[cur]);
if(it==s.end()) break;
erasesum+=it->second;
if(erasesum<-C[cur]){
it->second = erasesum+C[cur];
break;
}else{
s.erase(it);
}
}
return ;
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
vector<int> d;
for(int i=1;i<=n;i++){
int a;
cin>>a>>H[i]>>C[i];
d.push_back(H[i]);
if(a!=i) side[a].push_back(i);
}
sort(d.begin(),d.end());
d.resize(unique(d.begin(),d.end())-d.begin());
for(int i=1;i<=n;i++){
H[i] = lower_bound(d.begin(),d.end(),H[i])-d.begin()+1;
}
ll ans=0;
for(int i=1;i<=n;i++){
if(vis[i]) continue;
getsize(i);
map<int,ll> s;
get(s,i);
for(auto it : s){
ans+=it.second;
}
ans+=sum[i];
}
cout<<ans<<"\n";
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'void get(std::map<int, long long int>&, int)':
worst_reporter2.cpp:30:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i=0;i<side[cur].size();i++){
| ~^~~~~~~~~~~~~~~~~
worst_reporter2.cpp:35:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i=0;i<side[cur].size();i++){
| ~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
9 ms |
9212 KB |
Output is correct |
6 |
Correct |
6 ms |
8792 KB |
Output is correct |
7 |
Correct |
4 ms |
8792 KB |
Output is correct |
8 |
Correct |
8 ms |
9180 KB |
Output is correct |
9 |
Correct |
6 ms |
8932 KB |
Output is correct |
10 |
Correct |
5 ms |
8792 KB |
Output is correct |
11 |
Correct |
5 ms |
8796 KB |
Output is correct |
12 |
Correct |
5 ms |
10220 KB |
Output is correct |
13 |
Correct |
5 ms |
10076 KB |
Output is correct |
14 |
Correct |
6 ms |
9564 KB |
Output is correct |
15 |
Correct |
4 ms |
9564 KB |
Output is correct |
16 |
Correct |
11 ms |
9048 KB |
Output is correct |
17 |
Correct |
8 ms |
8792 KB |
Output is correct |
18 |
Correct |
4 ms |
8796 KB |
Output is correct |
19 |
Correct |
6 ms |
9560 KB |
Output is correct |
20 |
Correct |
4 ms |
9564 KB |
Output is correct |
21 |
Correct |
4 ms |
9560 KB |
Output is correct |
22 |
Correct |
5 ms |
9308 KB |
Output is correct |
23 |
Correct |
3 ms |
8804 KB |
Output is correct |
24 |
Correct |
6 ms |
9844 KB |
Output is correct |
25 |
Correct |
5 ms |
9564 KB |
Output is correct |
26 |
Correct |
4 ms |
10076 KB |
Output is correct |
27 |
Correct |
5 ms |
9564 KB |
Output is correct |
28 |
Correct |
5 ms |
9816 KB |
Output is correct |
29 |
Correct |
5 ms |
10076 KB |
Output is correct |
30 |
Correct |
5 ms |
10076 KB |
Output is correct |
31 |
Correct |
5 ms |
10076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
9 ms |
9212 KB |
Output is correct |
6 |
Correct |
6 ms |
8792 KB |
Output is correct |
7 |
Correct |
4 ms |
8792 KB |
Output is correct |
8 |
Correct |
8 ms |
9180 KB |
Output is correct |
9 |
Correct |
6 ms |
8932 KB |
Output is correct |
10 |
Correct |
5 ms |
8792 KB |
Output is correct |
11 |
Correct |
5 ms |
8796 KB |
Output is correct |
12 |
Correct |
5 ms |
10220 KB |
Output is correct |
13 |
Correct |
5 ms |
10076 KB |
Output is correct |
14 |
Correct |
6 ms |
9564 KB |
Output is correct |
15 |
Correct |
4 ms |
9564 KB |
Output is correct |
16 |
Correct |
11 ms |
9048 KB |
Output is correct |
17 |
Correct |
8 ms |
8792 KB |
Output is correct |
18 |
Correct |
4 ms |
8796 KB |
Output is correct |
19 |
Correct |
6 ms |
9560 KB |
Output is correct |
20 |
Correct |
4 ms |
9564 KB |
Output is correct |
21 |
Correct |
4 ms |
9560 KB |
Output is correct |
22 |
Correct |
5 ms |
9308 KB |
Output is correct |
23 |
Correct |
3 ms |
8804 KB |
Output is correct |
24 |
Correct |
6 ms |
9844 KB |
Output is correct |
25 |
Correct |
5 ms |
9564 KB |
Output is correct |
26 |
Correct |
4 ms |
10076 KB |
Output is correct |
27 |
Correct |
5 ms |
9564 KB |
Output is correct |
28 |
Correct |
5 ms |
9816 KB |
Output is correct |
29 |
Correct |
5 ms |
10076 KB |
Output is correct |
30 |
Correct |
5 ms |
10076 KB |
Output is correct |
31 |
Correct |
5 ms |
10076 KB |
Output is correct |
32 |
Correct |
8 ms |
9052 KB |
Output is correct |
33 |
Correct |
426 ms |
33980 KB |
Output is correct |
34 |
Correct |
244 ms |
19528 KB |
Output is correct |
35 |
Correct |
337 ms |
32960 KB |
Output is correct |
36 |
Correct |
209 ms |
19264 KB |
Output is correct |
37 |
Correct |
110 ms |
18884 KB |
Output is correct |
38 |
Correct |
81 ms |
18884 KB |
Output is correct |
39 |
Correct |
180 ms |
72132 KB |
Output is correct |
40 |
Correct |
143 ms |
72108 KB |
Output is correct |
41 |
Correct |
99 ms |
72132 KB |
Output is correct |
42 |
Correct |
154 ms |
47388 KB |
Output is correct |
43 |
Correct |
131 ms |
47300 KB |
Output is correct |
44 |
Correct |
421 ms |
31316 KB |
Output is correct |
45 |
Correct |
236 ms |
19184 KB |
Output is correct |
46 |
Correct |
63 ms |
18880 KB |
Output is correct |
47 |
Correct |
216 ms |
50760 KB |
Output is correct |
48 |
Correct |
118 ms |
43968 KB |
Output is correct |
49 |
Correct |
94 ms |
44036 KB |
Output is correct |
50 |
Correct |
236 ms |
40696 KB |
Output is correct |
51 |
Correct |
80 ms |
16056 KB |
Output is correct |
52 |
Correct |
236 ms |
56976 KB |
Output is correct |
53 |
Correct |
106 ms |
44996 KB |
Output is correct |
54 |
Correct |
107 ms |
72392 KB |
Output is correct |
55 |
Correct |
160 ms |
52432 KB |
Output is correct |
56 |
Correct |
153 ms |
66944 KB |
Output is correct |
57 |
Correct |
149 ms |
73416 KB |
Output is correct |
58 |
Correct |
175 ms |
65364 KB |
Output is correct |
59 |
Correct |
177 ms |
65224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
9 ms |
9212 KB |
Output is correct |
6 |
Correct |
6 ms |
8792 KB |
Output is correct |
7 |
Correct |
4 ms |
8792 KB |
Output is correct |
8 |
Correct |
8 ms |
9180 KB |
Output is correct |
9 |
Correct |
6 ms |
8932 KB |
Output is correct |
10 |
Correct |
5 ms |
8792 KB |
Output is correct |
11 |
Correct |
5 ms |
8796 KB |
Output is correct |
12 |
Correct |
5 ms |
10220 KB |
Output is correct |
13 |
Correct |
5 ms |
10076 KB |
Output is correct |
14 |
Correct |
6 ms |
9564 KB |
Output is correct |
15 |
Correct |
4 ms |
9564 KB |
Output is correct |
16 |
Correct |
11 ms |
9048 KB |
Output is correct |
17 |
Correct |
8 ms |
8792 KB |
Output is correct |
18 |
Correct |
4 ms |
8796 KB |
Output is correct |
19 |
Correct |
6 ms |
9560 KB |
Output is correct |
20 |
Correct |
4 ms |
9564 KB |
Output is correct |
21 |
Correct |
4 ms |
9560 KB |
Output is correct |
22 |
Correct |
5 ms |
9308 KB |
Output is correct |
23 |
Correct |
3 ms |
8804 KB |
Output is correct |
24 |
Correct |
6 ms |
9844 KB |
Output is correct |
25 |
Correct |
5 ms |
9564 KB |
Output is correct |
26 |
Correct |
4 ms |
10076 KB |
Output is correct |
27 |
Correct |
5 ms |
9564 KB |
Output is correct |
28 |
Correct |
5 ms |
9816 KB |
Output is correct |
29 |
Correct |
5 ms |
10076 KB |
Output is correct |
30 |
Correct |
5 ms |
10076 KB |
Output is correct |
31 |
Correct |
5 ms |
10076 KB |
Output is correct |
32 |
Correct |
8 ms |
9052 KB |
Output is correct |
33 |
Correct |
426 ms |
33980 KB |
Output is correct |
34 |
Correct |
244 ms |
19528 KB |
Output is correct |
35 |
Correct |
337 ms |
32960 KB |
Output is correct |
36 |
Correct |
209 ms |
19264 KB |
Output is correct |
37 |
Correct |
110 ms |
18884 KB |
Output is correct |
38 |
Correct |
81 ms |
18884 KB |
Output is correct |
39 |
Correct |
180 ms |
72132 KB |
Output is correct |
40 |
Correct |
143 ms |
72108 KB |
Output is correct |
41 |
Correct |
99 ms |
72132 KB |
Output is correct |
42 |
Correct |
154 ms |
47388 KB |
Output is correct |
43 |
Correct |
131 ms |
47300 KB |
Output is correct |
44 |
Correct |
421 ms |
31316 KB |
Output is correct |
45 |
Correct |
236 ms |
19184 KB |
Output is correct |
46 |
Correct |
63 ms |
18880 KB |
Output is correct |
47 |
Correct |
216 ms |
50760 KB |
Output is correct |
48 |
Correct |
118 ms |
43968 KB |
Output is correct |
49 |
Correct |
94 ms |
44036 KB |
Output is correct |
50 |
Correct |
236 ms |
40696 KB |
Output is correct |
51 |
Correct |
80 ms |
16056 KB |
Output is correct |
52 |
Correct |
236 ms |
56976 KB |
Output is correct |
53 |
Correct |
106 ms |
44996 KB |
Output is correct |
54 |
Correct |
107 ms |
72392 KB |
Output is correct |
55 |
Correct |
160 ms |
52432 KB |
Output is correct |
56 |
Correct |
153 ms |
66944 KB |
Output is correct |
57 |
Correct |
149 ms |
73416 KB |
Output is correct |
58 |
Correct |
175 ms |
65364 KB |
Output is correct |
59 |
Correct |
177 ms |
65224 KB |
Output is correct |
60 |
Runtime error |
358 ms |
524288 KB |
Execution killed with signal 9 |
61 |
Halted |
0 ms |
0 KB |
- |