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 Rust (1.15.1) (judge verdict: Accepted)

//---------- begin union_find ----------
#[allow(dead_code)]
mod union_find {
    pub struct UF {
        p: Vec<i32>,
        stack: Vec<usize>
    }
    impl UF {
        pub fn new(n: usize) -> UF {
            UF {
                p: vec![-1; n],
                stack: vec![],
            }
        }
        pub fn init(&mut self) {
            for p in self.p.iter_mut() {
                *p = -1;
            }
        }
        pub fn root(&mut self, mut x: usize) -> usize {
            let stack = &mut self.stack;
            while self.p[x] >= 0 {
                stack.push(x);
                x = self.p[x] as usize;
            }
            while let Some(v) = stack.pop() {
                self.p[v] = x as i32;
            }
            x
        }
        pub fn same(&mut self, x: usize, y: usize) -> bool {
            self.root(x) == self.root(y)
        }
        pub fn unite(&mut self, mut x: usize, mut y: usize) -> Option<(usize, usize)> {
            x = self.root(x);
            y = self.root(y);
            if x == y {
                return None;
            }
            if self.p[x] > self.p[y] {
                let s = x;
                x = y;
                y = s;
            }
            self.p[x] += self.p[y];
            self.p[y] = x as i32;
            Some((x, y))
        }
        pub fn get_size(&mut self, x: usize) -> usize {
            let r = self.root(x);
            (-self.p[r]) as usize
        }
    }
}
//---------- end union_find ----------

use std::io::Read;

fn run() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let n: usize = it.next().unwrap().parse().unwrap();
    let d: u64 = it.next().unwrap().parse().unwrap();
    let a: Vec<u64> = (0..n).map(|_| it.next().unwrap().parse().unwrap()).collect();
    let mut e = vec![];
    let mut stack = vec![(0, n)];
    while let Some((l, r)) = stack.pop() {
        if l + 1 == r {
            continue;
        }
        let m = (l + r) / 2;
        stack.push((l, m));
        stack.push((m, r));
        let mut local = (a[l], l);
        for i in (l + 1)..m {
            local.0 += d;
            if local.0 > a[i] {
                local = (a[i], i);
            }
        }
        for i in m..r {
            local.0 += d;
            e.push((local.1, i, local.0 + a[i]));
        }
        let mut local = (a[r - 1], r - 1);
        for i in (m..(r - 1)).rev() {
            local.0 += d;
            if local.0 > a[i] {
                local = (a[i], i);
            }
        }
        for i in (l..m).rev() {
            local.0 += d;
            e.push((i, local.1, local.0 + a[i]));
        }
    }
    e.sort_by_key(|e| e.2);
    let mut u = union_find::UF::new(n);
    let mut ans = 0;
    for (a, b, c) in e {
        if u.unite(a, b).is_some() {
            ans += c;
        }
    }
    println!("{}", ans);
}

fn main() {
    run();
}