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>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
ll h;
cin >> n >> h;
priority_queue<ll> left;
priority_queue<ll, vector<ll>, greater<ll>> right;
ll ans = 0;
int s;
cin >> s;
left.push(s);
right.push(s);
for (int i = 1; i < n; i++) {
cin >> s;
ll shift = i * h;
ll left_border = left.top() - shift;
ll right_border = right.top() + shift;
if (s < left_border) {
left.push(s + shift);
left.push(s + shift);
left.pop();
right.push(left_border - shift);
ans += abs(s - left_border);
} else if (s > right_border) {
right.push(s - shift);
right.push(s - shift);
right.pop();
left.push(right_border + shift);
ans += abs(s - right_border);
} else {
left.push(s + shift);
right.push(s - shift);
}
}
cout << ans;
return 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... |