#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define LL long long
#define st first
#define nd second
#define endl '\n'
using namespace std;
const int MAXN=300005;
int n,m,p[MAXN],c[MAXN];
LL a[MAXN],b[MAXN];
priority_queue<LL> pq[MAXN];
void swap2(int x,int y) {
if(pq[x].size()<pq[y].size()) {
pq[x].swap(pq[y]);
swap(a[x],a[y]);
swap(b[x],b[y]);
}
}
void merge(int x,int y) {
swap2(x,y);
a[x]+=a[y];
b[x]+=b[y];
while(!pq[y].empty()) {
pq[x].push(pq[y].top());
pq[y].pop();
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
for(int i=2;i<=n+m;++i) {
cin>>p[i]>>c[i];
if(i>n) {
a[i]=1;
b[i]=-c[i];
pq[i].push(c[i]);
pq[i].push(c[i]);
merge(p[i],i);
}
}
for(int i=n;i>1;--i) {
while(a[i]>1) {
--a[i];
b[i]+=pq[i].top();
pq[i].pop();
}
LL t1=pq[i].top();
pq[i].pop();
LL t2=pq[i].top();
pq[i].pop();
pq[i].push(t2+c[i]);
pq[i].push(t1+c[i]);
b[i]-=c[i];
merge(p[i],i);
}
while(a[1]>1) {
--a[1];
b[1]+=pq[1].top();
pq[1].pop();
}
cout<<pq[1].top()+b[1]<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
18584 KB |
Output is correct |
2 |
Correct |
0 ms |
18584 KB |
Output is correct |
3 |
Correct |
0 ms |
18584 KB |
Output is correct |
4 |
Correct |
0 ms |
18584 KB |
Output is correct |
5 |
Correct |
3 ms |
18584 KB |
Output is correct |
6 |
Correct |
3 ms |
18584 KB |
Output is correct |
7 |
Correct |
0 ms |
18584 KB |
Output is correct |
8 |
Correct |
3 ms |
18584 KB |
Output is correct |
9 |
Correct |
3 ms |
18584 KB |
Output is correct |
10 |
Correct |
3 ms |
18584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
18584 KB |
Output is correct |
2 |
Correct |
0 ms |
18584 KB |
Output is correct |
3 |
Correct |
3 ms |
18584 KB |
Output is correct |
4 |
Correct |
0 ms |
18584 KB |
Output is correct |
5 |
Correct |
3 ms |
18584 KB |
Output is correct |
6 |
Correct |
0 ms |
18584 KB |
Output is correct |
7 |
Correct |
0 ms |
18584 KB |
Output is correct |
8 |
Correct |
0 ms |
18584 KB |
Output is correct |
9 |
Correct |
0 ms |
18584 KB |
Output is correct |
10 |
Correct |
3 ms |
18584 KB |
Output is correct |
11 |
Correct |
0 ms |
18584 KB |
Output is correct |
12 |
Correct |
0 ms |
18584 KB |
Output is correct |
13 |
Correct |
3 ms |
18584 KB |
Output is correct |
14 |
Correct |
0 ms |
18584 KB |
Output is correct |
15 |
Correct |
6 ms |
18584 KB |
Output is correct |
16 |
Correct |
0 ms |
18584 KB |
Output is correct |
17 |
Correct |
0 ms |
18584 KB |
Output is correct |
18 |
Correct |
0 ms |
18584 KB |
Output is correct |
19 |
Correct |
0 ms |
18584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
18584 KB |
Output is correct |
2 |
Correct |
0 ms |
18584 KB |
Output is correct |
3 |
Correct |
0 ms |
18584 KB |
Output is correct |
4 |
Correct |
0 ms |
18584 KB |
Output is correct |
5 |
Correct |
3 ms |
18584 KB |
Output is correct |
6 |
Correct |
3 ms |
18584 KB |
Output is correct |
7 |
Correct |
0 ms |
18584 KB |
Output is correct |
8 |
Correct |
3 ms |
18584 KB |
Output is correct |
9 |
Correct |
3 ms |
18584 KB |
Output is correct |
10 |
Correct |
3 ms |
18584 KB |
Output is correct |
11 |
Correct |
3 ms |
18584 KB |
Output is correct |
12 |
Correct |
0 ms |
18584 KB |
Output is correct |
13 |
Correct |
3 ms |
18584 KB |
Output is correct |
14 |
Correct |
0 ms |
18584 KB |
Output is correct |
15 |
Correct |
3 ms |
18584 KB |
Output is correct |
16 |
Correct |
0 ms |
18584 KB |
Output is correct |
17 |
Correct |
0 ms |
18584 KB |
Output is correct |
18 |
Correct |
0 ms |
18584 KB |
Output is correct |
19 |
Correct |
0 ms |
18584 KB |
Output is correct |
20 |
Correct |
3 ms |
18584 KB |
Output is correct |
21 |
Correct |
0 ms |
18584 KB |
Output is correct |
22 |
Correct |
0 ms |
18584 KB |
Output is correct |
23 |
Correct |
3 ms |
18584 KB |
Output is correct |
24 |
Correct |
0 ms |
18584 KB |
Output is correct |
25 |
Correct |
6 ms |
18584 KB |
Output is correct |
26 |
Correct |
0 ms |
18584 KB |
Output is correct |
27 |
Correct |
0 ms |
18584 KB |
Output is correct |
28 |
Correct |
0 ms |
18584 KB |
Output is correct |
29 |
Correct |
0 ms |
18584 KB |
Output is correct |
30 |
Correct |
0 ms |
18584 KB |
Output is correct |
31 |
Correct |
0 ms |
18584 KB |
Output is correct |
32 |
Correct |
0 ms |
18584 KB |
Output is correct |
33 |
Correct |
3 ms |
18584 KB |
Output is correct |
34 |
Correct |
0 ms |
18724 KB |
Output is correct |
35 |
Correct |
6 ms |
18716 KB |
Output is correct |
36 |
Correct |
0 ms |
18716 KB |
Output is correct |
37 |
Correct |
3 ms |
18716 KB |
Output is correct |
38 |
Correct |
0 ms |
18716 KB |
Output is correct |
39 |
Correct |
3 ms |
18716 KB |
Output is correct |
40 |
Correct |
6 ms |
18584 KB |
Output is correct |
41 |
Correct |
0 ms |
18584 KB |
Output is correct |
42 |
Correct |
3 ms |
18584 KB |
Output is correct |
43 |
Correct |
6 ms |
18728 KB |
Output is correct |
44 |
Correct |
3 ms |
18716 KB |
Output is correct |
45 |
Correct |
0 ms |
18716 KB |
Output is correct |
46 |
Correct |
3 ms |
19008 KB |
Output is correct |
47 |
Correct |
6 ms |
19028 KB |
Output is correct |
48 |
Correct |
3 ms |
18872 KB |
Output is correct |
49 |
Correct |
3 ms |
18864 KB |
Output is correct |
50 |
Correct |
3 ms |
18872 KB |
Output is correct |
51 |
Correct |
0 ms |
18872 KB |
Output is correct |
52 |
Correct |
0 ms |
18864 KB |
Output is correct |
53 |
Correct |
0 ms |
18868 KB |
Output is correct |
54 |
Correct |
0 ms |
18908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
18584 KB |
Output is correct |
2 |
Correct |
0 ms |
18584 KB |
Output is correct |
3 |
Correct |
0 ms |
18584 KB |
Output is correct |
4 |
Correct |
0 ms |
18584 KB |
Output is correct |
5 |
Correct |
3 ms |
18584 KB |
Output is correct |
6 |
Correct |
3 ms |
18584 KB |
Output is correct |
7 |
Correct |
0 ms |
18584 KB |
Output is correct |
8 |
Correct |
3 ms |
18584 KB |
Output is correct |
9 |
Correct |
3 ms |
18584 KB |
Output is correct |
10 |
Correct |
3 ms |
18584 KB |
Output is correct |
11 |
Correct |
3 ms |
18584 KB |
Output is correct |
12 |
Correct |
0 ms |
18584 KB |
Output is correct |
13 |
Correct |
3 ms |
18584 KB |
Output is correct |
14 |
Correct |
0 ms |
18584 KB |
Output is correct |
15 |
Correct |
3 ms |
18584 KB |
Output is correct |
16 |
Correct |
0 ms |
18584 KB |
Output is correct |
17 |
Correct |
0 ms |
18584 KB |
Output is correct |
18 |
Correct |
0 ms |
18584 KB |
Output is correct |
19 |
Correct |
0 ms |
18584 KB |
Output is correct |
20 |
Correct |
3 ms |
18584 KB |
Output is correct |
21 |
Correct |
0 ms |
18584 KB |
Output is correct |
22 |
Correct |
0 ms |
18584 KB |
Output is correct |
23 |
Correct |
3 ms |
18584 KB |
Output is correct |
24 |
Correct |
0 ms |
18584 KB |
Output is correct |
25 |
Correct |
6 ms |
18584 KB |
Output is correct |
26 |
Correct |
0 ms |
18584 KB |
Output is correct |
27 |
Correct |
0 ms |
18584 KB |
Output is correct |
28 |
Correct |
0 ms |
18584 KB |
Output is correct |
29 |
Correct |
0 ms |
18584 KB |
Output is correct |
30 |
Correct |
0 ms |
18584 KB |
Output is correct |
31 |
Correct |
0 ms |
18584 KB |
Output is correct |
32 |
Correct |
0 ms |
18584 KB |
Output is correct |
33 |
Correct |
3 ms |
18584 KB |
Output is correct |
34 |
Correct |
0 ms |
18724 KB |
Output is correct |
35 |
Correct |
6 ms |
18716 KB |
Output is correct |
36 |
Correct |
0 ms |
18716 KB |
Output is correct |
37 |
Correct |
3 ms |
18716 KB |
Output is correct |
38 |
Correct |
0 ms |
18716 KB |
Output is correct |
39 |
Correct |
3 ms |
18716 KB |
Output is correct |
40 |
Correct |
6 ms |
18584 KB |
Output is correct |
41 |
Correct |
0 ms |
18584 KB |
Output is correct |
42 |
Correct |
3 ms |
18584 KB |
Output is correct |
43 |
Correct |
6 ms |
18728 KB |
Output is correct |
44 |
Correct |
3 ms |
18716 KB |
Output is correct |
45 |
Correct |
0 ms |
18716 KB |
Output is correct |
46 |
Correct |
3 ms |
19008 KB |
Output is correct |
47 |
Correct |
6 ms |
19028 KB |
Output is correct |
48 |
Correct |
3 ms |
18872 KB |
Output is correct |
49 |
Correct |
3 ms |
18864 KB |
Output is correct |
50 |
Correct |
3 ms |
18872 KB |
Output is correct |
51 |
Correct |
0 ms |
18872 KB |
Output is correct |
52 |
Correct |
0 ms |
18864 KB |
Output is correct |
53 |
Correct |
0 ms |
18868 KB |
Output is correct |
54 |
Correct |
0 ms |
18908 KB |
Output is correct |
55 |
Correct |
6 ms |
18984 KB |
Output is correct |
56 |
Correct |
26 ms |
20572 KB |
Output is correct |
57 |
Correct |
53 ms |
21812 KB |
Output is correct |
58 |
Correct |
63 ms |
22876 KB |
Output is correct |
59 |
Correct |
79 ms |
24196 KB |
Output is correct |
60 |
Correct |
126 ms |
25304 KB |
Output is correct |
61 |
Correct |
156 ms |
27044 KB |
Output is correct |
62 |
Correct |
166 ms |
27744 KB |
Output is correct |
63 |
Correct |
209 ms |
29260 KB |
Output is correct |
64 |
Correct |
216 ms |
29272 KB |
Output is correct |
65 |
Correct |
89 ms |
18584 KB |
Output is correct |
66 |
Correct |
109 ms |
18584 KB |
Output is correct |
67 |
Correct |
79 ms |
18584 KB |
Output is correct |
68 |
Correct |
143 ms |
26384 KB |
Output is correct |
69 |
Correct |
193 ms |
27024 KB |
Output is correct |
70 |
Correct |
163 ms |
27020 KB |
Output is correct |
71 |
Correct |
193 ms |
44380 KB |
Output is correct |
72 |
Correct |
233 ms |
44336 KB |
Output is correct |
73 |
Correct |
233 ms |
40392 KB |
Output is correct |
74 |
Correct |
186 ms |
41064 KB |
Output is correct |
75 |
Correct |
239 ms |
40972 KB |
Output is correct |
76 |
Correct |
179 ms |
41036 KB |
Output is correct |
77 |
Correct |
216 ms |
41120 KB |
Output is correct |
78 |
Correct |
203 ms |
39784 KB |
Output is correct |
79 |
Correct |
179 ms |
38992 KB |
Output is correct |