#define _USE_MATH_DEFINES #include using namespace std; //template #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rrep(i,a,b) for(int i=(a);i>(b);i--) #define ALL(v) (v).begin(),(v).end() typedef long long int ll; typedef pair P; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } templatevoid Fill(A(&array)[N],const T &val){fill((T*)array, (T*)(array+N), val);} const int inf = INT_MAX / 2; const ll INF = LLONG_MAX / 2; //template end int main(){ int n; scanf("%d",&n); vector a(n),sum(n+1,0); rep(i,0,n)scanf("%d",&a[i]); sort(ALL(a)); if(a.front()==a.back()){ printf("1\n"); return 0; } rep(i,0,n)sum[i+1]=sum[i]+a[i]; ll s1,s2,s3,s4,ans=INF; rep(i,0,n){ s1=a[i/2]*(i/2)-sum[i/2]; s2=(sum[i]-sum[i/2])-a[i/2]*(i-i/2); s3=a[(n+i)/2]*((n+i)/2-i)-(sum[(n+i)/2]-sum[i]); s4=(sum[n]-sum[(n+i)/2])-a[(n+i)/2]*(n-(n+i)/2); chmin(ans,s1+s2+s3+s4); } printf("%lld\n",ans); return 0; }