//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}; long long st,fi,te; 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=2;i<=n-1;i++){ st=1;fi=(i-1); while(st<=fi){ te=(st+fi)/2; if(a[n+1-te]+a[i-te]-2*a[i]<0){fi=te-1;} else{st=te+1;} } w=(rw[i]-rw[i-fi-1])+(rw[n]-rw[n-fi]); r=llmax(w-(2*fi+1)*a[i],r); } printf("%lld\n",r); return 0; }