import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; A.sort(); auto B = new long[](N+1); foreach (i; 0..N) B[i+1] = B[i] + A[i]; long sum(int l, int r) { if (r < l) return 0; return B[r+1] - B[l]; } long calc(int l, int r, long v) { int hi = min(l, N - r - 1) + 1; int lo = 0; while (hi - lo > 1) { int mid = (hi + lo) / 2; if (A[l - mid] + A[N - mid] > v) { lo = mid; } else { hi = mid; } } return sum(l-lo, l-1) + sum(N-lo, N-1) - v * lo; } long ans = 0; foreach (i; 1..N-1) { ans = max(ans, calc(i, i, A[i] * 2)); if (i < N - 2) ans = max(ans, calc(i, i + 1, A[i] + A[i+1])); } ans.writeln; }