#include <bits/stdc++.h>
template <class T>
using Vec = std::vector<T>;
using ll = long long;
template <class F>
struct RecLambda: private F {
explicit RecLambda(F &&f): F(std::forward<F>(f)) { }
template <class... Args>
decltype(auto) operator () (Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
struct HeapNode;
using Heap = std::unique_ptr<HeapNode>;
struct HeapNode {
ll value;
Heap left, right;
int height;
HeapNode(const ll value): value(value), left(), right(), height() { }
};
Heap merge(Heap left, Heap right) {
if (!left) return right;
if (!right) return left;
if (left -> value < right -> value) {
std::swap(left, right);
}
left -> right = merge(std::move(left -> right), std::move(right));
if (!(left -> left) || left -> left -> height < left -> right -> height) {
std::swap(left -> left, left -> right);
}
left -> height = (left -> right ? left -> right -> height : 0) + 1;
return left;
}
void push(Heap &heap, const ll value) {
heap = merge(std::move(heap), std::make_unique<HeapNode>(value));
}
ll top(Heap &heap) {
return heap -> value;
}
ll pop(Heap &heap) {
const auto ret = top(heap);
heap = merge(std::move(heap -> left), std::move(heap -> right));
return ret;
}
struct SlopeTrick {
ll last_slope, last_y;
Heap changes;
SlopeTrick(): last_slope(), last_y(), changes() {
push(changes, 0);
}
void absorb(SlopeTrick &&other) {
const auto x1 = top(changes);
const auto x2 = top(other.changes);
last_y += other.last_y + (x1 < x2 ? last_slope * (x2 - x1) : other.last_slope * (x1 - x2));
last_slope += other.last_slope;
changes = merge(std::move(changes), std::move(other.changes));
}
bool pop_last() {
const auto x1 = pop(changes);
if (!changes) return false;
const auto x2 = top(changes);
last_slope -= 1;
last_y += last_slope * (x2 - x1);
return true;
}
};
int main() {
int N, M;
std::cin >> N >> M;
Vec<Vec<std::pair<int, int>>> children(N + M);
for (int i = 1; i < N + M; ++i) {
int p, c;
std::cin >> p >> c;
p -= 1;
children[p].emplace_back(i, c);
}
Vec<SlopeTrick> data(N);
RecLambda([&](auto dfs, const int u) -> void {
for (const auto [v, c]: children[u]) {
if (v >= N) {
SlopeTrick trick;
trick.last_slope = 1;
push(trick.changes, c);
push(trick.changes, c);
data[u].absorb(std::move(trick));
}
else {
dfs(v);
while (data[v].last_slope > 1) {
data[v].pop_last();
}
const auto x1 = pop(data[v].changes);
const auto x2 = pop(data[v].changes);
push(data[v].changes, x2 + c);
push(data[v].changes, x1 + c);
data[u].absorb(std::move(data[v]));
}
}
})(0);
ll ans = data[0].last_y;
while (data[0].pop_last()) {
ans = std::min(ans, data[0].last_y);
}
std::cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
2 ms |
492 KB |
Output is correct |
32 |
Correct |
3 ms |
492 KB |
Output is correct |
33 |
Correct |
4 ms |
620 KB |
Output is correct |
34 |
Correct |
5 ms |
748 KB |
Output is correct |
35 |
Correct |
5 ms |
876 KB |
Output is correct |
36 |
Correct |
6 ms |
876 KB |
Output is correct |
37 |
Correct |
7 ms |
1004 KB |
Output is correct |
38 |
Correct |
8 ms |
1004 KB |
Output is correct |
39 |
Correct |
7 ms |
1004 KB |
Output is correct |
40 |
Correct |
8 ms |
2028 KB |
Output is correct |
41 |
Correct |
8 ms |
2028 KB |
Output is correct |
42 |
Correct |
7 ms |
1004 KB |
Output is correct |
43 |
Correct |
8 ms |
1516 KB |
Output is correct |
44 |
Correct |
8 ms |
1388 KB |
Output is correct |
45 |
Correct |
8 ms |
1388 KB |
Output is correct |
46 |
Correct |
9 ms |
1004 KB |
Output is correct |
47 |
Correct |
8 ms |
1004 KB |
Output is correct |
48 |
Correct |
8 ms |
748 KB |
Output is correct |
49 |
Correct |
9 ms |
1004 KB |
Output is correct |
50 |
Correct |
8 ms |
1004 KB |
Output is correct |
51 |
Correct |
8 ms |
1132 KB |
Output is correct |
52 |
Correct |
8 ms |
1004 KB |
Output is correct |
53 |
Correct |
9 ms |
1004 KB |
Output is correct |
54 |
Correct |
10 ms |
1260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
2 ms |
492 KB |
Output is correct |
32 |
Correct |
3 ms |
492 KB |
Output is correct |
33 |
Correct |
4 ms |
620 KB |
Output is correct |
34 |
Correct |
5 ms |
748 KB |
Output is correct |
35 |
Correct |
5 ms |
876 KB |
Output is correct |
36 |
Correct |
6 ms |
876 KB |
Output is correct |
37 |
Correct |
7 ms |
1004 KB |
Output is correct |
38 |
Correct |
8 ms |
1004 KB |
Output is correct |
39 |
Correct |
7 ms |
1004 KB |
Output is correct |
40 |
Correct |
8 ms |
2028 KB |
Output is correct |
41 |
Correct |
8 ms |
2028 KB |
Output is correct |
42 |
Correct |
7 ms |
1004 KB |
Output is correct |
43 |
Correct |
8 ms |
1516 KB |
Output is correct |
44 |
Correct |
8 ms |
1388 KB |
Output is correct |
45 |
Correct |
8 ms |
1388 KB |
Output is correct |
46 |
Correct |
9 ms |
1004 KB |
Output is correct |
47 |
Correct |
8 ms |
1004 KB |
Output is correct |
48 |
Correct |
8 ms |
748 KB |
Output is correct |
49 |
Correct |
9 ms |
1004 KB |
Output is correct |
50 |
Correct |
8 ms |
1004 KB |
Output is correct |
51 |
Correct |
8 ms |
1132 KB |
Output is correct |
52 |
Correct |
8 ms |
1004 KB |
Output is correct |
53 |
Correct |
9 ms |
1004 KB |
Output is correct |
54 |
Correct |
10 ms |
1260 KB |
Output is correct |
55 |
Correct |
20 ms |
2028 KB |
Output is correct |
56 |
Correct |
79 ms |
6636 KB |
Output is correct |
57 |
Correct |
152 ms |
10988 KB |
Output is correct |
58 |
Correct |
204 ms |
13932 KB |
Output is correct |
59 |
Correct |
283 ms |
18412 KB |
Output is correct |
60 |
Correct |
375 ms |
22636 KB |
Output is correct |
61 |
Correct |
430 ms |
25836 KB |
Output is correct |
62 |
Correct |
493 ms |
28412 KB |
Output is correct |
63 |
Correct |
588 ms |
33644 KB |
Output is correct |
64 |
Correct |
661 ms |
35564 KB |
Output is correct |
65 |
Correct |
475 ms |
99180 KB |
Output is correct |
66 |
Correct |
497 ms |
99124 KB |
Output is correct |
67 |
Correct |
462 ms |
38252 KB |
Output is correct |
68 |
Correct |
625 ms |
67436 KB |
Output is correct |
69 |
Correct |
659 ms |
63212 KB |
Output is correct |
70 |
Correct |
660 ms |
63212 KB |
Output is correct |
71 |
Correct |
854 ms |
39148 KB |
Output is correct |
72 |
Correct |
831 ms |
39148 KB |
Output is correct |
73 |
Correct |
863 ms |
38892 KB |
Output is correct |
74 |
Correct |
873 ms |
38892 KB |
Output is correct |
75 |
Correct |
872 ms |
38228 KB |
Output is correct |
76 |
Correct |
877 ms |
38508 KB |
Output is correct |
77 |
Correct |
940 ms |
37736 KB |
Output is correct |
78 |
Correct |
1035 ms |
38628 KB |
Output is correct |
79 |
Correct |
1186 ms |
52052 KB |
Output is correct |