#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+5;
const int INF = 1e9+5;
vector<int> tree[maxn];
multiset<pair<int, int>, greater<>> st[maxn];
int C[maxn], H[maxn];
void dfs(int x){
for(auto v: tree[x]){
dfs(v);
if(st[v].size() > st[x].size())st[x].swap(st[v]);
}
for(auto v: tree[x]){
for(auto ele: st[v])st[x].insert(ele);
}
st[x].insert({H[x], C[x]});
auto it = st[x].upper_bound({H[x], 0});
while(it != st[x].end() && C[x]){
if(it->second > C[x]){
pair<int, int> nw = *it;
nw.second -= C[x];
st[x].erase(it);
st[x].insert(nw);
C[x] = 0;
}
else {
C[x] -= it->second;
it = st[x].erase(it);
}
}
// cout << "SUS " << x << endl;
// for(auto ele: st[x])cout << ele.first << " " << ele.second << endl;
}
int main() {
int n;
cin >> n;
for(int i = 1; i <= n; i++){
int p;
cin >> p >> H[i] >> C[i];
if(i > 1)tree[p].push_back(i);
}
long long sum = accumulate(C, C+n+1, 0LL);
dfs(1);
for(auto ele: st[1])sum -= ele.second;
cout << sum;
}
// 4 = 4 cost 9
// 6 = 5 cost 6
// 2 = 3 cost 6
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15704 KB |
Output is correct |
2 |
Correct |
3 ms |
15708 KB |
Output is correct |
3 |
Correct |
4 ms |
15708 KB |
Output is correct |
4 |
Correct |
5 ms |
15912 KB |
Output is correct |
5 |
Correct |
10 ms |
16728 KB |
Output is correct |
6 |
Correct |
14 ms |
16728 KB |
Output is correct |
7 |
Correct |
12 ms |
16784 KB |
Output is correct |
8 |
Correct |
11 ms |
16820 KB |
Output is correct |
9 |
Correct |
11 ms |
16732 KB |
Output is correct |
10 |
Correct |
13 ms |
16728 KB |
Output is correct |
11 |
Correct |
11 ms |
16988 KB |
Output is correct |
12 |
Correct |
10 ms |
16732 KB |
Output is correct |
13 |
Correct |
11 ms |
16696 KB |
Output is correct |
14 |
Correct |
10 ms |
16448 KB |
Output is correct |
15 |
Correct |
10 ms |
16508 KB |
Output is correct |
16 |
Correct |
12 ms |
16996 KB |
Output is correct |
17 |
Correct |
13 ms |
17152 KB |
Output is correct |
18 |
Correct |
13 ms |
17432 KB |
Output is correct |
19 |
Correct |
9 ms |
16476 KB |
Output is correct |
20 |
Correct |
11 ms |
16476 KB |
Output is correct |
21 |
Correct |
10 ms |
16568 KB |
Output is correct |
22 |
Correct |
8 ms |
16420 KB |
Output is correct |
23 |
Correct |
9 ms |
16544 KB |
Output is correct |
24 |
Correct |
11 ms |
16564 KB |
Output is correct |
25 |
Correct |
11 ms |
16732 KB |
Output is correct |
26 |
Correct |
10 ms |
16568 KB |
Output is correct |
27 |
Correct |
9 ms |
16592 KB |
Output is correct |
28 |
Correct |
9 ms |
16576 KB |
Output is correct |
29 |
Correct |
9 ms |
16732 KB |
Output is correct |
30 |
Correct |
10 ms |
16620 KB |
Output is correct |
31 |
Correct |
10 ms |
16684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15704 KB |
Output is correct |
2 |
Correct |
3 ms |
15708 KB |
Output is correct |
3 |
Correct |
4 ms |
15708 KB |
Output is correct |
4 |
Correct |
5 ms |
15912 KB |
Output is correct |
5 |
Correct |
10 ms |
16728 KB |
Output is correct |
6 |
Correct |
14 ms |
16728 KB |
Output is correct |
7 |
Correct |
12 ms |
16784 KB |
Output is correct |
8 |
Correct |
11 ms |
16820 KB |
Output is correct |
9 |
Correct |
11 ms |
16732 KB |
Output is correct |
10 |
Correct |
13 ms |
16728 KB |
Output is correct |
11 |
Correct |
11 ms |
16988 KB |
Output is correct |
12 |
Correct |
10 ms |
16732 KB |
Output is correct |
13 |
Correct |
11 ms |
16696 KB |
Output is correct |
14 |
Correct |
10 ms |
16448 KB |
Output is correct |
15 |
Correct |
10 ms |
16508 KB |
Output is correct |
16 |
Correct |
12 ms |
16996 KB |
Output is correct |
17 |
Correct |
13 ms |
17152 KB |
Output is correct |
18 |
Correct |
13 ms |
17432 KB |
Output is correct |
19 |
Correct |
9 ms |
16476 KB |
Output is correct |
20 |
Correct |
11 ms |
16476 KB |
Output is correct |
21 |
Correct |
10 ms |
16568 KB |
Output is correct |
22 |
Correct |
8 ms |
16420 KB |
Output is correct |
23 |
Correct |
9 ms |
16544 KB |
Output is correct |
24 |
Correct |
11 ms |
16564 KB |
Output is correct |
25 |
Correct |
11 ms |
16732 KB |
Output is correct |
26 |
Correct |
10 ms |
16568 KB |
Output is correct |
27 |
Correct |
9 ms |
16592 KB |
Output is correct |
28 |
Correct |
9 ms |
16576 KB |
Output is correct |
29 |
Correct |
9 ms |
16732 KB |
Output is correct |
30 |
Correct |
10 ms |
16620 KB |
Output is correct |
31 |
Correct |
10 ms |
16684 KB |
Output is correct |
32 |
Correct |
12 ms |
16700 KB |
Output is correct |
33 |
Correct |
391 ms |
64360 KB |
Output is correct |
34 |
Correct |
399 ms |
63828 KB |
Output is correct |
35 |
Correct |
407 ms |
62272 KB |
Output is correct |
36 |
Correct |
498 ms |
64408 KB |
Output is correct |
37 |
Correct |
415 ms |
67196 KB |
Output is correct |
38 |
Correct |
404 ms |
72584 KB |
Output is correct |
39 |
Correct |
268 ms |
49368 KB |
Output is correct |
40 |
Correct |
275 ms |
49232 KB |
Output is correct |
41 |
Correct |
311 ms |
51704 KB |
Output is correct |
42 |
Correct |
266 ms |
38480 KB |
Output is correct |
43 |
Correct |
277 ms |
38500 KB |
Output is correct |
44 |
Correct |
382 ms |
83792 KB |
Output is correct |
45 |
Correct |
384 ms |
83868 KB |
Output is correct |
46 |
Correct |
415 ms |
105416 KB |
Output is correct |
47 |
Correct |
302 ms |
45120 KB |
Output is correct |
48 |
Correct |
351 ms |
47952 KB |
Output is correct |
49 |
Correct |
308 ms |
48468 KB |
Output is correct |
50 |
Correct |
232 ms |
39872 KB |
Output is correct |
51 |
Correct |
235 ms |
39772 KB |
Output is correct |
52 |
Correct |
289 ms |
45644 KB |
Output is correct |
53 |
Correct |
304 ms |
48544 KB |
Output is correct |
54 |
Correct |
216 ms |
49736 KB |
Output is correct |
55 |
Correct |
247 ms |
46160 KB |
Output is correct |
56 |
Correct |
231 ms |
51020 KB |
Output is correct |
57 |
Correct |
231 ms |
53584 KB |
Output is correct |
58 |
Correct |
295 ms |
51624 KB |
Output is correct |
59 |
Correct |
252 ms |
51792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15704 KB |
Output is correct |
2 |
Correct |
3 ms |
15708 KB |
Output is correct |
3 |
Correct |
4 ms |
15708 KB |
Output is correct |
4 |
Correct |
5 ms |
15912 KB |
Output is correct |
5 |
Correct |
10 ms |
16728 KB |
Output is correct |
6 |
Correct |
14 ms |
16728 KB |
Output is correct |
7 |
Correct |
12 ms |
16784 KB |
Output is correct |
8 |
Correct |
11 ms |
16820 KB |
Output is correct |
9 |
Correct |
11 ms |
16732 KB |
Output is correct |
10 |
Correct |
13 ms |
16728 KB |
Output is correct |
11 |
Correct |
11 ms |
16988 KB |
Output is correct |
12 |
Correct |
10 ms |
16732 KB |
Output is correct |
13 |
Correct |
11 ms |
16696 KB |
Output is correct |
14 |
Correct |
10 ms |
16448 KB |
Output is correct |
15 |
Correct |
10 ms |
16508 KB |
Output is correct |
16 |
Correct |
12 ms |
16996 KB |
Output is correct |
17 |
Correct |
13 ms |
17152 KB |
Output is correct |
18 |
Correct |
13 ms |
17432 KB |
Output is correct |
19 |
Correct |
9 ms |
16476 KB |
Output is correct |
20 |
Correct |
11 ms |
16476 KB |
Output is correct |
21 |
Correct |
10 ms |
16568 KB |
Output is correct |
22 |
Correct |
8 ms |
16420 KB |
Output is correct |
23 |
Correct |
9 ms |
16544 KB |
Output is correct |
24 |
Correct |
11 ms |
16564 KB |
Output is correct |
25 |
Correct |
11 ms |
16732 KB |
Output is correct |
26 |
Correct |
10 ms |
16568 KB |
Output is correct |
27 |
Correct |
9 ms |
16592 KB |
Output is correct |
28 |
Correct |
9 ms |
16576 KB |
Output is correct |
29 |
Correct |
9 ms |
16732 KB |
Output is correct |
30 |
Correct |
10 ms |
16620 KB |
Output is correct |
31 |
Correct |
10 ms |
16684 KB |
Output is correct |
32 |
Correct |
12 ms |
16700 KB |
Output is correct |
33 |
Correct |
391 ms |
64360 KB |
Output is correct |
34 |
Correct |
399 ms |
63828 KB |
Output is correct |
35 |
Correct |
407 ms |
62272 KB |
Output is correct |
36 |
Correct |
498 ms |
64408 KB |
Output is correct |
37 |
Correct |
415 ms |
67196 KB |
Output is correct |
38 |
Correct |
404 ms |
72584 KB |
Output is correct |
39 |
Correct |
268 ms |
49368 KB |
Output is correct |
40 |
Correct |
275 ms |
49232 KB |
Output is correct |
41 |
Correct |
311 ms |
51704 KB |
Output is correct |
42 |
Correct |
266 ms |
38480 KB |
Output is correct |
43 |
Correct |
277 ms |
38500 KB |
Output is correct |
44 |
Correct |
382 ms |
83792 KB |
Output is correct |
45 |
Correct |
384 ms |
83868 KB |
Output is correct |
46 |
Correct |
415 ms |
105416 KB |
Output is correct |
47 |
Correct |
302 ms |
45120 KB |
Output is correct |
48 |
Correct |
351 ms |
47952 KB |
Output is correct |
49 |
Correct |
308 ms |
48468 KB |
Output is correct |
50 |
Correct |
232 ms |
39872 KB |
Output is correct |
51 |
Correct |
235 ms |
39772 KB |
Output is correct |
52 |
Correct |
289 ms |
45644 KB |
Output is correct |
53 |
Correct |
304 ms |
48544 KB |
Output is correct |
54 |
Correct |
216 ms |
49736 KB |
Output is correct |
55 |
Correct |
247 ms |
46160 KB |
Output is correct |
56 |
Correct |
231 ms |
51020 KB |
Output is correct |
57 |
Correct |
231 ms |
53584 KB |
Output is correct |
58 |
Correct |
295 ms |
51624 KB |
Output is correct |
59 |
Correct |
252 ms |
51792 KB |
Output is correct |
60 |
Correct |
4 ms |
15704 KB |
Output is correct |
61 |
Incorrect |
3 ms |
15708 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |