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;
const int maxn = 2e5+5;
const int INF = 1e9+5;
vector<int> tree[maxn];
multiset<pair<int, int>, greater<>> st[maxn];
int C[maxn], H[maxn];
void dfs(int x){
for(auto v: tree[x]){
dfs(v);
if(st[v].size() > st[x].size())st[x].swap(st[v]);
}
for(auto v: tree[x]){
for(auto ele: st[v])st[x].insert(ele);
}
st[x].insert({H[x], C[x]});
auto it = st[x].upper_bound({H[x], 0});
while(it != st[x].end() && C[x]){
if(it->second > C[x]){
pair<int, int> nw = *it;
nw.second -= C[x];
st[x].erase(it);
st[x].insert(nw);
C[x] = 0;
}
else {
C[x] -= it->second;
it = st[x].erase(it);
}
}
// cout << "SUS " << x << endl;
// for(auto ele: st[x])cout << ele.first << " " << ele.second << endl;
}
int main() {
int n;
cin >> n;
for(int i = 1; i <= n; i++){
int p;
cin >> p >> H[i] >> C[i];
if(i > 1)tree[p].push_back(i);
}
long long sum = accumulate(C, C+n+1, 0LL);
dfs(1);
for(auto ele: st[1])sum -= ele.second;
cout << sum;
}
// 4 = 4 cost 9
// 6 = 5 cost 6
// 2 = 3 cost 6
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |