#include using namespace std; typedef long long int ll; typedef double ld; typedef pair ii; typedef vector vi; typedef vector vii; #define PB push_back #define ff first #define ss second #define MP make_pair #define FOR(prom, a, b) for ( ll prom = (a); prom < (ll)b; ++prom) #define F(a) FOR(i,0,a) #define FF(a) FOR(j, 0, a) #define EPS (1e-10) #define EQ(a, b) (fabs(a-b) <= fabs(a+b)*EPS) #define LINF (1<<62LL) #define DEB(x) cerr << ">>> " << #x << " ; " << x << "\n" ll pozice[1000008]; int main() { ll n,k,stred,potreba,potreba2; potreba = 0; potreba2 = 0; cin >> n >> k; F(n) cin >> pozice[i]; sort(pozice, &pozice[n]); if(n%2==0) { stred = pozice[n/2]; F(n/2) {potreba += abs(pozice[i] - (stred - ((n/2-i) * k))); /*cerr << potreba << endl;*/} FOR(i,(n/2)+1,n) {potreba += abs(pozice[i] - ((k*(i-n/2)) + stred)); /*cerr << potreba << endl;*/} stred = pozice[n/2+1]; F(n/2+1) {potreba2 += abs(pozice[i] - (stred - ((n/2+1-i) * k))); /*cerr << potreba << endl;*/} FOR(i,(n/2+1)+1,n) {potreba2 += abs(pozice[i] - ((k*(i-n/2+1)) + stred)); /*cerr << potreba << endl;*/} cout << min(potreba,potreba2) << endl; DEB(potreba); DEB(potreba2); return 0; } stred = pozice[n/2]; F(n/2) {potreba += abs(pozice[i] - (stred - ((n/2-i) * k))); } FOR(i,(n/2)+1,n) {potreba += abs(pozice[i] - ((k*(i-n/2)) + stred)); } //F(n) cout << pozice[i] << endl; cout << potreba << endl; return 0; }