def median(seq): if N % 2 == 1: return seq[(N-1)//2] else: return (seq[N//2-1]+seq[N//2]) / 2 N = int(input()) Y = list(sorted(map(int, input().split()))) med = median(Y) ans = sum(abs(Y_i - med) for Y_i in Y) print(int(ans))