/*
Magic Tree
https://oj.uz/problem/view/CEOI19_magictree
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, m, k; cin >> n >> m >> k;
static int p[N];
for (int i = 1; i < n; i++) cin >> p[i], p[i]--;
static pair<int, int> e[N];
while (m--) {
int i, d, w; cin >> i >> d >> w, i--;
e[i] = {d, w};
}
static map<int, long long> f[N];
for (int i = n - 1; i > 0; i--) {
if (e[i] != make_pair(0, 0)) {
auto [d, w] = e[i];
f[i][d] += w;
auto it = f[i].upper_bound(d);
while (w > 0 && it != f[i].end()) {
long long u = min((long long) w, it->second);
w -= u, it->second -= u, ++it;
}
}
int j = p[i];
if (f[i].size() > f[j].size()) swap(f[i], f[j]);
for (auto [k, v] : f[i]) f[j][k] += v;
}
long long ans = 0;
for (auto [k, v] : f[0]) ans += v;
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4932 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
16832 KB |
Output is correct |
2 |
Correct |
26 ms |
9600 KB |
Output is correct |
3 |
Correct |
146 ms |
39864 KB |
Output is correct |
4 |
Correct |
83 ms |
17592 KB |
Output is correct |
5 |
Correct |
101 ms |
22784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
58 ms |
11980 KB |
Output is correct |
5 |
Correct |
57 ms |
12056 KB |
Output is correct |
6 |
Correct |
279 ms |
12072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
12212 KB |
Output is correct |
2 |
Correct |
39 ms |
11064 KB |
Output is correct |
3 |
Correct |
36 ms |
9500 KB |
Output is correct |
4 |
Correct |
47 ms |
13512 KB |
Output is correct |
5 |
Correct |
36 ms |
8288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4932 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4936 KB |
Output is correct |
10 |
Correct |
62 ms |
16576 KB |
Output is correct |
11 |
Correct |
51 ms |
14796 KB |
Output is correct |
12 |
Correct |
32 ms |
8896 KB |
Output is correct |
13 |
Correct |
36 ms |
12836 KB |
Output is correct |
14 |
Correct |
30 ms |
7612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5588 KB |
Output is correct |
2 |
Correct |
12 ms |
6860 KB |
Output is correct |
3 |
Correct |
12 ms |
6860 KB |
Output is correct |
4 |
Correct |
12 ms |
6992 KB |
Output is correct |
5 |
Correct |
11 ms |
6460 KB |
Output is correct |
6 |
Correct |
12 ms |
6824 KB |
Output is correct |
7 |
Correct |
13 ms |
6812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4932 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4936 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
58 ms |
11980 KB |
Output is correct |
14 |
Correct |
57 ms |
12056 KB |
Output is correct |
15 |
Correct |
279 ms |
12072 KB |
Output is correct |
16 |
Correct |
62 ms |
16576 KB |
Output is correct |
17 |
Correct |
51 ms |
14796 KB |
Output is correct |
18 |
Correct |
32 ms |
8896 KB |
Output is correct |
19 |
Correct |
36 ms |
12836 KB |
Output is correct |
20 |
Correct |
30 ms |
7612 KB |
Output is correct |
21 |
Correct |
19 ms |
9812 KB |
Output is correct |
22 |
Correct |
68 ms |
18920 KB |
Output is correct |
23 |
Correct |
76 ms |
23992 KB |
Output is correct |
24 |
Correct |
147 ms |
40128 KB |
Output is correct |
25 |
Correct |
67 ms |
16820 KB |
Output is correct |
26 |
Correct |
101 ms |
19204 KB |
Output is correct |
27 |
Correct |
92 ms |
15180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4932 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4936 KB |
Output is correct |
10 |
Correct |
44 ms |
16832 KB |
Output is correct |
11 |
Correct |
26 ms |
9600 KB |
Output is correct |
12 |
Correct |
146 ms |
39864 KB |
Output is correct |
13 |
Correct |
83 ms |
17592 KB |
Output is correct |
14 |
Correct |
101 ms |
22784 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
58 ms |
11980 KB |
Output is correct |
19 |
Correct |
57 ms |
12056 KB |
Output is correct |
20 |
Correct |
279 ms |
12072 KB |
Output is correct |
21 |
Correct |
47 ms |
12212 KB |
Output is correct |
22 |
Correct |
39 ms |
11064 KB |
Output is correct |
23 |
Correct |
36 ms |
9500 KB |
Output is correct |
24 |
Correct |
47 ms |
13512 KB |
Output is correct |
25 |
Correct |
36 ms |
8288 KB |
Output is correct |
26 |
Correct |
62 ms |
16576 KB |
Output is correct |
27 |
Correct |
51 ms |
14796 KB |
Output is correct |
28 |
Correct |
32 ms |
8896 KB |
Output is correct |
29 |
Correct |
36 ms |
12836 KB |
Output is correct |
30 |
Correct |
30 ms |
7612 KB |
Output is correct |
31 |
Correct |
6 ms |
5588 KB |
Output is correct |
32 |
Correct |
12 ms |
6860 KB |
Output is correct |
33 |
Correct |
12 ms |
6860 KB |
Output is correct |
34 |
Correct |
12 ms |
6992 KB |
Output is correct |
35 |
Correct |
11 ms |
6460 KB |
Output is correct |
36 |
Correct |
12 ms |
6824 KB |
Output is correct |
37 |
Correct |
13 ms |
6812 KB |
Output is correct |
38 |
Correct |
19 ms |
9812 KB |
Output is correct |
39 |
Correct |
68 ms |
18920 KB |
Output is correct |
40 |
Correct |
76 ms |
23992 KB |
Output is correct |
41 |
Correct |
147 ms |
40128 KB |
Output is correct |
42 |
Correct |
67 ms |
16820 KB |
Output is correct |
43 |
Correct |
101 ms |
19204 KB |
Output is correct |
44 |
Correct |
92 ms |
15180 KB |
Output is correct |
45 |
Correct |
21 ms |
9884 KB |
Output is correct |
46 |
Correct |
61 ms |
19452 KB |
Output is correct |
47 |
Correct |
95 ms |
24384 KB |
Output is correct |
48 |
Correct |
142 ms |
40248 KB |
Output is correct |
49 |
Correct |
69 ms |
17592 KB |
Output is correct |
50 |
Correct |
102 ms |
19896 KB |
Output is correct |
51 |
Correct |
120 ms |
15924 KB |
Output is correct |