이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define LL long long
#define st first
#define nd second
#define endl '\n'
using namespace std;
const int MAXN=300005;
int n,m,p[MAXN],c[MAXN];
LL a[MAXN],b[MAXN];
priority_queue<LL> pq[MAXN];
void swap2(int x,int y) {
if(pq[x].size()<pq[y].size()) {
pq[x].swap(pq[y]);
swap(a[x],a[y]);
swap(b[x],b[y]);
}
}
void merge(int x,int y) {
swap2(x,y);
a[x]+=a[y];
b[x]+=b[y];
while(!pq[y].empty()) {
pq[x].push(pq[y].top());
pq[y].pop();
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
for(int i=2;i<=n+m;++i) {
cin>>p[i]>>c[i];
if(i>n) {
a[i]=1;
b[i]=-c[i];
pq[i].push(c[i]);
pq[i].push(c[i]);
merge(p[i],i);
}
}
for(int i=n;i>1;--i) {
while(a[i]>1) {
--a[i];
b[i]+=pq[i].top();
pq[i].pop();
}
LL t1=pq[i].top();
pq[i].pop();
LL t2=pq[i].top();
pq[i].pop();
pq[i].push(t2+c[i]);
pq[i].push(t1+c[i]);
b[i]-=c[i];
merge(p[i],i);
}
while(a[1]>1) {
--a[1];
b[1]+=pq[1].top();
pq[1].pop();
}
cout<<pq[1].top()+b[1]<<endl;
}
# | 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... |