#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair i_i; typedef pair ll_i; typedef pair d_i; typedef pair ll_ll; typedef pair d_d; struct edge { int u, v; ll w; }; ll MOD = 1000000007; ll _MOD = 1000000009; double EPS = 1e-10; int main() { ll B; cin >> B; int N; cin >> N; vector C(N); for (int i = 0; i < N; i++) cin >> C[i]; ll mini = LLONG_MAX; for (int i = 0; i < N; i++) { ll c = C[i], b = B, sum = 0; for (int i = 0; i < N; i++) if (C[i] > c) { b += C[i] - c; sum += C[i] - c; } bool ok = true; for (int i = 0; i < N; i++) if (C[i] < c) { if (b >= c - C[i]) { b -= c - C[i]; sum += c - C[i]; } else ok = false; } if (ok) mini = min(mini, sum); } cout << mini << endl; }