import java.io.*; import java.util.*; public class Main_yukicoder838 { private static Scanner sc; private static Printer pr; private static void solve() { final long INF = Long.MAX_VALUE / 2; int n = sc.nextInt(); int[] y = new int[n]; for (int i = 0; i < n; i++) { y[i] = sc.nextInt(); } Arrays.sort(y); long[] dp2 = new long[n + 1]; long[] dp3 = new long[n + 1]; Arrays.fill(dp2, INF); Arrays.fill(dp3, INF); dp2[0] = dp3[0] = 0; for (int i = 0; i < n; i++) { if (i + 1 - 2 >= 0) { dp2[i + 1] = Math.min(dp2[i + 1], dp2[i + 1 - 2] + y[i] - y[i - 1]); dp2[i + 1] = Math.min(dp2[i + 1], dp3[i + 1 - 2] + y[i] - y[i - 1]); } if (i + 1 - 3 >= 0) { dp3[i + 1] = Math.min(dp3[i + 1], dp3[i + 1 - 3] + y[i] - y[i - 1] + y[i - 1] - y[i - 2]); dp3[i + 1] = Math.min(dp3[i + 1], dp2[i + 1 - 3] + y[i] - y[i - 1] + y[i - 1] - y[i - 2]); } } pr.println(Math.min(dp2[n], dp3[n])); } // --------------------------------------------------- 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); } } }