#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <memory>
#include <complex>
#include <numeric>
#include <cstdio>
#include <iomanip>

#define REP(i,m,n) for(int i=int(m);i<int(n);i++)
#define RREP(i,m,n) for(int i=int(n)-1;i>=int(m);--i)
#define EACH(i,c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x)  cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s) do{}while(0)
#define dump(x) do{}while(0)
#define BR 
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long int;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VS = vector<string>;
using PII = pair<int,int>;
using VP = vector<PII>;

//struct edge {int from, to, cost;};

constexpr double EPS = 1e-10;
//constexpr double PI  = acos(-1.0);
//constexpr int INF = INT_MAX;
constexpr int MOD = 1'000'000'007;

template<class T> inline T sqr(T x) {return x*x;}

void calcPos(VI &lpos, VI &rpos, VLL &lcost, VLL &rcost, VP &b, int &k) {
    int n = b.size();
    vector<bool> ink(n, false);
    REP(i,0,k) ink[b[i].second] = true;
    if (ink[0]) lpos[0] = -1;
    else lpos[0] = 0;
    if (ink[n-1]) rpos[n-1] = n;
    else rpos[n-1] = n - 1;
    REP(i,1,n) {
        /*
        if (lpos[i-1] == -1)  {
            if (ink[i]) lpos[i] = -1;
            else lpos[i] = i;
        } else if (lcost[i] < lcost[lpos[i-1]]) lpos[i] = i;
        else lpos[i] = lpos[i-1];
        */
        if (ink[i]) lpos[i] = lpos[i-1];
        else if (lpos[i-1] == -1) lpos[i] = i;
        else if (lcost[i] < lcost[lpos[i-1]]) lpos[i] = i;
        else lpos[i] = lpos[i-1];
    }
    RREP(i,0,n-1) {
        /*
        if (rpos[i+1] == n) {
            if (ink[i]) rpos[i] = n;
            else rpos[i] = i;
        } else if (rcost[i] < rcost[rpos[i+1]]) rpos[i] = i;
        else rpos[i] = rpos[i+1];   
        */
        ///*
        if (ink[i]) rpos[i] = rpos[i+1];
        else if (rpos[i+1] == n) rpos[i] = i;
        else if (rcost[i] < rcost[rpos[i+1]]) rpos[i] = i;
        else rpos[i] = rpos[i+1];
        //*/
    }    
}

LL f(VI &a, VP &b, int &i, LL &sum, LL &d, LL &n, VLL &lcost, VLL &rcost) {
    LL ksum = sum;
    VI lpos(n, -1), rpos(n, n);
    calcPos(lpos, rpos, lcost, rcost, b, i);
    REP(j,0,i) {
        int index = b[j].second;
        LL v = b[j].first;
        if (index == 0 || index == n - 1) continue;
        ksum -= 2 * v;
        LL lcost, rcost;
        if (lpos[index - 1] == -1) lcost = 1LL << 60;
        else lcost = d * (abs(index - lpos[index - 1])) + a[index] + a[lpos[index - 1]];
        if (rpos[index + 1] == n) rcost = 1LL << 60;
        else rcost = d * (abs(index - rpos[index + 1])) + a[index] + a[rpos[index + 1]]; 
        ksum += min(lcost, rcost);
    }
    return ksum;
} 

void solve() {
    LL n,d;
    cin >> n >> d;
    VI a(n);
    VP b(n);
    VLL lcost(n), rcost(n);
    REP(i,0,n) {
        cin >> a[i];
        b[i] = MP(a[i], i);
        lcost[i] = a[i] + d * (n - i - 1);
        rcost[i] = a[i] + d * i;
    }
    if (n == 1) {
        cout << 0 << endl;
        return;
    } else if (n == 2) {
        cout << d + a[0] + a[1] << endl;
        return;
    }
    sort(all(b));
    reverse(all(b));
    LL asum = 0;
    REP(i,0,n) asum += a[i];
    LL sum = asum * 2 - a[0] - a[n-1] + (n - 1) * d;
    int L = 0,  R = n - 2;
    int cnt = 0;
    while (cnt++ < 15 && R - L > 1) {
        int m1 = (R-L)/3 + L, m2 = (R-L)*2/3 + L;
        //dump(m1);
        //dump(m2);
        LL m1v = f(a,b,m1,sum,d,n,lcost,rcost);
        LL m2v = f(a,b,m2,sum,d,n,lcost,rcost);
        //dump(m1v);
        //dump(m2v);
        if (m1v > m2v) L = m1;
        else R = m2;
    }
    //dump(L);
    //dump(R);
    LL ans = f(a,b,L,sum,d,n,lcost,rcost);
    REP(i,L+1-5,R+1+5) {
        if (i < 0 || i >= n - 1) continue;
        LL tmp = f(a,b,i,sum,d,n,lcost,rcost);
        if (ans > tmp) {
            //dump(i);
            //dump(tmp);
            ans = tmp;
        }
    }
    cout << ans << endl;
}

int main() {
    solve();
    
    return 0;
}