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
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define BOOST ios_base::sync_with_stdio(0), cin.tie(0)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
void test_case() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<int> b = a;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            b[j] = max(b[j], a[i] - abs(i - j) * k);
        }
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        ans += b[i] - a[i];
    }
    cout << ans << "\n";
}
int32_t main() {
    BOOST;
    int q = 1;
    // cin >> q;

    while (q--) {
        test_case();
    }
}