from bisect import bisect_left, bisect, insort_left, insort n = int(input()) y = sorted(map(int, input().split())) len_y = len(y) m = {} def f(a): if a not in m: l = bisect_left(y, a) m[a] = abs(sum(y[:l]) - a * l) + abs(sum(y[l:]) - a * (len_y - l)) return m[a] l = min(y) r = max(y) for _ in range(100): mid_l = (l * 2 + r) // 3 mid_r = (l + r * 2) // 3 if f(mid_l) > f(mid_r): l = mid_l else: r = mid_r ans = 1e15 for i in range(l, r + 1): ans = min(ans, f(i)) print(ans)