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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h> 

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define pb push_back
#define F first 
#define S second
#define INF (ll)2e18
#define pii pair<int,int>
#define int ll

void solve() {

    int n,k; cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; i++){
        cin >> a[i];
    }
    int ans = 0;
    for (int i = 1; i < n; i++){
        if (a[i-1]-k > a[i]){
            ans += a[i-1]-k-a[i];
            a[i] = a[i-1]-k;
        }
    }
    for (int i = n-2; i >= 0; i--){
        if (a[i+1]-k > a[i]){
            ans += a[i+1]-k-a[i];
            a[i] = a[i+1]-k;
        }
    }
    cout << ans << '\n';


}


int32_t main() {

    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}