#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)<; using vi = vector; using vll = vector; void solve() { int N; cin >> N; vi A(N); rep(i, N) { cin >> A[i]; } sort(all(A)); vll sum(N + 1); rep(i, N) { sum[i + 1] = sum[i] + A[i]; } ll ans = 0; rep(i, N) { int median = A[i]; int ok = 0, ng = min(N - i, i + 1); while (ng - ok > 1) { int x = (ok + ng) / 2; if (A[i - x] + A[N - i] - 2 * median > 0)ok = x; else ng = x; } smax(ans, (sum[i] - sum[i - ok]) + (sum[N]-sum[N-ok]) - 2ll*median*ok); } rep(i, N - 1) { int median2 = A[i] + A[i + 1]; int ok = 0, ng = min(N - (i + 1), i + 1); while (ng - ok > 1) { int x = (ok + ng) / 2; if (A[i - x] + A[N - i] - median2 > 0)ok = x; else ng = x; } smax(ans, (sum[i] - sum[i - ok]) + (sum[N] - sum[N - ok]) - median2 * ok); } cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); solve(); return 0; }