#include #include #include using namespace std; int main(){ int n;cin>>n; if(n < 3){ cout << 0 << endl; return 0; } vector a(n); for(int i = 0; n > i; i++){ cin>>a[i]; } long long ans = 0; sort(a.begin(),a.end()); long long nw = a[0]; long long ce = a[0]; for(int i = 1; i <= n-i; i++){ nw += a[i]+a[n-i]; if(i == n-1){ nw -= a[i]; ce = a[i-1]+a[i]; ans = max(ans,nw-ce*n/2); break; } ce = a[i]; ans = max(ans,nw-(i*2+1)*ce); } cout << ans << endl; }