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
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i < b; i++)
#define per(i, a, b) for(int i = a; i >= b; i--)
#define all(v) begin(v), end(v)
#define st first
#define nd second
using namespace std;
using ll = long long;
using bigi = __int128;
using pii = pair<ll, ll>;
const int N = 1005;
bool byl[N];
int tab[N];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    priority_queue<pair<int, int>> pq;
    rep(i, 1, n + 1){
        cin >> tab[i];
        pq.push({tab[i], i});
    }
    tab[0] = 1e9;
    tab[n + 1] = 1e9;
    int odp = 0;
    while(!pq.empty()){
        if(byl[pq.top().nd]) continue;
        int i = pq.top().nd;
        pq.pop();
        if(tab[i] - tab[i - 1] > k){
            odp += tab[i] - tab[i - 1] - k;
            tab[i - 1] = tab[i] - k;
            pq.push({tab[i - 1], i - 1});
        }
        if(tab[i] - tab[i + 1] > k){
            odp += tab[i] - tab[i + 1] - k;
            tab[i + 1] = tab[i] - k;
            pq.push({tab[i + 1], i + 1});
        }
    }
    cout << odp << '\n';
}