#pragma GCC optimize("O3") #pragma GCC target("avx") #include // #include #include using namespace std; #define int long long #define REP(i, n) for (int i = 0; i < (n); i++) int scan() { auto minus = false; int result = 0; while (true) { auto k = getchar_unlocked(); if (k == '-') minus = true; else if (k < '0' || k > '9') break; else result = 10 * result + k - '0'; } return result * (minus ? -1 : 1); } int get_median(int* A, int start, int end) { if (end - start <= 5) { nth_element(A + start, A + (start + end) / 2, A + end); return A[(start + end) / 2]; } int l = (end - start) / 5; int ls[l]; for (int i = 0; i < l; i++) ls[i] = get_median(A, start + i * 5, start + (i + 1) * 5); return get_median(ls, 0, l); } int Y[1000010]; signed main() { int n = scan(); REP(i, n) Y[i] = scan(); int mid = get_median(Y, 0, n); // nth_element(Y, Y + n / 2, Y + n); クイックセレクト // int mid = Y[n / 2]; int ans = 0; REP(i, n) ans += abs(Y[i] - mid); printf("%lld\n", ans); }