#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, lazy;
Heap left, right;
HeapNode(const ll value): value(value), lazy(0), left(), right() { }
};
void flush(Heap &heap) {
heap -> value += heap -> lazy;
if (heap -> left) heap -> left -> lazy += heap -> lazy;
if (heap -> right) heap -> right -> lazy += heap -> lazy;
heap -> lazy = 0;
}
Heap merge(Heap left, Heap right) {
if (!left) return right;
if (!right) return left;
flush(left);
flush(right);
if (left -> value < right -> value) {
std::swap(left, right);
}
left -> right = merge(std::move(left -> right), std::move(right));
std::swap(left -> left, left -> right);
return left;
}
void push(Heap &heap, const ll value) {
heap = merge(std::move(heap), std::make_unique<HeapNode>(value));
}
ll top(Heap &heap) {
flush(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;
}
void add(Heap &heap, const ll value) {
heap -> lazy += value;
}
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 |
384 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 |
384 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 |
2 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 |
6 ms |
876 KB |
Output is correct |
35 |
Correct |
6 ms |
876 KB |
Output is correct |
36 |
Correct |
7 ms |
876 KB |
Output is correct |
37 |
Correct |
8 ms |
1004 KB |
Output is correct |
38 |
Correct |
8 ms |
1004 KB |
Output is correct |
39 |
Correct |
8 ms |
1004 KB |
Output is correct |
40 |
Correct |
9 ms |
2028 KB |
Output is correct |
41 |
Correct |
9 ms |
2028 KB |
Output is correct |
42 |
Correct |
7 ms |
1004 KB |
Output is correct |
43 |
Correct |
9 ms |
1516 KB |
Output is correct |
44 |
Correct |
9 ms |
1388 KB |
Output is correct |
45 |
Correct |
9 ms |
1388 KB |
Output is correct |
46 |
Correct |
10 ms |
1004 KB |
Output is correct |
47 |
Correct |
10 ms |
1004 KB |
Output is correct |
48 |
Correct |
10 ms |
1004 KB |
Output is correct |
49 |
Correct |
11 ms |
1004 KB |
Output is correct |
50 |
Correct |
10 ms |
1004 KB |
Output is correct |
51 |
Correct |
10 ms |
1004 KB |
Output is correct |
52 |
Correct |
10 ms |
1004 KB |
Output is correct |
53 |
Correct |
11 ms |
1076 KB |
Output is correct |
54 |
Correct |
12 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 |
384 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 |
2 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 |
6 ms |
876 KB |
Output is correct |
35 |
Correct |
6 ms |
876 KB |
Output is correct |
36 |
Correct |
7 ms |
876 KB |
Output is correct |
37 |
Correct |
8 ms |
1004 KB |
Output is correct |
38 |
Correct |
8 ms |
1004 KB |
Output is correct |
39 |
Correct |
8 ms |
1004 KB |
Output is correct |
40 |
Correct |
9 ms |
2028 KB |
Output is correct |
41 |
Correct |
9 ms |
2028 KB |
Output is correct |
42 |
Correct |
7 ms |
1004 KB |
Output is correct |
43 |
Correct |
9 ms |
1516 KB |
Output is correct |
44 |
Correct |
9 ms |
1388 KB |
Output is correct |
45 |
Correct |
9 ms |
1388 KB |
Output is correct |
46 |
Correct |
10 ms |
1004 KB |
Output is correct |
47 |
Correct |
10 ms |
1004 KB |
Output is correct |
48 |
Correct |
10 ms |
1004 KB |
Output is correct |
49 |
Correct |
11 ms |
1004 KB |
Output is correct |
50 |
Correct |
10 ms |
1004 KB |
Output is correct |
51 |
Correct |
10 ms |
1004 KB |
Output is correct |
52 |
Correct |
10 ms |
1004 KB |
Output is correct |
53 |
Correct |
11 ms |
1076 KB |
Output is correct |
54 |
Correct |
12 ms |
1260 KB |
Output is correct |
55 |
Correct |
28 ms |
2156 KB |
Output is correct |
56 |
Correct |
99 ms |
7276 KB |
Output is correct |
57 |
Correct |
165 ms |
12140 KB |
Output is correct |
58 |
Correct |
232 ms |
15352 KB |
Output is correct |
59 |
Correct |
316 ms |
20460 KB |
Output is correct |
60 |
Correct |
408 ms |
25452 KB |
Output is correct |
61 |
Correct |
473 ms |
28908 KB |
Output is correct |
62 |
Correct |
530 ms |
31844 KB |
Output is correct |
63 |
Correct |
643 ms |
37740 KB |
Output is correct |
64 |
Correct |
713 ms |
40172 KB |
Output is correct |
65 |
Correct |
613 ms |
104044 KB |
Output is correct |
66 |
Correct |
601 ms |
103916 KB |
Output is correct |
67 |
Correct |
520 ms |
42988 KB |
Output is correct |
68 |
Correct |
714 ms |
72044 KB |
Output is correct |
69 |
Correct |
750 ms |
67820 KB |
Output is correct |
70 |
Correct |
751 ms |
67804 KB |
Output is correct |
71 |
Correct |
971 ms |
43372 KB |
Output is correct |
72 |
Correct |
989 ms |
43196 KB |
Output is correct |
73 |
Correct |
1036 ms |
42744 KB |
Output is correct |
74 |
Correct |
1041 ms |
42960 KB |
Output is correct |
75 |
Correct |
1071 ms |
42348 KB |
Output is correct |
76 |
Correct |
1112 ms |
42308 KB |
Output is correct |
77 |
Correct |
1172 ms |
41832 KB |
Output is correct |
78 |
Correct |
1268 ms |
42624 KB |
Output is correct |
79 |
Correct |
1596 ms |
55508 KB |
Output is correct |