Submission #972256

#TimeUsernameProblemLanguageResultExecution timeMemory
972256KenjikrabFireworks (APIO16_fireworks)C++14
100 / 100
175 ms62144 KiB
#include<bits/stdc++.h> #define int long long #define pii pair<int,int> #define fi first #define se second using namespace std; int const n_max=3e5+10; pii parent[n_max]; vector<pii> child[n_max]; int sum[n_max]; priority_queue<int> pq[n_max]; int a[n_max]; int b[n_max]; void merge(int u,int v) { if(pq[u].size()<pq[v].size()) swap(pq[u],pq[v]),swap(a[u],a[v]),swap(b[u],b[v]); while(pq[v].size()) pq[u].push(pq[v].top()), pq[v].pop(); a[u]+=a[v]; b[u]+=b[v]; } signed main() { cin.tie(nullptr)->sync_with_stdio(false); int n,m; cin>>n>>m; sum[1]=0; for(int i=2;i<=n+m;i++) { int p,c; cin>>p>>c; parent[i]={p,c}; child[p].push_back({i,c}); } for(int i=n+m;i>=n+1;i--) { pq[i].push(parent[i].se); pq[i].push(parent[i].se); a[i]++; b[i]=-parent[i].se; merge(parent[i].fi,i); } for(int i=n;i>=2;i--) { while(a[i]>1) { b[i]+=pq[i].top(); pq[i].pop(); a[i]--; } int temp1=pq[i].top();pq[i].pop(); int temp2=pq[i].top();pq[i].pop(); pq[i].push(temp1+parent[i].se); pq[i].push(temp2+parent[i].se); b[i]-=parent[i].se; merge(parent[i].fi,i); } while(a[1]>0) { b[1]+=pq[1].top(); pq[1].pop(); a[1]--; } cout<<b[1]; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...