import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } Arrays.sort(arr); long[][] dp = new long[n][2]; dp[0][1] = 0; dp[0][0] = Long.MAX_VALUE / 10; dp[1][0] = arr[1] - arr[0]; dp[1][1] = Long.MAX_VALUE / 10; for (int i = 2; i < n; i++) { dp[i][0] = Math.min(dp[i - 1][1] + arr[i] - arr[i - 1], dp[i - 2][1] + arr[i] - arr[i - 2]); dp[i][1] = dp[i - 1][0]; } System.out.println(dp[n - 1][0]); } }