#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) static_cast<int>((x).size())
template<class T, size_t D>
struct vec : vector<vec<T, D - 1>> {
template<class... Args>
vec(size_t n = 0, Args... args)
: vector<vec<T, D - 1>>(n, vec<T, D - 1>(args...)) {}
};
template<class T>
struct vec<T, 1> : vector<T> {
template<class... Args>
vec(Args... args)
: vector<T>(args...) {}
};
template<class T>
inline bool Minimize(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T>
inline bool Maximize(T& a, const T& b) { return a < b ? a = b, true : false; }
inline int Next(int i, int n) { return i == n - 1 ? 0 : i + 1; }
inline int Prev(int i, int n) { return !i ? n - 1 : i - 1; }
mt19937 rng(static_cast<uint32_t>(chrono::steady_clock::now().time_since_epoch().count()));
struct Function {
int64_t a, b;
priority_queue<int64_t>* slope_changing_points;
Function()
: a(0),
b(0),
slope_changing_points(new priority_queue<int64_t>()) {}
Function& operator+=(Function& o) {
a += o.a;
b += o.b;
if (slope_changing_points->size() < o.slope_changing_points->size()) {
swap(slope_changing_points, o.slope_changing_points);
}
while (o.slope_changing_points->size()) {
slope_changing_points->emplace(o.slope_changing_points->top());
o.slope_changing_points->pop();
}
return *this;
}
};
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n_junctions, n_explosives; cin >> n_junctions >> n_explosives;
vec<int, 1> parents(n_junctions + n_explosives, -1), parent_edges(n_junctions + n_explosives);
for (int i = 1; i < n_junctions + n_explosives; ++i) {
cin >> parents[i] >> parent_edges[i]; --parents[i];
}
vec<Function, 1> functions(n_junctions + n_explosives);
for (int i = n_junctions; i < n_junctions + n_explosives; ++i) {
functions[i].a = 1;
functions[i].b = -parent_edges[i];
functions[i].slope_changing_points->emplace(parent_edges[i]);
functions[i].slope_changing_points->emplace(parent_edges[i]);
functions[parents[i]] += functions[i];
}
for (int i = n_junctions - 1; i; --i) {
while (functions[i].a > 1) {
--functions[i].a;
functions[i].b += functions[i].slope_changing_points->top();
functions[i].slope_changing_points->pop();
}
functions[i].b -= parent_edges[i];
auto tmp_1 = functions[i].slope_changing_points->top(); functions[i].slope_changing_points->pop();
auto tmp_2 = functions[i].slope_changing_points->top(); functions[i].slope_changing_points->pop();
functions[i].slope_changing_points->emplace(tmp_1 + parent_edges[i]);
functions[i].slope_changing_points->emplace(tmp_2 + parent_edges[i]);
functions[parents[i]] += functions[i];
}
while (functions[0].a) {
--functions[0].a;
functions[0].b += functions[0].slope_changing_points->top();
functions[0].slope_changing_points->pop();
}
cout << functions[0].b << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
0 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
436 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
2 ms |
640 KB |
Output is correct |
34 |
Correct |
2 ms |
768 KB |
Output is correct |
35 |
Correct |
3 ms |
768 KB |
Output is correct |
36 |
Correct |
2 ms |
896 KB |
Output is correct |
37 |
Correct |
3 ms |
896 KB |
Output is correct |
38 |
Correct |
3 ms |
1024 KB |
Output is correct |
39 |
Correct |
3 ms |
1024 KB |
Output is correct |
40 |
Correct |
2 ms |
768 KB |
Output is correct |
41 |
Correct |
2 ms |
768 KB |
Output is correct |
42 |
Correct |
2 ms |
768 KB |
Output is correct |
43 |
Correct |
3 ms |
896 KB |
Output is correct |
44 |
Correct |
3 ms |
896 KB |
Output is correct |
45 |
Correct |
3 ms |
896 KB |
Output is correct |
46 |
Correct |
3 ms |
1152 KB |
Output is correct |
47 |
Correct |
3 ms |
1152 KB |
Output is correct |
48 |
Correct |
3 ms |
1024 KB |
Output is correct |
49 |
Correct |
4 ms |
1152 KB |
Output is correct |
50 |
Correct |
3 ms |
1024 KB |
Output is correct |
51 |
Correct |
3 ms |
896 KB |
Output is correct |
52 |
Correct |
3 ms |
1024 KB |
Output is correct |
53 |
Correct |
4 ms |
1024 KB |
Output is correct |
54 |
Correct |
3 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
0 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
436 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
2 ms |
640 KB |
Output is correct |
34 |
Correct |
2 ms |
768 KB |
Output is correct |
35 |
Correct |
3 ms |
768 KB |
Output is correct |
36 |
Correct |
2 ms |
896 KB |
Output is correct |
37 |
Correct |
3 ms |
896 KB |
Output is correct |
38 |
Correct |
3 ms |
1024 KB |
Output is correct |
39 |
Correct |
3 ms |
1024 KB |
Output is correct |
40 |
Correct |
2 ms |
768 KB |
Output is correct |
41 |
Correct |
2 ms |
768 KB |
Output is correct |
42 |
Correct |
2 ms |
768 KB |
Output is correct |
43 |
Correct |
3 ms |
896 KB |
Output is correct |
44 |
Correct |
3 ms |
896 KB |
Output is correct |
45 |
Correct |
3 ms |
896 KB |
Output is correct |
46 |
Correct |
3 ms |
1152 KB |
Output is correct |
47 |
Correct |
3 ms |
1152 KB |
Output is correct |
48 |
Correct |
3 ms |
1024 KB |
Output is correct |
49 |
Correct |
4 ms |
1152 KB |
Output is correct |
50 |
Correct |
3 ms |
1024 KB |
Output is correct |
51 |
Correct |
3 ms |
896 KB |
Output is correct |
52 |
Correct |
3 ms |
1024 KB |
Output is correct |
53 |
Correct |
4 ms |
1024 KB |
Output is correct |
54 |
Correct |
3 ms |
1024 KB |
Output is correct |
55 |
Correct |
7 ms |
2048 KB |
Output is correct |
56 |
Correct |
32 ms |
6972 KB |
Output is correct |
57 |
Correct |
50 ms |
11384 KB |
Output is correct |
58 |
Correct |
69 ms |
14392 KB |
Output is correct |
59 |
Correct |
100 ms |
19056 KB |
Output is correct |
60 |
Correct |
130 ms |
23540 KB |
Output is correct |
61 |
Correct |
141 ms |
27248 KB |
Output is correct |
62 |
Correct |
147 ms |
29804 KB |
Output is correct |
63 |
Correct |
180 ms |
35428 KB |
Output is correct |
64 |
Correct |
184 ms |
37104 KB |
Output is correct |
65 |
Correct |
106 ms |
28664 KB |
Output is correct |
66 |
Correct |
102 ms |
28792 KB |
Output is correct |
67 |
Correct |
104 ms |
28792 KB |
Output is correct |
68 |
Correct |
165 ms |
35440 KB |
Output is correct |
69 |
Correct |
174 ms |
36080 KB |
Output is correct |
70 |
Correct |
168 ms |
36080 KB |
Output is correct |
71 |
Correct |
212 ms |
51036 KB |
Output is correct |
72 |
Correct |
208 ms |
51176 KB |
Output is correct |
73 |
Correct |
179 ms |
47500 KB |
Output is correct |
74 |
Correct |
185 ms |
48008 KB |
Output is correct |
75 |
Correct |
180 ms |
46184 KB |
Output is correct |
76 |
Correct |
189 ms |
46380 KB |
Output is correct |
77 |
Correct |
196 ms |
45548 KB |
Output is correct |
78 |
Correct |
195 ms |
44904 KB |
Output is correct |
79 |
Correct |
179 ms |
43744 KB |
Output is correct |