#yuki837 import sys n=int(raw_input()) y=map(int,raw_input().split()) y.sort() if y[0]==y[n-1]: print 1 sys.exit() d=[0]*100000 temp=0 for i in xrange(n-1): temp+=y[i]-y[i/2] d[i]+=temp temp=0 for i in xrange(n-2,-1,-1): temp+=y[n-(n-i)/2]-y[i+1] d[i]+=temp m=1<<18 for i in xrange(n-1): m=min(m,d[i]) print m