#include #include #include #include #include #include using namespace std; #define int long long #define endl "\n" constexpr long long INF = (long long)1e18; constexpr long long MOD = 1'000'000'007; string yn(bool f){return f?"Yes":"No";} string YN(bool f){return f?"YES":"NO";} int N; vector a, sum; bool check(int m, int x){ int y = x / 2; if(m == 0) return true; if(x%2) { if(m > min(y, N - y - 3)) { return false; } else { int a = sum[y] - sum[y - m]; int b = sum[N] - sum[N - m]; if(a < b) return true; else return false; } } else { if(m > min(y, N - y - 2)) { return false; } else { int a = sum[y] - sum[y - m]; int b = sum[N] - sum[N - m]; if(a < b) return true; else return false; } } } signed main(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout<>N; a.resize(N); sum.resize(N+1); for(int i = 0; i < N; i++){ cin>>a[i]; a[i] <<= 1; } sort(a.begin(), a.end()); for(int i = 0; i < N; i++){ sum[i+1] = a[i] + sum[i]; } for(int i = 0; i < N * 2; i++){ int l = 0, h = N * 2, m = 0; while(l + 1 < h){ m = (l + h) >> 1; if(check(m, i)){ l = m; } else { h = m; } } int temp = 0; int A = sum[i/2] - sum[i/2 - l]; int B = sum[N] - sum[N - l]; temp = A + B - l * (a[i/2] + a[(i + 1)/2]); ans = max(ans, temp); } cout<