import java.util.*; import java.io.*; import java.math.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] li = new long[n]; long[] rsw = new long[n+1]; long ans = 0; for(int i = 0; i < n; i++){ li[i] = sc.nextInt(); } if(n <= 2){ System.out.println(0); return; } Arrays.sort(li); for(int i = 1; i <= n; i++){ rsw[i] += li[i-1]+rsw[i-1]; } for(int i = 1; i < n-1; i++){ int left = 1; int right = Math.min(i+1,n-i); while(Math.abs(right-left) > 1){ int mid = (left + right) /2; if(check(li,i,mid)){ left = mid; }else{ right = mid; } } long val = rsw[i] - rsw[i-left] + rsw[n]-rsw[n-left]; val -= li[i]*left*2; ans = Math.max(ans, val); } System.out.println(ans); } static boolean check(long[] li, int i, int mid){ int n = li.length; return (li[n-mid] + li[i-mid])-(2*li[i]) >= 0; } }