#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
using namespace std;
const int mxn=3e5+5;
vector<int>g[mxn];
ll p[mxn],c[mxn],n;
priority_queue<ll>q[mxn];
ll a[mxn]{0};ll b[mxn]{0};
void dfs(int u){
for(auto v:g[u]){
if(v>n)continue;
dfs(v);
while(a[v]>1){
a[v]--;
b[v]+=q[v].top();q[v].pop();
}
ll x=q[v].top();q[v].pop();
ll y=q[v].top();q[v].pop();
q[v].push(x+c[v]);q[v].push(y+c[v]);
b[v]-=c[v];
if(q[v].size()>q[u].size())swap(q[u],q[v]);
while(!q[v].empty())q[u].push(q[v].top()),q[v].pop();
a[u]+=a[v];b[u]+=b[v];
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int m;cin>>n>>m;
for(int i=2;i<=n+m;i++)cin>>p[i]>>c[i],g[p[i]].pb(i);
for(int i=n+1;i<=n+m;i++){
q[p[i]].push(c[i]);q[p[i]].push(c[i]);
b[p[i]]-=c[i];a[p[i]]++;
}dfs(1);
while(a[1]>0){
a[1]--;b[1]+=q[1].top();q[1].pop();
}cout<<b[1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21852 KB |
Output is correct |
2 |
Correct |
3 ms |
21852 KB |
Output is correct |
3 |
Correct |
4 ms |
21852 KB |
Output is correct |
4 |
Correct |
3 ms |
19804 KB |
Output is correct |
5 |
Correct |
3 ms |
19908 KB |
Output is correct |
6 |
Correct |
4 ms |
19804 KB |
Output is correct |
7 |
Correct |
3 ms |
21852 KB |
Output is correct |
8 |
Correct |
3 ms |
21852 KB |
Output is correct |
9 |
Correct |
3 ms |
21852 KB |
Output is correct |
10 |
Correct |
3 ms |
21852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21848 KB |
Output is correct |
2 |
Correct |
3 ms |
19804 KB |
Output is correct |
3 |
Correct |
3 ms |
21852 KB |
Output is correct |
4 |
Correct |
3 ms |
19804 KB |
Output is correct |
5 |
Correct |
4 ms |
21852 KB |
Output is correct |
6 |
Correct |
3 ms |
19804 KB |
Output is correct |
7 |
Correct |
4 ms |
21852 KB |
Output is correct |
8 |
Correct |
3 ms |
21848 KB |
Output is correct |
9 |
Correct |
3 ms |
21852 KB |
Output is correct |
10 |
Correct |
4 ms |
21980 KB |
Output is correct |
11 |
Correct |
3 ms |
21852 KB |
Output is correct |
12 |
Correct |
3 ms |
19804 KB |
Output is correct |
13 |
Correct |
3 ms |
22016 KB |
Output is correct |
14 |
Correct |
4 ms |
21852 KB |
Output is correct |
15 |
Correct |
4 ms |
21852 KB |
Output is correct |
16 |
Correct |
4 ms |
21852 KB |
Output is correct |
17 |
Correct |
3 ms |
21852 KB |
Output is correct |
18 |
Correct |
5 ms |
21852 KB |
Output is correct |
19 |
Correct |
5 ms |
21852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21852 KB |
Output is correct |
2 |
Correct |
3 ms |
21852 KB |
Output is correct |
3 |
Correct |
4 ms |
21852 KB |
Output is correct |
4 |
Correct |
3 ms |
19804 KB |
Output is correct |
5 |
Correct |
3 ms |
19908 KB |
Output is correct |
6 |
Correct |
4 ms |
19804 KB |
Output is correct |
7 |
Correct |
3 ms |
21852 KB |
Output is correct |
8 |
Correct |
3 ms |
21852 KB |
Output is correct |
9 |
Correct |
3 ms |
21852 KB |
Output is correct |
10 |
Correct |
3 ms |
21852 KB |
Output is correct |
11 |
Correct |
4 ms |
21848 KB |
Output is correct |
12 |
Correct |
3 ms |
19804 KB |
Output is correct |
13 |
Correct |
3 ms |
21852 KB |
Output is correct |
14 |
Correct |
3 ms |
19804 KB |
Output is correct |
15 |
Correct |
4 ms |
21852 KB |
Output is correct |
16 |
Correct |
3 ms |
19804 KB |
Output is correct |
17 |
Correct |
4 ms |
21852 KB |
Output is correct |
18 |
Correct |
3 ms |
21848 KB |
Output is correct |
19 |
Correct |
3 ms |
21852 KB |
Output is correct |
20 |
Correct |
4 ms |
21980 KB |
Output is correct |
21 |
Correct |
3 ms |
21852 KB |
Output is correct |
22 |
Correct |
3 ms |
19804 KB |
Output is correct |
23 |
Correct |
3 ms |
22016 KB |
Output is correct |
24 |
Correct |
4 ms |
21852 KB |
Output is correct |
25 |
Correct |
4 ms |
21852 KB |
Output is correct |
26 |
Correct |
4 ms |
21852 KB |
Output is correct |
27 |
Correct |
3 ms |
21852 KB |
Output is correct |
28 |
Correct |
5 ms |
21852 KB |
Output is correct |
29 |
Correct |
5 ms |
21852 KB |
Output is correct |
30 |
Correct |
3 ms |
19804 KB |
Output is correct |
31 |
Correct |
4 ms |
21852 KB |
Output is correct |
32 |
Correct |
4 ms |
22104 KB |
Output is correct |
33 |
Correct |
4 ms |
22108 KB |
Output is correct |
34 |
Correct |
5 ms |
22108 KB |
Output is correct |
35 |
Correct |
5 ms |
22108 KB |
Output is correct |
36 |
Correct |
5 ms |
20060 KB |
Output is correct |
37 |
Correct |
5 ms |
22108 KB |
Output is correct |
38 |
Correct |
5 ms |
22108 KB |
Output is correct |
39 |
Correct |
5 ms |
22108 KB |
Output is correct |
40 |
Correct |
5 ms |
22876 KB |
Output is correct |
41 |
Correct |
5 ms |
22876 KB |
Output is correct |
42 |
Correct |
5 ms |
22104 KB |
Output is correct |
43 |
Correct |
6 ms |
22620 KB |
Output is correct |
44 |
Correct |
5 ms |
22364 KB |
Output is correct |
45 |
Correct |
6 ms |
22364 KB |
Output is correct |
46 |
Correct |
5 ms |
22108 KB |
Output is correct |
47 |
Correct |
4 ms |
22108 KB |
Output is correct |
48 |
Correct |
5 ms |
22108 KB |
Output is correct |
49 |
Correct |
5 ms |
22108 KB |
Output is correct |
50 |
Correct |
5 ms |
22108 KB |
Output is correct |
51 |
Correct |
5 ms |
22224 KB |
Output is correct |
52 |
Correct |
5 ms |
22108 KB |
Output is correct |
53 |
Correct |
4 ms |
22088 KB |
Output is correct |
54 |
Correct |
4 ms |
22108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21852 KB |
Output is correct |
2 |
Correct |
3 ms |
21852 KB |
Output is correct |
3 |
Correct |
4 ms |
21852 KB |
Output is correct |
4 |
Correct |
3 ms |
19804 KB |
Output is correct |
5 |
Correct |
3 ms |
19908 KB |
Output is correct |
6 |
Correct |
4 ms |
19804 KB |
Output is correct |
7 |
Correct |
3 ms |
21852 KB |
Output is correct |
8 |
Correct |
3 ms |
21852 KB |
Output is correct |
9 |
Correct |
3 ms |
21852 KB |
Output is correct |
10 |
Correct |
3 ms |
21852 KB |
Output is correct |
11 |
Correct |
4 ms |
21848 KB |
Output is correct |
12 |
Correct |
3 ms |
19804 KB |
Output is correct |
13 |
Correct |
3 ms |
21852 KB |
Output is correct |
14 |
Correct |
3 ms |
19804 KB |
Output is correct |
15 |
Correct |
4 ms |
21852 KB |
Output is correct |
16 |
Correct |
3 ms |
19804 KB |
Output is correct |
17 |
Correct |
4 ms |
21852 KB |
Output is correct |
18 |
Correct |
3 ms |
21848 KB |
Output is correct |
19 |
Correct |
3 ms |
21852 KB |
Output is correct |
20 |
Correct |
4 ms |
21980 KB |
Output is correct |
21 |
Correct |
3 ms |
21852 KB |
Output is correct |
22 |
Correct |
3 ms |
19804 KB |
Output is correct |
23 |
Correct |
3 ms |
22016 KB |
Output is correct |
24 |
Correct |
4 ms |
21852 KB |
Output is correct |
25 |
Correct |
4 ms |
21852 KB |
Output is correct |
26 |
Correct |
4 ms |
21852 KB |
Output is correct |
27 |
Correct |
3 ms |
21852 KB |
Output is correct |
28 |
Correct |
5 ms |
21852 KB |
Output is correct |
29 |
Correct |
5 ms |
21852 KB |
Output is correct |
30 |
Correct |
3 ms |
19804 KB |
Output is correct |
31 |
Correct |
4 ms |
21852 KB |
Output is correct |
32 |
Correct |
4 ms |
22104 KB |
Output is correct |
33 |
Correct |
4 ms |
22108 KB |
Output is correct |
34 |
Correct |
5 ms |
22108 KB |
Output is correct |
35 |
Correct |
5 ms |
22108 KB |
Output is correct |
36 |
Correct |
5 ms |
20060 KB |
Output is correct |
37 |
Correct |
5 ms |
22108 KB |
Output is correct |
38 |
Correct |
5 ms |
22108 KB |
Output is correct |
39 |
Correct |
5 ms |
22108 KB |
Output is correct |
40 |
Correct |
5 ms |
22876 KB |
Output is correct |
41 |
Correct |
5 ms |
22876 KB |
Output is correct |
42 |
Correct |
5 ms |
22104 KB |
Output is correct |
43 |
Correct |
6 ms |
22620 KB |
Output is correct |
44 |
Correct |
5 ms |
22364 KB |
Output is correct |
45 |
Correct |
6 ms |
22364 KB |
Output is correct |
46 |
Correct |
5 ms |
22108 KB |
Output is correct |
47 |
Correct |
4 ms |
22108 KB |
Output is correct |
48 |
Correct |
5 ms |
22108 KB |
Output is correct |
49 |
Correct |
5 ms |
22108 KB |
Output is correct |
50 |
Correct |
5 ms |
22108 KB |
Output is correct |
51 |
Correct |
5 ms |
22224 KB |
Output is correct |
52 |
Correct |
5 ms |
22108 KB |
Output is correct |
53 |
Correct |
4 ms |
22088 KB |
Output is correct |
54 |
Correct |
4 ms |
22108 KB |
Output is correct |
55 |
Correct |
8 ms |
22876 KB |
Output is correct |
56 |
Correct |
21 ms |
27352 KB |
Output is correct |
57 |
Correct |
41 ms |
29640 KB |
Output is correct |
58 |
Correct |
50 ms |
31200 KB |
Output is correct |
59 |
Correct |
54 ms |
33672 KB |
Output is correct |
60 |
Correct |
74 ms |
35956 KB |
Output is correct |
61 |
Correct |
81 ms |
38092 KB |
Output is correct |
62 |
Correct |
88 ms |
39624 KB |
Output is correct |
63 |
Correct |
155 ms |
42184 KB |
Output is correct |
64 |
Correct |
111 ms |
42952 KB |
Output is correct |
65 |
Correct |
97 ms |
87416 KB |
Output is correct |
66 |
Correct |
108 ms |
87376 KB |
Output is correct |
67 |
Correct |
81 ms |
40528 KB |
Output is correct |
68 |
Correct |
129 ms |
64968 KB |
Output is correct |
69 |
Correct |
144 ms |
60616 KB |
Output is correct |
70 |
Correct |
121 ms |
60612 KB |
Output is correct |
71 |
Correct |
110 ms |
43632 KB |
Output is correct |
72 |
Correct |
99 ms |
44296 KB |
Output is correct |
73 |
Correct |
79 ms |
40384 KB |
Output is correct |
74 |
Correct |
73 ms |
40896 KB |
Output is correct |
75 |
Correct |
82 ms |
39616 KB |
Output is correct |
76 |
Correct |
84 ms |
40164 KB |
Output is correct |
77 |
Correct |
87 ms |
38972 KB |
Output is correct |
78 |
Correct |
100 ms |
39796 KB |
Output is correct |
79 |
Correct |
88 ms |
38728 KB |
Output is correct |