This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MX = 1e5+1;
vector<int> adj[MX];
pair<int, int> fruit[MX];
multiset<pair<int, int>> ms[MX];
void merge(multiset<pair<int, int>>& st, pair<int, int> pr) {
auto [d, w] = pr;
auto it = st.lower_bound(make_pair(d+1, -1));
while(w > 0 && it != st.end()) {
if(w >= it->second) {
it = st.erase(it);
w -= it->second;
} else {
st.insert({it->first, it->second - w});
st.erase(it);
w = 0;
}
}
st.insert(pr);
}
void dfs(int i, int p) {
int leaf = 1;
for(int a: adj[i]) {
if(a == p) continue;
leaf = 0;
dfs(a, i);
}
if(leaf) {
if(fruit[i].first)
ms[i] = {fruit[i]};
} else {
int mx = 0;
for(int a: adj[i]) {
if(a == p) continue;
if(ms[a].size() > ms[mx].size())
mx = a;
}
ms[i] = move(ms[mx]);
for(int a: adj[i]) {
if(a == p || a == mx) continue;
for(auto pr: ms[a]) {
ms[i].insert(pr);
}
ms[a].clear();
}
if(fruit[i].first)
merge(ms[i], fruit[i]);
}
}
int32_t main() {
//ios::sync_with_stdio(0);cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
for(int i=2; i<=n; i++) {
int p;
cin >> p;
adj[p].push_back(i);
adj[i].push_back(p);
}
for(int i=0; i<m; i++) {
int v, d, w;
cin >> v >> d >> w;
fruit[v] = {d, w};
}
dfs(1, 0);
int ans = 0;
for(auto [d, w]: ms[1]) {
ans += w;
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |