n = int(input()) A = list(map(int, input().split())) A.sort() min_sum = 4783483 for i in range(0, n - 1): m1 = A[i // 2] m2 = A[(i + 1 + n - 1) // 2] if m1 == m2: min_sum = 1 break sum1 = 0 sum2 = 0 for j in range(i+1): sum1 += abs(m1 - A[j]) for j in range(i + 1, n): sum2 += abs(m2 - A[j]) if i == 0: min_sum = sum1 + sum2 else: min_sum = min(min_sum, sum1 + sum2) print(min_sum)