// yukicoder: No.609 Noelちゃんと星々 // 2019.5.5 bal4u #include #include #if 1 #define gc() getchar_unlocked() #else #define gc() getchar() #endif int in() // 整数の入力(負数対応) { int n = 0, c = gc(); if (c == '-') { c = gc(); do n = 10*n + (c & 0xf), c = gc(); while (c >= '0'); return -n; } do n = 10*n + (c & 0xf), c = gc(); while (c >= '0'); return n; } int selection(int *a, int sz, int n) { int i, j, p, l, r, t; l = 0, r = sz-1; while (l < r) { p = a[(l+r)/2]; i = l-1, j = r+1; while (1) { while (a[++i] < p); while (a[--j] > p); if (i > j) break; t = a[i], a[i] = a[j], a[j] = t; } if (n < i) r = j; if (n > j) l = i; } return a[n]; } int y[100005]; int main() { int i, N, t; long long ans; N = in(); if (N == 1) { puts("0"); return 0; } for (i = 0; i < N; i++) y[i] = in(); t = selection(y, N, N>>1); ans = 0; for (i = 0; i < N; i++) { if (y[i] < t) ans += t - y[i]; else ans += y[i] - t; } printf("%lld\n", ans); return 0; }