from bisect import bisect_left, bisect, insort_left, insort n = int(input()) y = sorted(map(int, input().split())) sum_y = sum(y) med = y[n//2] l = bisect_left(y, med) sum_yl = sum(y[:l]) print(abs(sum_yl - med * l) + abs((sum_y - sum_yl) - med * (n - l)))