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); if (n <= 3) { System.out.println(arr[n - 1] - arr[0]); return; } long[] dp = new long[n]; dp[0] = Long.MAX_VALUE / 10; dp[1] = arr[1] - arr[0]; dp[2] = arr[2] - arr[0]; for (int i = 3; i < n; i++) { dp[i] = Math.min(dp[i - 2] + arr[i] - arr[i - 1], dp[i - 3] + arr[i] - arr[i - 2]); } System.out.println(dp[n - 1]); } }