#include <bits/stdc++.h>
template <typename T>
using minpq = std::priority_queue<T, std::vector<T>, std::greater<T>>;
constexpr int64_t inf = std::numeric_limits<int64_t>::max() / 2;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, H;
std::cin >> N >> H;
std::vector<int> S(N);
for (int i = 0; i < N; ++i) {
std::cin >> S[i];
}
std::priority_queue<int64_t> left;
minpq<int64_t> right;
left.push(-inf);
right.push(+inf);
int64_t cost = 0, offset = 0;
for (int i = 0; i < N; ++i) {
int64_t x = S[i];
int64_t l = left.top() - offset, r = right.top() + offset;
if (l <= x && x <= r) {
left.push(x + offset);
right.push(x - offset);
} else if (x < l) {
cost += l - x;
left.push(x + offset);
right.push(l - offset);
} else {
cost += x - r;
left.push(r + offset);
right.push(x - offset);
}
assert(left.top() - offset <= right.top() + offset);
offset += H;
}
std::cout << cost << '\n';
exit(0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
5412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |