#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
const ll mod = 1e9 + 7, pr = 31;
const int mxn = 2e5 + 5, mxq = 1e5 + 5, sq = 500, mxv = 5e4 + 1;
//const int base = (1 <<18);
const ll inf = 1e9 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
int n;
vt<int>adj[mxn + 1];
ll a[mxn + 1], h[mxn + 1], c[mxn + 1];
map<ll, ll>mp[mxn + 1];
void dfs(int s, int pre){
for(auto i: adj[s]){
if(i != pre){
dfs(i, s);
if(sz(mp[i]) > sz(mp[s])){
swap(mp[s], mp[i]);
}
for(auto j: mp[i])mp[s][j.fi] += j.se;
}
}
mp[s][h[s]] += c[s];
ll lft = c[s];
while(1){
auto it = mp[s].lower_bound(h[s]);
if(it == mp[s].begin())break;
--it;
if((*it).se <= lft){
lft -= (*it).se; mp[s].erase(it);
}else{
(*it).se -= lft; break;
}
}
}
void solve(){
cin >> n;
ll tot = 0;
for(int i = 1; i <= n; i++){
cin >> a[i] >> h[i] >> c[i]; tot += c[i];
if(i != 1)adj[a[i]].pb(i);
}
dfs(1, -1);
ll ans = 0;
for(auto i: mp[1]){
ans += i.se;
}
cout << tot - ans;
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("DATA.inp", "r", stdin);
//freopen("DATA.out", "w", stdout);
int tt; tt = 1;
while(tt--){
solve();
}
return(0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
19036 KB |
Output is correct |
2 |
Correct |
5 ms |
19036 KB |
Output is correct |
3 |
Correct |
5 ms |
19036 KB |
Output is correct |
4 |
Correct |
4 ms |
19036 KB |
Output is correct |
5 |
Correct |
8 ms |
20316 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
7 ms |
19548 KB |
Output is correct |
8 |
Correct |
9 ms |
20284 KB |
Output is correct |
9 |
Correct |
7 ms |
19976 KB |
Output is correct |
10 |
Correct |
7 ms |
19548 KB |
Output is correct |
11 |
Correct |
6 ms |
19548 KB |
Output is correct |
12 |
Correct |
7 ms |
20020 KB |
Output is correct |
13 |
Correct |
6 ms |
20152 KB |
Output is correct |
14 |
Correct |
7 ms |
19804 KB |
Output is correct |
15 |
Correct |
6 ms |
19804 KB |
Output is correct |
16 |
Correct |
9 ms |
20572 KB |
Output is correct |
17 |
Correct |
7 ms |
20140 KB |
Output is correct |
18 |
Correct |
7 ms |
19516 KB |
Output is correct |
19 |
Correct |
7 ms |
20060 KB |
Output is correct |
20 |
Correct |
7 ms |
19804 KB |
Output is correct |
21 |
Correct |
7 ms |
19900 KB |
Output is correct |
22 |
Correct |
6 ms |
19772 KB |
Output is correct |
23 |
Correct |
5 ms |
19548 KB |
Output is correct |
24 |
Correct |
8 ms |
20060 KB |
Output is correct |
25 |
Correct |
7 ms |
19892 KB |
Output is correct |
26 |
Correct |
6 ms |
20060 KB |
Output is correct |
27 |
Correct |
6 ms |
20060 KB |
Output is correct |
28 |
Correct |
6 ms |
20056 KB |
Output is correct |
29 |
Correct |
7 ms |
20320 KB |
Output is correct |
30 |
Correct |
7 ms |
20316 KB |
Output is correct |
31 |
Correct |
7 ms |
20268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
19036 KB |
Output is correct |
2 |
Correct |
5 ms |
19036 KB |
Output is correct |
3 |
Correct |
5 ms |
19036 KB |
Output is correct |
4 |
Correct |
4 ms |
19036 KB |
Output is correct |
5 |
Correct |
8 ms |
20316 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
7 ms |
19548 KB |
Output is correct |
8 |
Correct |
9 ms |
20284 KB |
Output is correct |
9 |
Correct |
7 ms |
19976 KB |
Output is correct |
10 |
Correct |
7 ms |
19548 KB |
Output is correct |
11 |
Correct |
6 ms |
19548 KB |
Output is correct |
12 |
Correct |
7 ms |
20020 KB |
Output is correct |
13 |
Correct |
6 ms |
20152 KB |
Output is correct |
14 |
Correct |
7 ms |
19804 KB |
Output is correct |
15 |
Correct |
6 ms |
19804 KB |
Output is correct |
16 |
Correct |
9 ms |
20572 KB |
Output is correct |
17 |
Correct |
7 ms |
20140 KB |
Output is correct |
18 |
Correct |
7 ms |
19516 KB |
Output is correct |
19 |
Correct |
7 ms |
20060 KB |
Output is correct |
20 |
Correct |
7 ms |
19804 KB |
Output is correct |
21 |
Correct |
7 ms |
19900 KB |
Output is correct |
22 |
Correct |
6 ms |
19772 KB |
Output is correct |
23 |
Correct |
5 ms |
19548 KB |
Output is correct |
24 |
Correct |
8 ms |
20060 KB |
Output is correct |
25 |
Correct |
7 ms |
19892 KB |
Output is correct |
26 |
Correct |
6 ms |
20060 KB |
Output is correct |
27 |
Correct |
6 ms |
20060 KB |
Output is correct |
28 |
Correct |
6 ms |
20056 KB |
Output is correct |
29 |
Correct |
7 ms |
20320 KB |
Output is correct |
30 |
Correct |
7 ms |
20316 KB |
Output is correct |
31 |
Correct |
7 ms |
20268 KB |
Output is correct |
32 |
Correct |
8 ms |
20316 KB |
Output is correct |
33 |
Correct |
309 ms |
81932 KB |
Output is correct |
34 |
Correct |
234 ms |
56920 KB |
Output is correct |
35 |
Correct |
259 ms |
78676 KB |
Output is correct |
36 |
Correct |
220 ms |
56864 KB |
Output is correct |
37 |
Correct |
142 ms |
38476 KB |
Output is correct |
38 |
Correct |
115 ms |
33360 KB |
Output is correct |
39 |
Correct |
124 ms |
58692 KB |
Output is correct |
40 |
Correct |
113 ms |
58792 KB |
Output is correct |
41 |
Correct |
87 ms |
58816 KB |
Output is correct |
42 |
Correct |
113 ms |
44648 KB |
Output is correct |
43 |
Correct |
104 ms |
44692 KB |
Output is correct |
44 |
Correct |
304 ms |
106900 KB |
Output is correct |
45 |
Correct |
188 ms |
71140 KB |
Output is correct |
46 |
Correct |
63 ms |
33528 KB |
Output is correct |
47 |
Correct |
183 ms |
54728 KB |
Output is correct |
48 |
Correct |
91 ms |
47772 KB |
Output is correct |
49 |
Correct |
74 ms |
47580 KB |
Output is correct |
50 |
Correct |
216 ms |
49260 KB |
Output is correct |
51 |
Correct |
73 ms |
36796 KB |
Output is correct |
52 |
Correct |
201 ms |
55252 KB |
Output is correct |
53 |
Correct |
84 ms |
48300 KB |
Output is correct |
54 |
Correct |
81 ms |
59016 KB |
Output is correct |
55 |
Correct |
172 ms |
56160 KB |
Output is correct |
56 |
Correct |
146 ms |
62184 KB |
Output is correct |
57 |
Correct |
119 ms |
65124 KB |
Output is correct |
58 |
Correct |
157 ms |
62564 KB |
Output is correct |
59 |
Correct |
152 ms |
62800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
19036 KB |
Output is correct |
2 |
Correct |
5 ms |
19036 KB |
Output is correct |
3 |
Correct |
5 ms |
19036 KB |
Output is correct |
4 |
Correct |
4 ms |
19036 KB |
Output is correct |
5 |
Correct |
8 ms |
20316 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
7 ms |
19548 KB |
Output is correct |
8 |
Correct |
9 ms |
20284 KB |
Output is correct |
9 |
Correct |
7 ms |
19976 KB |
Output is correct |
10 |
Correct |
7 ms |
19548 KB |
Output is correct |
11 |
Correct |
6 ms |
19548 KB |
Output is correct |
12 |
Correct |
7 ms |
20020 KB |
Output is correct |
13 |
Correct |
6 ms |
20152 KB |
Output is correct |
14 |
Correct |
7 ms |
19804 KB |
Output is correct |
15 |
Correct |
6 ms |
19804 KB |
Output is correct |
16 |
Correct |
9 ms |
20572 KB |
Output is correct |
17 |
Correct |
7 ms |
20140 KB |
Output is correct |
18 |
Correct |
7 ms |
19516 KB |
Output is correct |
19 |
Correct |
7 ms |
20060 KB |
Output is correct |
20 |
Correct |
7 ms |
19804 KB |
Output is correct |
21 |
Correct |
7 ms |
19900 KB |
Output is correct |
22 |
Correct |
6 ms |
19772 KB |
Output is correct |
23 |
Correct |
5 ms |
19548 KB |
Output is correct |
24 |
Correct |
8 ms |
20060 KB |
Output is correct |
25 |
Correct |
7 ms |
19892 KB |
Output is correct |
26 |
Correct |
6 ms |
20060 KB |
Output is correct |
27 |
Correct |
6 ms |
20060 KB |
Output is correct |
28 |
Correct |
6 ms |
20056 KB |
Output is correct |
29 |
Correct |
7 ms |
20320 KB |
Output is correct |
30 |
Correct |
7 ms |
20316 KB |
Output is correct |
31 |
Correct |
7 ms |
20268 KB |
Output is correct |
32 |
Correct |
8 ms |
20316 KB |
Output is correct |
33 |
Correct |
309 ms |
81932 KB |
Output is correct |
34 |
Correct |
234 ms |
56920 KB |
Output is correct |
35 |
Correct |
259 ms |
78676 KB |
Output is correct |
36 |
Correct |
220 ms |
56864 KB |
Output is correct |
37 |
Correct |
142 ms |
38476 KB |
Output is correct |
38 |
Correct |
115 ms |
33360 KB |
Output is correct |
39 |
Correct |
124 ms |
58692 KB |
Output is correct |
40 |
Correct |
113 ms |
58792 KB |
Output is correct |
41 |
Correct |
87 ms |
58816 KB |
Output is correct |
42 |
Correct |
113 ms |
44648 KB |
Output is correct |
43 |
Correct |
104 ms |
44692 KB |
Output is correct |
44 |
Correct |
304 ms |
106900 KB |
Output is correct |
45 |
Correct |
188 ms |
71140 KB |
Output is correct |
46 |
Correct |
63 ms |
33528 KB |
Output is correct |
47 |
Correct |
183 ms |
54728 KB |
Output is correct |
48 |
Correct |
91 ms |
47772 KB |
Output is correct |
49 |
Correct |
74 ms |
47580 KB |
Output is correct |
50 |
Correct |
216 ms |
49260 KB |
Output is correct |
51 |
Correct |
73 ms |
36796 KB |
Output is correct |
52 |
Correct |
201 ms |
55252 KB |
Output is correct |
53 |
Correct |
84 ms |
48300 KB |
Output is correct |
54 |
Correct |
81 ms |
59016 KB |
Output is correct |
55 |
Correct |
172 ms |
56160 KB |
Output is correct |
56 |
Correct |
146 ms |
62184 KB |
Output is correct |
57 |
Correct |
119 ms |
65124 KB |
Output is correct |
58 |
Correct |
157 ms |
62564 KB |
Output is correct |
59 |
Correct |
152 ms |
62800 KB |
Output is correct |
60 |
Correct |
4 ms |
19032 KB |
Output is correct |
61 |
Incorrect |
4 ms |
19288 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |