#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<tuple<int, int, int>> a(m);
for (auto &[d, v, w] : a) {
cin >> v >> d >> w;
v--;
}
vector<int> dep(n);
function<void(int)> dfs = [&](int u) {
for (int v : e[u]) {
dep[v] = dep[u] + 1;
dfs(v);
}
};
dfs(0);
sort(a.begin(), a.end(), [&](const tuple<int, int, int> &a, const tuple<int, int, int> &b) {
if (get<0>(a) != get<0>(b))
return get<0>(a) < get<0>(b);
return dep[get<1>(a)] > dep[get<1>(b)];
});
vector<i64> dp(n);
for (auto [d, v, w] : a) {
int u = v;
i64 x = dp[u];
dp[u] = w;
for (int v : e[u]) {
dp[u] += dp[v];
}
dp[u] = max(dp[u], x);
while (u != -1) {
i64 x = dp[u];
dp[u] = 0;
for (int v : e[u]) {
dp[u] += dp[v];
}
dp[u] = max(dp[u], x);
u = p[u];
}
}
cout << dp[0] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
6984 KB |
Output is correct |
2 |
Execution timed out |
2076 ms |
10444 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
452 KB |
Output is correct |
4 |
Execution timed out |
2060 ms |
16644 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
8876 KB |
Output is correct |
2 |
Correct |
81 ms |
8928 KB |
Output is correct |
3 |
Execution timed out |
2060 ms |
12364 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
78 ms |
8376 KB |
Output is correct |
11 |
Correct |
75 ms |
8328 KB |
Output is correct |
12 |
Execution timed out |
2068 ms |
11696 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1492 KB |
Output is correct |
2 |
Correct |
25 ms |
6308 KB |
Output is correct |
3 |
Correct |
22 ms |
6340 KB |
Output is correct |
4 |
Correct |
23 ms |
6328 KB |
Output is correct |
5 |
Correct |
74 ms |
4940 KB |
Output is correct |
6 |
Correct |
128 ms |
7432 KB |
Output is correct |
7 |
Correct |
181 ms |
9844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
452 KB |
Output is correct |
13 |
Execution timed out |
2060 ms |
16644 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
41 ms |
6984 KB |
Output is correct |
11 |
Execution timed out |
2076 ms |
10444 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |