N=int(input()) p=sorted(int(s) for s in input().split()) m=p[N//2] print(sum(abs(a-m) for a in p))