1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
 
using namespace std;

 
#define ll long long
#define ve vector
#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
 
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;


void solve(){
    int n, k;
    cin >> n >> k;
    ve<ll> v(n);
    for(auto& i : v)
        cin >> i;
    ll res = 0;
    for(int i = 0; i < n; i++){
        int x = i;
        while(x >= 0 && ((x && v[x] <= v[x-1] - k) || (x != n - 1 && v[x] <= v[x+1] - k))){
            ll ad = 0;
            if(x)
                ad = max(ad, v[x-1]-k-v[x]);
            if(x != n - 1)
                ad = max(ad, v[x+1]-k-v[x]);
            res += ad;
            v[x] += ad;
            x--;
        }
    }
    cout << res << "\n";
}
 
signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int T = 1;
    // cin >> T;
    while(T--) solve();
}