Submission #51693

#TimeUsernameProblemLanguageResultExecution timeMemory
51693tmwilliamlin168Fireworks (APIO16_fireworks)C++14
55 / 100
2070 ms217436 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long const int mxN=3e5; int n, m, p[mxN], cc[mxN]; bool d[mxN]; vector<int> adj[mxN]; ll c[mxN], yi0[mxN], yi1[mxN]; vector<ll> pc[mxN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for(int i=1; i<n+m; ++i) { cin >> p[i] >> c[i], --p[i]; ++cc[p[i]]; } for(int i=n+m-1; i; --i) { if(d[i]) continue; while(p[i]&&cc[p[i]]==1) { c[i]+=c[p[i]]; d[p[i]]=1; p[i]=p[p[i]]; } adj[p[i]].push_back(i); } for(int i=n+m-1; i>=0; --i) { if(d[i]) continue; if(i>=n) { yi1[i]=-1; pc[i].push_back(0); pc[i].push_back(0); } for(int j : adj[i]) { if(d[j]) continue; yi0[i]+=yi0[j]; yi1[i]+=yi1[j]; pc[i].insert(pc[i].end(), pc[j].begin(), pc[j].end()); } sort(pc[i].begin(), pc[i].end()); pc[i].erase(pc[i].begin()-yi1[i]+1, pc[i].end()); pc[i][-yi1[i]-1]+=c[i]; pc[i][-yi1[i]]+=c[i]; yi0[i]+=c[i]; // cout << yi0[i] << " " << yi1[i] << endl; } ll ans=yi0[0]; for(int i=0; i<-yi1[0]; ++i) ans+=(yi1[0]+i)*(pc[0][i]-(i?pc[0][i-1]:0)); cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...