#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i, n) for(ll i = 0;i < n;i++) #define REPR(i, n) for(ll i = n;i >= 0;i--) #define FOR(i, m, n) for(ll i = m;i < n;i++) #define FORR(i, m, n) for(ll i = m;i >= n;i--) #define REPO(i, n) for(ll i = 1;i <= n;i++) #define ll long long #define INF (ll)1 << 60 #define MINF (-1 * INF) #define ALL(n) n.begin(),n.end() #define MOD (ll)1000000007 #define P pair ll n, ans = INF, ck = -1; bool ok = true; int main() { cin >> n; vector s(n), t(n); REP(i, n) { cin >> s[i]; t[i] = s[i]; } if (n == 2) { if (s[0] == s[1])cout << 1 << endl; else cout << 0 << endl; return 0; } sort(ALL(t)); t.erase(unique(ALL(t)), t.end()); REP(i, n) { if (ck == -1)ck = s[i]; else if (ck != s[i]) ok = false; } ll b = t.size() / 3; if (ok) { cout << 1 << endl; return 0; } FOR(x, b - 10, b + 10) { FOR(y, (ll)t.size() - b - 10, (ll)t.size() - b + 10) { //cout << x << y << endl; if (x < y and x >= 0 and y < t.size()) { ll now = 0; REP(i, n) { now += min(abs(s[i] - t[x]), abs(s[i] - t[y])); } ans = min(ans, now); } } } cout << ans << endl; }