N = int(input()) Y = list(map(int, input().split())) Y.sort() if N <= 3: print(Y[-1] - Y[0]) exit() INF = float('inf') dp = [0] * N dp[0] = INF dp[1] = Y[1] - Y[0] dp[2] = Y[2] - Y[0] for i, y in enumerate(Y[3:], start=3): d2 = dp[i - 3] + (y - Y[i - 2]) # xxx o o o d3 = dp[i - 4] + (y - Y[i - 1]) + (Y[i - 2] - Y[i - 3]) # xxx o o o o dp[i] = min(d2, d3) print(dp[-1])