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: Accepted)

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#define lc (o<<1)
#define rc ((o<<1)|1)
#define mid ((l+r)>>1)
using namespace std;
const int N=200010;
int n,D,x,a[N];
long long f[N][2][20],ans;
struct tree{int tag; long long l,r,mn,del;} T[N<<2];
set<int> s;
int read() {
    int tmp=0, fh=1; char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') fh=-1; c=getchar();}
    while (c>='0'&&c<='9') tmp=tmp*10+c-48, c=getchar();
    return tmp*fh;
}
inline long long qmin(int l,int r,int t) {
    int k=log2(r-l+1);
    return min(f[l][t][k],f[r-(1<<k)+1][t][k]);
}
void build(int o,int l,int r) {
    T[o].tag=2; T[o].del=0;
    if (l<r) {
        build(lc,l,mid); build(rc,mid+1,r);
        T[o].mn=min(T[lc].mn,T[rc].mn);
        T[o].l=T[lc].l; T[o].r=T[rc].r;
    } else T[o].mn=T[o].l=T[o].r=f[l][1][0]+a[0];
}
void change_r(int o,int l,int r,int t,long long d) {
	//printf("change_r %d %d %lld %lld %lld\n",l,r,d,f[r][t][0],T[o].r);
    if (T[o].r<1e17&&T[o].r>f[r][t][0]+d) {
        T[o].tag=t; T[o].del=d; T[o].mn=qmin(l,r,t)+d;
        //printf("set_r %d %d %d %lld\n",l,r,T[o].tag,T[o].mn);
        T[o].l=f[l][t][0]+d; T[o].r=f[r][t][0]+d;
        return;
    }
    if (l==r) return;
	if (T[o].tag!=2) {
        if (T[lc].tag!=-1) {
            T[lc].tag=T[o].tag; T[lc].del=T[o].del;
            T[lc].mn=qmin(l,mid,T[o].tag)+T[o].del;
            T[lc].l=f[l][T[o].tag][0]+T[o].del; T[lc].r=f[mid][T[o].tag][0]+T[o].del;
        }
        if (T[rc].tag!=-1) {
            T[rc].tag=T[o].tag; T[rc].del=T[o].del;
            T[rc].mn=qmin(mid+1,r,T[o].tag)+T[o].del;
            T[rc].l=f[mid+1][T[o].tag][0]+T[o].del; T[rc].r=f[r][T[o].tag][0]+T[o].del;
        }
        T[o].tag=2; T[o].del=0;
    }
	change_r(lc,l,mid,t,d);
    if (T[rc].l<1e17&&T[rc].l>f[mid+1][t][0]+d) change_r(rc,mid+1,r,t,d);
    T[o].mn=min(T[lc].mn,T[rc].mn);
    T[o].l=T[lc].l; T[o].r=T[rc].r;
}
void change_l(int o,int l,int r,int t,long long d) {
	//printf("change_l %d %d %lld %lld\n",l,r,d,f[r][t][0]);
    if (T[o].l<1e17&&T[o].l>f[l][t][0]+d) {
        T[o].tag=t; T[o].del=d; T[o].mn=qmin(l,r,t)+d;
        //printf("set_l %d %d %d %lld\n",l,r,T[o].tag,T[o].mn);
        T[o].l=f[l][t][0]+d; T[o].r=f[r][t][0]+d;
        return;
    }
    if (l==r) return;
    if (T[o].tag!=2) {
        if (T[lc].tag!=-1) {
            T[lc].tag=T[o].tag; T[lc].del=T[o].del;
            T[lc].mn=qmin(l,mid,T[o].tag)+T[o].del;
            T[lc].l=f[l][T[o].tag][0]+T[o].del; T[lc].r=f[mid][T[o].tag][0]+T[o].del;
        }
        if (T[rc].tag!=-1) {
            T[rc].tag=T[o].tag; T[rc].del=T[o].del;
            T[rc].mn=qmin(mid+1,r,T[o].tag)+T[o].del;
            T[rc].l=f[mid+1][T[o].tag][0]+T[o].del; T[rc].r=f[r][T[o].tag][0]+T[o].del;
        }
        T[o].tag=2; T[o].del=0;
    }
	change_l(rc,mid+1,r,t,d);
    if (T[lc].r<1e17&&T[lc].r>f[mid][t][0]+d) change_l(lc,l,mid,t,d);
    T[o].mn=min(T[lc].mn,T[rc].mn);
    T[o].l=T[lc].l; T[o].r=T[rc].r;
}
int walk(int o,int l,int r) {
	//printf("walk %d %d %d %lld %d %lld\n",o,l,r,T[o].mn,T[o].tag,T[o].del);
    if (l==r) {ans+=T[o].mn; T[o].tag=-1; T[o].mn=T[o].l=T[o].r=1e17; return l;}
    if (T[o].tag!=2) {
        if (T[lc].tag!=-1) {
            T[lc].tag=T[o].tag; T[lc].del=T[o].del;
            T[lc].mn=qmin(l,mid,T[o].tag)+T[o].del;
            T[lc].l=f[l][T[o].tag][0]+T[o].del; T[lc].r=f[mid][T[o].tag][0]+T[o].del;
        }
        if (T[rc].tag!=-1) {
            T[rc].tag=T[o].tag; T[rc].del=T[o].del;
            T[rc].mn=qmin(mid+1,r,T[o].tag)+T[o].del;
            T[rc].l=f[mid+1][T[o].tag][0]+T[o].del; T[rc].r=f[r][T[o].tag][0]+T[o].del;
        }
        T[o].tag=2; T[o].del=0;
    }
    //printf("   %lld %lld %lld\n",T[o].mn,T[lc].mn,T[rc].mn);
    int pos;
    if (T[lc].mn==T[o].mn) {
        pos=walk(lc,l,mid);
        if (T[rc].l<1e17&&T[rc].l>a[pos]+a[mid+1]+(mid+1ll-pos)*D) change_r(rc,mid+1,r,1,a[pos]-1ll*pos*D);
    } else {
        pos=walk(rc,mid+1,r);
        if (T[lc].r<1e17&&T[lc].r>a[pos]+a[mid]+1ll*(pos-mid)*D) change_l(lc,l,mid,0,a[pos]+1ll*pos*D);
    }
    T[o].mn=min(T[lc].mn,T[rc].mn);
    T[o].l=T[lc].l; T[o].r=T[rc].r;
    return pos;
}
int main() {
    n=read(); D=read();
    for (int i=0;i<n;i++) a[i]=read(), f[i][0][0]=a[i]-1ll*i*D, f[i][1][0]=a[i]+1ll*i*D;
    //for (int i=0;i<n;i++) a[i]=rand()%100+1, printf("%d ",a[i]), f[i][0][0]=a[i]-1ll*i*D, f[i][1][0]=a[i]+1ll*i*D; puts("");
    for (int j=1;(1<<j)<=n;j++)
    for (int i=0;i+(1<<j)<=n;i++)
    f[i][0][j]=min(f[i][0][j-1],f[i+(1<<(j-1))][0][j-1]),
    f[i][1][j]=min(f[i][1][j-1],f[i+(1<<(j-1))][1][j-1]);
    build(1,1,n-1);
    //for (int i=1;i<n;i++) printf("%d ",walk(1,1,n-1)), printf("%lld %lld\n",ans,T[1].mn);
    for (int i=1;i<n;i++) walk(1,1,n-1);
    //printf("%d\n",s.size());
    printf("%lld\n",ans);
    return 0;
}