Submission #487497

#TimeUsernameProblemLanguageResultExecution timeMemory
487497leakedFireworks (APIO16_fireworks)C++14
7 / 100
8 ms12068 KiB
#include <bits/stdc++.h> #define f first #define s second #define m_p make_pair #define pb push_back #define vec vector #define sz(x) (int)(x).size() #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define pw(x) (1LL<<(x)) #define fast_iati ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define int long long using namespace std; template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);} template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);} typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; const int N=5e5+1; const ll inf=1e18; //#define int long long vec<pii> g[N]; ll h[N]; pll vl[N]; ll k[N],b[N]; ll ans=0; void dfs(int v){ if(!sz(g[v])){ vl[v]={h[v],h[v]}; k[v]=1,b[v]=-h[v]; // cout<<"D "<<v<<' '<<h[v]<<endl; } else{ vec<int> st; ll l=-inf,r=inf; for(auto &z : g[v]){ h[z.f]=h[v]+z.s; dfs(z.f); umax(l,h[v]); k[v]+=k[z.f];b[v]+=b[z.f]; st.pb(vl[z.f].f);st.pb(vl[z.f].s); } ll mn=inf; sort(rall(st)); vec<ll>go; ll _k=k[v],_b=b[v]; for(auto &z : st){ _k--;_b+=z; umin(mn,_k*z+_b); if(_k*z+_b==mn) go.pb(z); } reverse(all(go)); vl[v]={max(l,go[0]),min(r,go.back())}; ans+=mn;b[v]=-vl[v].s;k[v]=1; // cout<<"DEBUG "<<v<<' '<<mn<<' '<<vl[v].f<<' '<<vl[v].s<<' '<<b[v]<<endl; } } signed main(){ fast_iati; int n,m; cin>>n>>m; for(int i=1;i<n+m;i++){ int v,x; cin>>v>>x;--v; g[v].pb({i,x}); } for(int i=0;i<n;i++) assert(sz(g[i])); for(int i=n;i<n+m;i++) assert(!sz(g[i])); dfs(0); cout<<ans; 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...