Submission #953153

#TimeUsernameProblemLanguageResultExecution timeMemory
953153UnforgettableplFireworks (APIO16_fireworks)C++17
55 / 100
2052 ms106280 KiB
#include <bits/stdc++.h> #include <utility> using namespace std; #define int long long struct line{ int m,n; vector<int> points; line(int m,int n,vector<int> points):m(m),n(n),points(points){} void operator+=(const line &other){ m+=other.m; n+=other.n; vector<int> newpoints(points.size()+other.points.size()); merge(points.begin(), points.end(),other.points.begin(), other.points.end(),newpoints.begin()); points = newpoints; } }; int n,m; vector<pair<int,int>> adj[300001]; line dfs(int x,int p){ if(x>n)return {1,-adj[x][0].second,{adj[x][0].second,adj[x][0].second}}; int len = 0; line curr(0,0,{}); for(auto&i:adj[x]){ if(i.first!=p)curr+=dfs(i.first,x); else len = i.second; } int currsum = curr.n; int currslope = curr.m; for(auto i=curr.points.rbegin();i!=curr.points.rend();i++){ currslope--; if(currslope==-1)break; currsum+=*i; } while(curr.m>1){ curr.m--; curr.points.erase(--curr.points.end()); } curr.points[curr.points.size()-1]+=len; curr.points[curr.points.size()-2]+=len; curr.n = currsum-curr.points.back(); return curr; } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for(int i=2;i<=n+m;i++){ int p,c;cin>>p>>c; adj[i].emplace_back(p,c); adj[p].emplace_back(i,c); } auto ans = dfs(1,0); int currsum = ans.n; int currslope = ans.m; for(auto i=ans.points.rbegin();i!=ans.points.rend();i++){ currslope--; if(currslope==-1)break; currsum+=*i; } cout << currsum << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...