#include using namespace std; long long diffsum(const vector & Ys, int head, int tail){ long long result = 0LL; while (head < tail){ result += Ys[tail--] - Ys[head++]; } return result; } long long solve(int N, const vector & Ys){ int a_head = 0; int a_tail = 0; int b_head = 1; int b_tail = N - 1; int a_mid = (a_head + a_tail) / 2; int b_mid = (b_head + b_tail) / 2; long long score = diffsum(Ys, a_head, a_tail) + diffsum(Ys, b_head, b_tail); long long record = score; for (a_tail = 1, b_head = 2; b_head < N; b_head++, a_tail++){ a_mid = (a_head + a_tail) / 2; score += Ys[a_tail] - Ys[a_mid]; b_mid = (b_head + b_tail) / 2; score -= Ys[b_mid] - Ys[b_head - 1]; record = min(record, score); } return record; } int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector Ys(N, 0LL); for (auto & y: Ys) cin >> y; sort(Ys.begin(), Ys.end()); // Corner case if (Ys[0] == Ys[N - 1]){ cout << 1 << endl; return 0; } if (N == 2){ cout << 0 << endl; return 0; }else if (N == 3){ cout << min(Ys[2] - Ys[1], Ys[1] - Ys[0]) << endl; return 0; } // Regular case cout << solve(N, Ys) << endl; return 0; }