# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
92219 | moonrabbit2 | Fireworks (APIO16_fireworks) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll N=3e5+5;
int n,m,p[N],c[N];
struct data
{
ll a,b;
priority_queue<ll> *pq;
data operator +(data k){
data res;
tie(res.a,res.b)=P(a+k.a,b+k.b);
if(pq->size()>=k.pq->size()){
res.pq=pq;
while(k.pq->size()){
res.pq->push(k.pq->top());
k.pq->pop();
}
}
else{
res.pq=k.pq;
while(pq->size()){
res.pq->push(pq->top());
pq->pop();
}
}
return res;
}
};
data a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=2;i<=n+m;i++) cin>>p[i]>>c[i];
for(int i=n+m;i>=1;i--){
a[i].a=a[i].b=0;
a[i].pq=new priority_queue<ll>;
}
for(int i=n+m;i>n;i--){
a[i].a=1; a[i].b=-c[i];
a[i].pq->push(c[i]); a[i].pq->push(c[i]);
a[p[i]]=a[p[i]]+a[i];
}
for(int i=n;i>=2;i--){
while(a[i].a>1){
a[i].a--;
a[i].b+=a[i].pq->top();
a[i].pq->pop();
}
ll t1=a[i].pq->top();
a[i].pq->pop();
ll t2=a[i].pq->top();
a[i].pq->pop();
a[i].pq->push(t1+c[i]); a[i].pq->push(t2+c[i]);
a[i].b-=c[i];
a[p[i]]=a[p[i]]+a[i];
}
while(a[1].a>0){
a[1].a--;
a[1].b+=a[1].pq->top();
a[1].pq->pop();
}
cout<<a[1].b;
return 0;
}