#include <bits/stdc++.h>
using namespace std;
vector< vector<int> > g;
vector<int> d;
vector<long long> w;
vector< map<long long, long long> > dp;
void dfs(int u) {
for (auto& v : g[u]) {
dfs(v);
if ((int) dp[u].size() < (int) dp[v].size()) {
swap(dp[u], dp[v]);
}
for (auto& p : dp[v]) {
dp[u][p.first] += p.second;
}
}
dp[u][d[u]] += w[u];
auto it = dp[u].upper_bound(d[u]);
long long tmp = w[u];
while (it != dp[u].end()) {
if (it->second > tmp) {
it->second -= tmp;
break;
}
tmp -= it->second;
dp[u].erase(it++);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
g.resize(n + 1);
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
g[p].push_back(i);
}
d.assign(n + 1, 1);
w.resize(n + 1);
for (int i = 0; i < m; i++) {
int v;
cin >> v;
cin >> d[v] >> w[v];
}
dp.resize(n + 1);
dfs(1);
long long ans = 0;
for (auto& p : dp[1]) {
ans += p.second;
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
21536 KB |
Output is correct |
2 |
Correct |
34 ms |
19384 KB |
Output is correct |
3 |
Correct |
106 ms |
37684 KB |
Output is correct |
4 |
Correct |
79 ms |
20892 KB |
Output is correct |
5 |
Correct |
84 ms |
22060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
39 ms |
24508 KB |
Output is correct |
5 |
Correct |
55 ms |
28396 KB |
Output is correct |
6 |
Correct |
52 ms |
24556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
16208 KB |
Output is correct |
2 |
Correct |
46 ms |
15316 KB |
Output is correct |
3 |
Correct |
40 ms |
18740 KB |
Output is correct |
4 |
Correct |
31 ms |
16900 KB |
Output is correct |
5 |
Correct |
36 ms |
24760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
55 ms |
18656 KB |
Output is correct |
11 |
Correct |
44 ms |
17548 KB |
Output is correct |
12 |
Correct |
38 ms |
18052 KB |
Output is correct |
13 |
Correct |
31 ms |
16276 KB |
Output is correct |
14 |
Correct |
34 ms |
24144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3156 KB |
Output is correct |
2 |
Correct |
38 ms |
13976 KB |
Output is correct |
3 |
Correct |
25 ms |
14036 KB |
Output is correct |
4 |
Correct |
37 ms |
14028 KB |
Output is correct |
5 |
Correct |
23 ms |
15408 KB |
Output is correct |
6 |
Correct |
30 ms |
16372 KB |
Output is correct |
7 |
Correct |
23 ms |
17220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
39 ms |
24508 KB |
Output is correct |
14 |
Correct |
55 ms |
28396 KB |
Output is correct |
15 |
Correct |
52 ms |
24556 KB |
Output is correct |
16 |
Correct |
55 ms |
18656 KB |
Output is correct |
17 |
Correct |
44 ms |
17548 KB |
Output is correct |
18 |
Correct |
38 ms |
18052 KB |
Output is correct |
19 |
Correct |
31 ms |
16276 KB |
Output is correct |
20 |
Correct |
34 ms |
24144 KB |
Output is correct |
21 |
Correct |
14 ms |
5324 KB |
Output is correct |
22 |
Correct |
64 ms |
21456 KB |
Output is correct |
23 |
Correct |
78 ms |
24664 KB |
Output is correct |
24 |
Correct |
95 ms |
31460 KB |
Output is correct |
25 |
Correct |
54 ms |
20312 KB |
Output is correct |
26 |
Correct |
79 ms |
21568 KB |
Output is correct |
27 |
Correct |
59 ms |
19916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
64 ms |
21536 KB |
Output is correct |
11 |
Correct |
34 ms |
19384 KB |
Output is correct |
12 |
Correct |
106 ms |
37684 KB |
Output is correct |
13 |
Correct |
79 ms |
20892 KB |
Output is correct |
14 |
Correct |
84 ms |
22060 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
39 ms |
24508 KB |
Output is correct |
19 |
Correct |
55 ms |
28396 KB |
Output is correct |
20 |
Correct |
52 ms |
24556 KB |
Output is correct |
21 |
Correct |
42 ms |
16208 KB |
Output is correct |
22 |
Correct |
46 ms |
15316 KB |
Output is correct |
23 |
Correct |
40 ms |
18740 KB |
Output is correct |
24 |
Correct |
31 ms |
16900 KB |
Output is correct |
25 |
Correct |
36 ms |
24760 KB |
Output is correct |
26 |
Correct |
55 ms |
18656 KB |
Output is correct |
27 |
Correct |
44 ms |
17548 KB |
Output is correct |
28 |
Correct |
38 ms |
18052 KB |
Output is correct |
29 |
Correct |
31 ms |
16276 KB |
Output is correct |
30 |
Correct |
34 ms |
24144 KB |
Output is correct |
31 |
Correct |
5 ms |
3156 KB |
Output is correct |
32 |
Correct |
38 ms |
13976 KB |
Output is correct |
33 |
Correct |
25 ms |
14036 KB |
Output is correct |
34 |
Correct |
37 ms |
14028 KB |
Output is correct |
35 |
Correct |
23 ms |
15408 KB |
Output is correct |
36 |
Correct |
30 ms |
16372 KB |
Output is correct |
37 |
Correct |
23 ms |
17220 KB |
Output is correct |
38 |
Correct |
14 ms |
5324 KB |
Output is correct |
39 |
Correct |
64 ms |
21456 KB |
Output is correct |
40 |
Correct |
78 ms |
24664 KB |
Output is correct |
41 |
Correct |
95 ms |
31460 KB |
Output is correct |
42 |
Correct |
54 ms |
20312 KB |
Output is correct |
43 |
Correct |
79 ms |
21568 KB |
Output is correct |
44 |
Correct |
59 ms |
19916 KB |
Output is correct |
45 |
Correct |
15 ms |
5708 KB |
Output is correct |
46 |
Correct |
66 ms |
22496 KB |
Output is correct |
47 |
Correct |
83 ms |
25920 KB |
Output is correct |
48 |
Correct |
112 ms |
34520 KB |
Output is correct |
49 |
Correct |
60 ms |
21008 KB |
Output is correct |
50 |
Correct |
71 ms |
22512 KB |
Output is correct |
51 |
Correct |
65 ms |
20904 KB |
Output is correct |