#include using namespace std; /* #include #include using namespace __gnu_pbds; */ #define int long long #define REP(i,n) for(int i = 0;i < (int)(n);i++) #define RREP(i,n) for(int i = (int)n-1;i >= 0;i--) #define FOR(i,s,n) for(int i = s;i < (int)n;i++) #define RFOR(i,s,n) for(int i = (int)n-1;i >= s;i--) #define ALL(a) a.begin(),a.end() #define IN(a, x, b) (a<=x && xinline bool CHMAX(T&a,T b){if(ainline bool CHMIN(T&a,T b){if(a>b){a = b;return true;}return false;} templateistream&operator >>(istream&is,vector&vec){for(T&x:vec)is>>x;return is;} templateinline void in(T&t){cin>>t;} templateinline void in(T&t,Ts&...ts){cin>>t;in(ts...);} templateinline void out(T t){cout<inline void out(T t,Ts... ts){cout<vectormv(size_t a){return vector(a);} templateauto mv(size_t a,Ts... ts){return vector(ts...))>(a,mv(ts...));} templatetypename enable_if::value==0>::type fill(T&t,const V&v){t=v;} templatetypename enable_if::value!=0>::type fill(T&t,const V&v){for(auto &e:t)fill(e,v);} constexpr long long INF = 1e18; signed main(){ int N; in(N); auto a = mv(N); in(a); sort(ALL(a)); auto as = mv(N+1); REP(i,N)as[i+1] = as[i] + a[i]; int ans = 0; REP(i,N){ int ai = min(i,N-i-1); int lo = as[i]-as[i-ai]; int hi = as[N]-as[N-ai]; int cost = lo-a[i]*ai; cost += hi-a[i]*ai; CHMAX(ans,cost); } out(ans); }