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;
const int N = 200'200;
int n;
ll h;
ll s[N];
void solve() {
ll base = 0;
cin >> n >> h;
for (int i = 1; i <= n; i++)
cin >> s[i];
priority_queue<int> L;
priority_queue<int, vector<int>, greater<int>> R;
ll dl = 0, dr = 0;
L.push(s[1]);
R.push(s[1]);
for (int i = 2; i <= n; i++) {
dl -= h;
dr += h;
ll lb = L.top() + dl, rb = R.top() + dr;
if (lb <= s[i] && s[i] <= rb) {
L.push(s[i] - dl);
R.push(s[i] - dr);
} else if (s[i] < lb) {
L.push(s[i] - dl);
L.push(s[i] - dl);
L.pop();
R.push(lb - dr);
base += lb - s[i];
} else {
R.push(s[i] - dr);
R.push(s[i] - dr);
R.pop();
L.push(rb - dl);
base += s[i] - rb;
}
}
cout << base;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
solve();
}
# | 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... |