#include #include #include #include #include #include #include #include #include // require sort next_permutation count __gcd reverse etc. #include // require abs exit atof atoi #include // require scanf printf #include #include // require accumulate #include // require fabs #include #include #include #include // require setw #include // require stringstream #include // require memset #include // require tolower, toupper #include // require freopen #include // require srand #define rep(i,n) for(int i=0;i<(n);i++) #define ALL(A) A.begin(), A.end() #define INF 1LL<<60 using namespace std; typedef long long ll; typedef pair P; ll C[12]; int N; ll f(ll base ){ ll curr = 0LL; rep (i, N ) curr += abs (C[i] - base ); return curr; } ll ternary_search (ll lo, ll hi ){ if (N == 1 ) return 0LL; int cnt = 1000; while (cnt-- ){ ll cl = lo + (hi - lo )/3LL; ll ch = lo + 2LL*(hi - lo )/3LL; if (f(cl) > f(ch) ) lo = cl; else hi = ch; } // end while ll res = INF; for (ll base = max(lo-100LL, 0LL ); base <= hi; base++ ){ ll curr = f(base ); res = min (res, curr ); } // end for return res; } ll linear_search (ll lo, ll hi ){ ll res = INF; for (ll base = lo; base <= hi; base++ ){ ll curr = 0LL; rep (i, N ){ curr += abs(C[i] - base ); } // end rep res = min (res, curr ); } // end for return res; } int main() { memset (C, 0LL, sizeof (C ) ); ios_base::sync_with_stdio(0); ll B; cin >> B; cin >> N; ll sum = B; rep (i, N ) cin >> C[i], sum += C[i]; ll max_base = sum/(ll)N; ll lo = 0; ll hi = max_base + 1LL; ll res1 = ternary_search (lo, hi ); cout << res1 << endl; // ll res2 = linear_search (lo, hi ); // cout << res1 << ' ' << res2 << endl; return 0; }