#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> ii;
typedef complex<ld> cp;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<cp> vcp;
typedef vector<ld> vld;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vii> vvii;
#define fastIO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define forw(i,l,r) for( int i = (l) ; i < (r) ; i++ )
#define forb(i,r,l) for( int i = (r) ; i >= (l) ; i-- )
#define log2i(x) (32 - __builtin_clz((x)) - 1)
#define log2ll(x) (64 - __builtin_clzll((x)) - 1)
#define Pi 3.141592653589793
#define sz(x) (int)x.size()
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
const int N = 3e5+7;
int n,m;
int par[N],pos[N];
ll a[N],b[N],c[N];
priority_queue<int> q[N];
int add(int x, int y) {
if(sz(q[x])<sz(q[y])) swap(x,y);
while(sz(q[y])) {
q[x].push(q[y].top()); q[y].pop();
}
a[x]+=a[y]; b[x]+=b[y];
return x;
}
int main() {
fastIO;
//freopen("test.inp","r",stdin);
//freopen("test.out","w",stdout);
cin >> n >> m;
forw(i,1,n+m) {
cin >> par[i] >> c[i];
pos[i]=i; par[i]--;
}
forw(i,n,n+m) {
q[i].push(c[i]); q[i].push(c[i]);
a[i]=1; b[i]=-c[i];
pos[par[i]]=add(pos[par[i]],pos[i]);
}
forb(i,n-1,1) {
while(a[pos[i]]>1) {
a[pos[i]]--; b[pos[i]]+=q[pos[i]].top();
q[pos[i]].pop();
}
int x=q[pos[i]].top();
q[pos[i]].pop();
int y=q[pos[i]].top();
q[pos[i]].pop();
x+=c[i]; y+=c[i];
q[pos[i]].push(x); q[pos[i]].push(y);
b[pos[i]]-=c[i];
pos[par[i]]=add(pos[par[i]],pos[i]);
}
while(a[pos[0]]>0) {
a[pos[0]]--; b[pos[0]]+=q[pos[0]].top();
q[pos[0]].pop();
}
cout << b[pos[0]];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9716 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
10 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9676 KB |
Output is correct |
7 |
Correct |
7 ms |
9728 KB |
Output is correct |
8 |
Correct |
7 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9760 KB |
Output is correct |
10 |
Correct |
6 ms |
9656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
7 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
8 ms |
9728 KB |
Output is correct |
5 |
Correct |
8 ms |
9676 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9716 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
8 ms |
9720 KB |
Output is correct |
10 |
Correct |
8 ms |
9676 KB |
Output is correct |
11 |
Correct |
8 ms |
9764 KB |
Output is correct |
12 |
Correct |
7 ms |
9676 KB |
Output is correct |
13 |
Correct |
7 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9732 KB |
Output is correct |
15 |
Correct |
7 ms |
9716 KB |
Output is correct |
16 |
Correct |
7 ms |
9716 KB |
Output is correct |
17 |
Correct |
7 ms |
9676 KB |
Output is correct |
18 |
Correct |
7 ms |
9696 KB |
Output is correct |
19 |
Correct |
7 ms |
9724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9716 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
10 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9676 KB |
Output is correct |
7 |
Correct |
7 ms |
9728 KB |
Output is correct |
8 |
Correct |
7 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9760 KB |
Output is correct |
10 |
Correct |
6 ms |
9656 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
7 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9676 KB |
Output is correct |
14 |
Correct |
8 ms |
9728 KB |
Output is correct |
15 |
Correct |
8 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9716 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
8 ms |
9720 KB |
Output is correct |
20 |
Correct |
8 ms |
9676 KB |
Output is correct |
21 |
Correct |
8 ms |
9764 KB |
Output is correct |
22 |
Correct |
7 ms |
9676 KB |
Output is correct |
23 |
Correct |
7 ms |
9676 KB |
Output is correct |
24 |
Correct |
7 ms |
9732 KB |
Output is correct |
25 |
Correct |
7 ms |
9716 KB |
Output is correct |
26 |
Correct |
7 ms |
9716 KB |
Output is correct |
27 |
Correct |
7 ms |
9676 KB |
Output is correct |
28 |
Correct |
7 ms |
9696 KB |
Output is correct |
29 |
Correct |
7 ms |
9724 KB |
Output is correct |
30 |
Incorrect |
8 ms |
9736 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9716 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
10 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9676 KB |
Output is correct |
7 |
Correct |
7 ms |
9728 KB |
Output is correct |
8 |
Correct |
7 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9760 KB |
Output is correct |
10 |
Correct |
6 ms |
9656 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
7 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9676 KB |
Output is correct |
14 |
Correct |
8 ms |
9728 KB |
Output is correct |
15 |
Correct |
8 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9716 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
8 ms |
9720 KB |
Output is correct |
20 |
Correct |
8 ms |
9676 KB |
Output is correct |
21 |
Correct |
8 ms |
9764 KB |
Output is correct |
22 |
Correct |
7 ms |
9676 KB |
Output is correct |
23 |
Correct |
7 ms |
9676 KB |
Output is correct |
24 |
Correct |
7 ms |
9732 KB |
Output is correct |
25 |
Correct |
7 ms |
9716 KB |
Output is correct |
26 |
Correct |
7 ms |
9716 KB |
Output is correct |
27 |
Correct |
7 ms |
9676 KB |
Output is correct |
28 |
Correct |
7 ms |
9696 KB |
Output is correct |
29 |
Correct |
7 ms |
9724 KB |
Output is correct |
30 |
Incorrect |
8 ms |
9736 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |