#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
using ll=long long;
struct A{
multiset<ll> L,R;
ll c,mn;
A():c(0),mn(0){}
void pushL(ll x){
if(L.empty() || x<*L.rbegin()) L.emplace(x);
else{
R.emplace(x), L.emplace(*R.begin()), R.erase(R.begin());
mn+=x-*L.rbegin();
}
}
void pushR(ll x){
if(R.empty() || x>*R.begin()) R.emplace(x);
else{
L.emplace(x), R.emplace(*L.rbegin()), L.erase(prev(L.end()));
mn+=*R.begin()-x;
}
}
A& operator+=(const A &o){
for(auto &e:o.L) pushL(e);
for(auto &e:o.R) pushR(e);
c+=o.c;
return *this;
}
}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]].pushL(c[i]), t[p[i]].pushR(c[i]);
for(int i=n;i>1;--i){
while(t[i].R.size()>1){
auto itr=prev(t[i].R.end());
t[i].c+=*itr-*t[i].R.begin();
t[i].R.erase(itr);
}
auto itr1=prev(t[i].L.end()), itr2=t[i].R.begin();
t[i].L.emplace(*itr1+c[i]), t[i].L.erase(itr1);
t[i].R.emplace(*itr2+c[i]), t[i].R.erase(itr2);
t[p[i]]+=t[i];
}
cout<<t[1].mn+t[1].c;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
34392 KB |
Output is correct |
2 |
Correct |
7 ms |
34396 KB |
Output is correct |
3 |
Correct |
7 ms |
34632 KB |
Output is correct |
4 |
Correct |
7 ms |
34396 KB |
Output is correct |
5 |
Correct |
7 ms |
34396 KB |
Output is correct |
6 |
Correct |
7 ms |
34396 KB |
Output is correct |
7 |
Correct |
7 ms |
34396 KB |
Output is correct |
8 |
Correct |
7 ms |
34636 KB |
Output is correct |
9 |
Correct |
8 ms |
34396 KB |
Output is correct |
10 |
Correct |
7 ms |
34396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
34396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
34392 KB |
Output is correct |
2 |
Correct |
7 ms |
34396 KB |
Output is correct |
3 |
Correct |
7 ms |
34632 KB |
Output is correct |
4 |
Correct |
7 ms |
34396 KB |
Output is correct |
5 |
Correct |
7 ms |
34396 KB |
Output is correct |
6 |
Correct |
7 ms |
34396 KB |
Output is correct |
7 |
Correct |
7 ms |
34396 KB |
Output is correct |
8 |
Correct |
7 ms |
34636 KB |
Output is correct |
9 |
Correct |
8 ms |
34396 KB |
Output is correct |
10 |
Correct |
7 ms |
34396 KB |
Output is correct |
11 |
Incorrect |
7 ms |
34396 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
34392 KB |
Output is correct |
2 |
Correct |
7 ms |
34396 KB |
Output is correct |
3 |
Correct |
7 ms |
34632 KB |
Output is correct |
4 |
Correct |
7 ms |
34396 KB |
Output is correct |
5 |
Correct |
7 ms |
34396 KB |
Output is correct |
6 |
Correct |
7 ms |
34396 KB |
Output is correct |
7 |
Correct |
7 ms |
34396 KB |
Output is correct |
8 |
Correct |
7 ms |
34636 KB |
Output is correct |
9 |
Correct |
8 ms |
34396 KB |
Output is correct |
10 |
Correct |
7 ms |
34396 KB |
Output is correct |
11 |
Incorrect |
7 ms |
34396 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |