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 N 5<<17
#define pop(X) rt[X]=merge(ch[rt[X]][0],ch[rt[X]][1])
#define int long long
int deg[N],P[N],C[N],rt[N],ch[N][2],v[N],CC;
mt19937 rng(9234623);
int merge(int a,int b){
if(!a+!b)
return a+b;
if(v[a]<v[b])
swap(a,b);
int x=rng()%2;
ch[a][x]=merge(ch[a][x],b);
return a;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n,m,ans=0;
cin>>n>>m;
for(int i=2;i<=n+m;i++)
cin>>P[i]>>C[i],deg[P[i]]++,ans+=C[i];
for(int i=n+m;i>1;i--){
int l=0,r=0;
if(deg[i]){
while(--deg[i])
pop(i);
r=v[rt[i]];
pop(i);
l=v[rt[i]];
pop(i);
}
v[++CC]=l+C[i],v[++CC]=r+C[i];
rt[i]=merge(rt[i],merge(CC,CC-1));
rt[P[i]]=merge(rt[P[i]],rt[i]);
}
while(deg[1]--)
pop(1);
while(rt[1])
ans-=v[rt[1]],pop(1);
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |