#include <bits/stdc++.h>
#define rep(i,x,n) for(decltype(n) i=x; i<n; i++)
using namespace std;
using pq=priority_queue<long>;
int main() {
ios_base::sync_with_stdio(false);cin.tie(nullptr);
int N, M; long r=0;
cin>>N>>M;
vector<int> W(N+M);
vector<vector<int>> G(N);
rep(i, 1, N+M) {
int j;
cin>>j>>W[i];
G[j-1].push_back(i);
r+=W[i];
}
function<pq(int)> f=[&](int i) {
pq q;
if(i<N) {
for(int j:G[i]) {
pq p=f(j);
if(q.size()<p.size()) swap(q, p);
while(p.size()) q.push(p.top()), p.pop();
}
rep(j, 1, G[i].size()) q.pop();
long x=q.top()+W[i]; q.pop();
long y=q.top()+W[i]; q.pop();
q.push(x), q.push(y);
} else q.push(W[i]), q.push(W[i]);
return q;
};
auto q=f(0);
long p=q.top(), k=0; q.pop();
while(q.size())
r-=k++*(p-q.top()), p=q.top(), q.pop();
cout<<r-k*p;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
380 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
380 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
380 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
380 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
380 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
5 ms |
380 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
6 ms |
376 KB |
Output is correct |
33 |
Correct |
6 ms |
504 KB |
Output is correct |
34 |
Correct |
7 ms |
504 KB |
Output is correct |
35 |
Correct |
7 ms |
632 KB |
Output is correct |
36 |
Correct |
7 ms |
632 KB |
Output is correct |
37 |
Correct |
7 ms |
632 KB |
Output is correct |
38 |
Correct |
8 ms |
632 KB |
Output is correct |
39 |
Correct |
8 ms |
632 KB |
Output is correct |
40 |
Correct |
7 ms |
1272 KB |
Output is correct |
41 |
Correct |
7 ms |
1272 KB |
Output is correct |
42 |
Correct |
7 ms |
760 KB |
Output is correct |
43 |
Correct |
9 ms |
1016 KB |
Output is correct |
44 |
Correct |
8 ms |
760 KB |
Output is correct |
45 |
Correct |
8 ms |
764 KB |
Output is correct |
46 |
Correct |
8 ms |
504 KB |
Output is correct |
47 |
Correct |
8 ms |
504 KB |
Output is correct |
48 |
Correct |
8 ms |
504 KB |
Output is correct |
49 |
Correct |
8 ms |
504 KB |
Output is correct |
50 |
Correct |
7 ms |
632 KB |
Output is correct |
51 |
Correct |
7 ms |
504 KB |
Output is correct |
52 |
Correct |
8 ms |
504 KB |
Output is correct |
53 |
Correct |
8 ms |
504 KB |
Output is correct |
54 |
Correct |
8 ms |
680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
380 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
380 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
5 ms |
380 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
6 ms |
376 KB |
Output is correct |
33 |
Correct |
6 ms |
504 KB |
Output is correct |
34 |
Correct |
7 ms |
504 KB |
Output is correct |
35 |
Correct |
7 ms |
632 KB |
Output is correct |
36 |
Correct |
7 ms |
632 KB |
Output is correct |
37 |
Correct |
7 ms |
632 KB |
Output is correct |
38 |
Correct |
8 ms |
632 KB |
Output is correct |
39 |
Correct |
8 ms |
632 KB |
Output is correct |
40 |
Correct |
7 ms |
1272 KB |
Output is correct |
41 |
Correct |
7 ms |
1272 KB |
Output is correct |
42 |
Correct |
7 ms |
760 KB |
Output is correct |
43 |
Correct |
9 ms |
1016 KB |
Output is correct |
44 |
Correct |
8 ms |
760 KB |
Output is correct |
45 |
Correct |
8 ms |
764 KB |
Output is correct |
46 |
Correct |
8 ms |
504 KB |
Output is correct |
47 |
Correct |
8 ms |
504 KB |
Output is correct |
48 |
Correct |
8 ms |
504 KB |
Output is correct |
49 |
Correct |
8 ms |
504 KB |
Output is correct |
50 |
Correct |
7 ms |
632 KB |
Output is correct |
51 |
Correct |
7 ms |
504 KB |
Output is correct |
52 |
Correct |
8 ms |
504 KB |
Output is correct |
53 |
Correct |
8 ms |
504 KB |
Output is correct |
54 |
Correct |
8 ms |
680 KB |
Output is correct |
55 |
Correct |
13 ms |
1144 KB |
Output is correct |
56 |
Correct |
36 ms |
3448 KB |
Output is correct |
57 |
Correct |
67 ms |
5408 KB |
Output is correct |
58 |
Correct |
80 ms |
6988 KB |
Output is correct |
59 |
Correct |
120 ms |
8980 KB |
Output is correct |
60 |
Correct |
146 ms |
11124 KB |
Output is correct |
61 |
Correct |
172 ms |
12984 KB |
Output is correct |
62 |
Correct |
190 ms |
14048 KB |
Output is correct |
63 |
Correct |
245 ms |
16512 KB |
Output is correct |
64 |
Correct |
263 ms |
16980 KB |
Output is correct |
65 |
Correct |
149 ms |
60408 KB |
Output is correct |
66 |
Correct |
146 ms |
60536 KB |
Output is correct |
67 |
Correct |
139 ms |
22904 KB |
Output is correct |
68 |
Correct |
207 ms |
35436 KB |
Output is correct |
69 |
Correct |
233 ms |
32240 KB |
Output is correct |
70 |
Correct |
227 ms |
32116 KB |
Output is correct |
71 |
Correct |
243 ms |
12204 KB |
Output is correct |
72 |
Correct |
245 ms |
12284 KB |
Output is correct |
73 |
Correct |
218 ms |
11376 KB |
Output is correct |
74 |
Correct |
228 ms |
11356 KB |
Output is correct |
75 |
Correct |
219 ms |
11188 KB |
Output is correct |
76 |
Correct |
216 ms |
11220 KB |
Output is correct |
77 |
Correct |
231 ms |
11984 KB |
Output is correct |
78 |
Correct |
223 ms |
11848 KB |
Output is correct |
79 |
Correct |
215 ms |
14608 KB |
Output is correct |