#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define MOD 1000000007
#define INF (1ll<<62)
using namespace std;
typedef long long LL;
typedef pair<LL,LL> pii;
int N;
LL ans, C[202020], S[202020];
int P[202020], A[202020], B[202020];
bool chk[202020], now[202020], cyc[202020];
vector<int> adj[202020];
map<LL,LL> D[202020], M[202020];
void dfs(int u){
for (int v : adj[u]){
if (cyc[v]) continue;
dfs(v);
if (D[u].size() < D[v].size()) swap(D[u], D[v]), swap(C[u], C[v]);
for (pii t : D[v]) D[u][t.fi] += t.se;
C[u] += C[v];
}
if (!cyc[u]){
D[u][A[u]+1] += B[u], C[u] += B[u];
auto it = D[u].lower_bound(A[u]+1);
int dy = B[u];
while (it != D[u].begin()){
--it;
if (it->se > dy){
D[u][it->fi] -= dy;
dy = 0;
break;
}
else{
int x = it->fi;
dy -= it->se;
D[u].erase(x);
it = D[u].lower_bound(x);
}
}
C[u] -= dy;
}
}
int f(int u){
if (chk[u] && !now[u]) return 0;
if (chk[u]){
P[u] = u;
return u;
}
chk[u] = true, now[u] = true;
int r = f(P[u]);
if (r){
cyc[u] = true;
M[r][A[u]] += B[u];
S[r] += B[u];
}
if (!r || r == u){
now[u] = false;
return 0;
}
if (adj[u].size() > adj[r].size()) swap(adj[u], adj[r]);
for (int x : adj[u]) adj[r].pb(x);
now[u] = false;
return r;
}
int main(){
scanf("%d", &N);
for (int i=1; i<=N; i++){
scanf("%d %d %d", &P[i], &A[i], &B[i]);
adj[P[i]].pb(i);
}
for (int i=1; i<=N; i++) f(i);
for (int i=1; i<=N; i++) if (P[i] == i){
dfs(i);
LL mn = C[i] + S[i];
auto it = D[i].begin();
for (pii t : M[i]){
while (it != D[i].end() && it->fi <= t.fi){
C[i] += it->se, it++;
}
mn = min(mn, C[i] + S[i] - t.se);
}
ans += mn;
}
printf("%lld\n", ans);
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
worst_reporter2.cpp:75:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | scanf("%d %d %d", &P[i], &A[i], &B[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24012 KB |
Output is correct |
2 |
Correct |
17 ms |
24012 KB |
Output is correct |
3 |
Correct |
14 ms |
24012 KB |
Output is correct |
4 |
Correct |
17 ms |
24000 KB |
Output is correct |
5 |
Correct |
24 ms |
25164 KB |
Output is correct |
6 |
Correct |
19 ms |
24780 KB |
Output is correct |
7 |
Correct |
18 ms |
24524 KB |
Output is correct |
8 |
Correct |
23 ms |
25164 KB |
Output is correct |
9 |
Correct |
22 ms |
24780 KB |
Output is correct |
10 |
Correct |
22 ms |
24568 KB |
Output is correct |
11 |
Correct |
18 ms |
24396 KB |
Output is correct |
12 |
Correct |
19 ms |
25292 KB |
Output is correct |
13 |
Correct |
17 ms |
25160 KB |
Output is correct |
14 |
Correct |
24 ms |
24788 KB |
Output is correct |
15 |
Correct |
19 ms |
24780 KB |
Output is correct |
16 |
Correct |
25 ms |
25548 KB |
Output is correct |
17 |
Correct |
22 ms |
24908 KB |
Output is correct |
18 |
Correct |
17 ms |
24364 KB |
Output is correct |
19 |
Correct |
23 ms |
25036 KB |
Output is correct |
20 |
Correct |
25 ms |
24792 KB |
Output is correct |
21 |
Correct |
19 ms |
24768 KB |
Output is correct |
22 |
Correct |
22 ms |
24776 KB |
Output is correct |
23 |
Correct |
18 ms |
24524 KB |
Output is correct |
24 |
Correct |
20 ms |
25036 KB |
Output is correct |
25 |
Correct |
24 ms |
24792 KB |
Output is correct |
26 |
Correct |
19 ms |
25160 KB |
Output is correct |
27 |
Correct |
20 ms |
24960 KB |
Output is correct |
28 |
Correct |
19 ms |
25164 KB |
Output is correct |
29 |
Correct |
18 ms |
25344 KB |
Output is correct |
30 |
Correct |
23 ms |
25164 KB |
Output is correct |
31 |
Correct |
23 ms |
25136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24012 KB |
Output is correct |
2 |
Correct |
17 ms |
24012 KB |
Output is correct |
3 |
Correct |
14 ms |
24012 KB |
Output is correct |
4 |
Correct |
17 ms |
24000 KB |
Output is correct |
5 |
Correct |
24 ms |
25164 KB |
Output is correct |
6 |
Correct |
19 ms |
24780 KB |
Output is correct |
7 |
Correct |
18 ms |
24524 KB |
Output is correct |
8 |
Correct |
23 ms |
25164 KB |
Output is correct |
9 |
Correct |
22 ms |
24780 KB |
Output is correct |
10 |
Correct |
22 ms |
24568 KB |
Output is correct |
11 |
Correct |
18 ms |
24396 KB |
Output is correct |
12 |
Correct |
19 ms |
25292 KB |
Output is correct |
13 |
Correct |
17 ms |
25160 KB |
Output is correct |
14 |
Correct |
24 ms |
24788 KB |
Output is correct |
15 |
Correct |
19 ms |
24780 KB |
Output is correct |
16 |
Correct |
25 ms |
25548 KB |
Output is correct |
17 |
Correct |
22 ms |
24908 KB |
Output is correct |
18 |
Correct |
17 ms |
24364 KB |
Output is correct |
19 |
Correct |
23 ms |
25036 KB |
Output is correct |
20 |
Correct |
25 ms |
24792 KB |
Output is correct |
21 |
Correct |
19 ms |
24768 KB |
Output is correct |
22 |
Correct |
22 ms |
24776 KB |
Output is correct |
23 |
Correct |
18 ms |
24524 KB |
Output is correct |
24 |
Correct |
20 ms |
25036 KB |
Output is correct |
25 |
Correct |
24 ms |
24792 KB |
Output is correct |
26 |
Correct |
19 ms |
25160 KB |
Output is correct |
27 |
Correct |
20 ms |
24960 KB |
Output is correct |
28 |
Correct |
19 ms |
25164 KB |
Output is correct |
29 |
Correct |
18 ms |
25344 KB |
Output is correct |
30 |
Correct |
23 ms |
25164 KB |
Output is correct |
31 |
Correct |
23 ms |
25136 KB |
Output is correct |
32 |
Correct |
24 ms |
25164 KB |
Output is correct |
33 |
Correct |
445 ms |
85904 KB |
Output is correct |
34 |
Correct |
326 ms |
61144 KB |
Output is correct |
35 |
Correct |
383 ms |
82900 KB |
Output is correct |
36 |
Correct |
311 ms |
61192 KB |
Output is correct |
37 |
Correct |
211 ms |
42048 KB |
Output is correct |
38 |
Correct |
223 ms |
37824 KB |
Output is correct |
39 |
Correct |
221 ms |
72144 KB |
Output is correct |
40 |
Correct |
204 ms |
72136 KB |
Output is correct |
41 |
Correct |
137 ms |
72096 KB |
Output is correct |
42 |
Correct |
216 ms |
53540 KB |
Output is correct |
43 |
Correct |
252 ms |
53428 KB |
Output is correct |
44 |
Correct |
370 ms |
110976 KB |
Output is correct |
45 |
Correct |
315 ms |
75304 KB |
Output is correct |
46 |
Correct |
129 ms |
37604 KB |
Output is correct |
47 |
Correct |
313 ms |
63380 KB |
Output is correct |
48 |
Correct |
165 ms |
56500 KB |
Output is correct |
49 |
Correct |
135 ms |
56452 KB |
Output is correct |
50 |
Correct |
267 ms |
54140 KB |
Output is correct |
51 |
Correct |
142 ms |
41640 KB |
Output is correct |
52 |
Correct |
384 ms |
64048 KB |
Output is correct |
53 |
Correct |
161 ms |
56992 KB |
Output is correct |
54 |
Correct |
164 ms |
72060 KB |
Output is correct |
55 |
Correct |
297 ms |
65788 KB |
Output is correct |
56 |
Correct |
246 ms |
74052 KB |
Output is correct |
57 |
Correct |
212 ms |
78076 KB |
Output is correct |
58 |
Correct |
235 ms |
72068 KB |
Output is correct |
59 |
Correct |
243 ms |
72020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24012 KB |
Output is correct |
2 |
Correct |
17 ms |
24012 KB |
Output is correct |
3 |
Correct |
14 ms |
24012 KB |
Output is correct |
4 |
Correct |
17 ms |
24000 KB |
Output is correct |
5 |
Correct |
24 ms |
25164 KB |
Output is correct |
6 |
Correct |
19 ms |
24780 KB |
Output is correct |
7 |
Correct |
18 ms |
24524 KB |
Output is correct |
8 |
Correct |
23 ms |
25164 KB |
Output is correct |
9 |
Correct |
22 ms |
24780 KB |
Output is correct |
10 |
Correct |
22 ms |
24568 KB |
Output is correct |
11 |
Correct |
18 ms |
24396 KB |
Output is correct |
12 |
Correct |
19 ms |
25292 KB |
Output is correct |
13 |
Correct |
17 ms |
25160 KB |
Output is correct |
14 |
Correct |
24 ms |
24788 KB |
Output is correct |
15 |
Correct |
19 ms |
24780 KB |
Output is correct |
16 |
Correct |
25 ms |
25548 KB |
Output is correct |
17 |
Correct |
22 ms |
24908 KB |
Output is correct |
18 |
Correct |
17 ms |
24364 KB |
Output is correct |
19 |
Correct |
23 ms |
25036 KB |
Output is correct |
20 |
Correct |
25 ms |
24792 KB |
Output is correct |
21 |
Correct |
19 ms |
24768 KB |
Output is correct |
22 |
Correct |
22 ms |
24776 KB |
Output is correct |
23 |
Correct |
18 ms |
24524 KB |
Output is correct |
24 |
Correct |
20 ms |
25036 KB |
Output is correct |
25 |
Correct |
24 ms |
24792 KB |
Output is correct |
26 |
Correct |
19 ms |
25160 KB |
Output is correct |
27 |
Correct |
20 ms |
24960 KB |
Output is correct |
28 |
Correct |
19 ms |
25164 KB |
Output is correct |
29 |
Correct |
18 ms |
25344 KB |
Output is correct |
30 |
Correct |
23 ms |
25164 KB |
Output is correct |
31 |
Correct |
23 ms |
25136 KB |
Output is correct |
32 |
Correct |
24 ms |
25164 KB |
Output is correct |
33 |
Correct |
445 ms |
85904 KB |
Output is correct |
34 |
Correct |
326 ms |
61144 KB |
Output is correct |
35 |
Correct |
383 ms |
82900 KB |
Output is correct |
36 |
Correct |
311 ms |
61192 KB |
Output is correct |
37 |
Correct |
211 ms |
42048 KB |
Output is correct |
38 |
Correct |
223 ms |
37824 KB |
Output is correct |
39 |
Correct |
221 ms |
72144 KB |
Output is correct |
40 |
Correct |
204 ms |
72136 KB |
Output is correct |
41 |
Correct |
137 ms |
72096 KB |
Output is correct |
42 |
Correct |
216 ms |
53540 KB |
Output is correct |
43 |
Correct |
252 ms |
53428 KB |
Output is correct |
44 |
Correct |
370 ms |
110976 KB |
Output is correct |
45 |
Correct |
315 ms |
75304 KB |
Output is correct |
46 |
Correct |
129 ms |
37604 KB |
Output is correct |
47 |
Correct |
313 ms |
63380 KB |
Output is correct |
48 |
Correct |
165 ms |
56500 KB |
Output is correct |
49 |
Correct |
135 ms |
56452 KB |
Output is correct |
50 |
Correct |
267 ms |
54140 KB |
Output is correct |
51 |
Correct |
142 ms |
41640 KB |
Output is correct |
52 |
Correct |
384 ms |
64048 KB |
Output is correct |
53 |
Correct |
161 ms |
56992 KB |
Output is correct |
54 |
Correct |
164 ms |
72060 KB |
Output is correct |
55 |
Correct |
297 ms |
65788 KB |
Output is correct |
56 |
Correct |
246 ms |
74052 KB |
Output is correct |
57 |
Correct |
212 ms |
78076 KB |
Output is correct |
58 |
Correct |
235 ms |
72068 KB |
Output is correct |
59 |
Correct |
243 ms |
72020 KB |
Output is correct |
60 |
Correct |
16 ms |
24060 KB |
Output is correct |
61 |
Correct |
15 ms |
24012 KB |
Output is correct |
62 |
Correct |
14 ms |
24012 KB |
Output is correct |
63 |
Correct |
19 ms |
24040 KB |
Output is correct |
64 |
Correct |
459 ms |
71860 KB |
Output is correct |
65 |
Correct |
430 ms |
55052 KB |
Output is correct |
66 |
Correct |
451 ms |
71212 KB |
Output is correct |
67 |
Correct |
357 ms |
55128 KB |
Output is correct |
68 |
Correct |
292 ms |
41396 KB |
Output is correct |
69 |
Correct |
224 ms |
37900 KB |
Output is correct |
70 |
Correct |
357 ms |
62316 KB |
Output is correct |
71 |
Correct |
255 ms |
46316 KB |
Output is correct |
72 |
Correct |
448 ms |
69312 KB |
Output is correct |
73 |
Correct |
253 ms |
56968 KB |
Output is correct |
74 |
Correct |
437 ms |
62892 KB |
Output is correct |
75 |
Correct |
253 ms |
50424 KB |
Output is correct |
76 |
Correct |
224 ms |
50284 KB |
Output is correct |
77 |
Correct |
345 ms |
63196 KB |
Output is correct |
78 |
Correct |
193 ms |
50828 KB |
Output is correct |
79 |
Correct |
363 ms |
78520 KB |
Output is correct |
80 |
Correct |
269 ms |
61400 KB |
Output is correct |
81 |
Correct |
202 ms |
50688 KB |
Output is correct |
82 |
Correct |
296 ms |
69176 KB |
Output is correct |
83 |
Correct |
135 ms |
47928 KB |
Output is correct |
84 |
Correct |
391 ms |
60840 KB |
Output is correct |
85 |
Correct |
407 ms |
60960 KB |
Output is correct |
86 |
Correct |
381 ms |
57828 KB |
Output is correct |
87 |
Correct |
406 ms |
62148 KB |
Output is correct |
88 |
Correct |
346 ms |
64676 KB |
Output is correct |