N = int(input()) Y = list(map(int, input().split())) sub = [] for i in range(N): sum = 0 for j in range(N): sum += abs(Y[i] - Y[j]) sub.append(sum) print(min(sub))