#include <climits>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
int main() {
std::cin.tie(NULL)->sync_with_stdio(false);
int n;
std::cin >> n;
// ans = sum abs(prefix(a - c, i)) where c[] = final configuration
// c[i] = b[i] + x[i], where x[i] = some fertilizers we remove from position i
// so ans = sum abs(prefix(a - b, i) - prefix(x, i)) and we want to minimize this
// DP[i][x] = minimize ans with x = prefix(x, i)
// DP[i][x] = min(DP[i-1][y] + abs(prefix(a - b, i) - x, 0 <= x - y <= a[i])
// - here x = prefix(x, i) and y = prefix(x, i-1), and x[i] = x - y
// - range x[i] is [0, a[i]] -> 0 <= x - y <= a[i]
// Transform DP: DP[i][x] = min(DP[i-1][y], max(0, x-a[i]) <= y <= x) + abs(prefix(a - b, i) - x)
// we can do slope trick here as inductively it is convex linear
long long total = 0;
long long m = 0, c = 0; // leftmost m & c
long long shift = 0;
std::priority_queue<long long> pq_neg;
std::priority_queue<long long, std::vector<long long>, std::greater<long long>> pq_pos;
for (int i = 0; i < n; i++) {
long long a, b;
std::cin >> a >> b;
total += a - b;
// take min: shift positive grad all by a
if (!pq_pos.empty()) {
shift += a;
}
// insert
if (total > 0) {
// combine with y = total - x
m--;
c += total;
pq_neg.push(total);
pq_pos.push(total - shift);
} else {
// combine with y = x + (-total) if x >= 0, else -x -total
m--;
c += -total;
pq_neg.push(0);
pq_pos.push(-shift);
}
// normalize
while (pq_neg.top() > pq_pos.top() + shift) {
long long x_neg = pq_neg.top();
long long x_pos = pq_pos.top() + shift;
pq_neg.pop();
pq_pos.pop();
pq_neg.push(x_pos);
pq_pos.push(x_neg - shift);
}
}
// reorder pq_neg
std::vector<long long> xs(pq_neg.size());
while (!pq_neg.empty()) {
long long x = pq_neg.top();
pq_neg.pop();
xs[pq_neg.size()] = x;
}
for (long long x: xs) {
if (x > total) {
std::cout << m * total + c << std::endl;
return 0;
}
m++;
c -= x;
}
while (!pq_pos.empty()) {
long long x = pq_pos.top() + shift;
pq_pos.pop();
if (x > total) {
std::cout << m * total + c << std::endl;
return 0;
}
m++;
c -= x;
}
std::cout << m * total + c << std::endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
14 ms |
1976 KB |
Output is correct |
5 |
Correct |
32 ms |
3520 KB |
Output is correct |
6 |
Correct |
95 ms |
9904 KB |
Output is correct |
7 |
Correct |
109 ms |
19060 KB |
Output is correct |
8 |
Correct |
141 ms |
17064 KB |
Output is correct |
9 |
Correct |
126 ms |
16496 KB |
Output is correct |
10 |
Correct |
114 ms |
14236 KB |
Output is correct |
11 |
Correct |
114 ms |
14248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
14 ms |
1976 KB |
Output is correct |
5 |
Correct |
32 ms |
3520 KB |
Output is correct |
6 |
Correct |
95 ms |
9904 KB |
Output is correct |
7 |
Correct |
109 ms |
19060 KB |
Output is correct |
8 |
Correct |
141 ms |
17064 KB |
Output is correct |
9 |
Correct |
126 ms |
16496 KB |
Output is correct |
10 |
Correct |
114 ms |
14236 KB |
Output is correct |
11 |
Correct |
114 ms |
14248 KB |
Output is correct |
12 |
Correct |
44 ms |
5056 KB |
Output is correct |
13 |
Correct |
115 ms |
11692 KB |
Output is correct |
14 |
Correct |
113 ms |
19108 KB |
Output is correct |
15 |
Correct |
149 ms |
17060 KB |
Output is correct |
16 |
Correct |
191 ms |
16528 KB |
Output is correct |
17 |
Correct |
104 ms |
14280 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
540 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
14 ms |
1976 KB |
Output is correct |
12 |
Correct |
32 ms |
3520 KB |
Output is correct |
13 |
Correct |
95 ms |
9904 KB |
Output is correct |
14 |
Correct |
109 ms |
19060 KB |
Output is correct |
15 |
Correct |
141 ms |
17064 KB |
Output is correct |
16 |
Correct |
126 ms |
16496 KB |
Output is correct |
17 |
Correct |
114 ms |
14236 KB |
Output is correct |
18 |
Correct |
114 ms |
14248 KB |
Output is correct |
19 |
Correct |
44 ms |
5056 KB |
Output is correct |
20 |
Correct |
115 ms |
11692 KB |
Output is correct |
21 |
Correct |
113 ms |
19108 KB |
Output is correct |
22 |
Correct |
149 ms |
17060 KB |
Output is correct |
23 |
Correct |
191 ms |
16528 KB |
Output is correct |
24 |
Correct |
104 ms |
14280 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
540 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
564 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
48 ms |
5196 KB |
Output is correct |
34 |
Correct |
135 ms |
11684 KB |
Output is correct |
35 |
Correct |
217 ms |
19124 KB |
Output is correct |
36 |
Correct |
206 ms |
16556 KB |
Output is correct |
37 |
Correct |
133 ms |
17060 KB |
Output is correct |
38 |
Correct |
201 ms |
19096 KB |
Output is correct |
39 |
Correct |
116 ms |
15256 KB |
Output is correct |
40 |
Correct |
138 ms |
13976 KB |
Output is correct |
41 |
Correct |
114 ms |
14352 KB |
Output is correct |
42 |
Correct |
118 ms |
14188 KB |
Output is correct |
43 |
Correct |
113 ms |
14240 KB |
Output is correct |
44 |
Correct |
119 ms |
14168 KB |
Output is correct |
45 |
Correct |
191 ms |
19108 KB |
Output is correct |
46 |
Correct |
96 ms |
14500 KB |
Output is correct |
47 |
Correct |
130 ms |
12440 KB |
Output is correct |