#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>;
using pll=pair<ll,ll>;
const int mxN = 1e5+5;
const int mxM = mxN;
const int mxK = 1e5+5;
int N, M, K;
vector<int> al[mxN];
ii fruit[mxN];
set<ii> dfs(int u) {
set<ii> cur;
for (int v : al[u]) {
auto x = dfs(v);
if (SZ(cur) < SZ(x)) swap(cur,x);
for (auto y : x) cur.insert(y);
}
if (fruit[u] != ii(-1,-1)) {
ll sum = 0;
for (auto it = cur.rbegin(); it != cur.rend() && it->first > fruit[u].first; ++it) {
sum += it->second;
}
if (fruit[u].second >= sum) {
for (auto it = cur.rbegin(); it != cur.rend() && it->first > fruit[u].first;) {
auto jt = next(it);
assert(cur.find(*it) != cur.end());
cur.erase(cur.find(*it));
it = jt;
}
cur.insert(fruit[u]);
}
}
return cur;
}
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 ans = dfs(1);
ll sum = 0;
for (auto& x : ans) {
sum += x.second;
}
cout << sum << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
7032 KB |
Output is correct |
2 |
Correct |
51 ms |
13944 KB |
Output is correct |
3 |
Correct |
143 ms |
9184 KB |
Output is correct |
4 |
Correct |
79 ms |
8432 KB |
Output is correct |
5 |
Correct |
103 ms |
10260 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 |
133 ms |
9208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
3328 KB |
Output is correct |
2 |
Correct |
35 ms |
5632 KB |
Output is correct |
3 |
Correct |
34 ms |
5628 KB |
Output is correct |
4 |
Correct |
31 ms |
5624 KB |
Output is correct |
5 |
Correct |
14 ms |
4220 KB |
Output is correct |
6 |
Incorrect |
31 ms |
8576 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |