#include using namespace std; #define int long long #ifdef LOCAL_DEBUG #include "LOCAL_DEBUG.hpp" #endif signed main(){ int b,n; cin >> b >> n; vector a(n); bool check = true; for(int i = 0; i < n; i++){ cin >> a[i]; if(i > 0 && a[i-1] != a[i]) check = false; } if(check){ cout << 0 << endl; return 0; } auto f = [&](int t){ int cnt = 0; for(int i = 0; i < n; i++){ cnt += abs(t - a[i]); } return cnt; }; int left = 0,right = 1 << 30; //[left , t1 , t2 , right] for(int i = 0; i < 100; i++){ int t1 = (left * 2 + right) / 3; int t2 = (left + right * 2) / 3; t2 += 1; if(f(t1) > f(t2)) left = t1; else right = t2; } int ans = 1 << 30; for(int i = left; i <= right; i++){ ans = min(ans,f(i)); if(ans == 0){ cout << i << endl; return 0; } } cout << ans << endl; return 0; }