#include using namespace std; #define EPS (1e-10) #define REP (i,n) for (int i = 0; i<(n);i++) typedef long double ld; typedef long long ll; typedef pair ii; typedef pair dd; typedef vector vi; typedef vector vll; typedef vector vd; int main() { ios::sync_with_stdio(false); int N, a; cin >> N >> a; vi V(N); for(int i = 0; i < N; ++i) cin >> V[i]; sort(V.begin(), V.end()); int lb = V[0], ub = V[N-1]; int res; while(lb <= ub) { int mid = (lb+ub)/2; ll c1 = 0, c2 = 0; for(int i = 0; i < N; ++i) { if (V[i] > mid + i*a) c1++; else if (V[i] < mid + i*a) c2++; } if (c1 == c2) { res = mid; break; } else if (c1 < c2) { ub = mid-1; } else { lb = mid+1; } } ll ss = 0; // cout << res << endl; for(int i = 0; i < N; ++i) { ss += abs(V[i] - res-i*a); } cout << ss << endl; }