#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>> using v64 = vector; template v64 ruisekiwa(const Array src, int N) { v64 s(N, src[0]); for (int i = 1; i < N; ++i) s[i] = s[i-1] + src[i]; return s; } // [l, r] inline long long partSum(const v64 &s, int l, int r) { if (l > r) swap(l, r); return s[r] - (l <= 0 ? 0 : s[l-1]); } 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; } v64 s = ruisekiwa(a, N); // [l, r) const auto medianDiffSum = [&](int l, int r) -> i64 { const int N = r - l; const int mid = (l + r) / 2; if (N == 1) return 0; if (N % 2 == 0) { return partSum(s, mid, r-1) - partSum(s, l, mid-1); } else { return partSum(s, mid+1, r-1) - partSum(s, l, mid-1); } }; i64 ans = LINF; for (int d = 1; d < N; ++d) { const auto x = medianDiffSum(0, d); const auto y = medianDiffSum(d, N); eprintf("[0, %d) = %2lld, [%d, N) = %2lld\n", d, x, d, y); chmin(ans, x + y); } cout << (ans) << endl; return 0; } /* vim:set foldmethod=marker foldmarker=<<<,>>> : */