//set many funcs template //Ver.20190820 #include #include #include #include #include #include #define inf 1072114514 #define llinf 4154118101919364364 #define mod 1000000007 #define pi 3.1415926535897932384 #define size 524288 long long llmax(long long a,long long b){if(a>b){return a;}return b;} int llsortfncsj(const void *a,const void *b){if(*(long long *)a>*(long long *)b){return 1;}if(*(long long *)a==*(long long *)b){return 0;}return -1;} int main(void){ long long i,j,n,m,k,a[size],b,c,h,w,r=0,l,t; long long rw[size]={0}; scanf("%lld",&n); for(i=1;i<=n;i++){ scanf("%lld",&a[i]); } qsort(&a[1],n,sizeof(long long),llsortfncsj); for(i=1;i<=n;i++){ rw[i]=rw[i-1]+a[i]; } if(n<=2){puts("0");return 0;} for(i=1;(2*i+1)<=n;i++){ r=llmax(rw[i+1]+rw[n]-rw[n-i]-(2*i+1)*a[i+1],r); } for(i=1;i<=n;i++){ r=llmax(rw[i]+rw[n]-rw[n-i]-i*(a[i]+a[n-i+1]),r); } printf("%lld\n",r); return 0; }