#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define SZ(x) ll(x.size())
const ll MAXN = 1e6 + 10;
const ll LOG = 22;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; //998244353; //1e9 + 9;
ll n , sum , A[MAXN] , H[MAXN] , C[MAXN];
vector<int> adj[MAXN];
multiset<pii> st[MAXN];
void DFS(int v){
for(int u : adj[v]){
DFS(u);
if(st[u].size() > st[v].size()){
st[v].swap(st[u]);
}
for(auto &i : st[u]){
st[v].insert(i);
}
}
int x = C[v];
while(x > 0){
auto it = st[v].lower_bound({H[v] + 1 , -MOD});
if(it == st[v].end()) break;
pii A = (*it);
st[v].erase(it);
int mn = min(A.Y , x);
A.Y -= mn; x -= mn;
if(A.Y > 0){
st[v].insert(A);
}
}
st[v].insert({H[v] , C[v]});
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1 ; i <= n ; i++){
cin >> A[i] >> H[i] >> C[i];
if(i != 1){
adj[A[i]].push_back(i);
}
sum += C[i];
H[i] = MOD - H[i];
}
DFS(1);
for(auto &i : st[1]){
sum -= i.Y;
}
cout << sum << endl;
return 0;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
70736 KB |
Output is correct |
2 |
Correct |
42 ms |
70748 KB |
Output is correct |
3 |
Correct |
34 ms |
70660 KB |
Output is correct |
4 |
Correct |
33 ms |
70756 KB |
Output is correct |
5 |
Correct |
37 ms |
71804 KB |
Output is correct |
6 |
Correct |
41 ms |
71728 KB |
Output is correct |
7 |
Correct |
38 ms |
71672 KB |
Output is correct |
8 |
Correct |
42 ms |
71824 KB |
Output is correct |
9 |
Correct |
39 ms |
71860 KB |
Output is correct |
10 |
Correct |
48 ms |
71788 KB |
Output is correct |
11 |
Correct |
41 ms |
72036 KB |
Output is correct |
12 |
Correct |
38 ms |
71656 KB |
Output is correct |
13 |
Correct |
42 ms |
71740 KB |
Output is correct |
14 |
Correct |
54 ms |
71408 KB |
Output is correct |
15 |
Correct |
57 ms |
71364 KB |
Output is correct |
16 |
Correct |
46 ms |
72100 KB |
Output is correct |
17 |
Correct |
48 ms |
72120 KB |
Output is correct |
18 |
Correct |
40 ms |
72476 KB |
Output is correct |
19 |
Correct |
37 ms |
71548 KB |
Output is correct |
20 |
Correct |
40 ms |
71540 KB |
Output is correct |
21 |
Correct |
47 ms |
71572 KB |
Output is correct |
22 |
Correct |
53 ms |
71496 KB |
Output is correct |
23 |
Correct |
38 ms |
71404 KB |
Output is correct |
24 |
Correct |
40 ms |
71536 KB |
Output is correct |
25 |
Correct |
47 ms |
71612 KB |
Output is correct |
26 |
Correct |
39 ms |
71664 KB |
Output is correct |
27 |
Correct |
41 ms |
71492 KB |
Output is correct |
28 |
Correct |
41 ms |
71600 KB |
Output is correct |
29 |
Correct |
40 ms |
71792 KB |
Output is correct |
30 |
Correct |
38 ms |
71768 KB |
Output is correct |
31 |
Correct |
41 ms |
71724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
70736 KB |
Output is correct |
2 |
Correct |
42 ms |
70748 KB |
Output is correct |
3 |
Correct |
34 ms |
70660 KB |
Output is correct |
4 |
Correct |
33 ms |
70756 KB |
Output is correct |
5 |
Correct |
37 ms |
71804 KB |
Output is correct |
6 |
Correct |
41 ms |
71728 KB |
Output is correct |
7 |
Correct |
38 ms |
71672 KB |
Output is correct |
8 |
Correct |
42 ms |
71824 KB |
Output is correct |
9 |
Correct |
39 ms |
71860 KB |
Output is correct |
10 |
Correct |
48 ms |
71788 KB |
Output is correct |
11 |
Correct |
41 ms |
72036 KB |
Output is correct |
12 |
Correct |
38 ms |
71656 KB |
Output is correct |
13 |
Correct |
42 ms |
71740 KB |
Output is correct |
14 |
Correct |
54 ms |
71408 KB |
Output is correct |
15 |
Correct |
57 ms |
71364 KB |
Output is correct |
16 |
Correct |
46 ms |
72100 KB |
Output is correct |
17 |
Correct |
48 ms |
72120 KB |
Output is correct |
18 |
Correct |
40 ms |
72476 KB |
Output is correct |
19 |
Correct |
37 ms |
71548 KB |
Output is correct |
20 |
Correct |
40 ms |
71540 KB |
Output is correct |
21 |
Correct |
47 ms |
71572 KB |
Output is correct |
22 |
Correct |
53 ms |
71496 KB |
Output is correct |
23 |
Correct |
38 ms |
71404 KB |
Output is correct |
24 |
Correct |
40 ms |
71536 KB |
Output is correct |
25 |
Correct |
47 ms |
71612 KB |
Output is correct |
26 |
Correct |
39 ms |
71664 KB |
Output is correct |
27 |
Correct |
41 ms |
71492 KB |
Output is correct |
28 |
Correct |
41 ms |
71600 KB |
Output is correct |
29 |
Correct |
40 ms |
71792 KB |
Output is correct |
30 |
Correct |
38 ms |
71768 KB |
Output is correct |
31 |
Correct |
41 ms |
71724 KB |
Output is correct |
32 |
Correct |
42 ms |
71756 KB |
Output is correct |
33 |
Correct |
326 ms |
124452 KB |
Output is correct |
34 |
Correct |
332 ms |
123644 KB |
Output is correct |
35 |
Correct |
349 ms |
121772 KB |
Output is correct |
36 |
Correct |
363 ms |
123800 KB |
Output is correct |
37 |
Correct |
323 ms |
121816 KB |
Output is correct |
38 |
Correct |
386 ms |
133052 KB |
Output is correct |
39 |
Correct |
207 ms |
108796 KB |
Output is correct |
40 |
Correct |
183 ms |
108780 KB |
Output is correct |
41 |
Correct |
171 ms |
110568 KB |
Output is correct |
42 |
Correct |
192 ms |
97920 KB |
Output is correct |
43 |
Correct |
174 ms |
97832 KB |
Output is correct |
44 |
Correct |
312 ms |
143196 KB |
Output is correct |
45 |
Correct |
302 ms |
143336 KB |
Output is correct |
46 |
Correct |
355 ms |
164892 KB |
Output is correct |
47 |
Correct |
240 ms |
104424 KB |
Output is correct |
48 |
Correct |
236 ms |
102056 KB |
Output is correct |
49 |
Correct |
198 ms |
103256 KB |
Output is correct |
50 |
Correct |
232 ms |
99188 KB |
Output is correct |
51 |
Correct |
244 ms |
99212 KB |
Output is correct |
52 |
Correct |
254 ms |
105148 KB |
Output is correct |
53 |
Correct |
262 ms |
104348 KB |
Output is correct |
54 |
Correct |
142 ms |
108884 KB |
Output is correct |
55 |
Correct |
216 ms |
105552 KB |
Output is correct |
56 |
Correct |
184 ms |
110388 KB |
Output is correct |
57 |
Correct |
188 ms |
112880 KB |
Output is correct |
58 |
Correct |
197 ms |
111300 KB |
Output is correct |
59 |
Correct |
208 ms |
111196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
70736 KB |
Output is correct |
2 |
Correct |
42 ms |
70748 KB |
Output is correct |
3 |
Correct |
34 ms |
70660 KB |
Output is correct |
4 |
Correct |
33 ms |
70756 KB |
Output is correct |
5 |
Correct |
37 ms |
71804 KB |
Output is correct |
6 |
Correct |
41 ms |
71728 KB |
Output is correct |
7 |
Correct |
38 ms |
71672 KB |
Output is correct |
8 |
Correct |
42 ms |
71824 KB |
Output is correct |
9 |
Correct |
39 ms |
71860 KB |
Output is correct |
10 |
Correct |
48 ms |
71788 KB |
Output is correct |
11 |
Correct |
41 ms |
72036 KB |
Output is correct |
12 |
Correct |
38 ms |
71656 KB |
Output is correct |
13 |
Correct |
42 ms |
71740 KB |
Output is correct |
14 |
Correct |
54 ms |
71408 KB |
Output is correct |
15 |
Correct |
57 ms |
71364 KB |
Output is correct |
16 |
Correct |
46 ms |
72100 KB |
Output is correct |
17 |
Correct |
48 ms |
72120 KB |
Output is correct |
18 |
Correct |
40 ms |
72476 KB |
Output is correct |
19 |
Correct |
37 ms |
71548 KB |
Output is correct |
20 |
Correct |
40 ms |
71540 KB |
Output is correct |
21 |
Correct |
47 ms |
71572 KB |
Output is correct |
22 |
Correct |
53 ms |
71496 KB |
Output is correct |
23 |
Correct |
38 ms |
71404 KB |
Output is correct |
24 |
Correct |
40 ms |
71536 KB |
Output is correct |
25 |
Correct |
47 ms |
71612 KB |
Output is correct |
26 |
Correct |
39 ms |
71664 KB |
Output is correct |
27 |
Correct |
41 ms |
71492 KB |
Output is correct |
28 |
Correct |
41 ms |
71600 KB |
Output is correct |
29 |
Correct |
40 ms |
71792 KB |
Output is correct |
30 |
Correct |
38 ms |
71768 KB |
Output is correct |
31 |
Correct |
41 ms |
71724 KB |
Output is correct |
32 |
Correct |
42 ms |
71756 KB |
Output is correct |
33 |
Correct |
326 ms |
124452 KB |
Output is correct |
34 |
Correct |
332 ms |
123644 KB |
Output is correct |
35 |
Correct |
349 ms |
121772 KB |
Output is correct |
36 |
Correct |
363 ms |
123800 KB |
Output is correct |
37 |
Correct |
323 ms |
121816 KB |
Output is correct |
38 |
Correct |
386 ms |
133052 KB |
Output is correct |
39 |
Correct |
207 ms |
108796 KB |
Output is correct |
40 |
Correct |
183 ms |
108780 KB |
Output is correct |
41 |
Correct |
171 ms |
110568 KB |
Output is correct |
42 |
Correct |
192 ms |
97920 KB |
Output is correct |
43 |
Correct |
174 ms |
97832 KB |
Output is correct |
44 |
Correct |
312 ms |
143196 KB |
Output is correct |
45 |
Correct |
302 ms |
143336 KB |
Output is correct |
46 |
Correct |
355 ms |
164892 KB |
Output is correct |
47 |
Correct |
240 ms |
104424 KB |
Output is correct |
48 |
Correct |
236 ms |
102056 KB |
Output is correct |
49 |
Correct |
198 ms |
103256 KB |
Output is correct |
50 |
Correct |
232 ms |
99188 KB |
Output is correct |
51 |
Correct |
244 ms |
99212 KB |
Output is correct |
52 |
Correct |
254 ms |
105148 KB |
Output is correct |
53 |
Correct |
262 ms |
104348 KB |
Output is correct |
54 |
Correct |
142 ms |
108884 KB |
Output is correct |
55 |
Correct |
216 ms |
105552 KB |
Output is correct |
56 |
Correct |
184 ms |
110388 KB |
Output is correct |
57 |
Correct |
188 ms |
112880 KB |
Output is correct |
58 |
Correct |
197 ms |
111300 KB |
Output is correct |
59 |
Correct |
208 ms |
111196 KB |
Output is correct |
60 |
Correct |
37 ms |
70732 KB |
Output is correct |
61 |
Incorrect |
45 ms |
70752 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |