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
int h[200005];
int c[200005];
vector<int> adjl[200005];
///pos, increase
set<pair<int,int> > func(int node){
set<pair<int,int> > res1;
for (auto x : adjl[node]){
auto res = func(x);
if (res.size()>res1.size()) swap(res,res1);
for (auto y : res){
auto it = res1.lower_bound({y.first,-1});
if (it!=res1.end() && (*it).first==y.first){
int t = (*it).second;
res1.erase(it);
res1.insert({y.first,y.second+t});
}
else{
res1.insert({y.first,y.second});
}
}
}
pair<int,int> to_insert = {h[node]+1,c[node]};
int cursum = 0;
auto it = res1.lower_bound({h[node]+1,-1});
if (it!=res1.end() && (*it).first==to_insert.first){
int t = (*it).second;
res1.erase(it);
res1.insert({to_insert.first,to_insert.second+t});
}
else{
res1.insert(to_insert);
}
it = res1.lower_bound(to_insert);
bool done = false;
while (it!=res1.begin()){
it--;
cursum += (*it).second;
int curpos = (*it).first;
it = res1.erase(it);
if (cursum >= c[node]){
int rem = cursum-c[node];
if (rem!=0){
res1.insert({curpos,rem});
}
done = true;
break;
}
}
if (!done){
if (cursum!=0){
if ((!res1.empty()) && (*res1.begin()).first==0){
cursum += (*res1.begin()).second;
res1.erase(res1.begin());
res1.insert({0,cursum});
}
else
res1.insert({0,cursum});
}
}
else{
if ((!res1.empty()) && (*res1.begin()).first==0){
int t = (*res1.begin()).second;
res1.erase(res1.begin());
res1.insert({0,c[node]+t});
}
else
res1.insert({0,c[node]});
}
/*printf("node %lld, ret ",node);
for (auto x : res1){
printf("(%lld,%lld) ",x);
}
printf("\n");*/
return res1;
}
main(){
int n;
scanf("%lld",&n);
for (int x = 1; x<=n; x++){
int t;
scanf("%lld%lld%lld",&t,&h[x],&c[x]);
if (x!=1){
adjl[t].push_back(x);
}
}
auto res = func(1);
if (res.empty()){
printf("0");
}
else if ((*res.begin()).first!=0){
printf("0");
}
else{
printf("%lld",(*res.begin()).second);
}
}
Compilation message (stderr)
worst_reporter2.cpp:82:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
82 | main(){
| ^~~~
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:84:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | scanf("%lld",&n);
| ~~~~~^~~~~~~~~~~
worst_reporter2.cpp:87:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | scanf("%lld%lld%lld",&t,&h[x],&c[x]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |