#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 SlopeTrick {
ll last_slope, last_y;
std::priority_queue<ll> changes;
SlopeTrick(): last_slope(), last_y(), changes() {
changes.push(0);
}
void absorb(SlopeTrick &&other) {
const auto x1 = changes.top();
const auto x2 = other.changes.top();
last_y += other.last_y + (x1 < x2 ? last_slope * (x2 - x1) : other.last_slope * (x1 - x2));
last_slope += other.last_slope;
if (changes.size() < other.changes.size()) {
std::swap(changes, other.changes);
}
while (!other.changes.empty()) {
changes.push(other.changes.top());
other.changes.pop();
}
}
bool pop_last() {
const auto x1 = changes.top();
changes.pop();
if (changes.empty()) return false;
const auto x2 = changes.top();
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;
trick.changes.push(c);
trick.changes.push(c);
data[u].absorb(std::move(trick));
}
else {
dfs(v);
while (data[v].last_slope > 1) {
data[v].pop_last();
}
const auto x1 = data[v].changes.top();
data[v].changes.pop();
const auto x2 = data[v].changes.top();
data[v].changes.pop();
data[v].changes.push(x2 + c);
data[v].changes.push(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 |
384 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 |
384 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 |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 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 |
384 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 |
384 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 |
384 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 |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 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 |
384 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 |
2 ms |
620 KB |
Output is correct |
33 |
Correct |
4 ms |
636 KB |
Output is correct |
34 |
Correct |
4 ms |
748 KB |
Output is correct |
35 |
Correct |
5 ms |
876 KB |
Output is correct |
36 |
Correct |
6 ms |
880 KB |
Output is correct |
37 |
Correct |
7 ms |
1004 KB |
Output is correct |
38 |
Correct |
7 ms |
1004 KB |
Output is correct |
39 |
Correct |
6 ms |
1004 KB |
Output is correct |
40 |
Correct |
7 ms |
2176 KB |
Output is correct |
41 |
Correct |
9 ms |
2156 KB |
Output is correct |
42 |
Correct |
10 ms |
1132 KB |
Output is correct |
43 |
Correct |
6 ms |
1388 KB |
Output is correct |
44 |
Correct |
6 ms |
1260 KB |
Output is correct |
45 |
Correct |
6 ms |
1260 KB |
Output is correct |
46 |
Correct |
6 ms |
748 KB |
Output is correct |
47 |
Correct |
6 ms |
748 KB |
Output is correct |
48 |
Correct |
5 ms |
748 KB |
Output is correct |
49 |
Correct |
6 ms |
748 KB |
Output is correct |
50 |
Correct |
5 ms |
620 KB |
Output is correct |
51 |
Correct |
6 ms |
620 KB |
Output is correct |
52 |
Correct |
5 ms |
748 KB |
Output is correct |
53 |
Correct |
6 ms |
748 KB |
Output is correct |
54 |
Correct |
6 ms |
748 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 |
384 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 |
384 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 |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 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 |
384 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 |
2 ms |
620 KB |
Output is correct |
33 |
Correct |
4 ms |
636 KB |
Output is correct |
34 |
Correct |
4 ms |
748 KB |
Output is correct |
35 |
Correct |
5 ms |
876 KB |
Output is correct |
36 |
Correct |
6 ms |
880 KB |
Output is correct |
37 |
Correct |
7 ms |
1004 KB |
Output is correct |
38 |
Correct |
7 ms |
1004 KB |
Output is correct |
39 |
Correct |
6 ms |
1004 KB |
Output is correct |
40 |
Correct |
7 ms |
2176 KB |
Output is correct |
41 |
Correct |
9 ms |
2156 KB |
Output is correct |
42 |
Correct |
10 ms |
1132 KB |
Output is correct |
43 |
Correct |
6 ms |
1388 KB |
Output is correct |
44 |
Correct |
6 ms |
1260 KB |
Output is correct |
45 |
Correct |
6 ms |
1260 KB |
Output is correct |
46 |
Correct |
6 ms |
748 KB |
Output is correct |
47 |
Correct |
6 ms |
748 KB |
Output is correct |
48 |
Correct |
5 ms |
748 KB |
Output is correct |
49 |
Correct |
6 ms |
748 KB |
Output is correct |
50 |
Correct |
5 ms |
620 KB |
Output is correct |
51 |
Correct |
6 ms |
620 KB |
Output is correct |
52 |
Correct |
5 ms |
748 KB |
Output is correct |
53 |
Correct |
6 ms |
748 KB |
Output is correct |
54 |
Correct |
6 ms |
748 KB |
Output is correct |
55 |
Correct |
17 ms |
2284 KB |
Output is correct |
56 |
Correct |
74 ms |
8164 KB |
Output is correct |
57 |
Correct |
135 ms |
12896 KB |
Output is correct |
58 |
Correct |
177 ms |
17024 KB |
Output is correct |
59 |
Correct |
241 ms |
22112 KB |
Output is correct |
60 |
Correct |
307 ms |
27496 KB |
Output is correct |
61 |
Correct |
357 ms |
33500 KB |
Output is correct |
62 |
Correct |
390 ms |
35916 KB |
Output is correct |
63 |
Correct |
477 ms |
42968 KB |
Output is correct |
64 |
Correct |
500 ms |
43092 KB |
Output is correct |
65 |
Correct |
391 ms |
110172 KB |
Output is correct |
66 |
Correct |
397 ms |
110300 KB |
Output is correct |
67 |
Correct |
376 ms |
47592 KB |
Output is correct |
68 |
Correct |
391 ms |
60892 KB |
Output is correct |
69 |
Correct |
424 ms |
55816 KB |
Output is correct |
70 |
Correct |
424 ms |
56076 KB |
Output is correct |
71 |
Correct |
418 ms |
32984 KB |
Output is correct |
72 |
Correct |
411 ms |
32340 KB |
Output is correct |
73 |
Correct |
375 ms |
26840 KB |
Output is correct |
74 |
Correct |
376 ms |
26776 KB |
Output is correct |
75 |
Correct |
377 ms |
25432 KB |
Output is correct |
76 |
Correct |
371 ms |
25300 KB |
Output is correct |
77 |
Correct |
391 ms |
24916 KB |
Output is correct |
78 |
Correct |
377 ms |
23380 KB |
Output is correct |
79 |
Correct |
368 ms |
18008 KB |
Output is correct |