#include #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (int)(n) - 1; i >= 0; i--) using namespace std; using ll = long long; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector a(n); rep(i, n) cin >> a[i]; sort(a.begin(), a.end()); if (a[0] == a[n - 1]) { cout << 1 << '\n'; return 0; } vector sum(n + 1); rep(i, n) sum[i + 1] = sum[i] + a[i]; ll ans = 1e18; for (int i = 1; i < n; i++) { int x = i / 2; int y = (n - i) / 2 + i; ll cost = 0; cost += a[x]*x - (sum[x] - sum[0]); cost += (sum[i] - sum[x]) - a[x]*(i-x); cost += a[y]*(y-i) - (sum[y] - sum[i]); cost += (sum[n] - sum[y]) - a[y]*(n-y); ans = min(ans, cost); } cout << ans << '\n'; }