#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) #define minimize(a, x) a = std::min(a, x) #define maximize(a, x) a = std::max(a, x) using ll = long long; ll const inf = 10 * 1e9 + 10; ll B, N; vector C; ll check(ll const x) { ll b = B; ll res = 0; rep(i, N) { if(C[i] < x) { b -= x - C[i]; if(b < 0) { return inf; } } else { b += C[i] - x; } res += abs(C[i] - x); } return res; } int main() { cin >> B >> N; C.resize(N); rep(i, N) cin >> C[i]; sort(C.rbegin(), C.rend()); ll l = -1, r = 1e9 * 2 + 2; rep(_, 2000) { ll lm = (2 * l + r) / 3; ll rm = (l + 2 * r) / 3; auto rlm = check(lm); auto rrm = check(rm); if(rlm <= rrm) { r = rm; } else { l = lm; } } ll res = inf; REP(i, l-200, l+200) { minimize(res, check(i)); } cout << res << endl; return 0; }