#include <bits/stdc++.h>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, M, K;
std::cin >> N >> M >> K;
std::vector<int> p(N);
for (int u = 1; u < N; ++u) {
std::cin >> p[u];
--p[u];
}
std::vector<int> d(N), w(N);
for (int j = 0; j < M; ++j) {
int v;
std::cin >> v;
--v;
std::cin >> d[v] >> w[v];
}
std::vector<std::map<int, int64_t>> s(N);
for (int u = N - 1; u > 0; --u) {
auto iter = s[u].insert({d[u], 0}).first;
iter->second += w[u];
++iter;
if (iter != s[u].end()) {
int64_t sum = -w[u];
while (iter != s[u].end() && iter->second + sum < 0) {
sum += iter->second;
iter = s[u].erase(iter);
}
if (iter != s[u].end()) {
iter->second += sum;
}
}
int v = p[u];
if (s[v].size() < s[u].size()) {
std::swap(s[v], s[u]);
}
for (auto [d, w] : s[u]) {
s[v][d] += w;
}
}
int64_t sum = 0;
for (auto [d, w] : s[0]) {
sum += w;
}
std::cout << sum << '\n';
exit(0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
18640 KB |
Output is correct |
2 |
Correct |
26 ms |
9232 KB |
Output is correct |
3 |
Correct |
126 ms |
37860 KB |
Output is correct |
4 |
Correct |
65 ms |
16068 KB |
Output is correct |
5 |
Correct |
91 ms |
20568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
38 ms |
6112 KB |
Output is correct |
5 |
Correct |
48 ms |
10068 KB |
Output is correct |
6 |
Correct |
41 ms |
6252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
10704 KB |
Output is correct |
2 |
Correct |
40 ms |
9728 KB |
Output is correct |
3 |
Correct |
41 ms |
7504 KB |
Output is correct |
4 |
Correct |
50 ms |
12372 KB |
Output is correct |
5 |
Correct |
31 ms |
6092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
59 ms |
15812 KB |
Output is correct |
11 |
Correct |
55 ms |
14084 KB |
Output is correct |
12 |
Correct |
30 ms |
7588 KB |
Output is correct |
13 |
Correct |
29 ms |
12348 KB |
Output is correct |
14 |
Correct |
28 ms |
6108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2252 KB |
Output is correct |
2 |
Correct |
20 ms |
9464 KB |
Output is correct |
3 |
Correct |
17 ms |
9456 KB |
Output is correct |
4 |
Correct |
18 ms |
9576 KB |
Output is correct |
5 |
Correct |
15 ms |
12448 KB |
Output is correct |
6 |
Correct |
18 ms |
9932 KB |
Output is correct |
7 |
Correct |
20 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
38 ms |
6112 KB |
Output is correct |
14 |
Correct |
48 ms |
10068 KB |
Output is correct |
15 |
Correct |
41 ms |
6252 KB |
Output is correct |
16 |
Correct |
59 ms |
15812 KB |
Output is correct |
17 |
Correct |
55 ms |
14084 KB |
Output is correct |
18 |
Correct |
30 ms |
7588 KB |
Output is correct |
19 |
Correct |
29 ms |
12348 KB |
Output is correct |
20 |
Correct |
28 ms |
6108 KB |
Output is correct |
21 |
Correct |
18 ms |
5784 KB |
Output is correct |
22 |
Correct |
55 ms |
20104 KB |
Output is correct |
23 |
Correct |
68 ms |
24900 KB |
Output is correct |
24 |
Correct |
113 ms |
37720 KB |
Output is correct |
25 |
Correct |
63 ms |
16060 KB |
Output is correct |
26 |
Correct |
78 ms |
16992 KB |
Output is correct |
27 |
Correct |
51 ms |
10068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
46 ms |
18640 KB |
Output is correct |
11 |
Correct |
26 ms |
9232 KB |
Output is correct |
12 |
Correct |
126 ms |
37860 KB |
Output is correct |
13 |
Correct |
65 ms |
16068 KB |
Output is correct |
14 |
Correct |
91 ms |
20568 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
38 ms |
6112 KB |
Output is correct |
19 |
Correct |
48 ms |
10068 KB |
Output is correct |
20 |
Correct |
41 ms |
6252 KB |
Output is correct |
21 |
Correct |
50 ms |
10704 KB |
Output is correct |
22 |
Correct |
40 ms |
9728 KB |
Output is correct |
23 |
Correct |
41 ms |
7504 KB |
Output is correct |
24 |
Correct |
50 ms |
12372 KB |
Output is correct |
25 |
Correct |
31 ms |
6092 KB |
Output is correct |
26 |
Correct |
59 ms |
15812 KB |
Output is correct |
27 |
Correct |
55 ms |
14084 KB |
Output is correct |
28 |
Correct |
30 ms |
7588 KB |
Output is correct |
29 |
Correct |
29 ms |
12348 KB |
Output is correct |
30 |
Correct |
28 ms |
6108 KB |
Output is correct |
31 |
Correct |
4 ms |
2252 KB |
Output is correct |
32 |
Correct |
20 ms |
9464 KB |
Output is correct |
33 |
Correct |
17 ms |
9456 KB |
Output is correct |
34 |
Correct |
18 ms |
9576 KB |
Output is correct |
35 |
Correct |
15 ms |
12448 KB |
Output is correct |
36 |
Correct |
18 ms |
9932 KB |
Output is correct |
37 |
Correct |
20 ms |
7372 KB |
Output is correct |
38 |
Correct |
18 ms |
5784 KB |
Output is correct |
39 |
Correct |
55 ms |
20104 KB |
Output is correct |
40 |
Correct |
68 ms |
24900 KB |
Output is correct |
41 |
Correct |
113 ms |
37720 KB |
Output is correct |
42 |
Correct |
63 ms |
16060 KB |
Output is correct |
43 |
Correct |
78 ms |
16992 KB |
Output is correct |
44 |
Correct |
51 ms |
10068 KB |
Output is correct |
45 |
Correct |
19 ms |
5268 KB |
Output is correct |
46 |
Correct |
58 ms |
19012 KB |
Output is correct |
47 |
Correct |
69 ms |
23104 KB |
Output is correct |
48 |
Correct |
113 ms |
32936 KB |
Output is correct |
49 |
Correct |
68 ms |
16052 KB |
Output is correct |
50 |
Correct |
83 ms |
16300 KB |
Output is correct |
51 |
Correct |
57 ms |
9428 KB |
Output is correct |