#include #define all(vec) vec.begin(), vec.end() using namespace std; using ll = long long; using P = pair; using vll = vector; using vll2 = vector>; constexpr ll INF = (1LL << 30) - 1LL; constexpr ll LINF = (1LL << 60) - 1LL; constexpr ll MOD = 1e9 + 7; template void chmin(T &a, T b) { a = min(a, b); } template void chmax(T &a, T b) { a = max(a, b); } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vll a(n + 1), sum(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a.begin() + 1, a.end()); for (int i = 1; i <= n; i++) { sum[i] = sum[i - 1] + a[i]; } ll res = 0; for (int i = 1; i <= n; i++) { ll s; if (i % 2) { s = sum[n] - sum[n - i / 2] + sum[i / 2] - (i - 1LL) * a[i / 2 + 1]; } else { s = sum[n] - sum[n - i / 2 + 1] + sum[i / 2 - 1] - (i - 2LL) * (a[i / 2] + a[i / 2 + 1]) / 2LL; } chmax(res, s); } cout << res << endl; }