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