#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(long long record, int N, const vector & Ys){ int a_head = 0; int a_tail = 1; int b_head = 2; int b_tail = N - 1; int a_mid = (a_head + a_tail) / 2; int b_mid = (b_head + b_tail) / 2; long long sum_a = diffsum(Ys, a_head, a_tail); long long sum_b = diffsum(Ys, b_head, b_tail); record = min(record, sum_a + sum_b); for (a_tail = 2, b_head = 3; b_head < N - 1; b_head++, a_tail++){ a_mid = (a_head + a_tail) / 2; sum_a += Ys[a_tail] - Ys[a_mid]; b_mid = (b_head + b_tail) / 2; sum_b -= Ys[b_mid] - Ys[b_head - 1]; record = min(record, sum_a + sum_b); } 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; } long long record; record = diffsum(Ys, 0, N - 2); record = min(record, diffsum(Ys, 1, N - 1)); // Regular case cout << solve(record, N, Ys) << endl; return 0; }