#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
32092 KB |
Output is correct |
2 |
Correct |
7 ms |
32092 KB |
Output is correct |
3 |
Correct |
7 ms |
32344 KB |
Output is correct |
4 |
Correct |
7 ms |
32280 KB |
Output is correct |
5 |
Correct |
6 ms |
32088 KB |
Output is correct |
6 |
Correct |
6 ms |
32284 KB |
Output is correct |
7 |
Correct |
6 ms |
32092 KB |
Output is correct |
8 |
Correct |
14 ms |
30812 KB |
Output is correct |
9 |
Correct |
10 ms |
32180 KB |
Output is correct |
10 |
Correct |
7 ms |
32088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
32092 KB |
Output is correct |
2 |
Correct |
8 ms |
32284 KB |
Output is correct |
3 |
Incorrect |
7 ms |
32188 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
32092 KB |
Output is correct |
2 |
Correct |
7 ms |
32092 KB |
Output is correct |
3 |
Correct |
7 ms |
32344 KB |
Output is correct |
4 |
Correct |
7 ms |
32280 KB |
Output is correct |
5 |
Correct |
6 ms |
32088 KB |
Output is correct |
6 |
Correct |
6 ms |
32284 KB |
Output is correct |
7 |
Correct |
6 ms |
32092 KB |
Output is correct |
8 |
Correct |
14 ms |
30812 KB |
Output is correct |
9 |
Correct |
10 ms |
32180 KB |
Output is correct |
10 |
Correct |
7 ms |
32088 KB |
Output is correct |
11 |
Correct |
9 ms |
32092 KB |
Output is correct |
12 |
Correct |
8 ms |
32284 KB |
Output is correct |
13 |
Incorrect |
7 ms |
32188 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
32092 KB |
Output is correct |
2 |
Correct |
7 ms |
32092 KB |
Output is correct |
3 |
Correct |
7 ms |
32344 KB |
Output is correct |
4 |
Correct |
7 ms |
32280 KB |
Output is correct |
5 |
Correct |
6 ms |
32088 KB |
Output is correct |
6 |
Correct |
6 ms |
32284 KB |
Output is correct |
7 |
Correct |
6 ms |
32092 KB |
Output is correct |
8 |
Correct |
14 ms |
30812 KB |
Output is correct |
9 |
Correct |
10 ms |
32180 KB |
Output is correct |
10 |
Correct |
7 ms |
32088 KB |
Output is correct |
11 |
Correct |
9 ms |
32092 KB |
Output is correct |
12 |
Correct |
8 ms |
32284 KB |
Output is correct |
13 |
Incorrect |
7 ms |
32188 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |