#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]);
for (ll y = fruit[u].second; y && it != res.end();) {
auto x = *it;
auto jt = next(it);
res.erase(it);
ll d = min(y,x.second);
y -= d;
x.second -= d;
if (x.second) res.insert(x);
it = jt;
}
}
//cout << u << " :: ";
//for (auto& x : res) cout << x.first << " " << x.second << " | ";
//cout << endl;
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 |
6 ms |
2688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
6904 KB |
Output is correct |
2 |
Correct |
53 ms |
14072 KB |
Output is correct |
3 |
Correct |
148 ms |
8840 KB |
Output is correct |
4 |
Correct |
82 ms |
7540 KB |
Output is correct |
5 |
Correct |
110 ms |
9592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
2816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
83 ms |
5120 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 |
6 ms |
2688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
3200 KB |
Output is correct |
2 |
Correct |
31 ms |
5120 KB |
Output is correct |
3 |
Correct |
34 ms |
5120 KB |
Output is correct |
4 |
Correct |
36 ms |
5120 KB |
Output is correct |
5 |
Correct |
15 ms |
3964 KB |
Output is correct |
6 |
Correct |
33 ms |
8016 KB |
Output is correct |
7 |
Correct |
35 ms |
12280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Incorrect |
6 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 |
6 ms |
2688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |