#include <bits/stdc++.h>
typedef long long ll;
struct V {
ll w = 0;
std::vector<int> edges;
ll y0 = 0;
ll d0 = 0;
std::priority_queue<ll> points;
};
void solve(ll i, std::vector<V>& graph) {
V& v = graph[i];
if (v.edges.empty()) return;
for (auto j : v.edges) {
solve(j, graph);
V& w = graph[j];
v.y0 += w.y0;
v.d0 += w.d0;
if (v.points.size() < w.points.size()) {
std::swap(v.points, w.points);
}
while (!w.points.empty()) {
v.points.push(w.points.top());
w.points.pop();
}
}
if (i == 1) return;
v.y0 += v.w;
ll pv1 = v.points.top(); v.points.pop();
ll pv2 = v.points.top(); v.points.pop();
while (v.d0 + (ll)v.points.size() > -1) {
pv1 = pv2;
pv2 = v.points.top();
v.points.pop();
}
v.points.push(pv2 + v.w);
v.points.push(pv1 + v.w);
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
ll n, m;
std::cin >> n >> m;
std::vector<V> graph(n + m + 1);
for (ll i = 2; i <= n + m; i++) {
ll a, b;
std::cin >> a >> b;
graph[a].edges.push_back(i);
graph[i].w = b;
}
for (ll i = n + 1; i <= n + m; i++) {
graph[i].y0 = graph[i].w;
graph[i].d0 = -1;
graph[i].points.push(graph[i].w);
graph[i].points.push(graph[i].w);
}
solve(1, graph);
std::vector<ll> points;
while (!graph[1].points.empty()) {
points.push_back(graph[1].points.top());
graph[1].points.pop();
}
std::reverse(points.begin(), points.end());
ll x = 0;
ll y = graph[1].y0;
ll d = graph[1].d0;
ll min = y;
for (auto p : points) {
y += (ll) d * (p - x);
d++;
x = p;
min = std::min(min, y);
}
std::cout << min << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
2 ms |
724 KB |
Output is correct |
35 |
Correct |
2 ms |
852 KB |
Output is correct |
36 |
Correct |
2 ms |
852 KB |
Output is correct |
37 |
Correct |
3 ms |
980 KB |
Output is correct |
38 |
Correct |
3 ms |
980 KB |
Output is correct |
39 |
Correct |
3 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
1492 KB |
Output is correct |
41 |
Correct |
2 ms |
1464 KB |
Output is correct |
42 |
Correct |
2 ms |
948 KB |
Output is correct |
43 |
Correct |
3 ms |
1232 KB |
Output is correct |
44 |
Correct |
3 ms |
1236 KB |
Output is correct |
45 |
Correct |
3 ms |
1236 KB |
Output is correct |
46 |
Correct |
3 ms |
1236 KB |
Output is correct |
47 |
Correct |
3 ms |
1236 KB |
Output is correct |
48 |
Correct |
3 ms |
1108 KB |
Output is correct |
49 |
Correct |
3 ms |
1120 KB |
Output is correct |
50 |
Correct |
3 ms |
1108 KB |
Output is correct |
51 |
Correct |
4 ms |
976 KB |
Output is correct |
52 |
Correct |
3 ms |
980 KB |
Output is correct |
53 |
Correct |
3 ms |
1108 KB |
Output is correct |
54 |
Correct |
3 ms |
1196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
2 ms |
724 KB |
Output is correct |
35 |
Correct |
2 ms |
852 KB |
Output is correct |
36 |
Correct |
2 ms |
852 KB |
Output is correct |
37 |
Correct |
3 ms |
980 KB |
Output is correct |
38 |
Correct |
3 ms |
980 KB |
Output is correct |
39 |
Correct |
3 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
1492 KB |
Output is correct |
41 |
Correct |
2 ms |
1464 KB |
Output is correct |
42 |
Correct |
2 ms |
948 KB |
Output is correct |
43 |
Correct |
3 ms |
1232 KB |
Output is correct |
44 |
Correct |
3 ms |
1236 KB |
Output is correct |
45 |
Correct |
3 ms |
1236 KB |
Output is correct |
46 |
Correct |
3 ms |
1236 KB |
Output is correct |
47 |
Correct |
3 ms |
1236 KB |
Output is correct |
48 |
Correct |
3 ms |
1108 KB |
Output is correct |
49 |
Correct |
3 ms |
1120 KB |
Output is correct |
50 |
Correct |
3 ms |
1108 KB |
Output is correct |
51 |
Correct |
4 ms |
976 KB |
Output is correct |
52 |
Correct |
3 ms |
980 KB |
Output is correct |
53 |
Correct |
3 ms |
1108 KB |
Output is correct |
54 |
Correct |
3 ms |
1196 KB |
Output is correct |
55 |
Correct |
7 ms |
2260 KB |
Output is correct |
56 |
Correct |
26 ms |
7604 KB |
Output is correct |
57 |
Correct |
50 ms |
12740 KB |
Output is correct |
58 |
Correct |
69 ms |
16324 KB |
Output is correct |
59 |
Correct |
102 ms |
21500 KB |
Output is correct |
60 |
Correct |
125 ms |
26416 KB |
Output is correct |
61 |
Correct |
157 ms |
30716 KB |
Output is correct |
62 |
Correct |
201 ms |
33884 KB |
Output is correct |
63 |
Correct |
209 ms |
40120 KB |
Output is correct |
64 |
Correct |
236 ms |
41980 KB |
Output is correct |
65 |
Correct |
110 ms |
75596 KB |
Output is correct |
66 |
Correct |
107 ms |
75576 KB |
Output is correct |
67 |
Correct |
99 ms |
38216 KB |
Output is correct |
68 |
Correct |
170 ms |
60964 KB |
Output is correct |
69 |
Correct |
180 ms |
57996 KB |
Output is correct |
70 |
Correct |
194 ms |
57888 KB |
Output is correct |
71 |
Correct |
230 ms |
56876 KB |
Output is correct |
72 |
Correct |
192 ms |
56712 KB |
Output is correct |
73 |
Correct |
174 ms |
53292 KB |
Output is correct |
74 |
Correct |
224 ms |
53444 KB |
Output is correct |
75 |
Correct |
175 ms |
52044 KB |
Output is correct |
76 |
Correct |
185 ms |
52476 KB |
Output is correct |
77 |
Correct |
187 ms |
51340 KB |
Output is correct |
78 |
Correct |
202 ms |
49996 KB |
Output is correct |
79 |
Correct |
173 ms |
54756 KB |
Output is correct |