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;
 
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
#define lb(x,y) lower_bound(all(x),y)-begin(x)
mt19937 rng;
void solve() {
    int N, H; cin >> N >> H;
    vector<int> arr(N);
    for (int i = 0; i < N; i++) cin >> arr[i];
    priority_queue<ll> left, right;
    left.push(arr[0]);
    right.push(-arr[0]);
    ll lOff = 0, rOff = 0;
    ll a = 1, b = -arr[0];
    for (int i = 1; i < N; i++) {
        lOff -= H;
        rOff += H;
        b -= a * H;
        a++;
        b += -arr[i];
        if (arr[i] <= left.top() + lOff) {
            left.push(arr[i] - lOff);
            left.push(arr[i] - lOff);
            right.push(-(left.top() + lOff - rOff));
            left.pop();
        } else if (arr[i] <= -right.top() + rOff) {
            left.push(arr[i] - lOff);
            right.push(-(arr[i] - rOff));
        } else {
            right.push(-(arr[i] - rOff));
            right.push(-(arr[i] - rOff));
            left.push(-right.top() + rOff - lOff);
            right.pop();
        }
    }
    vector<ll> srt;
    while (!right.empty()) {
        srt.pb(-right.top() + rOff);
        right.pop();
    }
    reverse(all(srt));
    for (ll x : srt) {
        b += x;
    }
    cout << b << "\n";
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    rng = mt19937(chrono::steady_clock::now().time_since_epoch().count());
    
    solve();
    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... |