#include <bits/stdc++.h>
using namespace std;
using lint = long long;
struct Node {
lint a, b; // y = ax + b, for large x where x >= slope.top()
priority_queue<lint> *slope; // point-slope container
Node() : a(0), b(0) {
slope = new priority_queue<lint>();
}
Node& operator+=(Node o) {
a += o.a;
b += o.b;
if (slope->size() < o.slope->size()) {
swap(slope, o.slope);
}
while (!o.slope->empty()) {
slope->emplace(o.slope->top());
o.slope->pop();
}
return *this;
}
lint balance(int n = 1) {
while (a > n) {
a--; // ax + b = a(x - 1) + (b + x)
b += slope->top();
slope->pop();
}
return b;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> p(n + m, -1), c(n + m, 0);
for (int i = 1; i < n + m; i++) {
cin >> p[i] >> c[i];
p[i]--;
}
vector<Node> tree(n + m);
for (int i = n + m - 1; i >= n; i--) {
tree[i].a = 1;
tree[i].b = -c[i];
tree[i].slope->emplace(c[i]); // between slope -1 and 0
tree[i].slope->emplace(c[i]); // between slope 0 and 1
tree[p[i]] += tree[i];
}
for (int i = n - 1; i > 0; i--) {
tree[i].balance();
lint top1 = tree[i].slope->top();
tree[i].slope->pop();
lint top2 = tree[i].slope->top();
tree[i].slope->pop();
tree[i].slope->emplace(top1 + c[i]); // shift slope 1 by c[i]
tree[i].slope->emplace(top2 + c[i]); // shift slope 0 by c[i]
tree[i].b -= c[i]; // shift line to right by c[i]
tree[p[i]] += tree[i];
}
cout << tree[0].balance(0) << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
372 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
372 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
504 KB |
Output is correct |
32 |
Correct |
3 ms |
504 KB |
Output is correct |
33 |
Correct |
3 ms |
648 KB |
Output is correct |
34 |
Correct |
3 ms |
712 KB |
Output is correct |
35 |
Correct |
4 ms |
732 KB |
Output is correct |
36 |
Correct |
5 ms |
888 KB |
Output is correct |
37 |
Correct |
4 ms |
888 KB |
Output is correct |
38 |
Correct |
5 ms |
1020 KB |
Output is correct |
39 |
Correct |
8 ms |
1016 KB |
Output is correct |
40 |
Correct |
4 ms |
760 KB |
Output is correct |
41 |
Correct |
4 ms |
760 KB |
Output is correct |
42 |
Correct |
4 ms |
760 KB |
Output is correct |
43 |
Correct |
5 ms |
888 KB |
Output is correct |
44 |
Correct |
5 ms |
888 KB |
Output is correct |
45 |
Correct |
6 ms |
888 KB |
Output is correct |
46 |
Correct |
5 ms |
1144 KB |
Output is correct |
47 |
Correct |
5 ms |
1144 KB |
Output is correct |
48 |
Correct |
5 ms |
1016 KB |
Output is correct |
49 |
Correct |
5 ms |
1160 KB |
Output is correct |
50 |
Correct |
4 ms |
1016 KB |
Output is correct |
51 |
Correct |
4 ms |
888 KB |
Output is correct |
52 |
Correct |
5 ms |
1016 KB |
Output is correct |
53 |
Correct |
5 ms |
1016 KB |
Output is correct |
54 |
Correct |
5 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
372 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
504 KB |
Output is correct |
32 |
Correct |
3 ms |
504 KB |
Output is correct |
33 |
Correct |
3 ms |
648 KB |
Output is correct |
34 |
Correct |
3 ms |
712 KB |
Output is correct |
35 |
Correct |
4 ms |
732 KB |
Output is correct |
36 |
Correct |
5 ms |
888 KB |
Output is correct |
37 |
Correct |
4 ms |
888 KB |
Output is correct |
38 |
Correct |
5 ms |
1020 KB |
Output is correct |
39 |
Correct |
8 ms |
1016 KB |
Output is correct |
40 |
Correct |
4 ms |
760 KB |
Output is correct |
41 |
Correct |
4 ms |
760 KB |
Output is correct |
42 |
Correct |
4 ms |
760 KB |
Output is correct |
43 |
Correct |
5 ms |
888 KB |
Output is correct |
44 |
Correct |
5 ms |
888 KB |
Output is correct |
45 |
Correct |
6 ms |
888 KB |
Output is correct |
46 |
Correct |
5 ms |
1144 KB |
Output is correct |
47 |
Correct |
5 ms |
1144 KB |
Output is correct |
48 |
Correct |
5 ms |
1016 KB |
Output is correct |
49 |
Correct |
5 ms |
1160 KB |
Output is correct |
50 |
Correct |
4 ms |
1016 KB |
Output is correct |
51 |
Correct |
4 ms |
888 KB |
Output is correct |
52 |
Correct |
5 ms |
1016 KB |
Output is correct |
53 |
Correct |
5 ms |
1016 KB |
Output is correct |
54 |
Correct |
5 ms |
1016 KB |
Output is correct |
55 |
Correct |
9 ms |
2040 KB |
Output is correct |
56 |
Correct |
32 ms |
7032 KB |
Output is correct |
57 |
Correct |
55 ms |
11356 KB |
Output is correct |
58 |
Correct |
70 ms |
14412 KB |
Output is correct |
59 |
Correct |
102 ms |
19108 KB |
Output is correct |
60 |
Correct |
124 ms |
23624 KB |
Output is correct |
61 |
Correct |
142 ms |
27092 KB |
Output is correct |
62 |
Correct |
159 ms |
29716 KB |
Output is correct |
63 |
Correct |
208 ms |
35376 KB |
Output is correct |
64 |
Correct |
208 ms |
37104 KB |
Output is correct |
65 |
Correct |
113 ms |
28760 KB |
Output is correct |
66 |
Correct |
114 ms |
28776 KB |
Output is correct |
67 |
Correct |
114 ms |
28792 KB |
Output is correct |
68 |
Correct |
178 ms |
35568 KB |
Output is correct |
69 |
Correct |
193 ms |
36208 KB |
Output is correct |
70 |
Correct |
192 ms |
36140 KB |
Output is correct |
71 |
Correct |
233 ms |
50948 KB |
Output is correct |
72 |
Correct |
232 ms |
51124 KB |
Output is correct |
73 |
Correct |
209 ms |
47500 KB |
Output is correct |
74 |
Correct |
212 ms |
48136 KB |
Output is correct |
75 |
Correct |
218 ms |
46392 KB |
Output is correct |
76 |
Correct |
207 ms |
46312 KB |
Output is correct |
77 |
Correct |
217 ms |
45412 KB |
Output is correct |
78 |
Correct |
221 ms |
45004 KB |
Output is correct |
79 |
Correct |
204 ms |
43832 KB |
Output is correct |