#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<ctime>
#include<set>
#include<iomanip>
#define sqr(x) (x)*(x)
#define fz1(i,n) for ((i)=1;(i)<=(n);(i)++)
#define fd1(i,n) for ((i)=(n);(i)>=1;(i)--)
#define fz0g(i,n) for ((i)=0;(i)<=(n);(i)++)
#define fd0g(i,n) for ((i)=(n);(i)>=0;(i)--)
#define fz0k(i,n) for ((i)=0;(i)<(n);(i)++)
#define fd0k(i,n) for ((i)=(long long)((n)-1);(i)>=0;(i)--)
#define fz(i,x,y) for ((i)=(x);(i)<=(y);(i)++)
#define fd(i,y,x) for ((i)=(y);(i)>=(x);(i)--)
#define ff(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define rdst(st,len){static char ss[len];scanf(" %s",ss);(st)=ss;}
//#define T_ int
using namespace std;
//struct bit{T_ arr[1000005];int lowbit(int x){return x&(-x);}void add(int x,T_ y){for (int i=x;i<=n;i+=lowbit(i)) arr[i]+=y;}T_ query(int x){T_ s=0;for (int i=x;i>0;i-=lowbit(i)) s+=arr[i];return s;}};
//struct seg{struct treedot{int l,r;T_ sum,add;void update(T_ x){sum+=(r-l+1)*x; add+=x;}}c[800005];void pushdown(int id){if (c[id].add){c[id*2].update(c[id].add);c[id*2+1].update(c[id].add);c[id].add=0;}}void pushup(int id){c[id].sum=c[id*2].sum+c[id*2+1].sum;}void build(int id,int l,int r){if (l>r) return;c[id].l=l;c[id].r=r;c[id].sum=0;c[id].add=0;if (l==r) c[id].sum=0;else{int mid=l+(r-l)/2;build(id*2,l,mid);build(id*2+1,mid+1,r);pushup(id);}}void update(int id,int le,int ri,T_ x){if (le>c[id].r||ri<c[id].l) return;if (le<=c[id].l&&c[id].r<=ri) c[id].update(x);else{pushdown(id);update(id*2,le,ri,x);update(id*2+1,le,ri,x);pushup(id);}}T_ query(int id,int le,int ri){if (ri<c[id].l||c[id].r<le) return 0;if (le<=c[id].l&&c[id].r<=ri) return c[id].sum;T_ ans=0;pushdown(id);ans+=query(id*2,le,ri);ans+=query(id*2+1,le,ri);pushup(id);return ans;}};
//struct bit_2d{T_ a[2005][2005];int lowbit(int x){return x&(-x);}void add(int x,int y,T_ z){int i,j;for (i=x;i<=2000;i+=lowbit(i)){for (j=y;j<=2000;j+=lowbit(j)){a[i][j]+=z;}}}T_ query(int x,int y){int i,j;T_ s=0;for (i=x;i;i-=lowbit(i)){for (j=y;j;j-=lowbit(j)){s+=a[i][j];}}return s;}T_ query(int x,int y,int xx,int yy){return query(xx,yy)-query(x-1,yy)-query(xx,y-1)+query(x-1,y-1);}};
long long n,m,i,j,l[200005],r[200005],mil[200005][19],mir[200005][19],x,y,num[200005],d,lg[200005],ans;
long long fndl(long long x)
{
	if (l[x]==x) return x;
	return l[x]=fndl(l[x]);
}
long long fndr(long long x)
{
	if (r[x]==x) return x;
	return r[x]=fndr(r[x]);
}
void merge(long long x,long long y)
{
	if (x>y) swap(x,y);
	l[fndl(y)]=fndl(x);
	r[fndr(x)]=fndr(y);
}
long long ql(long long x,long long y)
{
	if (x>y) return 1ll<<60;
	long long k=lg[y-x+1];
	return min(mil[x][k],mil[y-(1<<k)+1][k]);
}
long long qr(long long x,long long y)
{
	if (x>y) return 1ll<<60;
	long long k=lg[y-x+1];
	return min(mir[x][k],mir[y-(1<<k)+1][k]);
}
bool isb(long long x,long long y)
{
	long long f=min(ql(1,fndl(x)-1)+qr(fndl(x),fndr(x)),qr(fndr(x)+1,n)+ql(fndl(x),fndr(x)));
	if (f<y) return 0;
	if (f==y) return 1;
	if (f>y) exit(-1);
}
void solve(long long x)
{
	long long i,j;
	for (i=fndl(x)-1;i>=1;i=fndl(i)-1)
	{
		long long d=ql(fndl(i),fndr(i))+qr(fndl(x),fndr(x));
		if (isb(i,d))
		{
			merge(i,x);
			ans+=d;
		}
		else break;
	}
	for (i=fndr(x)+1;i<=n;i=fndr(i)+1)
	{
		long long d=qr(fndl(i),fndr(i))+ql(fndl(x),fndr(x));
		if (isb(i,d))
		{
			merge(i,x);
			ans+=d;
		}
		else break;
	}
}
int main()
{
	scanf("%lld%lld",&n,&m);
	fz1(i,n) l[i]=r[i]=i;
	lg[1]=0;
	for (i=2;i<=n;i++) lg[i]=lg[i/2]+1; 
	fz1(i,n)
	{
		scanf("%lld",&num[i]);
		mil[i][0]=num[i]-i*m;
		mir[i][0]=num[i]+i*m;
	}
	for (j=1;(1<<j)<=n;j++)
	{
		for (i=1;i+(1<<j)-1<=n;i++)
		{
			mil[i][j]=min(mil[i][j-1],mil[i+(1<<(j-1))][j-1]);
			mir[i][j]=min(mir[i][j-1],mir[i+(1<<(j-1))][j-1]);
		}
	}
	fz1(i,n) solve(i);
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 68480