#include using namespace std; int main() { int n; cin >> n; int64_t a[n]; for(int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n); int64_t sum[n + 1] = {}; for(int i = 1; i <= n; ++i) { sum[i] = sum[i - 1] + a[i - 1]; } int64_t ans = 0; for(int i = 2; i <= 2 * (n - 2); ++i) { if(i % 2 == 0) { int c = i / 2; int64_t ok = 1, ng = c + 1; while(abs(ok - ng) > 1) { int d = (ok + ng) / 2; if((a[c - d] + a[n - d]) - 2 * a[c] > 0) { ok = d; } else { ng = d; } } int64_t r = sum[n] - sum[n - ok]; int64_t l = sum[c] - sum[c - ok]; int64_t tmp = r + l - a[c] * 2 * ok; ans = max(ans, tmp); // fprintf(stderr, "%d: %ld\n", i, tmp); } else { int lc = (i - 1) / 2, rc = (i + 1) / 2; int64_t ok = 1, ng = lc + 1; while(abs(ok - ng) > 1) { int d = (ok + ng) / 2; if((a[lc - d] + a[n - d]) - (a[lc] + a[rc]) > 0) { ok = d; } else { ng = d; } } int64_t r = sum[n] - sum[n - ok]; int64_t l = sum[lc] - sum[lc - ok]; int64_t tmp = r + l - (a[lc] + a[rc]) * ok; ans = max(ans, tmp); // fprintf(stderr, "%d: %ld\n", i, tmp); } } cout << ans << '\n'; return 0; }