#include<bits/stdc++.h>
using namespace std;
#define N 200100
#define int long long
int gonext[N],h[N],c[N],vis[N],ans;
vector<int>adj[N];
map<int,int>mp[N];
void merge(int a,int b){
if(mp[a].size()<mp[b].size())
swap(mp[a],mp[b]);
for(auto[i,j]:mp[b])
mp[a][i]+=j;
mp[b].clear();
}
void dfs(int n,int p,int q=1){
vis[n]=1;
for(auto i:adj[n])if(i-p)
dfs(i,n),merge(n,i);
mp[n][h[n]+1]+=c[n];
mp[n][h[n]];
mp[n][0]+=c[n];
if(q){
auto it=mp[n].lower_bound(h[n]);
while(it->second<c[n]){
prev(it)->second+=it->second;
it=--mp[n].erase(it);
}
it->second-=c[n];
} else mp[n][h[n]]-=c[n];
}
void docomp(int n){
while(!vis[n])
vis[n]=1,n=gonext[n];
vector<int>cyc;
int y=n;
do {
cyc.push_back(y);
y=gonext[y];
} while(y-n);
cyc.push_back(y);
for(int i=1;i<cyc.size();i++)
dfs(cyc[i],cyc[i-1],0);
for(int i=1;i+1<cyc.size();i++)
merge(n,cyc[i]);
int pref=0,cur=1e18;
for(auto[i,j]:mp[n])
cur=min(cur,pref+=j);
ans+=cur;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>gonext[i]>>h[i]>>c[i];
adj[gonext[i]].push_back(i);
}
for(int i=1;i<=n;i++)
if(!vis[i])
docomp(i);
cout<<ans<<'\n';
}
Compilation message
worst_reporter2.cpp: In function 'void docomp(long long int)':
worst_reporter2.cpp:41:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int i=1;i<cyc.size();i++)
| ~^~~~~~~~~~~
worst_reporter2.cpp:43:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int i=1;i+1<cyc.size();i++)
| ~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19032 KB |
Output is correct |
2 |
Correct |
3 ms |
19032 KB |
Output is correct |
3 |
Correct |
2 ms |
19036 KB |
Output is correct |
4 |
Correct |
3 ms |
18872 KB |
Output is correct |
5 |
Correct |
7 ms |
19548 KB |
Output is correct |
6 |
Correct |
5 ms |
19288 KB |
Output is correct |
7 |
Correct |
4 ms |
19292 KB |
Output is correct |
8 |
Correct |
6 ms |
19548 KB |
Output is correct |
9 |
Correct |
5 ms |
19292 KB |
Output is correct |
10 |
Correct |
6 ms |
19292 KB |
Output is correct |
11 |
Correct |
4 ms |
19292 KB |
Output is correct |
12 |
Correct |
5 ms |
19804 KB |
Output is correct |
13 |
Correct |
4 ms |
19876 KB |
Output is correct |
14 |
Correct |
5 ms |
19548 KB |
Output is correct |
15 |
Correct |
5 ms |
19624 KB |
Output is correct |
16 |
Correct |
6 ms |
19548 KB |
Output is correct |
17 |
Correct |
5 ms |
19544 KB |
Output is correct |
18 |
Correct |
4 ms |
19292 KB |
Output is correct |
19 |
Correct |
5 ms |
19808 KB |
Output is correct |
20 |
Correct |
4 ms |
19548 KB |
Output is correct |
21 |
Correct |
5 ms |
19548 KB |
Output is correct |
22 |
Correct |
5 ms |
19572 KB |
Output is correct |
23 |
Correct |
5 ms |
19292 KB |
Output is correct |
24 |
Correct |
5 ms |
19776 KB |
Output is correct |
25 |
Correct |
5 ms |
19800 KB |
Output is correct |
26 |
Correct |
5 ms |
20316 KB |
Output is correct |
27 |
Correct |
5 ms |
19776 KB |
Output is correct |
28 |
Correct |
5 ms |
19800 KB |
Output is correct |
29 |
Correct |
5 ms |
20060 KB |
Output is correct |
30 |
Correct |
5 ms |
20060 KB |
Output is correct |
31 |
Correct |
5 ms |
20060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19032 KB |
Output is correct |
2 |
Correct |
3 ms |
19032 KB |
Output is correct |
3 |
Correct |
2 ms |
19036 KB |
Output is correct |
4 |
Correct |
3 ms |
18872 KB |
Output is correct |
5 |
Correct |
7 ms |
19548 KB |
Output is correct |
6 |
Correct |
5 ms |
19288 KB |
Output is correct |
7 |
Correct |
4 ms |
19292 KB |
Output is correct |
8 |
Correct |
6 ms |
19548 KB |
Output is correct |
9 |
Correct |
5 ms |
19292 KB |
Output is correct |
10 |
Correct |
6 ms |
19292 KB |
Output is correct |
11 |
Correct |
4 ms |
19292 KB |
Output is correct |
12 |
Correct |
5 ms |
19804 KB |
Output is correct |
13 |
Correct |
4 ms |
19876 KB |
Output is correct |
14 |
Correct |
5 ms |
19548 KB |
Output is correct |
15 |
Correct |
5 ms |
19624 KB |
Output is correct |
16 |
Correct |
6 ms |
19548 KB |
Output is correct |
17 |
Correct |
5 ms |
19544 KB |
Output is correct |
18 |
Correct |
4 ms |
19292 KB |
Output is correct |
19 |
Correct |
5 ms |
19808 KB |
Output is correct |
20 |
Correct |
4 ms |
19548 KB |
Output is correct |
21 |
Correct |
5 ms |
19548 KB |
Output is correct |
22 |
Correct |
5 ms |
19572 KB |
Output is correct |
23 |
Correct |
5 ms |
19292 KB |
Output is correct |
24 |
Correct |
5 ms |
19776 KB |
Output is correct |
25 |
Correct |
5 ms |
19800 KB |
Output is correct |
26 |
Correct |
5 ms |
20316 KB |
Output is correct |
27 |
Correct |
5 ms |
19776 KB |
Output is correct |
28 |
Correct |
5 ms |
19800 KB |
Output is correct |
29 |
Correct |
5 ms |
20060 KB |
Output is correct |
30 |
Correct |
5 ms |
20060 KB |
Output is correct |
31 |
Correct |
5 ms |
20060 KB |
Output is correct |
32 |
Correct |
7 ms |
19548 KB |
Output is correct |
33 |
Correct |
210 ms |
41836 KB |
Output is correct |
34 |
Correct |
145 ms |
29780 KB |
Output is correct |
35 |
Correct |
208 ms |
39844 KB |
Output is correct |
36 |
Correct |
128 ms |
29776 KB |
Output is correct |
37 |
Correct |
84 ms |
29516 KB |
Output is correct |
38 |
Correct |
96 ms |
29384 KB |
Output is correct |
39 |
Correct |
115 ms |
57168 KB |
Output is correct |
40 |
Correct |
111 ms |
57248 KB |
Output is correct |
41 |
Correct |
75 ms |
57048 KB |
Output is correct |
42 |
Correct |
105 ms |
44792 KB |
Output is correct |
43 |
Correct |
106 ms |
44528 KB |
Output is correct |
44 |
Correct |
203 ms |
41296 KB |
Output is correct |
45 |
Correct |
131 ms |
29264 KB |
Output is correct |
46 |
Correct |
61 ms |
28828 KB |
Output is correct |
47 |
Correct |
151 ms |
48208 KB |
Output is correct |
48 |
Correct |
90 ms |
41336 KB |
Output is correct |
49 |
Correct |
72 ms |
41296 KB |
Output is correct |
50 |
Correct |
132 ms |
39112 KB |
Output is correct |
51 |
Correct |
69 ms |
26568 KB |
Output is correct |
52 |
Correct |
155 ms |
49504 KB |
Output is correct |
53 |
Correct |
85 ms |
42440 KB |
Output is correct |
54 |
Correct |
175 ms |
69972 KB |
Output is correct |
55 |
Correct |
123 ms |
50776 KB |
Output is correct |
56 |
Correct |
124 ms |
58220 KB |
Output is correct |
57 |
Correct |
112 ms |
62152 KB |
Output is correct |
58 |
Correct |
131 ms |
62672 KB |
Output is correct |
59 |
Correct |
125 ms |
62792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19032 KB |
Output is correct |
2 |
Correct |
3 ms |
19032 KB |
Output is correct |
3 |
Correct |
2 ms |
19036 KB |
Output is correct |
4 |
Correct |
3 ms |
18872 KB |
Output is correct |
5 |
Correct |
7 ms |
19548 KB |
Output is correct |
6 |
Correct |
5 ms |
19288 KB |
Output is correct |
7 |
Correct |
4 ms |
19292 KB |
Output is correct |
8 |
Correct |
6 ms |
19548 KB |
Output is correct |
9 |
Correct |
5 ms |
19292 KB |
Output is correct |
10 |
Correct |
6 ms |
19292 KB |
Output is correct |
11 |
Correct |
4 ms |
19292 KB |
Output is correct |
12 |
Correct |
5 ms |
19804 KB |
Output is correct |
13 |
Correct |
4 ms |
19876 KB |
Output is correct |
14 |
Correct |
5 ms |
19548 KB |
Output is correct |
15 |
Correct |
5 ms |
19624 KB |
Output is correct |
16 |
Correct |
6 ms |
19548 KB |
Output is correct |
17 |
Correct |
5 ms |
19544 KB |
Output is correct |
18 |
Correct |
4 ms |
19292 KB |
Output is correct |
19 |
Correct |
5 ms |
19808 KB |
Output is correct |
20 |
Correct |
4 ms |
19548 KB |
Output is correct |
21 |
Correct |
5 ms |
19548 KB |
Output is correct |
22 |
Correct |
5 ms |
19572 KB |
Output is correct |
23 |
Correct |
5 ms |
19292 KB |
Output is correct |
24 |
Correct |
5 ms |
19776 KB |
Output is correct |
25 |
Correct |
5 ms |
19800 KB |
Output is correct |
26 |
Correct |
5 ms |
20316 KB |
Output is correct |
27 |
Correct |
5 ms |
19776 KB |
Output is correct |
28 |
Correct |
5 ms |
19800 KB |
Output is correct |
29 |
Correct |
5 ms |
20060 KB |
Output is correct |
30 |
Correct |
5 ms |
20060 KB |
Output is correct |
31 |
Correct |
5 ms |
20060 KB |
Output is correct |
32 |
Correct |
7 ms |
19548 KB |
Output is correct |
33 |
Correct |
210 ms |
41836 KB |
Output is correct |
34 |
Correct |
145 ms |
29780 KB |
Output is correct |
35 |
Correct |
208 ms |
39844 KB |
Output is correct |
36 |
Correct |
128 ms |
29776 KB |
Output is correct |
37 |
Correct |
84 ms |
29516 KB |
Output is correct |
38 |
Correct |
96 ms |
29384 KB |
Output is correct |
39 |
Correct |
115 ms |
57168 KB |
Output is correct |
40 |
Correct |
111 ms |
57248 KB |
Output is correct |
41 |
Correct |
75 ms |
57048 KB |
Output is correct |
42 |
Correct |
105 ms |
44792 KB |
Output is correct |
43 |
Correct |
106 ms |
44528 KB |
Output is correct |
44 |
Correct |
203 ms |
41296 KB |
Output is correct |
45 |
Correct |
131 ms |
29264 KB |
Output is correct |
46 |
Correct |
61 ms |
28828 KB |
Output is correct |
47 |
Correct |
151 ms |
48208 KB |
Output is correct |
48 |
Correct |
90 ms |
41336 KB |
Output is correct |
49 |
Correct |
72 ms |
41296 KB |
Output is correct |
50 |
Correct |
132 ms |
39112 KB |
Output is correct |
51 |
Correct |
69 ms |
26568 KB |
Output is correct |
52 |
Correct |
155 ms |
49504 KB |
Output is correct |
53 |
Correct |
85 ms |
42440 KB |
Output is correct |
54 |
Correct |
175 ms |
69972 KB |
Output is correct |
55 |
Correct |
123 ms |
50776 KB |
Output is correct |
56 |
Correct |
124 ms |
58220 KB |
Output is correct |
57 |
Correct |
112 ms |
62152 KB |
Output is correct |
58 |
Correct |
131 ms |
62672 KB |
Output is correct |
59 |
Correct |
125 ms |
62792 KB |
Output is correct |
60 |
Correct |
3 ms |
19032 KB |
Output is correct |
61 |
Correct |
3 ms |
19036 KB |
Output is correct |
62 |
Correct |
3 ms |
18872 KB |
Output is correct |
63 |
Correct |
3 ms |
19100 KB |
Output is correct |
64 |
Correct |
184 ms |
39364 KB |
Output is correct |
65 |
Correct |
136 ms |
31100 KB |
Output is correct |
66 |
Correct |
171 ms |
39764 KB |
Output is correct |
67 |
Correct |
144 ms |
31312 KB |
Output is correct |
68 |
Correct |
98 ms |
30292 KB |
Output is correct |
69 |
Correct |
79 ms |
30288 KB |
Output is correct |
70 |
Correct |
319 ms |
64000 KB |
Output is correct |
71 |
Correct |
177 ms |
52420 KB |
Output is correct |
72 |
Correct |
297 ms |
71312 KB |
Output is correct |
73 |
Correct |
161 ms |
71100 KB |
Output is correct |
74 |
Correct |
216 ms |
54904 KB |
Output is correct |
75 |
Correct |
130 ms |
54720 KB |
Output is correct |
76 |
Correct |
152 ms |
53440 KB |
Output is correct |
77 |
Correct |
238 ms |
57416 KB |
Output is correct |
78 |
Correct |
109 ms |
49216 KB |
Output is correct |
79 |
Correct |
270 ms |
57028 KB |
Output is correct |
80 |
Correct |
154 ms |
50756 KB |
Output is correct |
81 |
Correct |
129 ms |
50380 KB |
Output is correct |
82 |
Correct |
189 ms |
71440 KB |
Output is correct |
83 |
Correct |
103 ms |
69532 KB |
Output is correct |
84 |
Correct |
238 ms |
62404 KB |
Output is correct |
85 |
Correct |
240 ms |
64712 KB |
Output is correct |
86 |
Correct |
190 ms |
62148 KB |
Output is correct |
87 |
Correct |
250 ms |
62184 KB |
Output is correct |
88 |
Correct |
198 ms |
62152 KB |
Output is correct |