#include <bits/stdc++.h>
#define nl '\n'
#ifdef LOCAL
#include "template/debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m;
std::cin >> n >> m;
std::vector<int> P(n + m), C(n + m);
for (int i = 1; i < n + m; i++) {
std::cin >> P[i] >> C[i];
--P[i];
}
// we do dfs now
// f(x) is the minimum value to get
std::vector<int64_t> a(n + m), b(n + m);
std::vector<std::priority_queue<int64_t>> dp(n + m);
auto merge = [&](int i, int j) {
a[i] += a[j];
b[i] += b[j];
if (dp[i].size() < dp[j].size()) {
std::swap(dp[i], dp[j]);
}
while (dp[j].size()) {
dp[i].push(dp[j].top());
dp[j].pop();
}
};
for (int i = n + m - 1; i >= n; i--) {
a[i] = 1;
b[i] = -C[i];
dp[i].push(C[i]);
dp[i].push(C[i]);
merge(P[i], i);
}
dbg("done peasants");
auto pop_right = [&](int i) -> int64_t {
int64_t popped = dp[i].top();
int64_t v = a[i] * popped + b[i];
--a[i];
b[i] = v - a[i] * popped;
dp[i].pop();
return popped;
};
auto insert_right = [&](int i, int64_t p) -> void {
int64_t v = a[i] * p + b[i];
a[i]++;
b[i] = v - a[i] * p;
dp[i].push(p);
};
for (int i = n - 1; i > 0; i--) {
while (a[i] > 1) {
pop_right(i);
}
auto t1 = pop_right(i) + C[i];
auto t0 = pop_right(i) + C[i];
insert_right(i, t0);
insert_right(i, t1);
b[i] += C[i];
merge(P[i], i);
}
while (a[0] > 0) {
pop_right(0);
}
std::cout << a[0] * dp[0].top() + b[0] << nl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
456 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
456 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
352 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
352 KB |
Output is correct |
32 |
Correct |
1 ms |
620 KB |
Output is correct |
33 |
Correct |
1 ms |
616 KB |
Output is correct |
34 |
Correct |
1 ms |
608 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
2 ms |
864 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
2 ms |
860 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
608 KB |
Output is correct |
43 |
Correct |
2 ms |
868 KB |
Output is correct |
44 |
Correct |
2 ms |
852 KB |
Output is correct |
45 |
Correct |
2 ms |
952 KB |
Output is correct |
46 |
Correct |
2 ms |
1116 KB |
Output is correct |
47 |
Correct |
2 ms |
1176 KB |
Output is correct |
48 |
Correct |
2 ms |
836 KB |
Output is correct |
49 |
Correct |
2 ms |
1116 KB |
Output is correct |
50 |
Correct |
2 ms |
720 KB |
Output is correct |
51 |
Correct |
2 ms |
860 KB |
Output is correct |
52 |
Correct |
2 ms |
860 KB |
Output is correct |
53 |
Correct |
2 ms |
860 KB |
Output is correct |
54 |
Correct |
2 ms |
1072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
456 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
352 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
352 KB |
Output is correct |
32 |
Correct |
1 ms |
620 KB |
Output is correct |
33 |
Correct |
1 ms |
616 KB |
Output is correct |
34 |
Correct |
1 ms |
608 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
2 ms |
864 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
2 ms |
860 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
608 KB |
Output is correct |
43 |
Correct |
2 ms |
868 KB |
Output is correct |
44 |
Correct |
2 ms |
852 KB |
Output is correct |
45 |
Correct |
2 ms |
952 KB |
Output is correct |
46 |
Correct |
2 ms |
1116 KB |
Output is correct |
47 |
Correct |
2 ms |
1176 KB |
Output is correct |
48 |
Correct |
2 ms |
836 KB |
Output is correct |
49 |
Correct |
2 ms |
1116 KB |
Output is correct |
50 |
Correct |
2 ms |
720 KB |
Output is correct |
51 |
Correct |
2 ms |
860 KB |
Output is correct |
52 |
Correct |
2 ms |
860 KB |
Output is correct |
53 |
Correct |
2 ms |
860 KB |
Output is correct |
54 |
Correct |
2 ms |
1072 KB |
Output is correct |
55 |
Correct |
5 ms |
1628 KB |
Output is correct |
56 |
Correct |
16 ms |
5720 KB |
Output is correct |
57 |
Correct |
27 ms |
9420 KB |
Output is correct |
58 |
Correct |
36 ms |
12020 KB |
Output is correct |
59 |
Correct |
47 ms |
15616 KB |
Output is correct |
60 |
Correct |
61 ms |
19404 KB |
Output is correct |
61 |
Correct |
71 ms |
22448 KB |
Output is correct |
62 |
Correct |
78 ms |
24516 KB |
Output is correct |
63 |
Correct |
101 ms |
28988 KB |
Output is correct |
64 |
Correct |
99 ms |
30496 KB |
Output is correct |
65 |
Correct |
51 ms |
21588 KB |
Output is correct |
66 |
Correct |
56 ms |
21756 KB |
Output is correct |
67 |
Correct |
49 ms |
21720 KB |
Output is correct |
68 |
Correct |
112 ms |
28412 KB |
Output is correct |
69 |
Correct |
102 ms |
29092 KB |
Output is correct |
70 |
Correct |
97 ms |
28892 KB |
Output is correct |
71 |
Correct |
135 ms |
44868 KB |
Output is correct |
72 |
Correct |
131 ms |
44064 KB |
Output is correct |
73 |
Correct |
113 ms |
40488 KB |
Output is correct |
74 |
Correct |
119 ms |
42152 KB |
Output is correct |
75 |
Correct |
118 ms |
40396 KB |
Output is correct |
76 |
Correct |
123 ms |
40128 KB |
Output is correct |
77 |
Correct |
123 ms |
39280 KB |
Output is correct |
78 |
Correct |
130 ms |
38560 KB |
Output is correct |
79 |
Correct |
105 ms |
37520 KB |
Output is correct |