import sys sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) N = I() Y = LI() Y.sort() Y = [0]+Y if N <= 4: if N == 2: print(Y[2]-Y[1]) elif N == 3: print(Y[3]-Y[1]) else: print(Y[4]-Y[3]+Y[2]-Y[1]) exit() dp = [0]*(N+1) dp[2] = Y[2]-Y[1] dp[3] = Y[3]-Y[1] dp[4] = dp[2]+Y[4]-Y[3] for i in range(5,N+1): dp[i] = min(dp[i-2]+Y[i]-Y[i-1],dp[i-3]+Y[i]-Y[i-2]) print(dp[-1])