import sys input = sys.stdin.readline N=int(input()) Y=list(map(int,input().split())) MIN=min(Y) Y=[y+MIN for y in Y] if max(Y)==min(Y): print(1) sys.exit() Y.sort() MIN=1 MAX=N-1 while True: x=(MIN+MAX)//2 y=x+1 B=Y[:x] C=Y[x:] SUM=sum(B) ANS=SUM LEN=len(B) NOW=0 for i in range(LEN): SUM+=(B[i]-NOW)*i+(NOW-B[i])*(LEN-i) ANS=min(ANS,SUM) NOW=B[i] SUM=sum(C) ANS2=SUM LEN=len(C) NOW=0 for i in range(LEN): SUM+=(C[i]-NOW)*i+(NOW-C[i])*(LEN-i) ANS2=min(ANS2,SUM) NOW=C[i] D=Y[:y] E=Y[y:] SUM=sum(D) ANS3=SUM LEN=len(D) NOW=0 for i in range(LEN): SUM+=(D[i]-NOW)*i+(NOW-D[i])*(LEN-i) ANS3=min(ANS3,SUM) NOW=D[i] SUM=sum(E) ANS4=SUM LEN=len(E) NOW=0 for i in range(LEN): SUM+=(E[i]-NOW)*i+(NOW-E[i])*(LEN-i) ANS4=min(ANS4,SUM) NOW=E[i] #print(x,y) #print(ANS,ANS2,ANS3,ANS4) if ANS+ANS2==ANS3+ANS4: print(ANS+ANS2) sys.exit() elif MAX==MIN: print(min(ANS+ANS2,ANS3+ANS4)) sys.exit() elif ANS+ANS2