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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
class Program{
	const int mod=1000000007;
	static long[,,] dat;
	static int m=1;
	static void Main(){
		int[] s=Array.ConvertAll(Console.ReadLine().Split(),int.Parse);
		long[] a=Array.ConvertAll(Console.ReadLine().Split(),long.Parse);
		long[][] h=new long[s[0]][];
		long ans=0;

		int n=s[0];
		while(m<n){m*=2;}
		dat=new long[m+n-1,2,2];

		for(int i=0;i<m+n-1;i++){
			dat[i,0,0]=long.MaxValue;
			dat[i,1,0]=long.MaxValue;
		}
		for(int i=0;i<s[0];i++){h[i]=new long[]{a[i],i};}
		Array.Sort(h,ca);
		fdu((int)h[0][1],h[0][0]+h[0][1]*s[1],0);
		fdu((int)h[0][1],h[0][0]+(s[0]-h[0][1]-1)*s[1],1);
		for(int i=1;i<s[0];i++){
			long[] q=fdg((int)h[i][1]+1,n-1,0,0,m-1,0);
			if(q[0]!=long.MaxValue){hpa(a[h[i][1]]+a[(int)q[1]]+(q[1]-h[i][1])*s[1],(int)h[i][1],(int)q[1]);}
			q=fdg(0,(int)h[i][1]-1,0,0,m-1,1);
			if(q[0]!=long.MaxValue){hpa(a[h[i][1]]+a[(int)q[1]]+(h[i][1]-q[1])*s[1],(int)h[i][1],(int)q[1]);}
			fdu((int)h[i][1],h[i][0]+h[i][1]*s[1],0);
			fdu((int)h[i][1],h[i][0]+(s[0]-h[i][1]-1)*s[1],1);
		}
		uf uf=new uf(n);
		while(he.Count>0){
			if(!uf.same((int)he[0][1],(int)he[0][2])){
				uf.union((int)he[0][1],(int)he[0][2]);
				ans+=he[0][0];
			}
			hpr();
		}
		Console.WriteLine(ans);
	}
	static int ca(long[] a,long[] b){
		if(a[0]>b[0]){return 1;}
		else if(a[0]<b[0]){return -1;}
		else{return 0;}
	}
	static void fdu(int q,long b,int z){
		int c=q;
		q+=m-1;
		dat[q,z,0]=b;
		dat[q,z,1]=c;
		while(q>0){
			if(dat[(q-1)/2,z,0]>b){
				dat[(q-1)/2,z,0]=b;
				dat[(q-1)/2,z,1]=c;
				q=(q-1)/2;
			}
			else{break;}
		}
	}
	static long[] fdg(int a,int b,int k,int l,int r,int z){
		if(r<a||b<l){return new long[]{long.MaxValue,0};}
		if(a<=l&&r<=b){return new long[]{dat[k,z,0],dat[k,z,1]};}
		else{
			long[] l1=fdg(a,b,k*2+1,l,(l+r)/2,z);
			long[] l2=fdg(a,b,k*2+2,(l+r+1)/2,r,z);
			if(l1[0]<l2[0]){return new long[]{l1[0],l1[1]};}
			else {return new long[]{l2[0],l2[1]};}
		}
	}
	static List<long[]> he=new List<long[]>();
	static void hpa(long a,int b,int c){
		he.Add(new long[]{a,b,c});
		int j=he.Count-1;
		while(he[j][0]<he[(j-1)/2][0]){//
			long v=he[j][0];he[j][0]=he[(j-1)/2][0];he[(j-1)/2][0]=v;
			v=he[j][1];he[j][1]=he[(j-1)/2][1];he[(j-1)/2][1]=v;
			v=he[j][2];he[j][2]=he[(j-1)/2][2];he[(j-1)/2][2]=v;
			j=(j-1)/2;
		}
	}
	static void hpr(){
		he[0]=he[he.Count-1];
		he.RemoveAt(he.Count-1);
		int j=0;
		while(true){
			if(j*2+2<he.Count){
				if(he[j*2+1][0]<he[j*2+2][0]){j=j*2+1;}//
				else{j=j*2+2;}
			}
			else if(j*2+1<he.Count){j=j*2+1;}
			else{break;}
			if(he[j][0]>=he[(j-1)/2][0]){break;}//
			long v=he[j][0];he[j][0]=he[(j-1)/2][0];he[(j-1)/2][0]=v;
			v=he[j][1];he[j][1]=he[(j-1)/2][1];he[(j-1)/2][1]=v;
			v=he[j][2];he[j][2]=he[(j-1)/2][2];he[(j-1)/2][2]=v;
		}
	}
}
public class uf{
	private int[] arr;
	private int[] hi;
	public uf(int n){
		arr=new int[n];
		hi=new int[n];
		for(int i=0;i<n;i++){arr[i]=-1;}
	}
	public void union(int a,int b){
		a=root(a);
		b=root(b);
		if(a!=b){
			if(hi[a]<hi[b]){
				int v=b;
				b=a;
				a=v;
			}
			arr[a]+=arr[b];
			hi[a]=Math.Max(hi[a],hi[b]+1);
			arr[b]=a;
		}
	}
	public bool same(int a,int b){return root(a)==root(b);}
	public int total(int a){return -arr[root(a)];}
	private int root(int a){return arr[a]<0?a:arr[a]=root(arr[a]);}
}