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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <utility>
#define LL long long
using namespace std;
const LL Maxn = 200010;
const LL inf = 1ll<<60; ///
LL n, D, a[Maxn];
LL fa[Maxn];
bool v[Maxn];
pair <LL, LL> seg[Maxn<<2], none;
LL ff(LL x) { return fa[x] == x ? x : fa[x] = ff(fa[x]); }
vector <LL> vec[Maxn];
struct lnode {
	LL x, y, d;
	bool operator<(const lnode &A) const { return d < A.d; }
}lis[Maxn<<1]; LL ll;
pair <LL, LL> _min(pair <LL, LL> x, pair <LL, LL> y) { return x < y ? x : y; }
pair <LL, LL> qry(LL now, LL L, LL R, LL l, LL r) {
	if(l > r) return none;
	if(L == l && R == r) return seg[now];
	LL mid = L+R>>1, lc = now<<1, rc = now<<1|1;
	if(r <= mid) return qry(lc, L, mid, l, r);
	else if(l > mid) return qry(rc, mid+1, R, l, r);
	else return _min(qry(lc, L, mid, l, mid), qry(rc, mid+1, R, mid+1, r));
}
void chg(LL now, LL L, LL R, LL x, LL k) {
	if(L == R){ seg[now].first = k; return; }
	LL mid = L+R>>1, lc = now<<1, rc = now<<1|1;
	if(x <= mid) chg(lc, L, mid, x, k);
	else chg(rc, mid+1, R, x, k);
	seg[now] = _min(seg[lc], seg[rc]);
}
void build_tree1(LL now, LL L, LL R) {
	if(L < R){
		LL mid = L+R>>1, lc = now<<1, rc = now<<1|1;
		build_tree1(lc, L, mid);
		build_tree1(rc, mid+1, R);
		seg[now] = _min(seg[lc], seg[rc]);
	} else {
		seg[now].first = a[L]-L*D;
		seg[now].second = L;
	}
}
void build_tree2(LL now, LL L, LL R) {
	if(L < R){
		LL mid = L+R>>1, lc = now<<1, rc = now<<1|1;
		build_tree2(lc, L, mid);
		build_tree2(rc, mid+1, R);
		seg[now] = _min(seg[lc], seg[rc]);
	} else {
		seg[now].first = a[L]+L*D;
		seg[now].second = L;
	}
}
int main() {
	LL i, j, k;
	scanf("%lld%lld", &n, &D);
	for(i = 1; i <= n; i++) scanf("%lld", &a[i]);
	for(i = 1; i <= n; i++) fa[i] = i;
	none.first = inf; none.second = 0;
	LL sum = n, ans = 0;
	while(sum > 1){
		for(i = 1; i <= n; i++) vec[i].clear();
		for(i = 1; i <= n; i++) vec[ff(i)].push_back(i);
		ll = 0;
		build_tree1(1, 1, n);
		for(i = 1; i <= n; i++){
			if(vec[i].size() == 0) continue;
			for(j = 0; j < vec[i].size(); j++){
				LL x = vec[i][j];
				chg(1, 1, n, x, inf);
			}
			for(j = 0; j < vec[i].size(); j++){
				LL x = vec[i][j];
				pair <LL, LL> p = qry(1, 1, n, 1, x-1);
				if(p.first != inf){
					ll++;
					lis[ll].x = p.second; lis[ll].y = x; lis[ll].d = (x-p.second)*D+a[p.second]+a[x];
				}
			}
			for(j = 0; j < vec[i].size(); j++){
				LL x = vec[i][j];
				chg(1, 1, n, x, a[x]-x*D);
			}
		}
		build_tree2(1, 1, n);
		for(i = 1; i <= n; i++){
			if(vec[i].size() == 0) continue;
			for(j = 0; j < vec[i].size(); j++){
				LL x = vec[i][j];
				chg(1, 1, n, x, inf);
			}
			for(j = 0; j < vec[i].size(); j++){
				LL x = vec[i][j];
				pair <LL, LL> p = qry(1, 1, n, x+1, n);
				if(p.first != inf){
					ll++;
					lis[ll].x = p.second; lis[ll].y = x; lis[ll].d = (p.second-x)*D+a[p.second]+a[x];
				}
			}
			for(j = 0; j < vec[i].size(); j++){
				LL x = vec[i][j];
				chg(1, 1, n, x, a[x]+x*D);
			}
		}
		for(i = 1; i <= n; i++) v[i] = false;
		sort(lis+1, lis+ll+1);
		for(i = 1; i <= ll; i++){
			if(v[ff(lis[i].y)] == true) continue;
			LL fx = ff(lis[i].x), fy = ff(lis[i].y);
			v[ff(lis[i].y)] = true;
			if(fx != fy){
				fa[fx] = fy;
				ans += lis[i].d;
			}
		}
		sum = 0;
		for(i = 1; i <= n; i++) if(fa[i] == i) sum++;
	}
	printf("%lld\n", ans);
	return 0;
}