#include #include #include #include #include #define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; /*-------------------------------------------------*/ int main() { cin.tie(0); ios::sync_with_stdio(false); // freopen("input.txt", "r", stdin); int n; cin >> n; vector y(n); REP(i, n) cin >> y[i]; sort(ALL(y)); vector cum(n + 1, 0); REP(i, n) cum[i + 1] = cum[i] + y[i]; long long ans = LINF; FOR(i, 1, n) { long long sum = 0; int left = (0 + i - 1) / 2; sum += y[left] * left - (cum[left] - cum[0]); sum += (cum[i] - cum[left]) - y[left] * (i - left); int right = (i + n - 1) / 2; sum += y[right] * (right - i) - (cum[right] - cum[i]); sum += (cum[n] - cum[right]) - y[right] * (n - right); ans = min(ans, sum); } cout << ans << '\n'; return 0; }