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;
#define endl '\n'
using ll=long long;
struct A{
multiset<ll> L,R;
ll mn;
A():mn(0){}
void insert(ll x1,ll x2,ll c){
mn+=c;
if(L.empty()) L.insert(x1), R.insert(x2);
else if(x2<*L.rbegin()){
mn+=*L.rbegin()-x2;
L.emplace(x1), L.emplace(x2);
R.emplace(*L.rbegin()), L.erase(prev(L.end()));
}
else if(x1>*R.begin()){
mn+=x1-*R.begin();
R.emplace(x1), R.emplace(x2);
L.emplace(*R.begin()), R.erase(R.begin());
}
else L.emplace(x1), R.emplace(x2);
}
}t[300005];
int p[300005];
ll c[300005];
int main(){
ios::sync_with_stdio(false); cin.tie(0);
int n,m; cin>>n>>m;
for(int i=2;i<=n+m;++i) cin>>p[i]>>c[i];
for(int i=n+m;i>n;--i) t[p[i]].insert(c[i],c[i],0);
for(int i=n;i>1;--i){
ll x1=*t[i].L.rbegin(), x2=*t[i].R.begin();
t[p[i]].insert(x1+c[i],x2+c[i],t[i].mn);
}
cout<<t[1].mn;
}
# | 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... |