#include #define rep(i,n) for(int i = 0; i < (n); ++i) #define srep(i,s,t) for (int i = s; i < t; ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) using namespace std; typedef long long int ll; typedef pair P; #define yn {puts("Yes");}else{puts("No");} #define MAX_N 200005 int main() { ll n; cin >> n; ll a[n]; rep(i,n)cin >> a[i]; sort(a,a+n); ll sum[n+1] = {}; rep(i,n){ sum[i+1] = a[i] + sum[i]; } ll ans = 0; if(n <= 2){ cout << ans << endl; return 0; } // 奇数のとき srep(i,1,n-1){ ll m = min((int)n - 1 - i, i); if(a[n-1] + a[i-1] - 2 * a[i] < 0)continue; if(a[n-m] + a[i-m] - 2 * a[i] >= 0){ ans = max(ans, sum[n] - sum[n-m] + sum[i] - sum[i-m] - m * 2 * a[i]); continue; } ll l = m; ll r = 1; while(r + 1 < l){ ll mm = (l + r) / 2; if(a[n-mm] + a[i-mm] - 2 * a[i] >= 0){ r = mm; }else{ l = mm; } } ans = max(ans, sum[n] - sum[n-r] + sum[i] - sum[i-r] - r * 2 * a[i]); } if(n == 3){ cout << ans << endl; return 0; } // 偶数の時 srep(i,2,n-1){ ll m = min((int)n - 1 - i, i-1); if(a[n-1] + a[i-2] - (a[i] + a[i-1]) < 0)continue; if(a[n-m] + a[i-1-m] - (a[i] + a[i-1]) >= 0){ ans = max(ans, sum[n] - sum[n-m] + sum[i-1] - sum[i-1-m] - m * (a[i] + a[i-1])); continue; } ll l = m; ll r = 1; while(r + 1 < l){ ll mm = (l + r) / 2; if(a[n-mm] + a[i-1-mm] - (a[i] + a[i-1]) >= 0){ r = mm; }else{ l = mm; } } ans = max(ans, sum[n] - sum[n-r] + sum[i-1] - sum[i-1-r] - r * (a[i] + a[i-1])); } cout << ans << endl; return 0; }