#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include #include #include #include //< in.txt > out.txt using namespace std; //std::ios::sync_with_stdio(false); //std::cin.tie(0); const long long MOD = 1e9 + 7; typedef long long LL; typedef long double LD; typedef pair PLL; typedef pair PDL; typedef pair PDD; typedef vector VLL; typedef vector VVLL; //typedef boost::multiprecision::cpp_int bigint; template void in(T& x) { cin >> x; } template void in(pair& p) { in(p.first); in(p.second); } template void in(vector& v, LL st = -1, LL en = -1) { if (st == -1) { st = 0; en = v.size() - 1; } for (LL n = st; n <= en; n++) { in(v[n]); } } LL B, N; VLL A; LL calc(LL c) { LL ans = 0; for (LL n = 0; n < N; n++) { ans += abs(A[n] - c); } return ans; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); cin >> B >> N; A.resize(N); in(A); LL SUM = 0; for (LL n = 0; n < N; n++)SUM += A[n]; LL left = 0, right = (SUM + B) / N; while (right - left > 2) { LL c1 = (left * 2 + right) / 3; LL c2 = (left + right * 2) / 3; LL f1 = calc(c1); LL f2 = calc(c2); if (f1 < f2)right = c2; else left = c1; } LL ans = 1e8; for (; left <= right; left++) { ans = min(ans, calc(left)); } cout << ans << "\n"; return 0; }