#[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);
}


Measured memory_bytes: 78076