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
#include <bits/stdc++.h>
#define pii pair<int, int>
#define For(i, l, r) for (int i=l;(l<=r?i<=r:i>=r);(l<=r?i++:i--))
#define DEBUG
#ifdef DEBUG
auto operator<<(auto &o,auto p)->decltype(p.first,o){return o<<'('<<p.first<<", "<<p.second<<')';}
auto operator<<(auto &o,auto x)->decltype(x.end(),o){o<<'{';int i=2;for(auto &e:x)o<<(", ")+i<<e,i=0;return o<<'}';}
#define LOG(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X);
#else
#define LOG(x...)(void)0
#endif
//#define int long long
using namespace std;

const int inf=1e9+1, M = 1LL << 40;

void solve(){
    int n, k;
    cin >> n >> k;
    vector<int> a(n), b(n);
    vector<pii> c(n);
    for (int i = 0; i < n; ++i){
        cin >> a[i];
        b[i] = a[i];
        c[i] = {a[i], i};
    }
    sort(c.begin(), c.end(), [](pii a, pii b){return a.first > b.first;});
    for (auto [_, i]: c){
        for (int j = i - 1; j >= 0; --j){
            b[j] = max(b[j], b[j + 1] - k);
        }
        for (int j = i + 1; j < n; ++j){
            b[j] = max(b[j], b[j - 1] - k);
        }
    }
    int res = 0;
    for (int i = 0; i < n; ++i)
        res += (b[i] - a[i]);
    cout << res << '\n';
}

signed main(){
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    //cin >> t;
    while(t--)
        solve();
}