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 (GCC 5.4.1) (judge verdict: Accepted)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.h>

typedef struct binaryHeap{
  void *h;
  size_t heapSize;
  size_t maxSize;
  size_t size;
  int (*cmp)(const void *,const void *);
} heap;

heap* newHeap(const size_t size,int (*cmpF)(const void *,const void *)){
  const size_t maxSize=1;
  heap *res=(heap *)malloc(sizeof(heap));
  res->h=malloc(size*(maxSize+1));
  res->heapSize=0;
  res->maxSize=maxSize;
  res->size=size;
  res->cmp=cmpF;
  return res;
}

int isEmpty(const heap *h){
  return h->heapSize==0;
}

void freeHeap(heap *h){
  free(h->h);
  free(h);
  return;
}

void initHeap(heap *h){
  h->heapSize=0;
  return;
}

static inline void heapFunc_swap(void * restrict a,void * restrict b,void * restrict tmp,const size_t size){
  if((size&7)==0){
    uint64_t *pa=a;
    uint64_t *pb=b;
    for(size_t k=0;k<size/sizeof(uint64_t);k++){
      uint64_t t=*pa;
      *pa=*pb;
      *pb=t;
      pa++;
      pb++;
    }
  } else if((size&3)==0){
    uint32_t *pa=a;
    uint32_t *pb=b;
    for(size_t k=0;k<size/sizeof(uint32_t);k++){
      uint32_t t=*pa;
      *pa=*pb;
      *pb=t;
      pa++;
      pb++;
    }
  } else {
    memcpy(tmp,a,size);
    memcpy(a,b,size);
    memcpy(b,tmp,size);
  }
  return;
}

void push(heap *q,const void *p){
  if(q->heapSize==q->maxSize){
    q->maxSize*=2;
    q->h=realloc(q->h,q->size*(q->maxSize+1));
  }
  q->heapSize+=1;
  char *h=(char *)(q->h);
  int k=q->heapSize;
  const size_t size=q->size;
  int (*cmp)(const void *,const void *)=q->cmp;
  memcpy(h+k*size,p,size);
  while(k>1){
    size_t parent=k/2;
    if(cmp(h+parent*size,h+k*size)<=0) return;
    heapFunc_swap(h+parent*size,h+k*size,h,size);
    k=parent;
  }
  return;
}

void pop(heap *q,void *res){
  char *h=(char *)(q->h);
  const size_t size=q->size;
  if(res!=NULL) memcpy(res,h+size,size);
  memcpy(h+size,h+size*q->heapSize,size);
  q->heapSize-=1;
  int (*cmp)(const void *,const void *)=q->cmp;
  const size_t n=q->heapSize;
  size_t k=1;
  while(2*k+1<=n){
    size_t next=cmp(h+2*k*size,h+(2*k+1)*size)<=0?2*k:2*k+1;
    if(cmp(h+k*size,h+next*size)<=0) return;
    heapFunc_swap(h+k*size,h+next*size,h,size);
    k=next;
  }
  if(2*k<=n && cmp(h+k*size,h+2*k*size)>0) heapFunc_swap(h+k*size,h+2*k*size,h,size);
  return;
}

void top(const heap *q,void *res){
  memcpy(res,(char *)q->h+q->size,q->size);
  return;
}

typedef struct UnionFind{
  int *parent;
  int n;
} UnionFind;

UnionFind* newUnionFind(int n){
  UnionFind *u=(UnionFind *)calloc(1,sizeof(UnionFind));
  u->parent=(int *)calloc(n,sizeof(int));
  u->n=n;
  for(int i=0;i<n;i++) u->parent[i]=-1;
  return u;
}

int root(UnionFind *u,int x){
  if(u->parent[x]<0) return x;
  return u->parent[x]=root(u,u->parent[x]);
}

int same(UnionFind *u,int x,int y){
  return root(u,x)==root(u,y);
}

int getSize(UnionFind *u,int x){
  return -u->parent[root(u,x)];
}

void unite(UnionFind *u,int x,int y){
  x=root(u,x);
  y=root(u,y);
  if(x==y) return;
  if(u->parent[x]>u->parent[y]){
    int swap=x;
    x=y;
    y=swap;
  }
  u->parent[x]+=u->parent[y];
  u->parent[y]=x;
}

typedef long long int int64;

typedef struct edge{
  int a,b;
  int64 c;
} edge;

int cmpEdge(const void *a,const void *b){
  int64 p=((edge *)a)->c;
  int64 q=((edge *)b)->c;
  return p==q?0:p<q?-1:1;
}

void enumerate(int *a,int l,int r,int d,heap *h){
  if(r-l<=1) return;
  int m=(l+r)/2;
  enumerate(a,l,m,d,h);
  enumerate(a,m,r,d,h);
  int64 cost=a[l]+d;
  int index=l;
  int i;
  for(i=l+1;i<m;i++){
    if(cost>a[i]){
      cost=a[i];
      index=i;
    }
    cost+=d;
  }
  for(i=m;i<r;i++){
    push(h,&((edge){index,i,cost+a[i]}));
    cost+=d;
  }
  cost=a[r-1]+d;
  index=r-1;
  for(i=r-2;i>=m;i--){
    if(cost>a[i]){
      cost=a[i];
      index=i;
    }
    cost+=d;
  }
  for(i=m-1;i>=l;i--){
    push(h,&((edge){index,i,cost+a[i]}));
    cost+=d;
  }
}

void run(void){
  int n,d;
  scanf("%d%d",&n,&d);
  int *a=(int *)calloc(n,sizeof(int));
  int i;
  for(i=0;i<n;i++) scanf("%d",a+i);
  heap *h=newHeap(sizeof(edge),cmpEdge);
  enumerate(a,0,n,d,h);
  int64 ans=0;
  UnionFind *u=newUnionFind(n);
  int cnt=0;
  while(cnt<n-1){
    edge e;
    pop(h,&e);
    if(same(u,e.a,e.b)) continue;
    ans+=e.c;
    unite(u,e.a,e.b);
    cnt++;
  }
  printf("%lld\n",ans);
}

int main(void){
  run();
  return 0;
}