#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(I,A,B) for(int I = (A); I < (B); ++I) typedef long long ll; // 揃える数を決めて三分探索 ll B, N; ll C[100]; // xで揃えた時の回数 ll calc (ll x) { ll ret = 0; FOR(i,0,N){ ret += max(C[i] - x, x - C[i]); } return ret; } int main(){ cin >> B >> N; ll csum = 0; FOR(i,0,N){ cin >> C[i]; csum += C[i]; } if(N == 1){ cout << 0 << endl; return 0; } ll l = 0, r = (B + csum) / N; while(r - l > 3){ ll ml = (r + 2 * l) / 3; ll mr = (2 * r + l) / 3; if(calc(ml) <= calc(mr)) r = mr; else l = ml; } ll ans = 1000000000000000LL; for(ll i = l; i <= r; i++) ans = min(ans, calc(i)); cout << ans << endl; return 0; }