# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
111515 | nxteru | Fireworks (APIO16_fireworks) | C++14 | 18 ms | 16768 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define F first
#define S second
#define PB push_back
ll n,m,ans,vs[300005];
priority_queue<ll>p[300005];
vector<P>g[300005];
void up(int v,ll c){
if(p[v].size()==0){
p[v].push(c);
p[v].push(c);
}else{
ll x=p[v].top();
p[v].pop();
ll y=p[v].top();
p[v].pop();
p[v].push(x+c);
p[v].push(y+c);
}
}
void merge(int x,int y){
if(p[vs[x]].size()<p[vs[y]].size())swap(x,y);
int py=y;
y=vs[y];
x=vs[x];
vs[py]=x;
if(p[y].size()==0)return;
ll mi=min(p[x].top(),p[y].top());
while(!p[y].empty()){
p[x].push(p[y].top());
p[y].pop();
}
p[x].pop();
ans+=p[x].top()-mi;
}
void dfs(int v){
if(g[v].size()==0)return;
for(int i=0;i<g[v].size();i++){
ll u=g[v][i].F,c=g[v][i].S;
dfs(u);
up(vs[u],c);
merge(v,u);
}
}
int main(void){
scanf("%lld%lld",&n,&m);
n+=m;
for(int i=0;i<n;i++)vs[i]=i;
for(int i=1;i<n;i++){
ll q,c;
scanf("%lld%lld",&q,&c);
g[--q].PB(P(i,c));
}
dfs(0);
printf("%lld\n",ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |