/*
#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){
// cout << node << endl;
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.erase(mp.begin());
// for(ii pr : mp) cout << pr.fi << " " << pr.se << endl;
int ans = 0;
for(ii pr : mp) ans += pr.se;
cout << sum - ans << endl;
exit(0);
}
// mp[mod] += cost[node];
int pon = val[node] - 1;
mp[pon] -= cost[node];
mp[pon + 1] += cost[node];
// for(ii pr : mp) cout << pr.fi << " " << pr.se << endl;
// cout << pon;
// cout << endl;
while(mp[pon] <= 0){
auto itr = prev(mp.find(pon));
itr->se += mp[pon];
mp.erase(pon);
pon = itr->fi;
}
mp[0] = 0;
// for(ii pr : mp) cout << pr.fi << " " << pr.se << endl;
// cout << pon;
// cout << endl;
}
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;
for1(i, 1, n) if(trigg[i]){
vector<ii> cac; int cop = i;
while(1){
cac.pb(ii(val[cop], cop));
cop = a[cop];
// cout << cop << " " << i << endl;
if(cop == i) break;
}
cac.pb(ii(0, 0));
sort(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);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
7 ms |
5716 KB |
Output is correct |
6 |
Correct |
10 ms |
5460 KB |
Output is correct |
7 |
Correct |
8 ms |
5460 KB |
Output is correct |
8 |
Correct |
9 ms |
5716 KB |
Output is correct |
9 |
Correct |
7 ms |
5564 KB |
Output is correct |
10 |
Correct |
6 ms |
5460 KB |
Output is correct |
11 |
Correct |
8 ms |
5460 KB |
Output is correct |
12 |
Correct |
8 ms |
6740 KB |
Output is correct |
13 |
Correct |
6 ms |
6852 KB |
Output is correct |
14 |
Correct |
8 ms |
6228 KB |
Output is correct |
15 |
Correct |
7 ms |
6228 KB |
Output is correct |
16 |
Correct |
7 ms |
5716 KB |
Output is correct |
17 |
Correct |
6 ms |
5532 KB |
Output is correct |
18 |
Correct |
6 ms |
5460 KB |
Output is correct |
19 |
Correct |
8 ms |
6100 KB |
Output is correct |
20 |
Correct |
6 ms |
6100 KB |
Output is correct |
21 |
Correct |
6 ms |
6100 KB |
Output is correct |
22 |
Correct |
8 ms |
5680 KB |
Output is correct |
23 |
Correct |
5 ms |
5460 KB |
Output is correct |
24 |
Correct |
9 ms |
6304 KB |
Output is correct |
25 |
Correct |
6 ms |
6100 KB |
Output is correct |
26 |
Correct |
6 ms |
6740 KB |
Output is correct |
27 |
Correct |
6 ms |
6100 KB |
Output is correct |
28 |
Correct |
7 ms |
6228 KB |
Output is correct |
29 |
Correct |
7 ms |
6484 KB |
Output is correct |
30 |
Correct |
8 ms |
6484 KB |
Output is correct |
31 |
Correct |
8 ms |
6484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
7 ms |
5716 KB |
Output is correct |
6 |
Correct |
10 ms |
5460 KB |
Output is correct |
7 |
Correct |
8 ms |
5460 KB |
Output is correct |
8 |
Correct |
9 ms |
5716 KB |
Output is correct |
9 |
Correct |
7 ms |
5564 KB |
Output is correct |
10 |
Correct |
6 ms |
5460 KB |
Output is correct |
11 |
Correct |
8 ms |
5460 KB |
Output is correct |
12 |
Correct |
8 ms |
6740 KB |
Output is correct |
13 |
Correct |
6 ms |
6852 KB |
Output is correct |
14 |
Correct |
8 ms |
6228 KB |
Output is correct |
15 |
Correct |
7 ms |
6228 KB |
Output is correct |
16 |
Correct |
7 ms |
5716 KB |
Output is correct |
17 |
Correct |
6 ms |
5532 KB |
Output is correct |
18 |
Correct |
6 ms |
5460 KB |
Output is correct |
19 |
Correct |
8 ms |
6100 KB |
Output is correct |
20 |
Correct |
6 ms |
6100 KB |
Output is correct |
21 |
Correct |
6 ms |
6100 KB |
Output is correct |
22 |
Correct |
8 ms |
5680 KB |
Output is correct |
23 |
Correct |
5 ms |
5460 KB |
Output is correct |
24 |
Correct |
9 ms |
6304 KB |
Output is correct |
25 |
Correct |
6 ms |
6100 KB |
Output is correct |
26 |
Correct |
6 ms |
6740 KB |
Output is correct |
27 |
Correct |
6 ms |
6100 KB |
Output is correct |
28 |
Correct |
7 ms |
6228 KB |
Output is correct |
29 |
Correct |
7 ms |
6484 KB |
Output is correct |
30 |
Correct |
8 ms |
6484 KB |
Output is correct |
31 |
Correct |
8 ms |
6484 KB |
Output is correct |
32 |
Correct |
8 ms |
5716 KB |
Output is correct |
33 |
Correct |
318 ms |
29012 KB |
Output is correct |
34 |
Correct |
228 ms |
17196 KB |
Output is correct |
35 |
Correct |
350 ms |
27012 KB |
Output is correct |
36 |
Correct |
215 ms |
17176 KB |
Output is correct |
37 |
Correct |
161 ms |
16816 KB |
Output is correct |
38 |
Correct |
151 ms |
16692 KB |
Output is correct |
39 |
Correct |
258 ms |
69384 KB |
Output is correct |
40 |
Correct |
232 ms |
69324 KB |
Output is correct |
41 |
Correct |
131 ms |
69340 KB |
Output is correct |
42 |
Correct |
266 ms |
44492 KB |
Output is correct |
43 |
Correct |
245 ms |
44360 KB |
Output is correct |
44 |
Correct |
280 ms |
27684 KB |
Output is correct |
45 |
Correct |
186 ms |
15848 KB |
Output is correct |
46 |
Correct |
97 ms |
15388 KB |
Output is correct |
47 |
Correct |
337 ms |
41288 KB |
Output is correct |
48 |
Correct |
155 ms |
40420 KB |
Output is correct |
49 |
Correct |
116 ms |
40428 KB |
Output is correct |
50 |
Correct |
193 ms |
25564 KB |
Output is correct |
51 |
Correct |
97 ms |
13640 KB |
Output is correct |
52 |
Correct |
337 ms |
47412 KB |
Output is correct |
53 |
Correct |
152 ms |
41292 KB |
Output is correct |
54 |
Correct |
155 ms |
69356 KB |
Output is correct |
55 |
Correct |
233 ms |
44200 KB |
Output is correct |
56 |
Correct |
232 ms |
55516 KB |
Output is correct |
57 |
Correct |
223 ms |
60736 KB |
Output is correct |
58 |
Correct |
270 ms |
56800 KB |
Output is correct |
59 |
Correct |
274 ms |
56852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
7 ms |
5716 KB |
Output is correct |
6 |
Correct |
10 ms |
5460 KB |
Output is correct |
7 |
Correct |
8 ms |
5460 KB |
Output is correct |
8 |
Correct |
9 ms |
5716 KB |
Output is correct |
9 |
Correct |
7 ms |
5564 KB |
Output is correct |
10 |
Correct |
6 ms |
5460 KB |
Output is correct |
11 |
Correct |
8 ms |
5460 KB |
Output is correct |
12 |
Correct |
8 ms |
6740 KB |
Output is correct |
13 |
Correct |
6 ms |
6852 KB |
Output is correct |
14 |
Correct |
8 ms |
6228 KB |
Output is correct |
15 |
Correct |
7 ms |
6228 KB |
Output is correct |
16 |
Correct |
7 ms |
5716 KB |
Output is correct |
17 |
Correct |
6 ms |
5532 KB |
Output is correct |
18 |
Correct |
6 ms |
5460 KB |
Output is correct |
19 |
Correct |
8 ms |
6100 KB |
Output is correct |
20 |
Correct |
6 ms |
6100 KB |
Output is correct |
21 |
Correct |
6 ms |
6100 KB |
Output is correct |
22 |
Correct |
8 ms |
5680 KB |
Output is correct |
23 |
Correct |
5 ms |
5460 KB |
Output is correct |
24 |
Correct |
9 ms |
6304 KB |
Output is correct |
25 |
Correct |
6 ms |
6100 KB |
Output is correct |
26 |
Correct |
6 ms |
6740 KB |
Output is correct |
27 |
Correct |
6 ms |
6100 KB |
Output is correct |
28 |
Correct |
7 ms |
6228 KB |
Output is correct |
29 |
Correct |
7 ms |
6484 KB |
Output is correct |
30 |
Correct |
8 ms |
6484 KB |
Output is correct |
31 |
Correct |
8 ms |
6484 KB |
Output is correct |
32 |
Correct |
8 ms |
5716 KB |
Output is correct |
33 |
Correct |
318 ms |
29012 KB |
Output is correct |
34 |
Correct |
228 ms |
17196 KB |
Output is correct |
35 |
Correct |
350 ms |
27012 KB |
Output is correct |
36 |
Correct |
215 ms |
17176 KB |
Output is correct |
37 |
Correct |
161 ms |
16816 KB |
Output is correct |
38 |
Correct |
151 ms |
16692 KB |
Output is correct |
39 |
Correct |
258 ms |
69384 KB |
Output is correct |
40 |
Correct |
232 ms |
69324 KB |
Output is correct |
41 |
Correct |
131 ms |
69340 KB |
Output is correct |
42 |
Correct |
266 ms |
44492 KB |
Output is correct |
43 |
Correct |
245 ms |
44360 KB |
Output is correct |
44 |
Correct |
280 ms |
27684 KB |
Output is correct |
45 |
Correct |
186 ms |
15848 KB |
Output is correct |
46 |
Correct |
97 ms |
15388 KB |
Output is correct |
47 |
Correct |
337 ms |
41288 KB |
Output is correct |
48 |
Correct |
155 ms |
40420 KB |
Output is correct |
49 |
Correct |
116 ms |
40428 KB |
Output is correct |
50 |
Correct |
193 ms |
25564 KB |
Output is correct |
51 |
Correct |
97 ms |
13640 KB |
Output is correct |
52 |
Correct |
337 ms |
47412 KB |
Output is correct |
53 |
Correct |
152 ms |
41292 KB |
Output is correct |
54 |
Correct |
155 ms |
69356 KB |
Output is correct |
55 |
Correct |
233 ms |
44200 KB |
Output is correct |
56 |
Correct |
232 ms |
55516 KB |
Output is correct |
57 |
Correct |
223 ms |
60736 KB |
Output is correct |
58 |
Correct |
270 ms |
56800 KB |
Output is correct |
59 |
Correct |
274 ms |
56852 KB |
Output is correct |
60 |
Incorrect |
3 ms |
5204 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |