Problem:
Score : 600 points

Problem Statement

There are N cities in Republic of AtCoder. The size of the i-th city is A_{i}.
Takahashi would like to build N-1 bidirectional roads connecting two cities so that any city can be reached from any other city by using these roads.

Assume that the cost of building a road connecting the i-th city and the j-th city is |i-j| \times D + A_{i} + A_{j}.
For Takahashi, find the minimum possible total cost to achieve the objective.

Constraints

1 \leq N \leq 2 \times 10^5

1 \leq D \leq 10^9

1 \leq A_{i} \leq 10^9

A_{i} and D are integers.

Input

Input is given from Standard Input in the following format:

N D
A_1 A_2 ... A_N

Output

Print the minimum possible total cost.

Sample Input 1

3 1
1 100 1

Sample Output 1

106

This cost can be achieved by, for example, building roads connecting City 1, 2 and City 1, 3.

Sample Input 2

3 1000
1 100 1

Sample Output 2

2202

Sample Input 3

6 14
25 171 7 1 17 162

Sample Output 3

497

Sample Input 4

12 5
43 94 27 3 69 99 56 25 8 15 46 8

Sample Output 4

658

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

/* --- author: dxm --- */
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

#define INF 1e9
#define Inf 1e18
#define rep(i,n) for(int (i)=0;(i)<n;i++)
#define REP(i,n) for(int (i)=1;(i)<=n;i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;

void qread(int &x){
	int neg=1;x=0;
	char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')neg=-1;c=getchar();}
	while(c>='0'&&c<='9')x=10*x+c-'0',c=getchar();
	x*=neg;
}

const int maxn=200005;
int n,d; 
int a[maxn];
ll ans;

class node{
public:
	pair<ll,int>val,sx,sy,tx,ty;
}t[maxn*4];
namespace Segtree{
	#define ls cur<<1
	#define rs cur<<1|1
	void pushup(int cur){
		t[cur].sx=min(t[ls].sx,t[rs].sx);
		t[cur].sy=min(t[ls].sy,t[rs].sy);
		t[cur].tx=min(t[ls].tx,t[rs].tx);
		t[cur].ty=min(t[ls].ty,t[rs].ty);
		t[cur].val=min(t[ls].val,t[rs].val);
		if(t[ls].sy.fst+t[rs].tx.fst<t[cur].val.fst)
			t[cur].val=mp(t[ls].sy.fst+t[rs].tx.fst,t[rs].tx.snd);
		if(t[rs].sx.fst+t[ls].ty.fst<t[cur].val.fst)
			t[cur].val=mp(t[rs].sx.fst+t[ls].ty.fst,t[ls].ty.snd);
	}
	void build(int cur,int l,int r){
		if(l==r){
			t[cur].val=mp(Inf,Inf);
			if(l==1){
				t[cur].sx=mp(1LL*l*d+a[l],l);
				t[cur].sy=mp(-1LL*l*d+a[l],l);
				t[cur].tx=mp(Inf,Inf);
				t[cur].ty=mp(Inf,Inf);
			}
			else{
				t[cur].sx=mp(Inf,Inf);
				t[cur].sy=mp(Inf,Inf);
				t[cur].tx=mp(1LL*l*d+a[l],l);
				t[cur].ty=mp(-1LL*l*d+a[l],l);
			}
			return;
		}
		int mid=l+r>>1;
		build(ls,l,mid);build(rs,mid+1,r);
		pushup(cur);
	}
	void update(int cur,int l,int r,int k){
		if(l==r){
			swap(t[cur].sx,t[cur].tx);
			swap(t[cur].sy,t[cur].ty);
			return;
		}
		int mid=l+r>>1;
		if(k<=mid)update(ls,l,mid,k);
		else update(rs,mid+1,r,k);
		pushup(cur); 
	}
}

int main(){
#ifdef FILE
	freopen("FILE.in","r",stdin);
	freopen("FILE.out","w",stdout);
#endif
	scanf("%d%d",&n,&d);
	REP(i,n)scanf("%d",&a[i]);
	Segtree::build(1,1,n);
	REP(i,n-1){
		node cur=t[1];
		ans+=cur.val.fst;
		Segtree::update(1,1,n,cur.val.snd);
	}
	printf("%lld\n",ans);
#ifdef TIME
	printf("Running Time = %d ms\n",int(clock()*1000.0/CLOCKS_PER_SEC));
#endif
	return 0;
}

/*
Input:
-----------------
Output:
*/