#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k;
cin >> n >> m >> k;
vector<int> p(n, -1);
vector<vector<int>> e(n);
for (int i = 1; i < n; i++) {
cin >> p[i];
p[i]--;
e[p[i]].push_back(i);
}
vector<int> w(n), d(n, k + 1);
for (int i = 0; i < m; i++) {
int v;
cin >> v;
v--;
cin >> d[v] >> w[v];
}
vector<map<int, i64>> g(n);
function<void(int)> dfs = [&](int u) {
for (int v : e[u]) {
dfs(v);
if (g[v].size() > g[u].size()) {
swap(g[u], g[v]);
}
for (auto [x, y] : g[v]) {
g[u][x] += y;
}
}
g[u][d[u]] += w[u];
i64 x = 0;
while (true) {
auto it = g[u].upper_bound(d[u]);
if (it == g[u].end())
break;
if (it->second <= w[u] - x) {
x += it->second;
g[u].erase(it);
} else {
it->second -= w[u];
it->second += x;
break;
}
}
};
dfs(0);
i64 ans = 0;
i64 sum = 0;
for (auto [x, y] : g[0]) {
sum += y;
ans = max(ans, sum);
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
21540 KB |
Output is correct |
2 |
Correct |
48 ms |
20552 KB |
Output is correct |
3 |
Correct |
132 ms |
37740 KB |
Output is correct |
4 |
Correct |
80 ms |
20940 KB |
Output is correct |
5 |
Correct |
151 ms |
22396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
52 ms |
27364 KB |
Output is correct |
5 |
Correct |
75 ms |
31592 KB |
Output is correct |
6 |
Correct |
79 ms |
27708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
15844 KB |
Output is correct |
2 |
Correct |
75 ms |
15040 KB |
Output is correct |
3 |
Correct |
61 ms |
19472 KB |
Output is correct |
4 |
Correct |
46 ms |
16740 KB |
Output is correct |
5 |
Correct |
61 ms |
27412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
83 ms |
18524 KB |
Output is correct |
11 |
Correct |
68 ms |
17584 KB |
Output is correct |
12 |
Correct |
53 ms |
19304 KB |
Output is correct |
13 |
Correct |
41 ms |
16204 KB |
Output is correct |
14 |
Correct |
46 ms |
27280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
3156 KB |
Output is correct |
2 |
Correct |
57 ms |
13932 KB |
Output is correct |
3 |
Correct |
47 ms |
14028 KB |
Output is correct |
4 |
Correct |
44 ms |
14084 KB |
Output is correct |
5 |
Correct |
26 ms |
15460 KB |
Output is correct |
6 |
Correct |
40 ms |
17088 KB |
Output is correct |
7 |
Correct |
36 ms |
18436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
52 ms |
27364 KB |
Output is correct |
14 |
Correct |
75 ms |
31592 KB |
Output is correct |
15 |
Correct |
79 ms |
27708 KB |
Output is correct |
16 |
Correct |
83 ms |
18524 KB |
Output is correct |
17 |
Correct |
68 ms |
17584 KB |
Output is correct |
18 |
Correct |
53 ms |
19304 KB |
Output is correct |
19 |
Correct |
41 ms |
16204 KB |
Output is correct |
20 |
Correct |
46 ms |
27280 KB |
Output is correct |
21 |
Correct |
25 ms |
5244 KB |
Output is correct |
22 |
Correct |
99 ms |
21116 KB |
Output is correct |
23 |
Correct |
93 ms |
24312 KB |
Output is correct |
24 |
Correct |
123 ms |
31168 KB |
Output is correct |
25 |
Correct |
102 ms |
20240 KB |
Output is correct |
26 |
Correct |
101 ms |
22208 KB |
Output is correct |
27 |
Correct |
84 ms |
21256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
92 ms |
21540 KB |
Output is correct |
11 |
Correct |
48 ms |
20552 KB |
Output is correct |
12 |
Correct |
132 ms |
37740 KB |
Output is correct |
13 |
Correct |
80 ms |
20940 KB |
Output is correct |
14 |
Correct |
151 ms |
22396 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
52 ms |
27364 KB |
Output is correct |
19 |
Correct |
75 ms |
31592 KB |
Output is correct |
20 |
Correct |
79 ms |
27708 KB |
Output is correct |
21 |
Correct |
83 ms |
15844 KB |
Output is correct |
22 |
Correct |
75 ms |
15040 KB |
Output is correct |
23 |
Correct |
61 ms |
19472 KB |
Output is correct |
24 |
Correct |
46 ms |
16740 KB |
Output is correct |
25 |
Correct |
61 ms |
27412 KB |
Output is correct |
26 |
Correct |
83 ms |
18524 KB |
Output is correct |
27 |
Correct |
68 ms |
17584 KB |
Output is correct |
28 |
Correct |
53 ms |
19304 KB |
Output is correct |
29 |
Correct |
41 ms |
16204 KB |
Output is correct |
30 |
Correct |
46 ms |
27280 KB |
Output is correct |
31 |
Correct |
9 ms |
3156 KB |
Output is correct |
32 |
Correct |
57 ms |
13932 KB |
Output is correct |
33 |
Correct |
47 ms |
14028 KB |
Output is correct |
34 |
Correct |
44 ms |
14084 KB |
Output is correct |
35 |
Correct |
26 ms |
15460 KB |
Output is correct |
36 |
Correct |
40 ms |
17088 KB |
Output is correct |
37 |
Correct |
36 ms |
18436 KB |
Output is correct |
38 |
Correct |
25 ms |
5244 KB |
Output is correct |
39 |
Correct |
99 ms |
21116 KB |
Output is correct |
40 |
Correct |
93 ms |
24312 KB |
Output is correct |
41 |
Correct |
123 ms |
31168 KB |
Output is correct |
42 |
Correct |
102 ms |
20240 KB |
Output is correct |
43 |
Correct |
101 ms |
22208 KB |
Output is correct |
44 |
Correct |
84 ms |
21256 KB |
Output is correct |
45 |
Correct |
19 ms |
5708 KB |
Output is correct |
46 |
Correct |
91 ms |
22104 KB |
Output is correct |
47 |
Correct |
121 ms |
25576 KB |
Output is correct |
48 |
Correct |
153 ms |
34184 KB |
Output is correct |
49 |
Correct |
82 ms |
20892 KB |
Output is correct |
50 |
Correct |
99 ms |
23192 KB |
Output is correct |
51 |
Correct |
90 ms |
22076 KB |
Output is correct |