#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
template <typename Monoid>
struct SegmentTree {
  typedef function<Monoid(Monoid, Monoid)> F;
  int n;
  F f;
  Monoid id;
  vector<Monoid> dat;
  SegmentTree(int n_, F f, Monoid id) : f(f), id(id) { init(n_); }
  void init(int n_) {
    n = 1;
    while (n < n_) n <<= 1;
    dat.assign(n << 1, id);
  }
  void build(const vector<Monoid> &v) {
    for (int i = 0; i < v.size(); ++i) dat[i + n] = v[i];
    for (int i = n - 1; i; --i) dat[i] = f(dat[i << 1 | 0], dat[i << 1 | 1]);
  }
  void update(int k, Monoid x) {
    dat[k += n] = x;
    while (k >>= 1) dat[k] = f(dat[k << 1 | 0], dat[k << 1 | 1]);
  }
  Monoid query(int a, int b) {
    if (a >= b) return id;
    Monoid vl = id, vr = id;
    for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) vl = f(vl, dat[l++]);
      if (r & 1) vr = f(dat[--r], vr);
    }
    return f(vl, vr);
  }
  Monoid operator[](int i) { return dat[i + n]; }
  template <typename C>
  int find(int a, int b, const C &check, int k, int l, int r) {
    if (!check(dat[k]) || r <= a || b <= l) return -1;
    if (l + 1 == r) return k - n;
    int vl = find(a, b, check, k << 1 | 0, l, (l + r) >> 1);
    if (~vl) return vl;
    return find(a, b, check, k << 1 | 1, (l + r) >> 1, r);
  }
  template <typename C>
  int find(int a, int b, const C &check) {
    return find(a, b, check, 1, 0, n);
  }
};
template <typename T>
struct Kruskal {
  struct edge {
    int from, to, used;
    T cost;
    edge(int from, int to, T cost) : from(from), to(to), cost(cost), used(0) {}
    bool operator<(const edge &e) const {
      if (cost != e.cost)
        return cost < e.cost;
      else if (from != e.from)
        return from < e.from;
      else
        return to < e.to;
    }
  };
  int num;
  vector<int> par, rank;
  vector<edge> es;
  Kruskal(int n) : num(n), par(n), rank(n, 1) {
    iota(par.begin(), par.end(), 0);
  }
  int root(int x) {
    if (par[x] == x) return x;
    return par[x] = root(par[x]);
  }
  bool merge(int x, int y) {
    x = root(x), y = root(y);
    if (x == y) return false;
    if (rank[x] < rank[y]) swap(x, y);
    par[y] = x;
    rank[x] += rank[y];
    --num;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return rank[root(x)]; }
  int count() { return num; }
  void add_edge(int u, int v, T c) { es.emplace_back(u, v, c); }
  T build() {
    sort(es.begin(), es.end());
    T res = 0;
    for (auto &e : es) {
      if (!same(e.from, e.to)) {
        res += e.cost;
        merge(e.from, e.to);
        e.used = 1;
      }
    }
    return res;
  }
  vector<int> used_es(T &c) {
    int E = es.size();
    map<edge, int> m;
    for (int i = 0; i < E; ++i) m[es[i]] = i;
    c = build();
    vector<int> list(E);
    for (int i = 0; i < E; ++i) list[m[es[i]]] = es[i].used;
    return list;
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  long long D;
  cin >> N >> D;
  vector<long long> A(N);
  for (int i = 0; i < N; ++i) cin >> A[i];
  auto f = [](pair<long long, int> a, pair<long long, int> b) {
    return b.first < a.first ? b : a;
  };
  SegmentTree<pair<long long, int>> seg1(N, f, {INF, -1}),
      seg2(N, f, {INF, -1});
  vector<pair<long long, int>> B(N), C(N);
  for (int i = 0; i < N; ++i) {
    B[i] = {A[i] - D * i, i};
    C[i] = {A[i] + D * i, i};
  }
  seg1.build(B);
  seg2.build(C);
  auto g = [&](int i) {
    auto p = seg1.query(0, i);
    return make_pair(B[i].first + p.first + 2 * i * D, p.second);
  };
  auto h = [&](int i) {
    auto p = seg2.query(i + 1, N);
    return make_pair(C[i].first + p.first - 2 * i * D, p.second);
  };
  Kruskal<long long> K(N);
  for (int i = 0; i < N; ++i) {
    auto G = g(i), H = h(i);
    if (~G.second) K.add_edge(i, G.second, G.first);
    if (~H.second) K.add_edge(i, H.second, H.first);
  }
  long long ans = K.build();
  cout << ans << '\n';
}
