#include using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 1000000007; constexpr bool debug = false; //---------------------------------// int main() { int N; cin >> N; vector Y(N); REP(i, N) scanf("%d", &Y[i]); sort(ALL(Y)); vector sum(N + 1); REP(i, N) sum[i + 1] = sum[i] + Y[i]; if (*min_element(ALL(Y)) == *max_element(ALL(Y))) { puts("1"); return 0; } ll ans = longINF; FOR(i, 1, N) { if (Y[i] == Y[i - 1]) continue; // [0, i), [i, N) int l = i / 2; int r = (N - i) / 2 + i; ll cur = (sum[i] - sum[l]) - (ll)Y[l] * (i - l); cur += (ll)Y[l] * (l - 0) - (sum[l] - sum[0]); cur += (sum[N] - sum[r]) - (ll)Y[r] * (N - r); cur += (ll)Y[r] * (r - i) - (sum[r] - sum[i]); chmin(ans, cur); } cout << ans << endl; }