/*
#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 = 2e5 + 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){
--chi[cop];
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 |
3 ms |
5352 KB |
Output is correct |
2 |
Correct |
3 ms |
5416 KB |
Output is correct |
3 |
Correct |
4 ms |
5408 KB |
Output is correct |
4 |
Correct |
4 ms |
5420 KB |
Output is correct |
5 |
Correct |
8 ms |
6100 KB |
Output is correct |
6 |
Correct |
7 ms |
5844 KB |
Output is correct |
7 |
Correct |
6 ms |
5808 KB |
Output is correct |
8 |
Correct |
9 ms |
6080 KB |
Output is correct |
9 |
Correct |
8 ms |
5848 KB |
Output is correct |
10 |
Correct |
9 ms |
5816 KB |
Output is correct |
11 |
Correct |
6 ms |
5824 KB |
Output is correct |
12 |
Correct |
12 ms |
7096 KB |
Output is correct |
13 |
Correct |
9 ms |
7096 KB |
Output is correct |
14 |
Correct |
12 ms |
6484 KB |
Output is correct |
15 |
Correct |
8 ms |
6484 KB |
Output is correct |
16 |
Correct |
11 ms |
6100 KB |
Output is correct |
17 |
Correct |
8 ms |
5824 KB |
Output is correct |
18 |
Correct |
7 ms |
5816 KB |
Output is correct |
19 |
Correct |
12 ms |
6452 KB |
Output is correct |
20 |
Correct |
9 ms |
6356 KB |
Output is correct |
21 |
Correct |
9 ms |
6456 KB |
Output is correct |
22 |
Correct |
7 ms |
6100 KB |
Output is correct |
23 |
Correct |
8 ms |
5844 KB |
Output is correct |
24 |
Correct |
9 ms |
6592 KB |
Output is correct |
25 |
Correct |
9 ms |
6452 KB |
Output is correct |
26 |
Correct |
10 ms |
7096 KB |
Output is correct |
27 |
Correct |
8 ms |
6460 KB |
Output is correct |
28 |
Correct |
8 ms |
6580 KB |
Output is correct |
29 |
Correct |
8 ms |
6868 KB |
Output is correct |
30 |
Correct |
9 ms |
6836 KB |
Output is correct |
31 |
Correct |
8 ms |
6740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5352 KB |
Output is correct |
2 |
Correct |
3 ms |
5416 KB |
Output is correct |
3 |
Correct |
4 ms |
5408 KB |
Output is correct |
4 |
Correct |
4 ms |
5420 KB |
Output is correct |
5 |
Correct |
8 ms |
6100 KB |
Output is correct |
6 |
Correct |
7 ms |
5844 KB |
Output is correct |
7 |
Correct |
6 ms |
5808 KB |
Output is correct |
8 |
Correct |
9 ms |
6080 KB |
Output is correct |
9 |
Correct |
8 ms |
5848 KB |
Output is correct |
10 |
Correct |
9 ms |
5816 KB |
Output is correct |
11 |
Correct |
6 ms |
5824 KB |
Output is correct |
12 |
Correct |
12 ms |
7096 KB |
Output is correct |
13 |
Correct |
9 ms |
7096 KB |
Output is correct |
14 |
Correct |
12 ms |
6484 KB |
Output is correct |
15 |
Correct |
8 ms |
6484 KB |
Output is correct |
16 |
Correct |
11 ms |
6100 KB |
Output is correct |
17 |
Correct |
8 ms |
5824 KB |
Output is correct |
18 |
Correct |
7 ms |
5816 KB |
Output is correct |
19 |
Correct |
12 ms |
6452 KB |
Output is correct |
20 |
Correct |
9 ms |
6356 KB |
Output is correct |
21 |
Correct |
9 ms |
6456 KB |
Output is correct |
22 |
Correct |
7 ms |
6100 KB |
Output is correct |
23 |
Correct |
8 ms |
5844 KB |
Output is correct |
24 |
Correct |
9 ms |
6592 KB |
Output is correct |
25 |
Correct |
9 ms |
6452 KB |
Output is correct |
26 |
Correct |
10 ms |
7096 KB |
Output is correct |
27 |
Correct |
8 ms |
6460 KB |
Output is correct |
28 |
Correct |
8 ms |
6580 KB |
Output is correct |
29 |
Correct |
8 ms |
6868 KB |
Output is correct |
30 |
Correct |
9 ms |
6836 KB |
Output is correct |
31 |
Correct |
8 ms |
6740 KB |
Output is correct |
32 |
Correct |
11 ms |
6068 KB |
Output is correct |
33 |
Correct |
357 ms |
34228 KB |
Output is correct |
34 |
Correct |
249 ms |
22408 KB |
Output is correct |
35 |
Correct |
362 ms |
32300 KB |
Output is correct |
36 |
Correct |
242 ms |
22296 KB |
Output is correct |
37 |
Correct |
201 ms |
21964 KB |
Output is correct |
38 |
Correct |
153 ms |
21820 KB |
Output is correct |
39 |
Correct |
278 ms |
74704 KB |
Output is correct |
40 |
Correct |
278 ms |
74684 KB |
Output is correct |
41 |
Correct |
142 ms |
74628 KB |
Output is correct |
42 |
Correct |
261 ms |
49656 KB |
Output is correct |
43 |
Correct |
233 ms |
49676 KB |
Output is correct |
44 |
Correct |
333 ms |
33668 KB |
Output is correct |
45 |
Correct |
214 ms |
21836 KB |
Output is correct |
46 |
Correct |
113 ms |
21388 KB |
Output is correct |
47 |
Correct |
390 ms |
47136 KB |
Output is correct |
48 |
Correct |
168 ms |
46284 KB |
Output is correct |
49 |
Correct |
117 ms |
46404 KB |
Output is correct |
50 |
Correct |
232 ms |
31584 KB |
Output is correct |
51 |
Correct |
105 ms |
19636 KB |
Output is correct |
52 |
Correct |
349 ms |
53584 KB |
Output is correct |
53 |
Correct |
184 ms |
47388 KB |
Output is correct |
54 |
Correct |
168 ms |
74828 KB |
Output is correct |
55 |
Correct |
293 ms |
49464 KB |
Output is correct |
56 |
Correct |
230 ms |
60216 KB |
Output is correct |
57 |
Correct |
234 ms |
65300 KB |
Output is correct |
58 |
Correct |
298 ms |
61488 KB |
Output is correct |
59 |
Correct |
275 ms |
61508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5352 KB |
Output is correct |
2 |
Correct |
3 ms |
5416 KB |
Output is correct |
3 |
Correct |
4 ms |
5408 KB |
Output is correct |
4 |
Correct |
4 ms |
5420 KB |
Output is correct |
5 |
Correct |
8 ms |
6100 KB |
Output is correct |
6 |
Correct |
7 ms |
5844 KB |
Output is correct |
7 |
Correct |
6 ms |
5808 KB |
Output is correct |
8 |
Correct |
9 ms |
6080 KB |
Output is correct |
9 |
Correct |
8 ms |
5848 KB |
Output is correct |
10 |
Correct |
9 ms |
5816 KB |
Output is correct |
11 |
Correct |
6 ms |
5824 KB |
Output is correct |
12 |
Correct |
12 ms |
7096 KB |
Output is correct |
13 |
Correct |
9 ms |
7096 KB |
Output is correct |
14 |
Correct |
12 ms |
6484 KB |
Output is correct |
15 |
Correct |
8 ms |
6484 KB |
Output is correct |
16 |
Correct |
11 ms |
6100 KB |
Output is correct |
17 |
Correct |
8 ms |
5824 KB |
Output is correct |
18 |
Correct |
7 ms |
5816 KB |
Output is correct |
19 |
Correct |
12 ms |
6452 KB |
Output is correct |
20 |
Correct |
9 ms |
6356 KB |
Output is correct |
21 |
Correct |
9 ms |
6456 KB |
Output is correct |
22 |
Correct |
7 ms |
6100 KB |
Output is correct |
23 |
Correct |
8 ms |
5844 KB |
Output is correct |
24 |
Correct |
9 ms |
6592 KB |
Output is correct |
25 |
Correct |
9 ms |
6452 KB |
Output is correct |
26 |
Correct |
10 ms |
7096 KB |
Output is correct |
27 |
Correct |
8 ms |
6460 KB |
Output is correct |
28 |
Correct |
8 ms |
6580 KB |
Output is correct |
29 |
Correct |
8 ms |
6868 KB |
Output is correct |
30 |
Correct |
9 ms |
6836 KB |
Output is correct |
31 |
Correct |
8 ms |
6740 KB |
Output is correct |
32 |
Correct |
11 ms |
6068 KB |
Output is correct |
33 |
Correct |
357 ms |
34228 KB |
Output is correct |
34 |
Correct |
249 ms |
22408 KB |
Output is correct |
35 |
Correct |
362 ms |
32300 KB |
Output is correct |
36 |
Correct |
242 ms |
22296 KB |
Output is correct |
37 |
Correct |
201 ms |
21964 KB |
Output is correct |
38 |
Correct |
153 ms |
21820 KB |
Output is correct |
39 |
Correct |
278 ms |
74704 KB |
Output is correct |
40 |
Correct |
278 ms |
74684 KB |
Output is correct |
41 |
Correct |
142 ms |
74628 KB |
Output is correct |
42 |
Correct |
261 ms |
49656 KB |
Output is correct |
43 |
Correct |
233 ms |
49676 KB |
Output is correct |
44 |
Correct |
333 ms |
33668 KB |
Output is correct |
45 |
Correct |
214 ms |
21836 KB |
Output is correct |
46 |
Correct |
113 ms |
21388 KB |
Output is correct |
47 |
Correct |
390 ms |
47136 KB |
Output is correct |
48 |
Correct |
168 ms |
46284 KB |
Output is correct |
49 |
Correct |
117 ms |
46404 KB |
Output is correct |
50 |
Correct |
232 ms |
31584 KB |
Output is correct |
51 |
Correct |
105 ms |
19636 KB |
Output is correct |
52 |
Correct |
349 ms |
53584 KB |
Output is correct |
53 |
Correct |
184 ms |
47388 KB |
Output is correct |
54 |
Correct |
168 ms |
74828 KB |
Output is correct |
55 |
Correct |
293 ms |
49464 KB |
Output is correct |
56 |
Correct |
230 ms |
60216 KB |
Output is correct |
57 |
Correct |
234 ms |
65300 KB |
Output is correct |
58 |
Correct |
298 ms |
61488 KB |
Output is correct |
59 |
Correct |
275 ms |
61508 KB |
Output is correct |
60 |
Correct |
3 ms |
5332 KB |
Output is correct |
61 |
Correct |
3 ms |
5332 KB |
Output is correct |
62 |
Correct |
4 ms |
5424 KB |
Output is correct |
63 |
Correct |
3 ms |
5424 KB |
Output is correct |
64 |
Correct |
362 ms |
27736 KB |
Output is correct |
65 |
Correct |
323 ms |
23592 KB |
Output is correct |
66 |
Correct |
397 ms |
32200 KB |
Output is correct |
67 |
Correct |
277 ms |
24092 KB |
Output is correct |
68 |
Correct |
193 ms |
23072 KB |
Output is correct |
69 |
Correct |
178 ms |
22776 KB |
Output is correct |
70 |
Correct |
238 ms |
59080 KB |
Output is correct |
71 |
Correct |
183 ms |
50204 KB |
Output is correct |
72 |
Correct |
278 ms |
74816 KB |
Output is correct |
73 |
Correct |
217 ms |
74712 KB |
Output is correct |
74 |
Correct |
285 ms |
53600 KB |
Output is correct |
75 |
Correct |
182 ms |
47280 KB |
Output is correct |
76 |
Correct |
148 ms |
47316 KB |
Output is correct |
77 |
Correct |
324 ms |
53640 KB |
Output is correct |
78 |
Correct |
239 ms |
47276 KB |
Output is correct |
79 |
Correct |
397 ms |
54624 KB |
Output is correct |
80 |
Correct |
259 ms |
48820 KB |
Output is correct |
81 |
Correct |
224 ms |
48304 KB |
Output is correct |
82 |
Correct |
251 ms |
74936 KB |
Output is correct |
83 |
Correct |
254 ms |
32380 KB |
Output is correct |
84 |
Correct |
335 ms |
63244 KB |
Output is correct |
85 |
Correct |
383 ms |
63340 KB |
Output is correct |
86 |
Correct |
375 ms |
63136 KB |
Output is correct |
87 |
Correct |
330 ms |
63240 KB |
Output is correct |
88 |
Correct |
361 ms |
63320 KB |
Output is correct |