#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct UnionFind {
    vector<ll> par, rank,
        cnt;  // par[i]:iの親の番号,rank[r]:rを根とする木の高さ,cnt[r]:rを根とする木の要素数

    UnionFind(ll N) {  //全て根として初期化
        for (ll i = 0; i < N; i++) par.push_back(i);
        rank.assign(N, 0);
        cnt.assign(N, 1);
    }

    ll root(ll x) {  // xの根を返す
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(ll x, ll y) {  // xの木とyの木を併合
        ll rx = root(x);
        ll ry = root(y);
        if (rx == ry) return;

        if (rank[rx] < rank[ry]) {
            par[rx] = ry;
            cnt[ry] += cnt[rx];
        } else {
            par[ry] = rx;
            cnt[rx] += cnt[ry];
            if (rank[x] == rank[y]) rank[rx]++;
        }
    }

    ll tsize(ll x) {  //　xの木の要素数を返す
        ll rx = root(x);
        return cnt[rx];
    }

    bool same(ll x, ll y) {  // xとyが同一の木に存在するかを返す
        ll rx = root(x);
        ll ry = root(y);
        return rx == ry;
    }
};

typedef pair<ll, int> P;
const P INF = P(1e18, -1);
// 点更新・区間取得
template <typename T>
class Segment {
    using u32 = uint_fast32_t;
    u32 n;
    vector<T> node;
    T e;                  // 単位元∧初期値
    function<T(T, T)> f;  // 更新
    function<T(T, T)> g;  // 取得

   public:
    Segment(u32 N, T e, function<T(T, T)> f, function<T(T, T)> g, vector<T> v = vector<T>())
        : e(e), f(f), g(g) {
        n = 1;
        while (n < N) n *= 2;
        node.assign(2 * n - 1, e);
        if (N == (u32)v.size()) {
            for (int i = 0; i < N; i++) node[i + n - 1] = v[i];
            for (int i = n - 2; i >= 0; i--) {
                node[i] = g(node[2 * i + 1], node[2 * i + 2]);
            }
        }
    }

    // k番目をaで更新
    void update(u32 k, T a) {
        k += n - 1;
        node[k] = f(node[k], a);
        while (k > 0) {
            k = (k - 1) / 2;
            node[k] = g(node[2 * k + 1], node[2 * k + 2]);
        }
    }

    // [a,b)の区間取得
    T get(u32 a, u32 b, u32 k, u32 l, u32 r) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return e;  // 含まない
        if (a <= l && r <= b)            // 完全に含む
            return node[k];
        else {
            T vl = get(a, b, 2 * k + 1, l, (l + r) / 2);
            T vr = get(a, b, 2 * k + 2, (l + r) / 2, r);
            return g(vl, vr);
        }
    }
    T get(u32 a, u32 b) { return get(a, b, 0, 0, n); }

    T operator[](u32 i) { return node[i + n - 1]; }
};

template <typename T>
struct Graph {
    int V, E;
    struct Edge {
        int from, to;
        T cost;
    };
    vector<vector<Edge>> edge;
    vector<vector<T>> dist;
    vector<Edge> es;

    Graph(int V, int E) : V(V), E(E) {
        edge.assign(V, vector<Edge>(0));
        dist.assign(V, vector<T>(0));
    }

    void input(bool is_direct, bool index0 = false) {
        int a, b;
        T c;
        for (int i = 0; i < E; i++) {
            cin >> a >> b;
            if (!index0) a--, b--;
            edge[a].push_back((Edge){a, b, c});
            es.push_back((Edge){a, b, c});
            if (!is_direct) {
                edge[b].push_back((Edge){b, a, c});
                es.push_back((Edge){b, a, c});
            }
        }
    }

    void add_edge(int from, int to, T cost, bool is_direct) {
        edge[from].push_back({from, to, cost});
        es.push_back({from, to, cost});
        if (!is_direct) {
            edge[to].push_back({to, from, cost});
            es.push_back({to, from, cost});
            E++;
        }
        E++;
    }

    T kruskal() {
        auto comp = [](const Edge& e1, const Edge& e2) { return e1.cost < e2.cost; };
        sort(es.begin(), es.end(), comp);
        UnionFind tree(V);
        T res = 0;
        for (const auto& e : es) {
            if (!tree.same(e.from, e.to)) {
                tree.unite(e.from, e.to);
                res += e.cost;
            }
        }
        return res;
    }
};

int main() {
    int N;
    ll D;
    cin >> N >> D;
    vector<P> A(N);
    vector<P> l(N), r(N);
    for (int i = 0; i < N; i++) {
        ll a;
        cin >> a;
        A[i] = make_pair(a, i);
        l[i] = P(a - D * i, i);
        r[i] = P(a + D * i, i);
    }
    auto f = [](P x, P y) { return y; };
    auto g = [](P x, P y) { return min(x, y); };
    Segment<P> lt(N, INF, f, g, l), rt(N, INF, f, g, r);

    sort(A.begin(), A.end(), greater<P>());
    Graph<ll> graph(N, 0);

    for (int i = 0; i < N; i++) {
        int p = A[i].second;
        P lv = lt.get(0, p);
        P rv = rt.get(p + 1, N);
        if (lv != INF) {
            graph.add_edge(p, lv.second, A[i].first + D * p + lv.first, false);
            lt.update(p, INF);
        }
        if (rv != INF) {
            graph.add_edge(p, rv.second, A[i].first - D * p + rv.first, false);
            rt.update(p, INF);
        }
    }

    cout << graph.kruskal() << endl;
    return 0;
}

Measured memory_bytes: 76624