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
#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, k;
    cin >> n >> k;
    vector<int> a(n), h(n);

    for (int i = 0; i < n; i++){
        cin >> a[i];
        h[i] = a[i];
    }

    for(int i=0; i<n; i++){
        for (int i = 1; i < n; 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 odp = 0;
    for (int i = 0; i < n; i++)
        odp += h[i] - a[i];
    cout << odp;
}