#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[1000000];

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;
cout << sum2-sum1-tax;
return 0;
}
