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];
vector<pl>costs[lim];
ll pwr[lim],cost[lim];
map<ll,ll> *mps[lim];
ll root[lim],nxt[lim];
ll vis[lim];
void dp(ll pos){
// debug(pos);
if(adjl[pos].size() == 0&&root[pos]==0){
mps[pos] = new map<ll,ll>();
mps[pos]->operator[](0)=0;
mps[pos]->operator[](pwr[pos] + 1) = cost[pos];
}
else{
ll maxsz=0,maxszidx=0;
for(int i=0;i<adjl[pos].size();i++){
ll u=adjl[pos][i];
dp(u);
if(mps[u]->size()>maxsz){
maxsz=mps[u]->size();
maxszidx=u;
}
}
if(maxszidx!=0)mps[pos] = mps[maxszidx];
else mps[pos] = new map<ll,ll>();
for(int i=0;i<adjl[pos].size();i++){
ll u=adjl[pos][i];
if(u==maxszidx)continue;
for(auto p:*mps[u]){
ll k,v;
tie(k,v)=p;
mps[pos]->operator[](k)+=v;
}
}
if(root[pos]==pos){
// cout<<"pos "<<pos<<" is a root\n";
for(pl pr:costs[pos]){
ll p,c;
tie(p,c)=pr;
mps[pos]->operator[](0) += c;
mps[pos]->operator[](p + 1) += c;
}
for(pl pr:costs[pos]){
ll p,c;
// cout<<"yeeting "<<p<<" "<<c<<"\n";
tie(p,c)=pr;
auto it=mps[pos]->upper_bound(p);
ll rem=c;
while(rem>0){
it--;
if(rem<=it->second)it->second-=rem,rem=0;
else rem-=it->second,it=mps[pos]->erase(it);
}
}
}
else{
mps[pos]->operator[](0) += cost[pos];
mps[pos]->operator[](pwr[pos] + 1) += cost[pos];
auto it=mps[pos]->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=mps[pos]->erase(it);
}
}
}
return;
}
ll dfs(ll pos,ll idx){
vis[pos]=idx;
if(vis[nxt[pos]]==0){
ll k = dfs(nxt[pos],idx);
if(k!=-1){
root[pos]=k;
costs[k].emplace_back(pwr[pos],cost[pos]);
if(k!=pos)return k;
}
return -1;
}
else if(vis[nxt[pos]]==idx){
root[nxt[pos]]=root[pos]=nxt[pos];
costs[nxt[pos]].emplace_back(pwr[pos],cost[pos]);
if(nxt[pos]==pos)return -1;
else return nxt[pos];
}
else return -1;
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(NULL);
ll n;
cin>>n;
fill(root,root+n+1,-1);
for(int i=1;i<=n;i++){
ll a;
cin>>a>>pwr[i]>>cost[i];
nxt[i]=a;
}
for(int i=1;i<=n;i++){
if(vis[i]==0)dfs(i,i);
}
for(int i=1;i<=n;i++){
if(i!=nxt[i]&&root[i]==-1){
if(root[nxt[i]]!=-1){
adjl[root[nxt[i]]].push_back(i);
}
else adjl[nxt[i]].push_back(i);
}
}
ll ans=0;
/* for(int i=1;i<=n;i++){
cout<<i<<": "<<root[i]<<"\n";
}*/
for(int i=1;i<=n;i++){
if(root[i]==i){
// cout<<"rooti=i:"<<i<<"\n";
// for(auto x:costs[i]){
// cout<<x.first<<" "<<x.second<<"\n";
// }
dp(i);
// for(auto x:*mps[i]){
// cout<<x.first<<" "<<x.second<<"\n";
// }
ans+=mps[i]->begin()->second;
}
}
cout<<ans<<"\n";
/* for(int i=1;i<=n;i++){
cout<<i<<": ";
for(auto x:adjl[i])cout<<x<<" ";
cout<<"\n";
}*/
//dp(1);
//cout<<mps[1]->begin()->second<<"\n";
return 0;
}
/*
4
2 25 55
3 20 42
4 30 69
1 28 39
1 27 50
*/
Compilation message (stderr)
worst_reporter2.cpp: In function 'void dp(ll)':
worst_reporter2.cpp:27:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i=0;i<adjl[pos].size();i++){
| ~^~~~~~~~~~~~~~~~~
worst_reporter2.cpp:30:21: 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]
30 | if(mps[u]->size()>maxsz){
| ~~~~~~~~~~~~~~^~~~~~
worst_reporter2.cpp:37:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | 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... |