# 110. 数蝌蚪

n, k = map(int, input().split())
L = [int(i) for i in input().split()]
for i in range(n):
L[i] -= k*i
L.sort()
if n%2:
target = L[n//2]
else:
target = (L[n//2]+L[n//2-1])//2
if target < 0:
target = 0
res = 0
for i in range(n):
res += abs(L[i]-target)
print(res)


#include <array>
#include <cstdint>
#include <iostream>
int main() {
uint32_t n, k; std::cin >> n >> k;
for (uint32_t i = 0; i < n; ++i)
uint32_t lo = 0, hi = 10001;
while (lo != hi) {
uint32_t mid = lo + (hi - lo) / 2, standard = mid;
uint32_t lower = 0, higher = 0, exact = 0;
for (uint32_t i = 0; i < n; ++i, standard += k)
++exact;
++lower;
else
++higher;
if (exact + higher < lower) {
hi = mid;
} else if (exact + lower < higher) {
lo = mid + 1;
} else {
lo = hi = mid;
break;
}
}
uint64_t cost = 0;
auto standard = lo;
for (u32 i = 0; i < n; ++i, standard += k)
else
std::cout << cost << '\n';
}


#include<cstdio>
#include<cctype>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;

long long sub[300001];
int main()
{
#ifdef debug
freopen("testdata.in", "r", stdin);
freopen("testdata.out", "w", stdout);
#endif
int n, k, b;
long long result = 0, a = 0;
scanf("%d%d", &n, &k);
for(int i = 0; i < n; i++)
{
scanf("%d", &b);
sub[i] = b - a;
a += k;
}
sort(sub, sub + n);
if(sub[n/2] <= 0)
{
int temp = sub[n/2];
for(int i = 0; i < n; i++) sub[i] += temp;
}
for(int i = 0; i < n; i++) result += sub[i];
printf("%lld\n", result);
return 0;
}


### include

using namespace std;
int main() {
long long n,k;
cin>>n>>k;
long long b[n];
long long sum=0,a,p;
for(int i = 0; i < n; i++) {
cin>>a;
b[i]=a-ik;
}
sort(b,b+n);
long long mid;
if(n&1)
mid=b[n/2];
else
mid=(b[n/2]+b[n/2-1])/2;
if(mid<0)
mid=0;
// for(int i =0; i < n; i++) {
// cin>>a;
// if(i==0)
// p=a;
// b[i]=i
k+p-a;//
// }
// sort(b,b+n);
// long long mid;
// if(b[n/2]<p) {
// mid=b[n/2];
// } else {
// mid=p;
// }
for(int i =0; i < n; i++) {
sum+=abs(b[i]-mid);
}
cout<<sum<<endl;
return 0;
}