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: Wrong Answer)

/*
ID: htc9811
LANG: C++
TASK:
*/

#include<stdio.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<ctime>
#include<complex>
#include<list>
#include<limits.h>
using namespace std;

const int N=2e5+5;
int n,cur;
long long a[N];
long long d,ans;

struct Seg{
	long long length;
	int x,y;
	Seg(){};
	Seg(long long length,int x,int y):length(length),x(x),y(y){};
};

struct UF{
	int p[N],rnk[N];
	void init(int n){
		for(int i=0;i<n;i++) p[i]=i;
		for(int i=0;i<n;i++) rnk[i]=1;
	}
	int fp(int x){
		return x==p[x]?x:p[x]=fp(p[x]);
	}
	void un(int x,int y){
		if(rnk[x]>rnk[y]) p[y]=x;
		if(rnk[x]<rnk[y]) p[x]=y;
		if(rnk[x]==rnk[y]) p[x]=y,rnk[y]++;
	}
};

inline bool comp(Seg &x,Seg &y){
	return x.length<y.length;
}

vector<Seg> seg;
UF uf;

void solve(int left,int right){
	if(left>=right) return;
	int middle=(left+right)>>1;
	int pl=left;
	long long vl=a[left];
	for(int i=left+1;i<=middle;i++){
		long long cv=a[i]-d*i;
		if(cv<vl){
			vl=cv;
			pl=i;
		}
	}
	int pr=middle+1;
	long long vr=a[middle+1]+d*(middle+1);
	for(int i=middle+1;i<=right;i++){
		long long cv=a[i]+d*i;
		if(cv<vr){
			vr=cv;
			pr=i;
		}
	}
	for(int i=left;i<=middle;i++) seg.push_back(Seg(a[i]-d*i+vr,i,pr));
	for(int i=middle+1;i<=right;i++) seg.push_back(Seg(a[i]+d*i+vl,pl,i));
	solve(left,middle);
	solve(middle+1,right);
}

int main(){
//	freopen("cc3.in","r",stdin);
//	freopen("cc1.out","w",stdout);
	scanf("%d%lld",&n,&d);
	uf.init(n);
	for(int i=0;i<n;i++) scanf("%lld",a+i);
	solve(0,n-1);
	sort(seg.begin(),seg.end(),comp);
//	for(int i=0;i<seg.size();i++){
//		printf("%d:x->%d y->%d length->%d\n",i,seg[i].x,seg[i].y,seg[i].length);
//	}
//	puts("");
	for(int i=1;i<n;i++){
		while(true){
			int nx=seg[cur].x;
			int ny=seg[cur].y;
			long long len=seg[cur++].length;
			nx=uf.fp(nx);
			ny=uf.fp(ny);
			if(nx!=ny){
				uf.un(nx,ny);
				ans+=len;
				break;
			}
		}
	}
	printf("%lld\n",ans);
	return 0;
}