N = int(input()) Y = sorted(map(int, input().split())) if N == 2: print(Y[1] - Y[0]) exit() dp = [10 ** 9] * N dp[1] = Y[1] - Y[0] dp[2] = Y[2] - Y[0] for i in range(3, N): dp[i] = min(dp[i - 2] + Y[i] - Y[i - 1], dp[i - 3] + Y[i] - Y[i - 2]) print(dp[-1])