#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const long long inf = 1LL << 62;
int main() {
	int N;
	cin >> N;
	vector<long long> A(N);
	for (int i = 0; i < N; ++i) cin >> A[i];
	sort(A.begin(), A.end());
	vector<long long> S(N + 1);
	for (int i = 0; i < N; ++i) S[i + 1] = S[i] + A[i];
	long long ans = -inf;
	for (int i = 0; i < N; ++i) {
		int l = 0, r = min(i, N - i - 1) + 1;
		while (r - l > 1) {
			int m = (l + r) >> 1;
			if (A[N - m] + A[i - m] >= 2 * A[m]) l = m;
			else r = m;
		}
		ans = max(ans, S[N] - S[N - l] + S[i + 1] - S[i - l] - A[i] * (2 * l + 1));
	}
	for (int i = 0; i < N - 1; ++i) {
		int l = 0, r = min(i, N - i - 2) + 1;
		while (r - l > 1) {
			int m = (l + r) >> 1;
			if (A[N - m] + A[i - m] >= 2 * A[m]) l = m;
			else r = m;
		}
		ans = max(ans, S[N] - S[N - l] + S[i + 2] - S[i - l] - (A[i] + A[i + 1]) * (l + 1));
	}
	cout << ans << endl;
	return 0;
}