#!/usr/bin/env python3 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines # %% N, *Y = map(int, read().split()) Y.sort() # %% INF = 10**18 segments = [INF] + [y - x for x, y in zip(Y, Y[1:])] + [INF] # %% dp = 0, 0 for x in segments: dp = dp[1], min(dp) + x # %% answer = min(dp) print(answer)