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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.*;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.IntFunction;

public class Main {

    static int N, D;
    static int[] A;

    public static void main(String[] args) {
        FastScanner sc = new FastScanner(System.in);
        N = sc.nextInt();
        D = sc.nextInt();
        A = sc.nextIntArray(N);

        System.out.println(solve());
    }

    static City INF = new City(-1, Long.MAX_VALUE);

    static long solve() {
        // 解説のやりかた
        // 各都市の左右一つを選んでクラスカル法
        BinaryOperator<City> merge = (a, b) -> a.cost < b.cost ? a : b;
        RangeGetObj<City> lseg = new RangeGetObj<>(N, INF, merge, City[]::new);
        for (int i = 0; i < N; i++) {
            long cost = D * (N-i-1) + A[i];
            lseg.update(i, new City(i, cost));
        }
        RangeGetObj<City> rseg = new RangeGetObj<>(N, INF, merge, City[]::new);
        for (int i = 0; i < N; i++) {
            long cost = D * i + A[i];
            rseg.update(i, new City(i, cost));
        }

        PriorityQueue<Road> q = new PriorityQueue<>(Comparator.comparingLong(r -> r.cost));

        // Aが大きいもの順に実行
        //   Aより小さいもの、という制限がついている
        int[][] B = new int[N][2];
        for (int i = 0; i < N; i++) {
            B[i][0] = i;
            B[i][1] = A[i];
        }
        Arrays.sort(B, Comparator.comparingInt(b -> -b[1]));
        for (int bi = 0; bi < N; bi++) {
            int i = B[bi][0];
            int a = B[bi][1];

            City lc = lseg.query(0, i);
            if( lc != INF ) {
                q.add( new Road(i, lc.i, cost(i, lc.i)) );
            }
            City rc = rseg.query(i+1, N);
            if( rc != INF ) {
                q.add( new Road(i, rc.i, cost(i, rc.i)) );
            }

            lseg.update(i, INF);
            rseg.update(i, INF);
        }

        UnionFind uf = new UnionFind(N);

        int cnt = 0;
        long ans = 0;
        while(true) {
            Road road = q.poll();
            if( !uf.isSame(road.from, road.to) ) {
                uf.unite(road.from, road.to);
                ans += road.cost;
                cnt++;

                if( cnt == N-1 ) {
                    break;
                }
            }
        }
        return ans;
    }

    static Road cheapest(int i, RangeGetObj<City> lseg, RangeGetObj<City> rseg) {
        City lCity = lseg.query(0, i);
        City rCity = rseg.query(i+1, N);

        Road lRoad = lCity == INF ? null : new Road(i, lCity.i, cost(i, lCity.i));
        Road rRoad = rCity == INF ? null : new Road(i, rCity.i, cost(i, rCity.i));

        if( lRoad != null && rRoad != null ) {
            return lRoad.cost < rRoad.cost ? lRoad : rRoad;
        } else if( lRoad != null ) {
            return lRoad;
        } else {
            return rRoad;
        }
    }

    static long cost(int a, int b) {
        return (long)A[a] + A[b] + (long)Math.abs(a-b) * D;
    }

    static class City {
        int i;
        long cost;

        public City(int i, long cost) {
            this.i = i;
            this.cost = cost;
        }

        public String toString() {
            return "City(" + i + ", " + cost + ")";
        }
    }

    static class Road {
        int from, to;
        long cost;

        public Road(int from, int to, long cost) {
            this.from = from;
            this.to = to;
            this.cost = cost;
        }
    }

    static class RangeGetObj<A> {

        int size;
        A[] tree;
        A identity;
        BinaryOperator<A> merge;

        RangeGetObj(int size, A identity, BinaryOperator<A> merge, IntFunction<A[]> mkArr) {
            this.size = 1;
            while( this.size < size ) this.size *= 2;
            this.tree = mkArr.apply(this.size*2);
            this.identity = identity;
            this.merge = merge;
            Arrays.fill(tree, identity);
        }

        void update(int idx, A a) {
            idx += size;
            tree[idx] = a;

            while(idx > 0) {
                idx /= 2;
                tree[idx] = merge.apply(tree[idx*2], tree[idx*2+1]);
            }
        }

        // [from, to)
        A query(int from, int to) {
            return _query(from, to, 1, 0, size);
        }

        private A _query(int from, int to, int idx, int l, int r) {
            if (r <= from || to <= l) return identity;

            if (from <= l && r <= to) {
                return tree[idx];

            } else {
                int mid = (l+r)/2;
                A vl = _query(from, to, idx*2, l, mid);
                A vr = _query(from, to, idx*2+1, mid, r);
                return merge.apply(vl, vr);
            }
        }

        A get(int idx) {
            return tree[idx+size];
        }
    }

    static class UnionFind {

        private final int[] parent;
        private final int[] rank;

        public UnionFind(int n) {
            parent = new int[n];
            rank = new int[n];
            for (int i = 0; i < n; i++) {
                parent[i] = i;
                rank[i] = 0;
            }
        }

        public int root(int i) {
            if( parent[i] == i ) {
                return i;
            } else {
                return parent[i] = root(parent[i]);
            }
        }

        public int unite(int i, int j) {
            int ri = root(i);
            int rj = root(j);
            if( ri == rj ) return ri;

            if( rank[ri] < rank[rj] ) {
                parent[ri] = rj;
                return rj;

            } else {
                parent[rj] = ri;
                if( rank[ri] == rank[rj] ) {
                    rank[ri]++;
                }
                return ri;
            }
        }

        public boolean isSame(int a, int b) {
            return root(a) == root(b);
        }
    }

    @SuppressWarnings("unused")
    static class FastScanner {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        FastScanner(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
            tokenizer = null;
        }

        String next() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        String nextLine() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    return reader.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken("\n");
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        int[] nextIntArray(int n, int delta) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt() + delta;
            return a;
        }

        long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }
    }

    static <A> void writeLines(A[] as, Function<A, String> f) {
        PrintWriter pw = new PrintWriter(System.out);
        for (A a : as) {
            pw.println(f.apply(a));
        }
        pw.flush();
    }

    static void writeLines(int[] as) {
        PrintWriter pw = new PrintWriter(System.out);
        for (int a : as) pw.println(a);
        pw.flush();
    }

    static void writeLines(long[] as) {
        PrintWriter pw = new PrintWriter(System.out);
        for (long a : as) pw.println(a);
        pw.flush();
    }

    static int max(int... as) {
        int max = Integer.MIN_VALUE;
        for (int a : as) max = Math.max(a, max);
        return max;
    }

    static int min(int... as) {
        int min = Integer.MAX_VALUE;
        for (int a : as) min = Math.min(a, min);
        return min;
    }

    static void debug(Object... args) {
        StringJoiner j = new StringJoiner(" ");
        for (Object arg : args) {
            if (arg instanceof int[]) j.add(Arrays.toString((int[]) arg));
            else if (arg instanceof long[]) j.add(Arrays.toString((long[]) arg));
            else if (arg instanceof double[]) j.add(Arrays.toString((double[]) arg));
            else if (arg instanceof Object[]) j.add(Arrays.toString((Object[]) arg));
            else j.add(arg.toString());
        }
        System.err.println(j.toString());
    }
}