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

#define ll long long
#define N 200010
#define INFLL 0x3f3f3f3f3f3f3f3f
int n, m;
ll d;
struct node
{
    int id; ll v;
    void scan(int id)
    {
        this->id = id;
        scanf("%lld", &v);
    }
    bool operator < (const node &other) const { return v < other.v; }
}a[N];
struct Edge
{
    int u, v; ll w;
    Edge () {}
    Edge (int u, int v, ll w) : u(u), v(v), w(w) {}
    bool operator < (const Edge &other) const { return w < other.w; }
}edge[N << 2];

struct SEG
{
    struct node
    {
        ll Min; int pos;
        node () {}
        node (ll Min, int pos) : Min(Min), pos(pos) {}
        void init() { Min = INFLL, pos = -1; }
        node operator + (const node &other) const
        {
            node res; res.init();
            if (Min < other.Min)
            {
                res.Min = Min;
                res.pos = pos;
            }
            else
            {
                res.Min = other.Min;
                res.pos = other.pos;
            }
            return res;
        }
    }a[N << 2], res;
    void build(int id, int l, int r)
    {
        a[id].init(); 
        if (l == r) return;
        int mid = (l + r) >> 1;
        build(id << 1, l, mid);
        build(id << 1 | 1, mid + 1, r);
    }
    void update(int id, int l, int r, int pos, ll v)
    {
        if (l == r) 
        {
            a[id] = node(v, pos);
            return;
        }
        int mid = (l + r) >> 1;
        if (pos <= mid) update(id << 1, l, mid, pos, v);
        else update(id << 1 | 1, mid + 1, r, pos, v);
        a[id] = a[id << 1] + a[id << 1 | 1];
    }
    void query(int id, int l, int r, int ql, int qr)
    {
        if (qr < ql) return;
        if (l >= ql && r <= qr)
        {
            res = res + a[id];
            return;
        }
        int mid = (l + r) >> 1;
        if (ql <= mid) query(id << 1, l, mid, ql, qr);
        if (qr > mid) query(id << 1 | 1, mid + 1, r, ql, qr);
    }
}seg[2];

int pre[N];
int find(int x) { return pre[x] == 0 ? x : pre[x] = find(pre[x]); }
ll Kruskal()
{
    memset(pre, 0, sizeof pre);
    sort(edge + 1, edge + 1 + m);
    int cnt = 1;
    ll res = 0;
    for (int i = 1; i <= m; ++i)
    {
        int u = edge[i].u, v = edge[i].v; ll w = edge[i].w;
        int fu = find(u), fv = find(v);
        if (fu == fv) continue;
        pre[fu] = fv;
        ++cnt;
        res += w;
        if (cnt == n) return res;
    }
    return res;
}

int main()
{
    while (scanf("%d%lld", &n, &d) != EOF)
    {    
        m = 0;
        for (int i = 1; i <= n; ++i) a[i].scan(i);
        for (int i = 0; i < 2; ++i) seg[i].build(1, 1, n);
        sort(a + 1, a + 1 + n);
        for (int i = 1; i <= n; ++i)
        {
            int u = a[i].id, v;
            for (int j = 0; j < 2; ++j)
                seg[j].res.init();
            seg[0].query(1, 1, n, 1, u - 1);
            seg[1].query(1, 1, n, u + 1, n);
            for (int j = 0; j < 2; ++j) if (seg[j].res.pos != -1)
            {
                v = seg[j].res.pos;
                edge[++m] = Edge(u, v, a[i].v + 1ll * (j ? -1 : 1) * d * u + seg[j].res.Min);
            }
            seg[0].update(1, 1, n, u, a[i].v - d * u);
            seg[1].update(1, 1, n, u, a[i].v + d * u);
        }
        printf("%lld\n", Kruskal());
    }
    return 0;
}

Measured memory_bytes: 33536