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: Time Limit Exceeded)

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

/**
 * Union-Find tree.
 * Verified by yukicoder No.94 (http://yukicoder.me/submissions/82111)
 */
struct UnionFind { disj: Vec<usize> }

impl UnionFind {
    fn new(n: usize) -> Self {
        let mut disj = vec![0; n];
        for i in 0 .. n {
            disj[i] = i;
        }
        UnionFind { disj: disj }
    }
    fn root(self: &mut Self, x: usize) -> usize {
        if x != self.disj[x] {
            let par = self.disj[x];
            let r = self.root(par);
            self.disj[x] = r;
        }
        return self.disj[x];
    }
    fn unite(self: &mut Self, x: usize, y: usize) {
        let xr = self.root(x);
        let yr = self.root(y);
        self.disj[xr] = yr;
    }
    fn is_same_set(self: &mut Self, x: usize, y: usize) -> bool {
        return self.root(x) == self.root(y);
    }
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (write!(out,$($format)*).unwrap());
    }
    input! {
        n: usize,
        d: i64,
        a: [i64; n],
    }
    let mut tot = 0;
    let mut conn = n;
    let mut uf = UnionFind::new(n);
    let mut lft = vec![0; n];
    let mut rgt = vec![0; n];
    for i in 0 .. n {
        lft[i] = a[i] - i as i64 * d;
        rgt[i] = a[i] + i as i64 * d;
    }
    const INF: i64 = 1 << 50;
    while conn > 1 {
        let mut mi = vec![(INF, n * n); n];
        let mut col = vec![0; n];
        for i in 0 .. n { col[i] = uf.root(i); }
        let mut pool: Vec<(i64, usize)> = Vec::new();
        for i in 0 .. n {
            if i > 0 {
                let mut tmp = (INF, n * n);
                if col[i] != col[pool[0].1] {
                    tmp = (pool[0].0 + rgt[i], pool[0].1 * n + i);
                } else if pool.len() >= 2 {
                    tmp = (pool[1].0 + rgt[i], pool[1].1 * n + i);
                }
                mi[col[i]] = min(mi[col[i]], tmp);
            }
            pool.push((lft[i], i));
            pool.sort();
            if pool.len() >= 3 {
                pool.pop();
            }
        }
        pool.clear();
        for i in (0 .. n).rev() {
            if i + 1 < n {
                let mut tmp = (INF, n * n);
                if col[i] != col[pool[0].1] {
                    tmp = (pool[0].0 + lft[i], pool[0].1 + n * i);
                } else if pool.len() >= 2 {
                    tmp = (pool[1].0 + lft[i], pool[1].1 + n * i);
                }
                mi[col[i]] = min(mi[col[i]], tmp);
            }
            pool.push((rgt[i], i));
            pool.sort();
            if pool.len() >= 3 {
                pool.pop();
            }
        }
        for i in 0 .. n {
            if mi[i].0 < INF {
                let x = mi[i].1 / n;
                let y = mi[i].1 % n;
                if !uf.is_same_set(x, y) {
                    uf.unite(x, y);
                    tot += mi[i].0;
                    conn -= 1;
                }
            }
        }
    }
    puts!("{}\n", tot);
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}