import itertools n = int(input()) y = tuple(map(int, input().split())) m = {} def f(a): if a not in m: m[a] = tuple(itertools.accumulate(abs(b - a) for b in y))[-1] return m[a] l = min(y) r = max(y) while r - l > 10: 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)