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)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($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)*}
    };
}

#[allow(unused_macros)]
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)*}
    };

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

#[allow(unused_macros)]
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, bytes) => {
        read_value!($next, String).into_bytes()
    };

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

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

// int BIT[1000], a[1000], n;
// void update(int x, int delta)
// {
//       for(; x <= n; x += x&-x)
//         BIT[x] += delta;
// }
// int query(int x)
// {
//      int sum = 0;
//      for(; x > 0; x -= x&-x)
//         sum += BIT[x];
//      return sum;
// }

use std::ops::AddAssign;

struct BinaryIndexedTree<T> {
    v: Vec<T>,
}

impl<T: Clone + Copy + From<i64> + AddAssign> BinaryIndexedTree<T> {
    fn new(n: usize) -> BinaryIndexedTree<T> {
        BinaryIndexedTree {
            v: vec![0.into(); n],
        }
    }

    fn update(&mut self, x: usize, delta: T) {
        let mut x = x;
        while x < self.v.len() {
            self.v[x] += delta;
            x += x & (!x + 1);
        }
    }

    fn query(&self, x: usize) -> T {
        let mut x = x;
        let mut sum = 0.into();
        while x > 0 {
            sum += self.v[x];
            x -= x & (!x + 1);
        }
        sum
    }
}

use std::cmp::min;

trait Bounded {
    fn min_value() -> Self;
    fn max_value() -> Self;
}

impl Bounded for i64 {
    fn min_value() -> Self {
        Self::max_value()
    }
    fn max_value() -> Self {
        Self::max_value()
    }
}

#[derive(Debug)]
enum SegmentTree<T> {
    Leaf {
        index: usize,
        value: T,
    },
    Node {
        range: (usize, usize),
        value: (T, usize),
        l: Box<SegmentTree<T>>,
        r: Box<SegmentTree<T>>,
    },
}

impl<T: From<i64> + Ord + Bounded + Clone + Copy> SegmentTree<T> {
    fn new(n: usize) -> SegmentTree<T> {
        Self::new_rec(0, n)
    }

    fn new_rec(start: usize, end: usize) -> SegmentTree<T> {
        if end - start == 1 {
            SegmentTree::Leaf {
                index: start,
                value: T::max_value(),
            }
        } else {
            let m = (start + end) / 2;
            SegmentTree::Node {
                range: (start, end),
                value: (T::max_value(), start),
                l: Box::new(Self::new_rec(start, m)),
                r: Box::new(Self::new_rec(m, end)),
            }
        }
    }

    fn update(&mut self, i: usize, v: T) {
        match self {
            &mut SegmentTree::Leaf { ref mut value, .. } => {
                *value = min(*value, v);
            }
            &mut SegmentTree::Node {
                range,
                ref mut value,
                ref mut l,
                ref mut r,
            } => {
                let m = (range.0 + range.1) / 2;
                if i < m {
                    l.update(i, v);
                } else {
                    r.update(i, v);
                }
                *value = min(*value, min(l.value(), r.value()));
            }
        }
    }

    fn minimum(&self, start: usize, end: usize) -> (T, usize) {
        match self {
            &SegmentTree::Leaf { index, value } => {
                if start <= index && index < end {
                    (value, index)
                } else {
                    (T::max_value(), start)
                }
            }
            &SegmentTree::Node {
                range,
                value,
                ref l,
                ref r,
            } => {
                if start <= range.0 && range.1 <= end {
                    value
                } else if end <= range.0 || range.1 <= start {
                    (T::max_value(), start)
                } else {
                    min(l.minimum(start, end), r.minimum(start, end))
                }
            }
        }
    }

    fn value(&self) -> (T, usize) {
        match self {
            &SegmentTree::Leaf { index, value } => (value, index),
            &SegmentTree::Node { value, .. } => value,
        }
    }
}

struct UnionFind {
    parent: Vec<usize>,
}

impl UnionFind {
    fn new(n: usize) -> UnionFind {
        UnionFind {
            parent: (0..n).collect(),
        }
    }

    fn union(&mut self, i: usize, j: usize) {
        let pi = self.find(i);
        let pj = self.find(j);
        self.parent[pi] = pj;
    }

    fn find(&mut self, i: usize) -> usize {
        if self.parent[i] == i {
            i
        } else {
            let p = self.parent[i];
            let ret = self.find(p);
            self.parent[i] = ret;
            ret
        }
    }
}

fn main() {
    input! {
        n: usize,
        d: i64,
        a: [i64; n],
    }

    let mut es = vec![];

    let mut ai = a
        .iter()
        .enumerate()
        .map(|(i, v)| (*v, i))
        .collect::<Vec<_>>();
    ai.sort();

    const INF: i64 = 1_000_000_000_000_000_000;

    let mut st1 = SegmentTree::new(n);
    let mut st2 = SegmentTree::new(n);
    for i in 0..n {
        st1.update(i, INF);
        st2.update(i, INF);
    }

    for (au, u) in ai {
        let (mi, v) = st1.minimum(0, u);
        if mi != INF {
            // u->v, weight = (u-v).abs()*d + a[u] + a[v]
            // println!("{:?}", (u, v, mi));
            let w = (u - v) as i64 * d + a[u] + a[v];
            es.push((w, (u, v)));
        }

        let (mi, v) = st2.minimum(u, n);
        if mi != INF {
            // u->v, weight = (u-v).abs()*d + a[u] + a[v]
            // println!("{:?}", (u, v));
            let w = (v - u) as i64 * d + a[u] + a[v];
            es.push((w, (u, v)));
        }

        st1.update(u, au - u as i64 * d);
        st2.update(u, au + u as i64 * d);
    }
    es.sort();

    let mut uf = UnionFind::new(n);
    let mut ans = 0;

    for (w, (u, v)) in es {
        if uf.find(u) == uf.find(v) {
            continue;
        }
        ans += w;
        uf.union(u, v);
    }

    println!("{}", ans);
}