This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
auto add_left = [&](int64_t x) {
right.push(x - offset);
int64_t y = right.top() + offset;
right.pop();
cost += x - y;
left.push(y + offset);
};
auto add_right = [&](int64_t x) {
left.push(x + offset);
int64_t y = left.top() - offset;
left.pop();
cost += y - x;
right.push(y - offset);
};
for (int i = 0; i < N; ++i) {
int64_t x = S[i];
add_left(x);
add_right(x);
offset += H;
}
std::cout << cost << '\n';
exit(0);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |