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;
typedef long long ll;
ll s[202020];
multiset <ll> R;
multiset <ll, greater <ll> > L;
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n; ll h;
cin >> n >> h;
for(int i = 1; i <= n; i++) {
cin >> s[i];
}
ll l = s[1], r = s[1], ans = 0;
L.insert(s[1]), R.insert(s[1]);
for(int i = 2; i <= n; i++) {
l -= h, r += h;
if(s[i] < l) {
ans += l - s[i];
L.insert(s[i] + h * (i - 1));
L.insert(s[i] + h * (i - 1));
ll d = *L.begin() - *(next(L.begin()));
r = l; l = l - d;
R.insert((*R.begin())-r+l);
L.erase(*L.begin());
}
else if(s[i] > r) {
ans += s[i] - r;
R.insert(s[i] - h * (i - 1));
R.insert(s[i] - h * (i - 1));
ll d = *(next(R.begin())) - *(R.begin());
l = r; r = l + d;
L.insert((*L.begin())+r-l);
R.erase(*R.begin());
}
else {
l = r = s[i];
L.insert(s[i] + h * (i - 1));
R.insert(s[i] - h * (i - 1));
}
}
cout << ans;
}
# | 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... |