import statistics import math def sum_dist(l, r): if (r - l) % 2 == 0: right = ruiseki[r] - ruiseki[(l+r)//2] left = ruiseki[(l+r)//2] - ruiseki[l] return - left + right else: right = ruiseki[r] - ruiseki[(l+r)//2 + 1] left = ruiseki[(l+r)//2] - ruiseki[l] return - left + right n = int(input()) a = list(map(int, input().split())) a = sorted(a) if a.count(a[0]) == len(a): print(1) exit() ruiseki = [0]*(n+1) for i in range(n): ruiseki[i+1] = ruiseki[i] + a[i] ans = float("inf") for i in range(0, n): ans = min(ans, sum_dist(0, i) + sum_dist(i, n)) print(ans)