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: Time Limit Exceeded)

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define SZ(x) ((int)x.size())
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define rep(i,a,b) for (register int i=a; i<=b; i++)
#define per(i,a,b) for (register int i=a; i>=b; i--)
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef pair<ll,ll> Pll;
typedef vector<int> Vi;
inline void read(int &x) {
	x=0; char c=getchar(); int f=1;
	while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
	while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;
}
inline ui R() {
	static ui seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
const int N = 666666;
int n,d,a[N];ll res;
set<Pll>Set;bool vis[N];ll dis[N];
ll solve(){
	ll res=1LL*(n-1)*d;
	rep(i,1,n)res+=2LL*a[i];res-=a[1]+a[n];
	return res;
}
int main() {
	read(n);read(d);rep(i,1,n)read(a[i]);
	int s=1;rep(i,2,n)if(a[i]<a[s])s=i;
	vis[s]=1;rep(i,1,n)dis[i]=a[s]+a[i]+1LL*(i-s>0?i-s:s-i)*d;
	rep(i,1,n)if(i!=s)Set.insert(Pll(dis[i],i));
	rep(t,1,n-1){
		int u=(*Set.begin()).se;Set.erase(Set.begin());
		res+=dis[u];vis[u]=1;
		if(1.0*clock()/CLOCKS_PER_SEC>1.5)cout<<solve();
		rep(i,u+1,n){
			if(a[u]+a[i]+1LL*d*(i-u)>=dis[i])break;
			if(!vis[i]){
				Set.erase(Pll(dis[i],i));
				Set.insert(Pll(a[u]+a[i]+1LL*d*(i-u),i));
			}
			dis[i]=a[u]+a[i]+1LL*d*(i-u);
		}
		per(i,u-1,1){
			if(a[u]+a[i]+1LL*d*(u-i)>=dis[i])break;
			if(!vis[i]){
				Set.erase(Pll(dis[i],i));
				Set.insert(Pll(a[u]+a[i]+1LL*d*(u-i),i));
			}
			dis[i]=a[u]+a[i]+1LL*d*(u-i);
		}
	}
	cout<<res;
	return 0;
}