import bisect n= int(input()) height_list = sorted([ int(v) for v in input().split() ]) mean = sum(height_list)//n l = (bisect.bisect_left(height_list,mean)) r = (bisect.bisect_right(height_list,mean)) def move(i): global height_list for j in height_list: move_list = [ abs(height_list[i] - j) for j in height_list] return sum(move_list) for i in range(r,n-1): minr = 10**15 if move(i) < move(i+1): minr = move(i) break minr = min(minr,move(n-1)) for i in range(l,1,-1): minl = 10**15 if move(i) < move(i-1): minl = move(i) break minl = min(minl,move(0)) print(min(minl,minr))