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]; for (int i = 0; i < N; ++i) { Y[i] = sc.nextLong(); } Arrays.sort(Y); long[] dp = new long[N]; Arrays.fill(dp, Long.MAX_VALUE / 3); for (int i = 1; i < N; ++i) { dp[i] = Math.min(dp[i], (i >= 2 ? dp[i - 2] : 0) + Math.abs(Y[i] - Y[i - 1])); if (i >= 2) dp[i] = Math.min(dp[i], (i >= 3 ? dp[i - 3] : 0) + Math.abs(Y[i] - Y[i - 1]) + Math.abs(Y[i - 1] - Y[i - 2])); } System.out.println(dp[N - 1]); } void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }