#include "bits/stdc++.h" using namespace std; using i64 = long long; template struct DisjointSparseTable{ function f; vector> v; DisjointSparseTable(vector& inp, function f) : f(f){ int n = inp.size(); int b; for(b = 0; (1 << b) <= inp.size(); ++b); v.assign(b, vector(n)); for(int i = 0; i < n; ++i) v[0][i] = inp[i]; for(int i = 1; i < b; ++i){ int siz = 1 << i; for(int j = 0; j < n; j += siz << 1){ int t = min(j + siz, n); v[i][t - 1] = inp[t - 1]; for(int k = t - 2; k >= j; --k) v[i][k] = f(inp[k], v[i][k + 1]); if(t >= n) break; v[i][t] = inp[t]; int r = min(t + siz, n); for(int k = t + 1; k < r; ++k) v[i][k] = f(v[i][k - 1], inp[k]); } } } T get(int l, int r){ if(l >= --r) return v[0][l]; int p = 31 - __builtin_clz(l ^ r); return f(v[p][l], v[p][r]); } }; signed main(){ int n; cin >> n; vector a(n); for(auto& x : a) cin >> x; sort(a.begin(), a.end()); DisjointSparseTable d(a, [](auto x, auto y){return x + y;}); auto f = [&](int i, int len){ int x = a[i - len]; int y = a[n - len]; return x + y - 2 * a[i] >= 0; }; i64 ans = 0; for(int i = 1; i < n - 1; ++i){ int ok = 0, ng = min(i + 1, n - i); while(ng - ok > 1){ int mid = (ok + ng) / 2; (f(i, mid) ? ok : ng) = mid; } if(ok == 0) continue; ans = max(ans, d.get(n - ok, n) + d.get(i - ok, i) - a[i] * 2 * ok); } cout << ans << endl; }