#pragma GCC optimize "O3" #pragma GCC target "avx" #include #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i)) #define ALL(x) begin(x), end(x) using ll = long long; using namespace std; template inline void chmin(T & a, U const & b) { a = min(a, b); } ll solve(int n, vector & a) { sort(ALL(a)); if (a.front() == a.back()) { return 1; } ll ans = LLONG_MAX; REP3 (m, 1, n) { ll sum = 0; REP (i, m) { sum += abs(a[i] - a[i < m ? m / 2 : m + (n - m) / 2]); } chmin(ans, sum); } return ans; } int main() { int n; scanf("%d", &n); vector a(n); REP (i, n) { scanf("%d", &a[i]); } printf("%lld\n", solve(n, a)); return 0; }