#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<ll> 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();
}
ll x=q[pos[i]].top();
q[pos[i]].pop();
ll 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9636 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9756 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9756 KB |
Output is correct |
9 |
Correct |
6 ms |
9704 KB |
Output is correct |
10 |
Correct |
7 ms |
9700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9748 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
8 ms |
9716 KB |
Output is correct |
5 |
Correct |
9 ms |
9708 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9644 KB |
Output is correct |
10 |
Correct |
6 ms |
9672 KB |
Output is correct |
11 |
Correct |
7 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
7 ms |
9660 KB |
Output is correct |
14 |
Correct |
8 ms |
9676 KB |
Output is correct |
15 |
Correct |
9 ms |
9676 KB |
Output is correct |
16 |
Correct |
9 ms |
9764 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
9 ms |
9676 KB |
Output is correct |
19 |
Correct |
8 ms |
9676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9636 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9756 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9756 KB |
Output is correct |
9 |
Correct |
6 ms |
9704 KB |
Output is correct |
10 |
Correct |
7 ms |
9700 KB |
Output is correct |
11 |
Correct |
6 ms |
9748 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9676 KB |
Output is correct |
14 |
Correct |
8 ms |
9716 KB |
Output is correct |
15 |
Correct |
9 ms |
9708 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
7 ms |
9676 KB |
Output is correct |
19 |
Correct |
6 ms |
9644 KB |
Output is correct |
20 |
Correct |
6 ms |
9672 KB |
Output is correct |
21 |
Correct |
7 ms |
9676 KB |
Output is correct |
22 |
Correct |
6 ms |
9676 KB |
Output is correct |
23 |
Correct |
7 ms |
9660 KB |
Output is correct |
24 |
Correct |
8 ms |
9676 KB |
Output is correct |
25 |
Correct |
9 ms |
9676 KB |
Output is correct |
26 |
Correct |
9 ms |
9764 KB |
Output is correct |
27 |
Correct |
6 ms |
9676 KB |
Output is correct |
28 |
Correct |
9 ms |
9676 KB |
Output is correct |
29 |
Correct |
8 ms |
9676 KB |
Output is correct |
30 |
Correct |
7 ms |
9676 KB |
Output is correct |
31 |
Correct |
8 ms |
9732 KB |
Output is correct |
32 |
Correct |
7 ms |
9860 KB |
Output is correct |
33 |
Correct |
9 ms |
9804 KB |
Output is correct |
34 |
Correct |
8 ms |
9932 KB |
Output is correct |
35 |
Correct |
9 ms |
9932 KB |
Output is correct |
36 |
Correct |
8 ms |
9932 KB |
Output is correct |
37 |
Correct |
10 ms |
10072 KB |
Output is correct |
38 |
Correct |
10 ms |
10060 KB |
Output is correct |
39 |
Correct |
8 ms |
10060 KB |
Output is correct |
40 |
Correct |
7 ms |
9804 KB |
Output is correct |
41 |
Correct |
7 ms |
9860 KB |
Output is correct |
42 |
Correct |
9 ms |
9824 KB |
Output is correct |
43 |
Correct |
10 ms |
10060 KB |
Output is correct |
44 |
Correct |
9 ms |
9984 KB |
Output is correct |
45 |
Correct |
9 ms |
10060 KB |
Output is correct |
46 |
Correct |
11 ms |
10240 KB |
Output is correct |
47 |
Correct |
10 ms |
10444 KB |
Output is correct |
48 |
Correct |
10 ms |
10116 KB |
Output is correct |
49 |
Correct |
10 ms |
10244 KB |
Output is correct |
50 |
Correct |
9 ms |
10116 KB |
Output is correct |
51 |
Correct |
8 ms |
10112 KB |
Output is correct |
52 |
Correct |
8 ms |
10188 KB |
Output is correct |
53 |
Correct |
10 ms |
10188 KB |
Output is correct |
54 |
Correct |
9 ms |
10188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9636 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9756 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9756 KB |
Output is correct |
9 |
Correct |
6 ms |
9704 KB |
Output is correct |
10 |
Correct |
7 ms |
9700 KB |
Output is correct |
11 |
Correct |
6 ms |
9748 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9676 KB |
Output is correct |
14 |
Correct |
8 ms |
9716 KB |
Output is correct |
15 |
Correct |
9 ms |
9708 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
7 ms |
9676 KB |
Output is correct |
19 |
Correct |
6 ms |
9644 KB |
Output is correct |
20 |
Correct |
6 ms |
9672 KB |
Output is correct |
21 |
Correct |
7 ms |
9676 KB |
Output is correct |
22 |
Correct |
6 ms |
9676 KB |
Output is correct |
23 |
Correct |
7 ms |
9660 KB |
Output is correct |
24 |
Correct |
8 ms |
9676 KB |
Output is correct |
25 |
Correct |
9 ms |
9676 KB |
Output is correct |
26 |
Correct |
9 ms |
9764 KB |
Output is correct |
27 |
Correct |
6 ms |
9676 KB |
Output is correct |
28 |
Correct |
9 ms |
9676 KB |
Output is correct |
29 |
Correct |
8 ms |
9676 KB |
Output is correct |
30 |
Correct |
7 ms |
9676 KB |
Output is correct |
31 |
Correct |
8 ms |
9732 KB |
Output is correct |
32 |
Correct |
7 ms |
9860 KB |
Output is correct |
33 |
Correct |
9 ms |
9804 KB |
Output is correct |
34 |
Correct |
8 ms |
9932 KB |
Output is correct |
35 |
Correct |
9 ms |
9932 KB |
Output is correct |
36 |
Correct |
8 ms |
9932 KB |
Output is correct |
37 |
Correct |
10 ms |
10072 KB |
Output is correct |
38 |
Correct |
10 ms |
10060 KB |
Output is correct |
39 |
Correct |
8 ms |
10060 KB |
Output is correct |
40 |
Correct |
7 ms |
9804 KB |
Output is correct |
41 |
Correct |
7 ms |
9860 KB |
Output is correct |
42 |
Correct |
9 ms |
9824 KB |
Output is correct |
43 |
Correct |
10 ms |
10060 KB |
Output is correct |
44 |
Correct |
9 ms |
9984 KB |
Output is correct |
45 |
Correct |
9 ms |
10060 KB |
Output is correct |
46 |
Correct |
11 ms |
10240 KB |
Output is correct |
47 |
Correct |
10 ms |
10444 KB |
Output is correct |
48 |
Correct |
10 ms |
10116 KB |
Output is correct |
49 |
Correct |
10 ms |
10244 KB |
Output is correct |
50 |
Correct |
9 ms |
10116 KB |
Output is correct |
51 |
Correct |
8 ms |
10112 KB |
Output is correct |
52 |
Correct |
8 ms |
10188 KB |
Output is correct |
53 |
Correct |
10 ms |
10188 KB |
Output is correct |
54 |
Correct |
9 ms |
10188 KB |
Output is correct |
55 |
Correct |
13 ms |
10624 KB |
Output is correct |
56 |
Correct |
31 ms |
13428 KB |
Output is correct |
57 |
Correct |
54 ms |
15940 KB |
Output is correct |
58 |
Correct |
73 ms |
17724 KB |
Output is correct |
59 |
Correct |
91 ms |
20288 KB |
Output is correct |
60 |
Correct |
120 ms |
22668 KB |
Output is correct |
61 |
Correct |
142 ms |
24988 KB |
Output is correct |
62 |
Correct |
165 ms |
26364 KB |
Output is correct |
63 |
Correct |
172 ms |
29544 KB |
Output is correct |
64 |
Correct |
195 ms |
30396 KB |
Output is correct |
65 |
Correct |
88 ms |
19292 KB |
Output is correct |
66 |
Correct |
90 ms |
19308 KB |
Output is correct |
67 |
Correct |
110 ms |
19268 KB |
Output is correct |
68 |
Correct |
146 ms |
28288 KB |
Output is correct |
69 |
Correct |
180 ms |
28984 KB |
Output is correct |
70 |
Correct |
173 ms |
29032 KB |
Output is correct |
71 |
Correct |
209 ms |
45940 KB |
Output is correct |
72 |
Correct |
215 ms |
46056 KB |
Output is correct |
73 |
Correct |
178 ms |
42560 KB |
Output is correct |
74 |
Correct |
184 ms |
43144 KB |
Output is correct |
75 |
Correct |
186 ms |
41432 KB |
Output is correct |
76 |
Correct |
185 ms |
41656 KB |
Output is correct |
77 |
Correct |
200 ms |
41116 KB |
Output is correct |
78 |
Correct |
225 ms |
40368 KB |
Output is correct |
79 |
Correct |
181 ms |
38396 KB |
Output is correct |