#include #include #include #include using namespace std; typedef long long ll; vectorc; int n; ll cnt(int a) { ll cnt = 0; for (int i = 0; i < n; i++) { cnt += abs(c[i] - a); } return cnt; } int main() { int b; cin >> b >> n; c.resize(n); ll sum = 0; for (int i = 0; i < n; i++) { cin >> c[i]; sum += c[i]; } sort(c.begin(), c.end()); ll l=c[0], r=(sum+b)/n+1; //範囲[l,r) while (r - l > 1) { int mid = (l + r) / 2; if (cnt(mid) < cnt(mid-1)) { l = mid; } else r = mid; } cout << cnt(l) << endl; return 0; }