1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <bits/stdc++.h>
using namespace std;

int main () {
  ios_base::sync_with_stdio(0); cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto& x: a) cin >> x;
  int64_t z = accumulate(a.begin(), a.end(), 0LL);
  for (int i = 0; i+1 < n; i++) a[i+1] = max(a[i+1], a[i]-k);
  for (int i = n-2; i >= 0; i--) a[i] = max(a[i], a[i+1]-k);
  cout << accumulate(a.begin(), a.end(), 0LL) - z << '\n';
}