#include #include #include int main(){ int N; std::cin >> N; std::vector v(N); for(int i = 0; i < N; i++){ std::cin >> v[i]; } long long high = 1e9; long long low = -1e9; for(int i = 0; i < 100; i++){ long long midu = (2 * high + low) / 3; long long midl = (high + 2 * low) / 3; long long cntu = 0; long long cntl = 0; for(int j = 0; j < N; j++){ cntu += std::abs(v[j] - midu); cntl += std::abs(v[j] - midl); } if(cntl <= cntu)high = midu; else low = midl; std::cerr << low << " " << high << std::endl; } long long ans = 0; for(int i = 0; i < N; i++){ ans += std::abs(v[i] - high); } std::cout << ans << std::endl; }