#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
int n,p[200005],h[200005];
ll val[200005];
vector<int> adj[200005];
set<ll> ss[200005];
map<ll,ll> temp[200005];
bool mark[200005],vis[200005];
void dfs1(int x) {
vis[x]=true;
for (auto s : adj[x]) if (!vis[s]) dfs1(s);
}
void dfs2(int x) {
for (auto s : adj[x]) dfs2(s);
if (!mark[x]) {
if (adj[x].size()) {
swap(ss[x],ss[adj[x][0]]);
swap(temp[x],temp[adj[x][0]]);
for (int i=1; i<adj[x].size(); ++i) {
int c=adj[x][i];
if (ss[c].size()>ss[x].size()) swap(temp[x],temp[c]), swap(ss[x],ss[c]);
for (auto s : ss[c]) temp[x][s]+=temp[c][s], ss[x].insert(s);
}
}
temp[x][0]+=val[x]; ss[x].insert(0);
temp[x][h[x]]-=val[x]; ss[x].insert(h[x]);
temp[x][h[x]+1]+=val[x]; ss[x].insert(h[x]+1);
if (temp[x][h[x]]<0) {
ll k=-temp[x][h[x]];
auto it=ss[x].find(h[x]);
temp[x][h[x]]=0;
auto h=it; --it;
ss[x].erase(h);
while (true) {
if (temp[x][*it]<k) {
k-=temp[x][*it];
temp[x][*it]=0;
auto h=it; --it;
ss[x].erase(h);
} else {
temp[x][*it]-=k;
if (temp[x][*it]==0) ss[x].erase(it);
break;
}
}
}
} else {
ss[x].insert(0);
temp[x][0]=val[x];
for (auto s : adj[x]) temp[x][0]+=temp[s][0];
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin>>n;
for (int i=1; i<=n; ++i) {
cin>>p[i]>>h[i]>>val[i];
adj[p[i]].push_back(i);
}
ll ans=0;
for (int i=1; i<=n; ++i) {
if (!vis[i]) {
dfs1(i);
while (!vis[p[i]]) {
i=p[i];
dfs1(i);
}
for (int j=0; j<adj[p[i]].size(); ++j) {
if (adj[p[i]][j]==i) {
swap(adj[p[i]][j],adj[p[i]].back());
adj[p[i]].pop_back();
break;
}
}
int idx=p[i];
bool same=true;
while (idx!=i) {
if (val[idx]!=val[i]) same=false;
idx=p[idx];
}
if (!same) {
idx=p[i];
while (idx!=i) {
mark[idx]=true;
idx=p[idx];
}
mark[i]=true;
}
dfs2(i);
ans+=temp[i][0];
}
}
cout<<ans;
}
Compilation message
worst_reporter2.cpp: In function 'void dfs2(int)':
worst_reporter2.cpp:26:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for (int i=1; i<adj[x].size(); ++i) {
| ~^~~~~~~~~~~~~~
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:83:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int j=0; j<adj[p[i]].size(); ++j) {
| ~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23840 KB |
Output is correct |
2 |
Correct |
11 ms |
23708 KB |
Output is correct |
3 |
Correct |
10 ms |
23764 KB |
Output is correct |
4 |
Correct |
10 ms |
23764 KB |
Output is correct |
5 |
Correct |
21 ms |
26316 KB |
Output is correct |
6 |
Correct |
18 ms |
25600 KB |
Output is correct |
7 |
Correct |
16 ms |
25172 KB |
Output is correct |
8 |
Correct |
22 ms |
26392 KB |
Output is correct |
9 |
Correct |
18 ms |
25568 KB |
Output is correct |
10 |
Correct |
16 ms |
25112 KB |
Output is correct |
11 |
Correct |
14 ms |
24660 KB |
Output is correct |
12 |
Correct |
19 ms |
25628 KB |
Output is correct |
13 |
Correct |
15 ms |
24980 KB |
Output is correct |
14 |
Correct |
19 ms |
25244 KB |
Output is correct |
15 |
Correct |
18 ms |
24596 KB |
Output is correct |
16 |
Correct |
23 ms |
27008 KB |
Output is correct |
17 |
Correct |
18 ms |
25976 KB |
Output is correct |
18 |
Correct |
14 ms |
24660 KB |
Output is correct |
19 |
Correct |
18 ms |
25620 KB |
Output is correct |
20 |
Correct |
16 ms |
25044 KB |
Output is correct |
21 |
Correct |
12 ms |
25044 KB |
Output is correct |
22 |
Correct |
16 ms |
25744 KB |
Output is correct |
23 |
Correct |
15 ms |
25172 KB |
Output is correct |
24 |
Correct |
18 ms |
25616 KB |
Output is correct |
25 |
Correct |
14 ms |
25044 KB |
Output is correct |
26 |
Correct |
16 ms |
25684 KB |
Output is correct |
27 |
Correct |
18 ms |
25696 KB |
Output is correct |
28 |
Correct |
20 ms |
25724 KB |
Output is correct |
29 |
Correct |
17 ms |
25812 KB |
Output is correct |
30 |
Correct |
17 ms |
25620 KB |
Output is correct |
31 |
Correct |
17 ms |
25684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23840 KB |
Output is correct |
2 |
Correct |
11 ms |
23708 KB |
Output is correct |
3 |
Correct |
10 ms |
23764 KB |
Output is correct |
4 |
Correct |
10 ms |
23764 KB |
Output is correct |
5 |
Correct |
21 ms |
26316 KB |
Output is correct |
6 |
Correct |
18 ms |
25600 KB |
Output is correct |
7 |
Correct |
16 ms |
25172 KB |
Output is correct |
8 |
Correct |
22 ms |
26392 KB |
Output is correct |
9 |
Correct |
18 ms |
25568 KB |
Output is correct |
10 |
Correct |
16 ms |
25112 KB |
Output is correct |
11 |
Correct |
14 ms |
24660 KB |
Output is correct |
12 |
Correct |
19 ms |
25628 KB |
Output is correct |
13 |
Correct |
15 ms |
24980 KB |
Output is correct |
14 |
Correct |
19 ms |
25244 KB |
Output is correct |
15 |
Correct |
18 ms |
24596 KB |
Output is correct |
16 |
Correct |
23 ms |
27008 KB |
Output is correct |
17 |
Correct |
18 ms |
25976 KB |
Output is correct |
18 |
Correct |
14 ms |
24660 KB |
Output is correct |
19 |
Correct |
18 ms |
25620 KB |
Output is correct |
20 |
Correct |
16 ms |
25044 KB |
Output is correct |
21 |
Correct |
12 ms |
25044 KB |
Output is correct |
22 |
Correct |
16 ms |
25744 KB |
Output is correct |
23 |
Correct |
15 ms |
25172 KB |
Output is correct |
24 |
Correct |
18 ms |
25616 KB |
Output is correct |
25 |
Correct |
14 ms |
25044 KB |
Output is correct |
26 |
Correct |
16 ms |
25684 KB |
Output is correct |
27 |
Correct |
18 ms |
25696 KB |
Output is correct |
28 |
Correct |
20 ms |
25724 KB |
Output is correct |
29 |
Correct |
17 ms |
25812 KB |
Output is correct |
30 |
Correct |
17 ms |
25620 KB |
Output is correct |
31 |
Correct |
17 ms |
25684 KB |
Output is correct |
32 |
Correct |
19 ms |
26388 KB |
Output is correct |
33 |
Correct |
577 ms |
153836 KB |
Output is correct |
34 |
Correct |
435 ms |
110184 KB |
Output is correct |
35 |
Correct |
555 ms |
148504 KB |
Output is correct |
36 |
Correct |
420 ms |
110264 KB |
Output is correct |
37 |
Correct |
266 ms |
72720 KB |
Output is correct |
38 |
Correct |
272 ms |
58644 KB |
Output is correct |
39 |
Correct |
588 ms |
98052 KB |
Output is correct |
40 |
Correct |
277 ms |
73000 KB |
Output is correct |
41 |
Correct |
108 ms |
72892 KB |
Output is correct |
42 |
Correct |
505 ms |
80972 KB |
Output is correct |
43 |
Correct |
285 ms |
55976 KB |
Output is correct |
44 |
Correct |
586 ms |
198688 KB |
Output is correct |
45 |
Correct |
435 ms |
136140 KB |
Output is correct |
46 |
Correct |
161 ms |
58784 KB |
Output is correct |
47 |
Correct |
634 ms |
100016 KB |
Output is correct |
48 |
Correct |
223 ms |
76148 KB |
Output is correct |
49 |
Correct |
159 ms |
76092 KB |
Output is correct |
50 |
Correct |
300 ms |
100944 KB |
Output is correct |
51 |
Correct |
151 ms |
79160 KB |
Output is correct |
52 |
Correct |
476 ms |
100640 KB |
Output is correct |
53 |
Correct |
212 ms |
76588 KB |
Output is correct |
54 |
Correct |
307 ms |
98164 KB |
Output is correct |
55 |
Correct |
489 ms |
101020 KB |
Output is correct |
56 |
Correct |
405 ms |
102760 KB |
Output is correct |
57 |
Correct |
405 ms |
104020 KB |
Output is correct |
58 |
Correct |
428 ms |
100412 KB |
Output is correct |
59 |
Correct |
389 ms |
100432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23840 KB |
Output is correct |
2 |
Correct |
11 ms |
23708 KB |
Output is correct |
3 |
Correct |
10 ms |
23764 KB |
Output is correct |
4 |
Correct |
10 ms |
23764 KB |
Output is correct |
5 |
Correct |
21 ms |
26316 KB |
Output is correct |
6 |
Correct |
18 ms |
25600 KB |
Output is correct |
7 |
Correct |
16 ms |
25172 KB |
Output is correct |
8 |
Correct |
22 ms |
26392 KB |
Output is correct |
9 |
Correct |
18 ms |
25568 KB |
Output is correct |
10 |
Correct |
16 ms |
25112 KB |
Output is correct |
11 |
Correct |
14 ms |
24660 KB |
Output is correct |
12 |
Correct |
19 ms |
25628 KB |
Output is correct |
13 |
Correct |
15 ms |
24980 KB |
Output is correct |
14 |
Correct |
19 ms |
25244 KB |
Output is correct |
15 |
Correct |
18 ms |
24596 KB |
Output is correct |
16 |
Correct |
23 ms |
27008 KB |
Output is correct |
17 |
Correct |
18 ms |
25976 KB |
Output is correct |
18 |
Correct |
14 ms |
24660 KB |
Output is correct |
19 |
Correct |
18 ms |
25620 KB |
Output is correct |
20 |
Correct |
16 ms |
25044 KB |
Output is correct |
21 |
Correct |
12 ms |
25044 KB |
Output is correct |
22 |
Correct |
16 ms |
25744 KB |
Output is correct |
23 |
Correct |
15 ms |
25172 KB |
Output is correct |
24 |
Correct |
18 ms |
25616 KB |
Output is correct |
25 |
Correct |
14 ms |
25044 KB |
Output is correct |
26 |
Correct |
16 ms |
25684 KB |
Output is correct |
27 |
Correct |
18 ms |
25696 KB |
Output is correct |
28 |
Correct |
20 ms |
25724 KB |
Output is correct |
29 |
Correct |
17 ms |
25812 KB |
Output is correct |
30 |
Correct |
17 ms |
25620 KB |
Output is correct |
31 |
Correct |
17 ms |
25684 KB |
Output is correct |
32 |
Correct |
19 ms |
26388 KB |
Output is correct |
33 |
Correct |
577 ms |
153836 KB |
Output is correct |
34 |
Correct |
435 ms |
110184 KB |
Output is correct |
35 |
Correct |
555 ms |
148504 KB |
Output is correct |
36 |
Correct |
420 ms |
110264 KB |
Output is correct |
37 |
Correct |
266 ms |
72720 KB |
Output is correct |
38 |
Correct |
272 ms |
58644 KB |
Output is correct |
39 |
Correct |
588 ms |
98052 KB |
Output is correct |
40 |
Correct |
277 ms |
73000 KB |
Output is correct |
41 |
Correct |
108 ms |
72892 KB |
Output is correct |
42 |
Correct |
505 ms |
80972 KB |
Output is correct |
43 |
Correct |
285 ms |
55976 KB |
Output is correct |
44 |
Correct |
586 ms |
198688 KB |
Output is correct |
45 |
Correct |
435 ms |
136140 KB |
Output is correct |
46 |
Correct |
161 ms |
58784 KB |
Output is correct |
47 |
Correct |
634 ms |
100016 KB |
Output is correct |
48 |
Correct |
223 ms |
76148 KB |
Output is correct |
49 |
Correct |
159 ms |
76092 KB |
Output is correct |
50 |
Correct |
300 ms |
100944 KB |
Output is correct |
51 |
Correct |
151 ms |
79160 KB |
Output is correct |
52 |
Correct |
476 ms |
100640 KB |
Output is correct |
53 |
Correct |
212 ms |
76588 KB |
Output is correct |
54 |
Correct |
307 ms |
98164 KB |
Output is correct |
55 |
Correct |
489 ms |
101020 KB |
Output is correct |
56 |
Correct |
405 ms |
102760 KB |
Output is correct |
57 |
Correct |
405 ms |
104020 KB |
Output is correct |
58 |
Correct |
428 ms |
100412 KB |
Output is correct |
59 |
Correct |
389 ms |
100432 KB |
Output is correct |
60 |
Incorrect |
10 ms |
23764 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |