n=int(input()) a=list(map(int,input().split())) a.sort() dp=[] med=len(a)//2 for i in range(n): if n%2!=0: dp.append(abs(a[i]-a[med])) else: med_i=(a[med]+a[med-1])//2 dp.append(abs(a[i]-med_i)) print(sum(dp))