N = int(input()) Y = list(map(int, input().split())) Y.sort() E = [] N -= 1 for i in range(N): E.append(Y[i + 1] - Y[i]) inf = 10 ** 18 pre0 = 0 pre1 = inf for i in range(N): dp0 = pre1 dp1 = min(pre0, pre1) + E[i] pre0, dp0 = dp0, pre0 pre1, dp1 = dp1, pre1 print(pre1)