n = int(input()) Y = list(map(int, input().split())) Y.sort() mid = Y[n // 2] print(sum(abs(y - mid) for y in Y))