#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int s, n, a;
int  ans=0;
vector<int> arr;
///vector<int> arr2;
int indexof[10000000];

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> s;
for(int i=0; i<n; ++i)
{
    cin >> a;
    indexof[a] = i;
    arr.push_back(a);
}
sort(arr.begin(), arr.end());
int sum1=0, sum2=0;
int tax=0, t=0;
for(int i=0; i<n/2; ++i)
{


    t= abs(indexof[arr[n-1-i]]-indexof[arr[i]]);
    if(arr[n-1-i]-arr[i]-t*s > 0)
    {
        sum1+=arr[i];
        sum2+=arr[n-1-i];
        tax+=t;
    }
}
tax*=s;
ans = sum2-sum1-tax;
for(int i=0; i<n-1; ++i)
{
    for(int j=i+1; j<n; ++j)
    {
        t= abs(indexof[arr[j]]-indexof[arr[i]]);
        if(arr[j]-arr[i] - t*s < 0)
        {
            ans += abs(arr[j]-arr[i] - t*s);
        }
    }
}
cout << ans;
return 0;
}
