This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifdef local
#define debug(x) cout<<#x<<" "<<x<<endl;
#else
#define debug(x)
#endif
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pl;
const ll mod=1e9+7,lim=2e5+5,bitlim=15,inf=1e9;
vector<ll>adjl[lim];
ll pwr[lim],cost[lim];
map<ll,ll> dp(ll pos){
if(adjl[pos].size() == 0){
map<ll,ll>ret;
ret[0] = 0;
ret[pwr[pos] + 1] = cost[pos];
return ret;
}
else{
map<ll,ll> *ret;
vector<map<ll,ll> >vv;
ll maxsz=0,maxszidx=0;
for(auto u:adjl[pos]){
vv.push_back(dp(u));
if(vv.back().size()>maxsz){
maxsz=vv.back().size();
maxszidx=vv.size()-1;
}
}
ret = &vv[maxszidx];
for(int i=0;i<adjl[pos].size();i++){
if(i==maxszidx)continue;
for(auto p:vv[i]){
ll k,v;
tie(k,v)=p;
ret->operator[](k)+=v;
}
}
ret->operator[](0) += cost[pos];
ret->operator[](pwr[pos] + 1) += cost[pos];
auto it=ret->upper_bound(pwr[pos]);
ll rem=cost[pos];
while(rem>0){
it--;
if(rem<=it->second)it->second-=rem,rem=0;
else rem-=it->second,it=ret->erase(it);
}
return *ret;
}
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(NULL);
ll n;
cin>>n;
for(int i=1;i<=n;i++){
ll a;
cin>>a>>pwr[i]>>cost[i];
if(i!=a)adjl[a].push_back(i);
}
cout<<dp(1).begin()->second<<"\n";
return 0;
}
Compilation message (stderr)
worst_reporter2.cpp: In function 'std::map<long long int, long long int> dp(ll)':
worst_reporter2.cpp:27:23: warning: comparison of integer expressions of different signedness: 'std::map<long long int, long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
27 | if(vv.back().size()>maxsz){
| ~~~~~~~~~~~~~~~~^~~~~~
worst_reporter2.cpp:33:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int i=0;i<adjl[pos].size();i++){
| ~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |