#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
int const n_max=3e5+10;
pii parent[n_max];
vector<pii> child[n_max];
int sum[n_max];
priority_queue<int> pq[n_max];
int a[n_max];
int b[n_max];
void merge(int u,int v)
{
if(pq[u].size()<pq[v].size()) swap(pq[u],pq[v]),swap(a[u],a[v]),swap(b[u],b[v]);
while(pq[v].size()) pq[u].push(pq[v].top()), pq[v].pop();
a[u]+=a[v];
b[u]+=b[v];
}
signed main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int n,m;
cin>>n>>m;
sum[1]=0;
for(int i=2;i<=n+m;i++)
{
int p,c;
cin>>p>>c;
parent[i]={p,c};
child[p].push_back({i,c});
}
for(int i=n+m;i>=n+1;i--)
{
pq[i].push(parent[i].se);
pq[i].push(parent[i].se);
a[i]++;
b[i]=-parent[i].se;
merge(parent[i].fi,i);
}
for(int i=n;i>=2;i--)
{
while(a[i]>1)
{
b[i]+=pq[i].top();
pq[i].pop();
a[i]--;
}
int temp1=pq[i].top();pq[i].pop();
int temp2=pq[i].top();pq[i].pop();
pq[i].push(temp1+parent[i].se);
pq[i].push(temp2+parent[i].se);
b[i]-=parent[i].se;
merge(parent[i].fi,i);
}
while(a[1]>0)
{
b[1]+=pq[1].top();
pq[1].pop();
a[1]--;
}
cout<<b[1];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
22872 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
4 ms |
22876 KB |
Output is correct |
4 |
Correct |
5 ms |
22876 KB |
Output is correct |
5 |
Correct |
4 ms |
22968 KB |
Output is correct |
6 |
Correct |
4 ms |
22876 KB |
Output is correct |
7 |
Correct |
5 ms |
23048 KB |
Output is correct |
8 |
Correct |
5 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22876 KB |
Output is correct |
10 |
Correct |
4 ms |
22876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
22872 KB |
Output is correct |
2 |
Correct |
5 ms |
22876 KB |
Output is correct |
3 |
Correct |
4 ms |
22876 KB |
Output is correct |
4 |
Correct |
4 ms |
22976 KB |
Output is correct |
5 |
Correct |
4 ms |
22876 KB |
Output is correct |
6 |
Correct |
5 ms |
22876 KB |
Output is correct |
7 |
Correct |
6 ms |
22876 KB |
Output is correct |
8 |
Correct |
5 ms |
20828 KB |
Output is correct |
9 |
Correct |
4 ms |
20824 KB |
Output is correct |
10 |
Correct |
4 ms |
20828 KB |
Output is correct |
11 |
Correct |
4 ms |
20828 KB |
Output is correct |
12 |
Correct |
4 ms |
20992 KB |
Output is correct |
13 |
Correct |
5 ms |
20828 KB |
Output is correct |
14 |
Correct |
4 ms |
21080 KB |
Output is correct |
15 |
Correct |
4 ms |
20828 KB |
Output is correct |
16 |
Correct |
4 ms |
20828 KB |
Output is correct |
17 |
Correct |
5 ms |
20572 KB |
Output is correct |
18 |
Correct |
5 ms |
20948 KB |
Output is correct |
19 |
Correct |
4 ms |
20824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
22872 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
4 ms |
22876 KB |
Output is correct |
4 |
Correct |
5 ms |
22876 KB |
Output is correct |
5 |
Correct |
4 ms |
22968 KB |
Output is correct |
6 |
Correct |
4 ms |
22876 KB |
Output is correct |
7 |
Correct |
5 ms |
23048 KB |
Output is correct |
8 |
Correct |
5 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22876 KB |
Output is correct |
10 |
Correct |
4 ms |
22876 KB |
Output is correct |
11 |
Correct |
5 ms |
22872 KB |
Output is correct |
12 |
Correct |
5 ms |
22876 KB |
Output is correct |
13 |
Correct |
4 ms |
22876 KB |
Output is correct |
14 |
Correct |
4 ms |
22976 KB |
Output is correct |
15 |
Correct |
4 ms |
22876 KB |
Output is correct |
16 |
Correct |
5 ms |
22876 KB |
Output is correct |
17 |
Correct |
6 ms |
22876 KB |
Output is correct |
18 |
Correct |
5 ms |
20828 KB |
Output is correct |
19 |
Correct |
4 ms |
20824 KB |
Output is correct |
20 |
Correct |
4 ms |
20828 KB |
Output is correct |
21 |
Correct |
4 ms |
20828 KB |
Output is correct |
22 |
Correct |
4 ms |
20992 KB |
Output is correct |
23 |
Correct |
5 ms |
20828 KB |
Output is correct |
24 |
Correct |
4 ms |
21080 KB |
Output is correct |
25 |
Correct |
4 ms |
20828 KB |
Output is correct |
26 |
Correct |
4 ms |
20828 KB |
Output is correct |
27 |
Correct |
5 ms |
20572 KB |
Output is correct |
28 |
Correct |
5 ms |
20948 KB |
Output is correct |
29 |
Correct |
4 ms |
20824 KB |
Output is correct |
30 |
Correct |
5 ms |
20828 KB |
Output is correct |
31 |
Correct |
4 ms |
21080 KB |
Output is correct |
32 |
Correct |
5 ms |
21084 KB |
Output is correct |
33 |
Correct |
5 ms |
21084 KB |
Output is correct |
34 |
Correct |
5 ms |
21080 KB |
Output is correct |
35 |
Correct |
5 ms |
21084 KB |
Output is correct |
36 |
Correct |
7 ms |
21176 KB |
Output is correct |
37 |
Correct |
6 ms |
21340 KB |
Output is correct |
38 |
Correct |
6 ms |
21384 KB |
Output is correct |
39 |
Correct |
6 ms |
21340 KB |
Output is correct |
40 |
Correct |
5 ms |
21084 KB |
Output is correct |
41 |
Correct |
5 ms |
21084 KB |
Output is correct |
42 |
Correct |
5 ms |
21084 KB |
Output is correct |
43 |
Correct |
6 ms |
21340 KB |
Output is correct |
44 |
Correct |
6 ms |
21340 KB |
Output is correct |
45 |
Correct |
6 ms |
21340 KB |
Output is correct |
46 |
Correct |
6 ms |
21084 KB |
Output is correct |
47 |
Correct |
7 ms |
21084 KB |
Output is correct |
48 |
Correct |
7 ms |
20828 KB |
Output is correct |
49 |
Correct |
7 ms |
21084 KB |
Output is correct |
50 |
Correct |
7 ms |
20828 KB |
Output is correct |
51 |
Correct |
7 ms |
20828 KB |
Output is correct |
52 |
Correct |
7 ms |
20828 KB |
Output is correct |
53 |
Correct |
8 ms |
20808 KB |
Output is correct |
54 |
Correct |
8 ms |
20824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
22872 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
4 ms |
22876 KB |
Output is correct |
4 |
Correct |
5 ms |
22876 KB |
Output is correct |
5 |
Correct |
4 ms |
22968 KB |
Output is correct |
6 |
Correct |
4 ms |
22876 KB |
Output is correct |
7 |
Correct |
5 ms |
23048 KB |
Output is correct |
8 |
Correct |
5 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22876 KB |
Output is correct |
10 |
Correct |
4 ms |
22876 KB |
Output is correct |
11 |
Correct |
5 ms |
22872 KB |
Output is correct |
12 |
Correct |
5 ms |
22876 KB |
Output is correct |
13 |
Correct |
4 ms |
22876 KB |
Output is correct |
14 |
Correct |
4 ms |
22976 KB |
Output is correct |
15 |
Correct |
4 ms |
22876 KB |
Output is correct |
16 |
Correct |
5 ms |
22876 KB |
Output is correct |
17 |
Correct |
6 ms |
22876 KB |
Output is correct |
18 |
Correct |
5 ms |
20828 KB |
Output is correct |
19 |
Correct |
4 ms |
20824 KB |
Output is correct |
20 |
Correct |
4 ms |
20828 KB |
Output is correct |
21 |
Correct |
4 ms |
20828 KB |
Output is correct |
22 |
Correct |
4 ms |
20992 KB |
Output is correct |
23 |
Correct |
5 ms |
20828 KB |
Output is correct |
24 |
Correct |
4 ms |
21080 KB |
Output is correct |
25 |
Correct |
4 ms |
20828 KB |
Output is correct |
26 |
Correct |
4 ms |
20828 KB |
Output is correct |
27 |
Correct |
5 ms |
20572 KB |
Output is correct |
28 |
Correct |
5 ms |
20948 KB |
Output is correct |
29 |
Correct |
4 ms |
20824 KB |
Output is correct |
30 |
Correct |
5 ms |
20828 KB |
Output is correct |
31 |
Correct |
4 ms |
21080 KB |
Output is correct |
32 |
Correct |
5 ms |
21084 KB |
Output is correct |
33 |
Correct |
5 ms |
21084 KB |
Output is correct |
34 |
Correct |
5 ms |
21080 KB |
Output is correct |
35 |
Correct |
5 ms |
21084 KB |
Output is correct |
36 |
Correct |
7 ms |
21176 KB |
Output is correct |
37 |
Correct |
6 ms |
21340 KB |
Output is correct |
38 |
Correct |
6 ms |
21384 KB |
Output is correct |
39 |
Correct |
6 ms |
21340 KB |
Output is correct |
40 |
Correct |
5 ms |
21084 KB |
Output is correct |
41 |
Correct |
5 ms |
21084 KB |
Output is correct |
42 |
Correct |
5 ms |
21084 KB |
Output is correct |
43 |
Correct |
6 ms |
21340 KB |
Output is correct |
44 |
Correct |
6 ms |
21340 KB |
Output is correct |
45 |
Correct |
6 ms |
21340 KB |
Output is correct |
46 |
Correct |
6 ms |
21084 KB |
Output is correct |
47 |
Correct |
7 ms |
21084 KB |
Output is correct |
48 |
Correct |
7 ms |
20828 KB |
Output is correct |
49 |
Correct |
7 ms |
21084 KB |
Output is correct |
50 |
Correct |
7 ms |
20828 KB |
Output is correct |
51 |
Correct |
7 ms |
20828 KB |
Output is correct |
52 |
Correct |
7 ms |
20828 KB |
Output is correct |
53 |
Correct |
8 ms |
20808 KB |
Output is correct |
54 |
Correct |
8 ms |
20824 KB |
Output is correct |
55 |
Correct |
10 ms |
21592 KB |
Output is correct |
56 |
Correct |
26 ms |
25180 KB |
Output is correct |
57 |
Correct |
41 ms |
28508 KB |
Output is correct |
58 |
Correct |
52 ms |
30924 KB |
Output is correct |
59 |
Correct |
63 ms |
34892 KB |
Output is correct |
60 |
Correct |
87 ms |
38712 KB |
Output is correct |
61 |
Correct |
102 ms |
41788 KB |
Output is correct |
62 |
Correct |
114 ms |
43976 KB |
Output is correct |
63 |
Correct |
133 ms |
48508 KB |
Output is correct |
64 |
Correct |
125 ms |
49864 KB |
Output is correct |
65 |
Correct |
68 ms |
42324 KB |
Output is correct |
66 |
Correct |
67 ms |
42324 KB |
Output is correct |
67 |
Correct |
85 ms |
42324 KB |
Output is correct |
68 |
Correct |
117 ms |
47048 KB |
Output is correct |
69 |
Correct |
130 ms |
47820 KB |
Output is correct |
70 |
Correct |
135 ms |
47824 KB |
Output is correct |
71 |
Correct |
175 ms |
61888 KB |
Output is correct |
72 |
Correct |
140 ms |
62144 KB |
Output is correct |
73 |
Correct |
119 ms |
57576 KB |
Output is correct |
74 |
Correct |
143 ms |
58308 KB |
Output is correct |
75 |
Correct |
128 ms |
56184 KB |
Output is correct |
76 |
Correct |
123 ms |
55996 KB |
Output is correct |
77 |
Correct |
138 ms |
55044 KB |
Output is correct |
78 |
Correct |
132 ms |
54308 KB |
Output is correct |
79 |
Correct |
117 ms |
53512 KB |
Output is correct |