import java.io.*; import java.util.*; public class Main_yukicoder837 { private static Scanner sc; private static Printer pr; private static void solve() { int n = sc.nextInt(); y = new int[n]; for (int i = 0; i < n; i++) { y[i] = sc.nextInt(); } Arrays.sort(y); if (y[0] == y[n - 1]) { pr.println(1); return; } cumsum = new long[n + 1]; for (int i = 0; i < n; i++) { cumsum[i + 1] = cumsum[i] + y[i]; } long ans = Long.MAX_VALUE; for (int i = 1; i < n; i++) { ans = Math.min(ans, f(0, i) + f(i, n)); } pr.println(ans); } private static long[] cumsum; private static int[] y; private static long f(int i, int j) { int n = j - i; int mid = i + (n - 1) / 2; long tmp1 = cumsum[j] - cumsum[mid + 1]; long tmp2 = cumsum[mid + 1] - cumsum[i]; long tmp = tmp1 - tmp2; if (n % 2 == 1) { tmp += y[mid]; } return tmp; } // --------------------------------------------------- public static void main(String[] args) { sc = new Scanner(System.in); pr = new Printer(System.out); solve(); pr.close(); sc.close(); } static class Printer extends PrintWriter { Printer(OutputStream out) { super(out); } } }