import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; class Main { public static void main(String[] args) { new Main().run(); } void run() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] Y = new long[n]; boolean f = true; for (int i = 0; i < n; ++i) { Y[i] = sc.nextLong(); f &= Y[i] == Y[0]; } if (f) { System.out.println(1); return; } Arrays.sort(Y); long[] sum = new long[n]; for (int i = 0; i < n; ++i) { sum[i] = (i > 0 ? sum[i - 1] : 0) + Y[i]; } long ans = Long.MAX_VALUE / 3; for (int i = 1; i < n; ++i) { // [0, i) // [i, n) long cur = 0; int m = i / 2; cur += Y[m] * (m + 1) - sum[m]; cur += (sum[i - 1] - sum[m]) - Y[m] * (i - 1 - m); m = (n - i) / 2; cur += Y[i + m] * (m + 1) - (sum[i + m] - sum[i - 1]); cur += (sum[n - 1] - sum[i + m]) - Y[i + m] * ((n - 1) - (i + m)); ans = Math.min(ans, cur); } System.out.println(ans); } void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }