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)

//#include "pch.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <utility>
#include <queue>
#include <algorithm>
#include <functional>
#include <string>

#define ll long long
#define fri(n) for(i=0;i<(n);i++)
#define frj(n) for(j=0;j<(n);i++)
#define min(p,q) ((p)<(q)?(p):(q))
#define max(p,q) ((p)>(q)?(p):(q))
#define swap(p,q) r=(p);(p)=(q);(q)=r
#define INF 1000000000000000000//10^18
#define INFINT 2000000001//2*10^9+1
#define MOD 1000000007
#define PI acos(-1)

using namespace std;



int main(void)
{
	//変数の宣言
	ll int n,d;
	static ll int a[200010];
	static ll int localmin[200010];
	static ll int road[200010][2];

	//よく使う変数
	int i,j,k,l;
	int flag=0;
	ll int ans=0;
	int count=0;
	ll int temp,temp1,temp2;
	int max,min;
	int len;
	int sum=0;


	//データの読み込み
	scanf("%lld %lld",&n,&d);
//	scanf_s("%lld %lld",&n,&d);

	fri(n){
		scanf("%lld",&a[i]);
//		scanf_s("%lld",&a[i]);	
	}

	fri(n){
		localmin[i]=-1;
	}

	//	printf("nは%dです\n", n);
	//	printf("データの読み込み終了\n");
	//実際の処理

	temp=0;
	fri(n){
		if(a[temp]>=d*(i-temp)+a[i]){
			temp=i;
		}else if(a[temp]+d*(i-temp)>a[i]){
			localmin[count]=temp;
			count++;
			temp=i;
		}
	}
	localmin[count]=temp;
	count++;

	//	for(i=0;i<count;i++){
	//		printf("localmin[%d]=%lld\n",i,localmin[i]);
	//	}

	temp=0;
	for(i=0;i<count-1;i++){
		road[i][0]=localmin[i];
		road[i][1]=localmin[i+1];
		//		printf("i=%d,(%lld,%lld)\n",i,road[i][0],road[i][1]);
	}
	temp=count-1;
	temp1=-1;
	temp2=localmin[0];
	count=1;
	for(i=0;i<n;i++){
		if(temp1==-1){
			if(i<temp2){
				road[temp][0]=i;
				road[temp][1]=temp2;
				temp++;
			}else if(i==temp2){
				temp1=temp2;
				temp2=localmin[1];
			}
		}else if(temp2!=-1){
			if(i<temp2){
				if(a[temp1]+(i-temp1)*d<a[temp2]+(temp2-i)*d){
					road[temp][0]=temp1;
					road[temp][1]=i;
					temp++;
				}else{
					road[temp][0]=i;
					road[temp][1]=temp2;
					temp++;
				}
			}else if(i==temp2){
				temp1=temp2;
				temp2=localmin[count+1];
				count++;
			}
		}else{
			road[temp][0]=temp1;
			road[temp][1]=i;
			temp++;
		}
	}

	fri(temp){
		ans=ans+a[road[i][0]]+a[road[i][1]]+llabs(road[i][1]-road[i][0])*d;
				printf("(%lld,%lld)",road[i][0],road[i][1]);
	}


	//	printf("計算部分終了\n");

	//出力

	printf("%lld",ans);


	//	printf("結果の出力終了\n");


	return 0;
}