Submission #51691

#TimeUsernameProblemLanguageResultExecution timeMemory
51691tmwilliamlin168Fireworks (APIO16_fireworks)C++14
0 / 100
17 ms14640 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long const int mxN=3e5; int n, m, p[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]; adj[p[i]].push_back(i); } for(int i=1; i<n; ++i) { if(adj[i].size()==1) { c[adj[i][0]]+=c[i]; adj[p[i]].push_back(adj[i][0]); } } for(int i=n+m-1; i>=0; --i) { if(adj[i].size()==1&&i) continue; if(i>=n) { yi1[i]=-1; pc[i].push_back(0); pc[i].push_back(0); } for(int j : adj[i]) { if(!yi0[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]; } ll ans=yi0[0]; for(int i=0; i<-yi1[0]; ++i) ans+=(yi1[0]+i)*(pc[0][i+1]-pc[0][i]); 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...