Submission #31229

#TimeUsernameProblemLanguageResultExecution timeMemory
31229tatatanFireworks (APIO16_fireworks)C++11
100 / 100
239 ms44380 KiB
#include <bits/stdc++.h> #define mp make_pair #define pb push_back #define pii pair<int,int> #define LL long long #define st first #define nd second #define endl '\n' using namespace std; const int MAXN=300005; int n,m,p[MAXN],c[MAXN]; LL a[MAXN],b[MAXN]; priority_queue<LL> pq[MAXN]; void swap2(int x,int y) { if(pq[x].size()<pq[y].size()) { pq[x].swap(pq[y]); swap(a[x],a[y]); swap(b[x],b[y]); } } void merge(int x,int y) { swap2(x,y); a[x]+=a[y]; b[x]+=b[y]; while(!pq[y].empty()) { pq[x].push(pq[y].top()); pq[y].pop(); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>m; for(int i=2;i<=n+m;++i) { cin>>p[i]>>c[i]; if(i>n) { a[i]=1; b[i]=-c[i]; pq[i].push(c[i]); pq[i].push(c[i]); merge(p[i],i); } } for(int i=n;i>1;--i) { while(a[i]>1) { --a[i]; b[i]+=pq[i].top(); pq[i].pop(); } LL t1=pq[i].top(); pq[i].pop(); LL t2=pq[i].top(); pq[i].pop(); pq[i].push(t2+c[i]); pq[i].push(t1+c[i]); b[i]-=c[i]; merge(p[i],i); } while(a[1]>1) { --a[1]; b[1]+=pq[1].top(); pq[1].pop(); } cout<<pq[1].top()+b[1]<<endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...