#include #include #include #include #include #include #include #include using namespace std; using ll = long long; using P = pair; const long long MOD = 1e9+7; 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; } ll ternary_search(int l, int r, ll f(int p)) { ll res = 1e18; for (int _l = 0; _l < 100; _l++) { int c = (l * 2 + r) / 3, d = (l + r * 2) / 3; //cout << c << " " << d << endl; ll fc = f(c), fd = f(d); chmin(res, min(fc, fd)); if (fc > fd) l = c; else r = d; } return res; } vector y(200000); int n; //y0 y1 y2 y3... // p1 p2 p3 ... ll f(int p) { ll sum = 0; for (int i = 0; i < p; i++) { sum += abs(y.at(i) - y.at(p / 2)); //sum += abs(y.at(i) - (y.at(0) + y.at(p - 1)) / 2); } for (int i = p; i < n; i++) { sum += abs(y.at(i) - y.at(p + (n - p) / 2)); //sum += abs(y.at(i) - (y.at(p) + y.at(n - 1)) / 2); } return sum; } int main() { cin >> n; y.resize(n); for (int i = 0; i < n; i++) { cin >> y.at(i); } sort(y.begin(), y.end()); if (y.at(0) == y.at(n - 1)) { cout << 1 << endl; return 0; } //for (int i = 1; i < n; i++) cout << f(i) << endl; cout << ternary_search(0, n, f) << endl; }