#include using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector a(n); for (auto&& e : a) { cin >> e; } sort(begin(a), end(a)); vector c(n + 1); for (int i = n; i--; ) { c[i] = a[i] + c[i + 1]; } long long res = 0; for (int k = 3; k <= n; ++k) { long long crr = 0; if (~k & 1) { crr += c[0] - c[k / 2 + 1]; crr += c[n - (k / 2 - 1)] - c[n]; crr -= (long long)(a[k / 2 - 1] + a[k / 2]) * k / 2; } else { crr += c[0] - c[(k + 1) / 2]; crr += c[n - k / 2] - c[n]; crr -= (long long)a[k / 2] * k; } res = max(res, crr); } cout << res << '\n'; }