1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>

using namespace std;

#define int long long

signed main(){
    int n,k; cin>>n>>k;
    vector<int>h(n),a(n);
    for(int i=0; n>i; i++){
        cin>>h[i];
        a[i] = h[i];
    }
    for(int i=1; n>i; i++){
        h[i] = max(h[i], h[i-1] - k);
    }
    for(int i=n - 2; i >= 0; i--){
        h[i] = max(h[i], h[i+1] - k);
    }
    int res = 0;
    for(int i=0; n>i; i++) res += (h[i] - a[i]);
    cout<<res;
}