N=int(input()) L=list(map(int,input().split())) c=max(L) d=min(L) R=[abs(i-c) for i in L] K=[abs(i-d) for i in L] if sum(K)>sum(R): print(sum(R)) else: print(sum(K))