#include #include #include #include #include using namespace std; void solve198() { long long B, N; cin >> B >> N; long long ans =1000000000000000000; vector BOX(N); long long val = 0; for (int i = 0; i < N; i++) { cin >> BOX[i]; val += BOX[i]; } val = val / N; vector> goal(N); for (int i = 0; i < N; i++) { goal[i] = make_pair(abs(BOX[i] - val),i); } sort(BOX.begin(), BOX.end()); reverse(BOX.begin(), BOX.end()); sort(goal.begin(), goal.end()); long long tmp = B; for (int i = 0; i < N; i++) { B = tmp; long long count = 0; val = BOX[goal[i].second]; for (int i = 0; i < N; i++) { B += BOX[i] - val; count += abs(BOX[i] - val); } if (B >= 0){ ans = min(ans, count); } } cout << ans << endl; return; } int main(){ solve198(); return 0; }