#include #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) #define ALL(x) std::begin(x), std::end(x) using namespace std; template inline void chmax(T & a, U const & b) { a = max(a, b); } template istream & operator >> (istream & in, vector & xs) { REP (i, xs.size()) { in >> xs[i]; } return in; } int64_t solve(int n, vector a) { sort(ALL(a)); vector acc(n + 1); partial_sum(ALL(a), acc.begin() + 1); int64_t ans = INT64_MIN; REP (i, (n + 1) / 2) { chmax(ans, a[i] + (acc[i] - acc[0]) + (acc[n] - acc[n - i]) - (2 * i + 1) * a[i]); chmax(ans, a[i] + a[i + 1] + (acc[i] - acc[0]) + (acc[n] - acc[n - i]) - (2 * i + 2) * (a[i] + a[i + 1]) / 2); } return ans; } int main() { int n; cin >> n; vector a(n); cin >> a; cout << solve(n, a) << endl; return 0; }