#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll N=3e5+5;
int n,m,p[N],c[N];
struct ddata
{
ll a,b;
priority_queue<ll> *pq;
ddata operator +(ddata k){
ddata res;
tie(res.a,res.b)=P(a+k.a,b+k.b);
if(pq->size()>=k.pq->size()){
res.pq=pq;
while(k.pq->size()){
res.pq->push(k.pq->top());
k.pq->pop();
}
}
else{
res.pq=k.pq;
while(pq->size()){
res.pq->push(pq->top());
pq->pop();
}
}
return res;
}
};
ddata a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=2;i<=n+m;i++) cin>>p[i]>>c[i];
for(int i=n+m;i>=1;i--){
a[i].a=a[i].b=0;
a[i].pq=new priority_queue<ll>;
}
for(int i=n+m;i>n;i--){
a[i].a=1; a[i].b=-c[i];
a[i].pq->push(c[i]); a[i].pq->push(c[i]);
a[p[i]]=a[p[i]]+a[i];
}
for(int i=n;i>=2;i--){
while(a[i].a>1){
a[i].a--;
a[i].b+=a[i].pq->top();
a[i].pq->pop();
}
ll t1=a[i].pq->top();
a[i].pq->pop();
ll t2=a[i].pq->top();
a[i].pq->pop();
a[i].pq->push(t1+c[i]); a[i].pq->push(t2+c[i]);
a[i].b-=c[i];
a[p[i]]=a[p[i]]+a[i];
}
while(a[1].a>0){
a[1].a--;
a[1].b+=a[1].pq->top();
a[1].pq->pop();
}
cout<<a[1].b;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
388 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
420 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
380 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
388 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
420 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
504 KB |
Output is correct |
32 |
Correct |
3 ms |
632 KB |
Output is correct |
33 |
Correct |
3 ms |
632 KB |
Output is correct |
34 |
Correct |
3 ms |
700 KB |
Output is correct |
35 |
Correct |
3 ms |
888 KB |
Output is correct |
36 |
Correct |
4 ms |
888 KB |
Output is correct |
37 |
Correct |
4 ms |
1016 KB |
Output is correct |
38 |
Correct |
4 ms |
1016 KB |
Output is correct |
39 |
Correct |
4 ms |
1020 KB |
Output is correct |
40 |
Correct |
3 ms |
760 KB |
Output is correct |
41 |
Correct |
3 ms |
760 KB |
Output is correct |
42 |
Correct |
3 ms |
888 KB |
Output is correct |
43 |
Correct |
4 ms |
888 KB |
Output is correct |
44 |
Correct |
4 ms |
888 KB |
Output is correct |
45 |
Correct |
4 ms |
888 KB |
Output is correct |
46 |
Correct |
5 ms |
1144 KB |
Output is correct |
47 |
Correct |
5 ms |
1272 KB |
Output is correct |
48 |
Correct |
4 ms |
1116 KB |
Output is correct |
49 |
Correct |
4 ms |
1144 KB |
Output is correct |
50 |
Correct |
4 ms |
888 KB |
Output is correct |
51 |
Correct |
4 ms |
1016 KB |
Output is correct |
52 |
Correct |
4 ms |
1016 KB |
Output is correct |
53 |
Correct |
4 ms |
1016 KB |
Output is correct |
54 |
Correct |
4 ms |
1128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
388 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
420 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
504 KB |
Output is correct |
32 |
Correct |
3 ms |
632 KB |
Output is correct |
33 |
Correct |
3 ms |
632 KB |
Output is correct |
34 |
Correct |
3 ms |
700 KB |
Output is correct |
35 |
Correct |
3 ms |
888 KB |
Output is correct |
36 |
Correct |
4 ms |
888 KB |
Output is correct |
37 |
Correct |
4 ms |
1016 KB |
Output is correct |
38 |
Correct |
4 ms |
1016 KB |
Output is correct |
39 |
Correct |
4 ms |
1020 KB |
Output is correct |
40 |
Correct |
3 ms |
760 KB |
Output is correct |
41 |
Correct |
3 ms |
760 KB |
Output is correct |
42 |
Correct |
3 ms |
888 KB |
Output is correct |
43 |
Correct |
4 ms |
888 KB |
Output is correct |
44 |
Correct |
4 ms |
888 KB |
Output is correct |
45 |
Correct |
4 ms |
888 KB |
Output is correct |
46 |
Correct |
5 ms |
1144 KB |
Output is correct |
47 |
Correct |
5 ms |
1272 KB |
Output is correct |
48 |
Correct |
4 ms |
1116 KB |
Output is correct |
49 |
Correct |
4 ms |
1144 KB |
Output is correct |
50 |
Correct |
4 ms |
888 KB |
Output is correct |
51 |
Correct |
4 ms |
1016 KB |
Output is correct |
52 |
Correct |
4 ms |
1016 KB |
Output is correct |
53 |
Correct |
4 ms |
1016 KB |
Output is correct |
54 |
Correct |
4 ms |
1128 KB |
Output is correct |
55 |
Correct |
8 ms |
2040 KB |
Output is correct |
56 |
Correct |
31 ms |
6908 KB |
Output is correct |
57 |
Correct |
49 ms |
11356 KB |
Output is correct |
58 |
Correct |
66 ms |
14452 KB |
Output is correct |
59 |
Correct |
92 ms |
19160 KB |
Output is correct |
60 |
Correct |
124 ms |
23600 KB |
Output is correct |
61 |
Correct |
152 ms |
27148 KB |
Output is correct |
62 |
Correct |
186 ms |
29676 KB |
Output is correct |
63 |
Correct |
191 ms |
35556 KB |
Output is correct |
64 |
Correct |
191 ms |
37176 KB |
Output is correct |
65 |
Correct |
92 ms |
28664 KB |
Output is correct |
66 |
Correct |
98 ms |
28664 KB |
Output is correct |
67 |
Correct |
90 ms |
28792 KB |
Output is correct |
68 |
Correct |
140 ms |
35396 KB |
Output is correct |
69 |
Correct |
159 ms |
36160 KB |
Output is correct |
70 |
Correct |
174 ms |
36032 KB |
Output is correct |
71 |
Correct |
187 ms |
51088 KB |
Output is correct |
72 |
Correct |
185 ms |
51044 KB |
Output is correct |
73 |
Correct |
171 ms |
47468 KB |
Output is correct |
74 |
Correct |
172 ms |
48052 KB |
Output is correct |
75 |
Correct |
166 ms |
46308 KB |
Output is correct |
76 |
Correct |
175 ms |
46336 KB |
Output is correct |
77 |
Correct |
191 ms |
45356 KB |
Output is correct |
78 |
Correct |
183 ms |
45032 KB |
Output is correct |
79 |
Correct |
169 ms |
43832 KB |
Output is correct |