#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); auto f =[&](int x)->bool{ if(x == 0)return true; return (a[N-x]+a[i-x])>=a[i]*2; }; int l = 0, r = ai + 1; while(r - l > 1){ int c = (l + r) / 2; if(f(c)) l = c; else r = c; } int lo = as[i]-as[i-l]; int hi = as[N]-as[N-l]; int cost = lo-a[i]*l; cost += hi-a[i]*l; CHMAX(ans,cost); } out(ans); }