#include <bits/stdc++.h>
using namespace std;
const int SZ = 200005;
const long long INF = 1000000000000005LL;
long long ori[SZ];
pair<long long, int> arr1[SZ];
pair<long long, int> arr2[SZ];
long long n, d;
long long myf(long long i, long long j) {
  return i * d + ori[i] - j * d + ori[j];
}
int main(void) {
  scanf("%lld %lld", &n, &d);
  for (int i = 1; i <= n; i++) {
    long long x;
    scanf("%d", &x);
    ori[i] = x;
    arr1[i] = make_pair(x + (long long)i * d, i);
    arr2[i] = make_pair(x - (long long)i * d, i);
  }
  sort(arr1, arr1 + n);
  sort(arr2, arr2 + n);
  long long ans = 0LL;
  int m[4] = {arr1[0].second, arr1[1].second, arr2[0].second, arr2[1].second};
  for (int i = 2; i <= n; i++) {
    long long mn = INF;
    if (i < m[0]) {
      long long tmp = myf(m[0], i);
      if (mn > tmp) mn = tmp;
    } else if (i < m[1]) {
      long long tmp = myf(m[1], i);
      if (mn > tmp) mn = tmp;
    } else if (i > m[2]) {
      long long tmp = myf(i, m[2]);
      if (mn > tmp) mn = tmp;
    } else if (i > m[3]) {
      long long tmp = myf(i, m[3]);
      if (mn > tmp) mn = tmp;
    }
    if (mn != INF) ans += mn;
  }
  printf("%lld", ans);
  return 0;
}
