import numpy as np N = int(input()) Y = np.array(list(map(int, input().split()))) print(sum(abs(Y - int(np.median(Y)))))