from collections import Counter from bisect import bisect def main(): N = int(input()) Y = tuple(map(int, input().split())) c = Counter(Y).most_common() if len(c) == 1: print(0) return elif len(c) == 2: print((c[0][0] - c[1][0]) * c[1][1]) return else: y = sorted(Y) dist = float('inf') mid = (y[0] + y[1]) / 2 x = bisect(y, mid) for i in (y[x], y[x-1]): n = 0 for j, k in c: n += abs(i - j) * k dist = min(dist, n) print(dist) main()