#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
using ll=long long;
using ii=pair<int,int>;
const int mxN = 1e5+5;
int N, M, K;
vector<int> al[mxN];
ii fruit[mxN];
map<int,ll> dfs(int u) {
map<int,ll> res;
for (int v : al[u]) {
auto x = dfs(v);
if (SZ(res) < SZ(x)) swap(res,x);
for (auto& y : x) {
if (res.count(y.first)) res[y.first] += y.second;
else res.insert(y);
}
}
if (fruit[u] != ii(-1,-1)) {
auto it = res.upper_bound(fruit[u].first);
if (it != res.begin() && prev(it)->first == fruit[u].first) {
auto x = *prev(it);
res.erase(prev(it));
x.second += fruit[u].second;
it = res.insert(x).first;
} else res.insert(fruit[u]);
if (it != res.end()) {
auto x = *it;
res.erase(it);
x.second -= fruit[u].second;
res.insert(x);
}
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M >> K;
FOR(i,2,N){
int P; cin >> P;
al[P].push_back(i);
}
FOR(i,1,N) fruit[i] = ii(-1,-1);
FOR(i,1,M){
int V, D, W; cin >> V >> D >> W;
fruit[V] = ii(D,W);
}
auto res = dfs(1);
ll ans = 0;
for (auto& x : res) ans += x.second;
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Incorrect |
5 ms |
2688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
6904 KB |
Output is correct |
2 |
Correct |
52 ms |
13944 KB |
Output is correct |
3 |
Correct |
147 ms |
8824 KB |
Output is correct |
4 |
Correct |
94 ms |
7668 KB |
Output is correct |
5 |
Correct |
124 ms |
9592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
2944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
82 ms |
5112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Incorrect |
5 ms |
2688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
3200 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Incorrect |
5 ms |
2688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Incorrect |
5 ms |
2688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |