import statistics n = int(input()) Y = list(map(int, input().split())) m = statistics.median_low(Y) print(sum([abs(y - m) for y in Y]))