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;
vector<vector<int>> g;
vector<int> h, ert;
vector<map<int, long long>> mps;
void bejar(int akt){
for(int x : g[akt]){
bejar(x);
if(mps[x].size() > mps[akt].size()) swap(mps[akt], mps[x]);
for(auto p : mps[x]) mps[akt][p.first] += p.second;
}
auto it = mps[akt].find(h[akt]);
if(it == mps[akt].end()){
mps[akt][h[akt]] = ert[akt];
it = mps[akt].find(h[akt]);
} else{
mps[akt][h[akt]] += ert[akt];
}
long long tmp = ert[akt];
while(it != mps[akt].begin() && prev(it)->second < tmp){
tmp -= prev(it)->second;
mps[akt].erase(prev(it));
}
if(it != mps[akt].begin()) prev(it)->second -= tmp;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
g.resize(n);
h.resize(n);
ert.resize(n);
mps.resize(n);
for(int i = 0; i < n; i++){
int a;
cin>>a>>h[i]>>ert[i];
--a;
if(a != i) g[a].push_back(i);
}
bejar(0);
long long meg = accumulate(ert.begin(), ert.end(), 0ll);
for(auto p : mps[0]) meg -= p.second;
cout<<meg<<'\n';
return 0;
}
/*
6
1 6 5
1 3 6
1 8 4
3 4 9
2 2 5
2 5 6
20
15 62 418848971
13 5 277275513
14 60 80376452
12 14 256845164
12 42 481331310
6 86 290168639
3 98 947342135
3 19 896070909
16 39 48034188
8 29 925729089
18 97 420006994
13 51 454182928
19 61 822405612
13 37 148425187
15 77 474094143
14 27 272926693
18 43 566552069
9 93 790433300
10 73 61654171
14 28 334498030
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |