import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); long[] values = new long[n]; for (int i = 0; i < n; i++) { values[i] = sc.nextInt(); } Arrays.sort(values); if (n == 2) { System.out.println(values[1] - values[0]); return; } long[] dp = new long[n + 1]; dp[0] = 0; dp[1] = Long.MAX_VALUE / 2; dp[2] = values[1] - values[0]; dp[3] = values[2] - values[0]; for (int i = 4; i <= n; i++) { dp[i] = Math.min(dp[i - 2] + values[i - 1] - values[i - 2], dp[i - 3] + values[i - 1] - values[i - 3]); } System.out.println(dp[n]); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String nextLine() throws Exception { return br.readLine(); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }