/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
// lethal option
#include<bits/stdc++.h>
using namespace std;
#define all(flg) flg.begin(), flg.end()
#define int long long
#define pb push_back
#define fi first
#define se second
#define eb emplace_back
#define ii pair<int, int>
#define vi vector<int>
#define PI 3.141592653589793238462643383279502884
#define ll long long
#define ld long double
#define for1(i, ff, gg) for(int i = ff; i <= gg; ++i)
#define for2(i, ff, gg) for(int i = ff; i >= gg; --i)
const ll mod = 1e9 + 7;
const int maxN = 2e6 + 5;
const ll oo = 1e18 + 7;
int n, a[maxN];
int x, y, z, k;
int bonus = 0;
int val[maxN], cost[maxN];
int chi[maxN];
bool trigg[maxN];
int sum = 0;
vi vc[maxN];
int par[maxN];
int find(int i){
if(i == par[i]) return i;
return par[i] = find(par[i]);
}
void dfs(int node, map<int, int>& mp){
mp[0] = oo;
for(int cc : vc[node]){
map<int, int> sec;
dfs(cc, sec);
if(mp.size() < sec.size()) swap(mp, sec);
for(ii pr : sec) mp[pr.fi] += pr.se;
}
if(node == 0){
mp[0] = 0;
int ans = 0;
for(ii pr : mp) ans += pr.se;
cout << sum - ans << endl;
exit(0);
}
int pon = val[node] - 1;
mp[pon] -= cost[node];
mp[pon + 1] += cost[node];
while(mp[pon] <= 0){
auto itr = prev(mp.find(pon));
itr->se += mp[pon];
mp.erase(pon);
pon = itr->fi;
}
mp[0] = 0;
}
bool repag[maxN];
signed main(){
// freopen(".inp", "r", stdin);
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for1(i, 1, n){
cin >> a[i] >> val[i] >> cost[i];
++chi[a[i]];
sum += cost[i];
}
memset(trigg, 1, sizeof(trigg));
for1(i, 1, n) if(chi[i] == 0){
int cop = i;
while(chi[cop] == 0){
trigg[cop] = 0;
cop = a[cop];
--chi[cop];
}
}
for1(i, 1, n) par[i] = i;
memset(repag, 0, sizeof(repag));
for1(i, 1, n) if(trigg[i]){
if(repag[i]) continue;
vector<ii> cac; int cop = i;
while(1){
cac.pb(ii(val[cop], cop));
cop = a[cop];
repag[cop] = 1;
// cout << cop << " " << i << endl;
if(cop == i) break;
}
sort(all(cac));
cac.pb(ii(0, 0));
reverse(all(cac));
for1(i, 1, (int)cac.size() - 1){
x = cac[i - 1].se;
y = cac[i].se;
vc[x].pb(y);
par[x] = y;
}
}
for1(i, 1, n){
if(!trigg[i]) vc[find(a[i])].pb(i);
}
map<int, int> mp;
dfs(0, mp);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
51156 KB |
Output is correct |
2 |
Correct |
25 ms |
51236 KB |
Output is correct |
3 |
Correct |
25 ms |
51196 KB |
Output is correct |
4 |
Correct |
26 ms |
51180 KB |
Output is correct |
5 |
Correct |
30 ms |
51760 KB |
Output is correct |
6 |
Correct |
28 ms |
51476 KB |
Output is correct |
7 |
Correct |
28 ms |
51412 KB |
Output is correct |
8 |
Correct |
33 ms |
51712 KB |
Output is correct |
9 |
Correct |
34 ms |
51556 KB |
Output is correct |
10 |
Correct |
30 ms |
51540 KB |
Output is correct |
11 |
Correct |
30 ms |
51412 KB |
Output is correct |
12 |
Correct |
31 ms |
52820 KB |
Output is correct |
13 |
Correct |
30 ms |
52708 KB |
Output is correct |
14 |
Correct |
30 ms |
52200 KB |
Output is correct |
15 |
Correct |
30 ms |
52180 KB |
Output is correct |
16 |
Correct |
32 ms |
51816 KB |
Output is correct |
17 |
Correct |
37 ms |
51512 KB |
Output is correct |
18 |
Correct |
34 ms |
51460 KB |
Output is correct |
19 |
Correct |
31 ms |
52116 KB |
Output is correct |
20 |
Correct |
30 ms |
52012 KB |
Output is correct |
21 |
Correct |
32 ms |
52128 KB |
Output is correct |
22 |
Correct |
29 ms |
51708 KB |
Output is correct |
23 |
Correct |
27 ms |
51452 KB |
Output is correct |
24 |
Correct |
30 ms |
52180 KB |
Output is correct |
25 |
Correct |
33 ms |
52076 KB |
Output is correct |
26 |
Correct |
30 ms |
52776 KB |
Output is correct |
27 |
Correct |
33 ms |
52116 KB |
Output is correct |
28 |
Correct |
35 ms |
52228 KB |
Output is correct |
29 |
Correct |
38 ms |
52472 KB |
Output is correct |
30 |
Correct |
36 ms |
52464 KB |
Output is correct |
31 |
Correct |
33 ms |
52528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
51156 KB |
Output is correct |
2 |
Correct |
25 ms |
51236 KB |
Output is correct |
3 |
Correct |
25 ms |
51196 KB |
Output is correct |
4 |
Correct |
26 ms |
51180 KB |
Output is correct |
5 |
Correct |
30 ms |
51760 KB |
Output is correct |
6 |
Correct |
28 ms |
51476 KB |
Output is correct |
7 |
Correct |
28 ms |
51412 KB |
Output is correct |
8 |
Correct |
33 ms |
51712 KB |
Output is correct |
9 |
Correct |
34 ms |
51556 KB |
Output is correct |
10 |
Correct |
30 ms |
51540 KB |
Output is correct |
11 |
Correct |
30 ms |
51412 KB |
Output is correct |
12 |
Correct |
31 ms |
52820 KB |
Output is correct |
13 |
Correct |
30 ms |
52708 KB |
Output is correct |
14 |
Correct |
30 ms |
52200 KB |
Output is correct |
15 |
Correct |
30 ms |
52180 KB |
Output is correct |
16 |
Correct |
32 ms |
51816 KB |
Output is correct |
17 |
Correct |
37 ms |
51512 KB |
Output is correct |
18 |
Correct |
34 ms |
51460 KB |
Output is correct |
19 |
Correct |
31 ms |
52116 KB |
Output is correct |
20 |
Correct |
30 ms |
52012 KB |
Output is correct |
21 |
Correct |
32 ms |
52128 KB |
Output is correct |
22 |
Correct |
29 ms |
51708 KB |
Output is correct |
23 |
Correct |
27 ms |
51452 KB |
Output is correct |
24 |
Correct |
30 ms |
52180 KB |
Output is correct |
25 |
Correct |
33 ms |
52076 KB |
Output is correct |
26 |
Correct |
30 ms |
52776 KB |
Output is correct |
27 |
Correct |
33 ms |
52116 KB |
Output is correct |
28 |
Correct |
35 ms |
52228 KB |
Output is correct |
29 |
Correct |
38 ms |
52472 KB |
Output is correct |
30 |
Correct |
36 ms |
52464 KB |
Output is correct |
31 |
Correct |
33 ms |
52528 KB |
Output is correct |
32 |
Correct |
30 ms |
51660 KB |
Output is correct |
33 |
Correct |
332 ms |
75092 KB |
Output is correct |
34 |
Correct |
240 ms |
63208 KB |
Output is correct |
35 |
Correct |
300 ms |
73008 KB |
Output is correct |
36 |
Correct |
258 ms |
63120 KB |
Output is correct |
37 |
Correct |
197 ms |
62784 KB |
Output is correct |
38 |
Correct |
162 ms |
62796 KB |
Output is correct |
39 |
Correct |
277 ms |
115408 KB |
Output is correct |
40 |
Correct |
261 ms |
115408 KB |
Output is correct |
41 |
Correct |
144 ms |
115348 KB |
Output is correct |
42 |
Correct |
274 ms |
90416 KB |
Output is correct |
43 |
Correct |
249 ms |
90284 KB |
Output is correct |
44 |
Correct |
291 ms |
73668 KB |
Output is correct |
45 |
Correct |
225 ms |
61772 KB |
Output is correct |
46 |
Correct |
128 ms |
61384 KB |
Output is correct |
47 |
Correct |
329 ms |
87164 KB |
Output is correct |
48 |
Correct |
217 ms |
86348 KB |
Output is correct |
49 |
Correct |
139 ms |
86448 KB |
Output is correct |
50 |
Correct |
226 ms |
71644 KB |
Output is correct |
51 |
Correct |
149 ms |
59608 KB |
Output is correct |
52 |
Correct |
337 ms |
93636 KB |
Output is correct |
53 |
Correct |
182 ms |
87316 KB |
Output is correct |
54 |
Correct |
151 ms |
115404 KB |
Output is correct |
55 |
Correct |
228 ms |
90244 KB |
Output is correct |
56 |
Correct |
255 ms |
101580 KB |
Output is correct |
57 |
Correct |
249 ms |
106668 KB |
Output is correct |
58 |
Correct |
287 ms |
102768 KB |
Output is correct |
59 |
Correct |
306 ms |
102768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
51156 KB |
Output is correct |
2 |
Correct |
25 ms |
51236 KB |
Output is correct |
3 |
Correct |
25 ms |
51196 KB |
Output is correct |
4 |
Correct |
26 ms |
51180 KB |
Output is correct |
5 |
Correct |
30 ms |
51760 KB |
Output is correct |
6 |
Correct |
28 ms |
51476 KB |
Output is correct |
7 |
Correct |
28 ms |
51412 KB |
Output is correct |
8 |
Correct |
33 ms |
51712 KB |
Output is correct |
9 |
Correct |
34 ms |
51556 KB |
Output is correct |
10 |
Correct |
30 ms |
51540 KB |
Output is correct |
11 |
Correct |
30 ms |
51412 KB |
Output is correct |
12 |
Correct |
31 ms |
52820 KB |
Output is correct |
13 |
Correct |
30 ms |
52708 KB |
Output is correct |
14 |
Correct |
30 ms |
52200 KB |
Output is correct |
15 |
Correct |
30 ms |
52180 KB |
Output is correct |
16 |
Correct |
32 ms |
51816 KB |
Output is correct |
17 |
Correct |
37 ms |
51512 KB |
Output is correct |
18 |
Correct |
34 ms |
51460 KB |
Output is correct |
19 |
Correct |
31 ms |
52116 KB |
Output is correct |
20 |
Correct |
30 ms |
52012 KB |
Output is correct |
21 |
Correct |
32 ms |
52128 KB |
Output is correct |
22 |
Correct |
29 ms |
51708 KB |
Output is correct |
23 |
Correct |
27 ms |
51452 KB |
Output is correct |
24 |
Correct |
30 ms |
52180 KB |
Output is correct |
25 |
Correct |
33 ms |
52076 KB |
Output is correct |
26 |
Correct |
30 ms |
52776 KB |
Output is correct |
27 |
Correct |
33 ms |
52116 KB |
Output is correct |
28 |
Correct |
35 ms |
52228 KB |
Output is correct |
29 |
Correct |
38 ms |
52472 KB |
Output is correct |
30 |
Correct |
36 ms |
52464 KB |
Output is correct |
31 |
Correct |
33 ms |
52528 KB |
Output is correct |
32 |
Correct |
30 ms |
51660 KB |
Output is correct |
33 |
Correct |
332 ms |
75092 KB |
Output is correct |
34 |
Correct |
240 ms |
63208 KB |
Output is correct |
35 |
Correct |
300 ms |
73008 KB |
Output is correct |
36 |
Correct |
258 ms |
63120 KB |
Output is correct |
37 |
Correct |
197 ms |
62784 KB |
Output is correct |
38 |
Correct |
162 ms |
62796 KB |
Output is correct |
39 |
Correct |
277 ms |
115408 KB |
Output is correct |
40 |
Correct |
261 ms |
115408 KB |
Output is correct |
41 |
Correct |
144 ms |
115348 KB |
Output is correct |
42 |
Correct |
274 ms |
90416 KB |
Output is correct |
43 |
Correct |
249 ms |
90284 KB |
Output is correct |
44 |
Correct |
291 ms |
73668 KB |
Output is correct |
45 |
Correct |
225 ms |
61772 KB |
Output is correct |
46 |
Correct |
128 ms |
61384 KB |
Output is correct |
47 |
Correct |
329 ms |
87164 KB |
Output is correct |
48 |
Correct |
217 ms |
86348 KB |
Output is correct |
49 |
Correct |
139 ms |
86448 KB |
Output is correct |
50 |
Correct |
226 ms |
71644 KB |
Output is correct |
51 |
Correct |
149 ms |
59608 KB |
Output is correct |
52 |
Correct |
337 ms |
93636 KB |
Output is correct |
53 |
Correct |
182 ms |
87316 KB |
Output is correct |
54 |
Correct |
151 ms |
115404 KB |
Output is correct |
55 |
Correct |
228 ms |
90244 KB |
Output is correct |
56 |
Correct |
255 ms |
101580 KB |
Output is correct |
57 |
Correct |
249 ms |
106668 KB |
Output is correct |
58 |
Correct |
287 ms |
102768 KB |
Output is correct |
59 |
Correct |
306 ms |
102768 KB |
Output is correct |
60 |
Correct |
25 ms |
51156 KB |
Output is correct |
61 |
Correct |
30 ms |
51192 KB |
Output is correct |
62 |
Correct |
29 ms |
51268 KB |
Output is correct |
63 |
Correct |
28 ms |
51156 KB |
Output is correct |
64 |
Incorrect |
161 ms |
63196 KB |
Output isn't correct |
65 |
Halted |
0 ms |
0 KB |
- |