#include using namespace std; vector c; int n; long long search(long long m) { long long cnt = 0; for (int i = 0; i < n; i++) cnt += abs(m - c[i]); return (cnt); } int main() { long long b; cin >> b >> n; c.resize(n); for (int i = 0; i < n; i++) cin >> c[i]; if (n == 1) { cout << 0 << endl; return (0); } long long sum = b; for (int i = 0; i < n; i++) sum += c[i]; sort(c.begin(), c.end()); long long tv = c[0], fv = sum / n + 1; while (fv - tv >= 3) { long long mid1 = (tv * 2 + fv) / 3; long long mid2 = (tv + fv * 2) / 3; if (search(mid1) > search(mid2)) tv = mid1; //左側が高い else fv = mid2; //右側が高い } cout << search((tv + fv) / 2) << endl; return (0); }