import statistics N = int(input()) Y = tuple(map(int, input().split())) M = statistics.median_low(Y) print(sum(abs(y - M) for y in Y))