#include <bits/stdc++.h>
using namespace std;
#define int int64_t
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
int N, M; cin >> N >> M;
N += M;
int c[N];
priority_queue<int> dp[N];
vector<int> g[N];
for(int i = 1; i < N; ++i) {
int p; cin >> p >> c[i];
g[p-1].push_back(i);
}
for(int u = N; u--; ) {
if(!empty(g[u])) {
for(int v : g[u]) {
assert(size(dp[v]) > size(g[v]));
for(int i = size(g[v]); --i > 0; )
dp[v].pop();
assert(size(dp[v]) > 1);
int x = dp[v].top(); dp[v].pop();
int y = dp[v].top(); dp[v].pop();
dp[v].push(x + c[v]);
dp[v].push(y + c[v]);
if(size(dp[v]) > size(dp[u]))
dp[u].swap(dp[v]);
}
for(int v : g[u])
while(!empty(dp[v]))
dp[u].push(dp[v].top()), dp[v].pop();
} else {
dp[u].push(0);
dp[u].push(0);
}
}
int ans = accumulate(c + 1, c + N, 0LL), d = -M, x {};
vector<int> res;
while(!empty(dp[0]))
res.push_back(dp[0].top()), dp[0].pop();
for(int i = size(res) - 1; d < 0; --i, ++d) {
ans += d * (res[i] - x);
x = res[i];
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
276 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
248 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
276 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
248 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
504 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
2 ms |
716 KB |
Output is correct |
36 |
Correct |
2 ms |
852 KB |
Output is correct |
37 |
Correct |
3 ms |
852 KB |
Output is correct |
38 |
Correct |
4 ms |
980 KB |
Output is correct |
39 |
Correct |
3 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
852 KB |
Output is correct |
41 |
Correct |
2 ms |
852 KB |
Output is correct |
42 |
Correct |
3 ms |
852 KB |
Output is correct |
43 |
Correct |
3 ms |
848 KB |
Output is correct |
44 |
Correct |
3 ms |
840 KB |
Output is correct |
45 |
Correct |
3 ms |
852 KB |
Output is correct |
46 |
Correct |
3 ms |
1148 KB |
Output is correct |
47 |
Correct |
3 ms |
1108 KB |
Output is correct |
48 |
Correct |
3 ms |
992 KB |
Output is correct |
49 |
Correct |
3 ms |
1108 KB |
Output is correct |
50 |
Correct |
3 ms |
928 KB |
Output is correct |
51 |
Correct |
3 ms |
852 KB |
Output is correct |
52 |
Correct |
3 ms |
980 KB |
Output is correct |
53 |
Correct |
4 ms |
1028 KB |
Output is correct |
54 |
Correct |
3 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
276 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
248 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
504 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
2 ms |
716 KB |
Output is correct |
36 |
Correct |
2 ms |
852 KB |
Output is correct |
37 |
Correct |
3 ms |
852 KB |
Output is correct |
38 |
Correct |
4 ms |
980 KB |
Output is correct |
39 |
Correct |
3 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
852 KB |
Output is correct |
41 |
Correct |
2 ms |
852 KB |
Output is correct |
42 |
Correct |
3 ms |
852 KB |
Output is correct |
43 |
Correct |
3 ms |
848 KB |
Output is correct |
44 |
Correct |
3 ms |
840 KB |
Output is correct |
45 |
Correct |
3 ms |
852 KB |
Output is correct |
46 |
Correct |
3 ms |
1148 KB |
Output is correct |
47 |
Correct |
3 ms |
1108 KB |
Output is correct |
48 |
Correct |
3 ms |
992 KB |
Output is correct |
49 |
Correct |
3 ms |
1108 KB |
Output is correct |
50 |
Correct |
3 ms |
928 KB |
Output is correct |
51 |
Correct |
3 ms |
852 KB |
Output is correct |
52 |
Correct |
3 ms |
980 KB |
Output is correct |
53 |
Correct |
4 ms |
1028 KB |
Output is correct |
54 |
Correct |
3 ms |
1108 KB |
Output is correct |
55 |
Correct |
7 ms |
2072 KB |
Output is correct |
56 |
Correct |
27 ms |
7068 KB |
Output is correct |
57 |
Correct |
52 ms |
11724 KB |
Output is correct |
58 |
Correct |
76 ms |
15220 KB |
Output is correct |
59 |
Correct |
94 ms |
20152 KB |
Output is correct |
60 |
Correct |
130 ms |
24972 KB |
Output is correct |
61 |
Correct |
137 ms |
28580 KB |
Output is correct |
62 |
Correct |
168 ms |
31468 KB |
Output is correct |
63 |
Correct |
195 ms |
37732 KB |
Output is correct |
64 |
Correct |
237 ms |
39308 KB |
Output is correct |
65 |
Correct |
95 ms |
33356 KB |
Output is correct |
66 |
Correct |
90 ms |
33336 KB |
Output is correct |
67 |
Correct |
93 ms |
33336 KB |
Output is correct |
68 |
Correct |
161 ms |
37452 KB |
Output is correct |
69 |
Correct |
179 ms |
37944 KB |
Output is correct |
70 |
Correct |
177 ms |
37920 KB |
Output is correct |
71 |
Correct |
235 ms |
53776 KB |
Output is correct |
72 |
Correct |
219 ms |
53924 KB |
Output is correct |
73 |
Correct |
197 ms |
50140 KB |
Output is correct |
74 |
Correct |
199 ms |
50340 KB |
Output is correct |
75 |
Correct |
188 ms |
49004 KB |
Output is correct |
76 |
Correct |
210 ms |
48832 KB |
Output is correct |
77 |
Correct |
198 ms |
47648 KB |
Output is correct |
78 |
Correct |
199 ms |
46484 KB |
Output is correct |
79 |
Correct |
199 ms |
51312 KB |
Output is correct |