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
63
64
65
#include "bits/stdc++.h""
using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define FOR(x,b,e) for(x=b;x<=e;x++)
#define FORD(x,b,e) for(x=b;x>=e;x--)
#define REP(x,n) for(x = 0; x<(n);x++)

#define ALL(c) (c).begin(), (c).end()
#define VAR(v,n) __typeof(n) v = (n)
#define SIZE(x) (int)x.size()
#define FOREACH(x,c) for(VAR(x, (c).begin()); x != (c).end(); x++)
#define PB push_back
#define ST first
#define ND second

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n,k;
    cin>>n>>k;

    int tab[n+1], h[n+1], i,a,imin, r, res = 0;
    tab[n] = 0;
    h[n] = 0;
    REP(i,n){
        cin>>a;
        tab[i] = a;
        h[i] = 0;
    }

    REP(i,n){
 //       tab[i]<tab[i+1]?imin=i:imin=i+1;
 //       r = abs(tab[i]-tab[i+1]);
        h[i] = max(tab[i], h[i-1]-k);
        h[i] = max(h[i], h[i+1]-k);
       // cout<<tab[i]<<" "<<tab[i+1]<<endl;
    }
           FORD(i,n-1,0){
 //       tab[i]<tab[i+1]?imin=i:imin=i+1;
 //       r = abs(tab[i]-tab[i+1]);
        h[i] = max(tab[i], h[i-1]-k);
        h[i] = max(h[i], h[i+1]-k);
       // cout<<tab[i]<<" "<<tab[i+1]<<endl;
    }



    REP(i,n){
    //    cout<<h[i]<<" "<<tab[i]<<" "<<h[i]-tab[i]<<endl;
        res+=h[i] - tab[i];
    }

    cout<<res<<endl;






return 0;
}