/**
* author: wxhtzdy
* created: 26.04.2023 14:44:59
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<vector<int>> g(n);
for (int i = 1; i < n; i++) {
int p;
cin >> p;
--p;
g[p].push_back(i);
g[i].push_back(p);
}
vector<int> a(n, -1), b(n);
for (int i = 0; i < m; i++) {
int v, d, w;
cin >> v >> d >> w;
--v; --d;
a[v] = d;
b[v] = w;
}
vector<int> rt(n);
vector<vector<pair<int, int>>> vals(n);
function<void(int, int)> Dfs = [&](int v, int pv) {
int f = -1;
for (int u : g[v]) {
if (u == pv) {
continue;
}
Dfs(u, v);
if (f == -1 || vals[rt[f]].size() < vals[rt[u]].size()) {
f = u;
}
}
rt[v] = v;
for (int u : g[v]) {
if (u == pv) {
continue;
}
for (auto& p : vals[rt[u]]) {
vals[rt[v]].push_back(p);
}
}
sort(vals[rt[v]].begin(), vals[rt[v]].end());
if (a[v] != -1) {
for (auto& p : vals[rt[v]]) {
if (p.first > a[v]) {
long long s = 0;
for (auto& q : vals[rt[v]]) {
if (q.first >= p.first && q.second > 0) {
s += q.second;
}
}
vals[rt[v]].emplace_back(p.first, -min(s, 1LL * b[v]));
break;
}
}
vals[rt[v]].emplace_back(a[v], b[v]);
}
};
Dfs(0, 0);
sort(vals[rt[0]].begin(), vals[rt[0]].end());
long long ans = 0;
long long sum = 0;
for (auto& p : vals[rt[0]]) {
sum += p.second;
ans = max(ans, sum);
}
assert(ans == sum);
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
13916 KB |
Output is correct |
2 |
Execution timed out |
2103 ms |
720844 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
28 ms |
11464 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
151 ms |
23940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2388 KB |
Output is correct |
2 |
Correct |
47 ms |
9780 KB |
Output is correct |
3 |
Correct |
46 ms |
9796 KB |
Output is correct |
4 |
Correct |
51 ms |
9880 KB |
Output is correct |
5 |
Correct |
16 ms |
9932 KB |
Output is correct |
6 |
Incorrect |
307 ms |
136924 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |