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<bits/stdc++.h>
#define N 200020
#define LOGN 20
using namespace std;
int n,d,first[N],nxt[N*LOGN*10],tot=0;
long long Ans,a[N],inf=1e18;
int fa[N],cnt=0;
long long Read()
{
	long long x=0,f=1;
	char c=getchar();
	while (c<'0'||c>'9') 
	{
		if (c=='-') f=-1;
		c=getchar();
	}
	while (c>='0'&&c<='9') 
	{
		x=x*10+c-'0';
		c=getchar();
	}
	return f*x;
}
struct Edge
{
	int u,v,w;
}edge[N*LOGN*10];
bool cmp(const Edge a,const Edge b)
{
	if (a.w<b.w) return true;
	else return false;
}
void Add(int u,int v)
{
	int w=abs(u-v)*d+a[u]+a[v];
	tot++;
	nxt[tot]=first[u];
	first[u]=tot;
	edge[tot]=(Edge){u,v,w};
	return;
}
int Build(int l,int r,int flag)
{
	if (l==r) return l;
	int mid=(l+r)>>1;
	int minl=Build(l,mid,-1),minr=Build(mid+1,r,1);
	for (int i=l;i<=mid;i++) Add(i,minr);
	for (int i=mid+1;i<=r;i++) Add(i,minl);
	long long minlr=inf;
	int point=0;
	for (int i=l;i<=r;i++) 
	    if (i*d*flag+a[i]<minlr) point=i,minlr=i*d*flag+a[i];
	return point;
}
int Get(int x)
{
	if (x==fa[x]) return x;
	else return fa[x]=Get(fa[x]);
}
void Join(int x,int y)
{
	fa[x]=y;
	return;
}
long long Krus()
{
	long long ret=0;
	for (int i=1;i<=n;i++) fa[i]=i;
	sort(edge+1,edge+tot+1,cmp);
	for (int i=1;i<=tot;i++)
	{
		int u=edge[i].u,v=edge[i].v;
		int f1=Get(u),f2=Get(v);
		if (f1!=f2)
		{
			cnt++;
			ret+=1LL*edge[i].w;
			Join(f1,f2);
		}
	}
	return ret;
}
int main()
{
	//freopen("testdata.in","r",stdin);
	memset(first,-1,sizeof(first));
	n=Read(),d=Read();
	for (int i=1;i<=n;i++) a[i]=Read();
	Build(1,n,1);
	Ans=Krus();
	printf("%lld\n",Ans);
	return 0;
}