#include using namespace std; #define int long long #define rep(i,l,r) for(int i=(int)(l);i<(int)(r);i++) #define all(x) (x).begin(),(x).end() #define pb push_back templatebool chmax(T &a,T b){if(abool chmin(T &a,T b){if(a>b){a=b;return 1;}return 0;} typedef pair pii; typedef vector vi; typedef vector vvi; const int inf = 1LL<<60; const int mod = 1e9 + 7; const double eps = 1e-9; /*{ }*/ signed main(){ int n; cin >> n; vi y(n); rep(i, 0, n) cin >> y[i]; if(n == 2){ cout << (y[0] == y[1]) << endl; return 0; } sort(all(y)); if(y[0] == y[n-1]){ cout << 1 << endl; return 0; } if(y[0] == y[n-2]){ cout << 0 << endl; return 0; } vi s(n+1); rep(i, 0, n) s[i+1] = s[i] + y[i]; int ans = inf; rep(i, 1, n){ int m1 = i/2; int m2 = (i+n)/2; int t1 = y[m1]*m1 - s[m1]; int t2 = s[i]-s[m1] - y[m1]*(i-m1); int t3 = y[m2]*(m2-i) - (s[m2]-s[i]); int t4 = s[n]-s[m2] - y[m2]*(n-m2); chmin(ans, t1+t2+t3+t4); } cout << ans << endl; return 0; }