#include #include #include using namespace std; #define ll long long #define all(V) V.begin(), V.end() int main() { int N, a, b; cin >> N; vector Y(N); for (int i = 0;i < N;i++) cin >> Y[i]; sort(all(Y)); if (Y[0] == Y[N - 1]) { cout << 1 << endl; return 0; } a = 0, b = N / 2; ll ansa = 0, ansb = 0, ansl = 0; for (int i = 0;i < N;i++) { if (i <= a) ansa += abs(Y[a] - Y[i]); else ansb += abs(Y[b] - Y[i]); } ansl = ansa + ansb; for (int i = 1;i < N;i++) { if (i % 2) { ansa += abs(Y[a] - Y[i]); ansb -= abs(Y[b] - Y[i]); if (N % 2) ansb -= Y[b + 1] - Y[b]; b++; } else { a++; ansa += abs(Y[a] - Y[i]); ansb -= abs(Y[b] - Y[i]); } ansl = min(ansa + ansb, ansl); } cout << ansl << endl; }