#include "bits/stdc++.h" // Custom Header <<< #define ALL(x) x.begin(), x.end() #define rep(i, s, n) for(int i(s); i < int(n); ++i) #define endl '\n' #ifndef YDK #define eprintf(...) #endif using namespace std; using i64 = long long; using pii = pair; templateinline bool chmax(A &a, const B &b){return b>a ? a=b,1 : 0;} templateinline bool chmin(A &a, const B &b){return b>> struct FastIO { FastIO() { cin.tie(nullptr); ios::sync_with_stdio(false); } } fastIO_ydk ; signed main() { int N; int a[100010]; cin >> N; rep(i, 0, N) cin >> a[i]; sort(a, a+N); if (a[0] == a[N-1]) { cout << (1) << endl; return 0; } i64 x[100010] {}; i64 y[100010] {}; for (int i = 1; i < N; ++i) x[i] = x[i-1] + a[i] - a[i/2]; reverse(a, a+N); for (int i = 1; i < N; ++i) y[i] = y[i-1] - a[i] + a[i/2]; reverse(y, y+N); i64 ans = LINF; for (int i = 0; i+1 < N; ++i) { chmin(ans, x[i] + y[i+1]); } cout << (ans) << endl; return 0; } /* vim:set foldmethod=marker foldmarker=<<<,>>> : */