1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long long k, n, res=0;
    cin >> n >> k;
    vector<long long> a(n), orig(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        orig[i] = a[i];
    }
    for (int i = 1; i < n; i++)
        a[i] = max(a[i], a[i-1] - k);
    for (int i = n-2; i >= 0; i--)
        a[i] = max(a[i], a[i+1] - k);
    for (int i = 0; i < n; i++)
        res += a[i] - orig[i];
    cout << res;
}