#include <bits/stdc++.h>
using namespace std;
#define int long long   // aaaaaaaaaaaaaaaaaa
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define SIZE(buff) (sizeof(buff)/sizeof(buff[0]))
typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}
template<typename T, typename U>ostream& operator << (ostream& os, pair<T, U>& pair_var)
{os << "(" << pair_var.first << ", " << pair_var.second << ")";return os;}
// head
int q,m,n,k,res,tmp,tmp2,tmp3;
vector<pair<int,int>> a;
//int b[200010] = {};
int dist [100002];
deque<PII> que;
//int dp[305][305][305] = {};
string s;
vector<pair<int,int>> g[200010];
int d;

class UnionFind {
private:
    int n;
    vector<int> uni;
public:
    UnionFind(int _n) {
        n = _n;
        uni.clear();
        uni.resize(n, -1);
    }
    int root(int x) {
        if (uni[x] < 0) return x;
        return uni[x] = root(uni[x]);
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    void unite(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return;
        if (uni[x] > uni[y]) swap(x, y);
        uni[x] += uni[y];
        uni[y] = x;

    }
    int getSize(int x) {
        return -uni[root(x)];
    }
    void print() {
        for (auto x : uni) cout << x << " ";
        cout << endl;
    }
};
class SegmentTree {
  const static ll inf = (1LL << 60) - 1;
  int N, N0;
  ll data[4*200005][2];

public:
  SegmentTree(int N) {
    N0 = 1;
    while(N0 < N) N0 <<= 1;
    rep(j,0,2){
    rep(i, 0,2*N0+4) {data[i][j] = inf;}}
  }

  void update(int k, ll x,int y) {
    k += N0-1;
    data[k][0] = x;
    data[k][1] = y;
    while(k > 0) {
      k = (k - 1) / 2;
			if(data[2*k+1][0] > data[2*k+2][0]) {data[k][0] = data[2*k+2][0];data[k][1] = data[2*k+2][1];}
			else {data[k][0] = data[2*k+1][0];data[k][1] = data[2*k+1][1];}
    }
  }

  tuple<int,int> __query(int a, int b, int k, int l, int r) {
    if(a <= l && r <= b) {
      return forward_as_tuple(data[k][0],data[k][1]);
    }
    if(r <= a || b <= l) {
      return forward_as_tuple(inf,inf);
    }
		ll lv,lc,rv,rc;
    tie(lv,lc) = __query(a, b, 2*k+1, l, (l+r)/2);
    tie(rv,rc) = __query(a, b, 2*k+2, (l+r)/2, r);
		if (lv > rv) return forward_as_tuple(rv,rc);
		else return forward_as_tuple(lv,lc);
  }

  tuple<int,int> query(int a, int b) {
    return __query(a, b, 0, 0, N0);
  }
	void print() {
			rep(i,0,N0) cout << data[i][0] << " "<<data[i][1]<<" ";
			cout << endl;}
};

signed main() {
	cin.tie(0);
    ios::sync_with_stdio(false);
	std::cin >>n>>d;
	rep(i,0,n){
		cin>>tmp;
		a.pb({tmp,i});
	}
	vector<pair<int,int>> b = a;
	sort(all(a));
  res = 0;
  UnionFind UF(n);
  SegmentTree STR(n);
  SegmentTree STL(n);
	vector<pair<int, pair<int, int> > > kore;
	ll inf = (1LL << 60) - 1;
	rep(i,0,n){
		pair<int,int> c = a[i];
		ll hi1,hi2,mi1,mi2;
		tie(hi1,hi2) = STL.query(0,c.se);
		tie(mi1,mi2) = STR.query(c.se,n);
		if(hi2 != inf){kore.pb({b[c.se].fi + d*(-hi2+c.se)+b[hi2].fi,{c.se,hi2}});}
		if(mi2 != inf){kore.pb({b[c.se].fi + d*(mi2-c.se)+b[mi2].fi,{c.se,mi2}});}
		STL.update(c.se,c.fi+(n-c.se)*d,c.se);
		STR.update(c.se,c.fi+c.se*d,c.se);
	}
	sort(all(kore));
  for (auto i :kore){
      if(UF.same(i.se.fi,i.se.se)){
          continue;
      }
        UF.unite(i.se.fi,i.se.se);
          res += i.fi;
  }
  cout<<res<<endl;
}


Measured memory_bytes: 51048