#include #define rep(i,n) for(int i=(0);i<(n);i++) using namespace std; typedef long long ll; typedef unsigned long long ull; template bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } int main(){ cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector y(n); rep(i, n) cin >> y[i]; ll from = -1e10; ll to = 1e10; auto calc = [&y](ll x){ ll ret = 0ll; for(ll z : y) ret += abs(z - x); return ret; }; while(to - from > 1){ ll mid = (from + to) / 2; (calc(mid) - calc(mid-1) > 0 ? to : from) = mid; } cout << calc(from) << endl; }