#include #include #include using lint = long long; constexpr lint INF = 1LL << 60; void solve() { int n; std::cin >> n; std::vector xs(n); for (auto& x : xs) std::cin >> x; std::sort(xs.begin(), xs.end()); if (xs.front() == xs.back()) { std::cout << 1 << std::endl; return; } std::vector fore(n + 1, 0); { lint sum = 0; int pmi = -1; for (int i = 0; i < n; ++i) { sum += xs[i]; int mi = i / 2; if (pmi < mi) sum -= xs[mi] * 2; fore[i + 1] = sum + (mi + 1) * xs[mi] - (i - mi) * xs[mi]; pmi = mi; } } std::reverse(xs.begin(), xs.end()); std::vector back(n + 1, 0); { lint sum = 0; int pmi = -1; for (int i = 0; i < n; ++i) { sum -= xs[i]; int mi = i / 2; if (pmi < mi) sum += xs[mi] * 2; back[i + 1] = sum - (mi + 1) * xs[mi] + (i - mi) * xs[mi]; pmi = mi; } } lint ans = INF; for (int i = 0; i <= n; ++i) { ans = std::min(ans, fore[i] + back[n - i]); } std::cout << ans << std::endl; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }