// includes {{{
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
// #include<deque>
// #include<multiset>
// #include<bitset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

int n;
ll d;
const int N = 2e5;
int a[N];

/// --- Union Find Library {{{ ///
struct UF {
  int n, comps;
  vector< int > par;
  UF(int n) : n(n), comps(n), par(n, -1) {}
  int find(int x) { return par[x] < 0 ? x : par[x] = find(par[x]); }
  int size(int x) { return -par[find(x)]; }
  int size() { return n; }
  int components() { return comps; }
  bool same(int a, int b) { return find(a) == find(b); }
  bool unite(int a, int b) {
    a = find(a);
    b = find(b);
    if(a == b) return 0;
    if(par[a] < par[b]) swap(a, b);
    par[b] += par[a];
    par[a] = b;
    comps--;
    return 1;
  }
};
/// }}}--- ///

// require UnionFind
// F(component-size, belongs) -> vector<(cost, to)>
template < class T, class F >
T boruvka(int n, const F &f) {
  UF uf(n);
  T res(0);
  bool update = 1;
  vector< int > belongs(n), rev(n);
  while(update) {
    update = 0;
    int ptr = 0;
    for(int i = 0; i < n; i++)
      if(i == uf.find(i)) rev[ptr] = i, belongs[i] = ptr++;
    for(int i = 0; i < n; i++) belongs[i] = belongs[uf.find(i)];
    auto v = f(ptr, belongs);
    for(int i = 0; i < ptr; i++)
      if(v[i].second >= 0 && uf.unite(rev[i], rev[v[i].second])) res += v[i].first, update = 1;
    if(!update) break;
  }
  return res;
}

// smin, smax {{{
// clang-format off
template < class T, class U > inline void smin(T &a, const U &b) { a = a < (T) b ? a : b; }
template < class T, class U > inline void smax(T &a, const U &b) { a = a < (T) b ? b : a; }
// clang-format on
// }}}

constexpr ll inf = 1e18;
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> d;
  for(int i = 0; i < n; i++) {
    cin >> a[i];
  }

  using P = pair<ll, int>;
  auto f = [&] (int sz, const vector<int>& belongs) {
    vector<P> res(sz, P(inf, -1));
    {
      P mi0(inf, -1);
      P mi1(inf, -1);
      for(int i = 0; i < n; i++) {
        if(mi0.second != -1 && mi0.second != belongs[i])
          smin(res[belongs[i]], P(mi0.first + i * d + a[i], mi0.second));
        else if(mi1.second != -1 && mi1.second != belongs[i])
          smin(res[belongs[i]], P(mi1.first + i * d + a[i], mi1.second));
        P it(-i * d + a[i], belongs[i]);
        if(mi0.second == -1 || mi0 > it) {
          if(mi0.second != it.second) mi1 = mi0;
          mi0 = it;
        } else if(mi1.second == -1 || (mi0.second != belongs[i] && mi1 > it)) mi1 = it;
      }
    }
    {
      P mi0(inf, -1);
      P mi1(inf, -1);
      for(int i = n - 1; i >= 0; i--) {
        if(mi0.second != -1 && mi0.second != belongs[i])
          smin(res[belongs[i]], P(mi0.first - i * d + a[i], mi0.second));
        else if(mi1.second != -1 && mi1.second != belongs[i])
          smin(res[belongs[i]], P(mi1.first - i * d + a[i], mi1.second));
        P it(i * d + a[i], belongs[i]);
        if(mi0.second == -1 || mi0 > it) {
          if(mi0.second != it.second) mi1 = mi0;
          mi0 = it;
        } else if(mi1.second == -1 || (mi0.second != belongs[i] && mi1 > it)) mi1 = it;
      }
    }
    return res;
  };

  cout << boruvka<ll>(n, f) << endl;
  return 0;
}


Measured memory_bytes: 6528