#include using namespace std; using ll = long long; using PAIR = pair; using PAIRLL = pair; using vi = vector; using vll = vector; using vvi = vector; using vs = vector; #define rep(i,n) for(int i=0;i=0;i--) #define FOR(i,a,b) for(ll i=a;i<=ll(b);i++) #define FORD(i,a,b) for(ll i=a;i>=ll(b);i--) #define FORA(i,I) for(const auto& i:I) //x:コンテナ #define ALL(x) x.begin(),x.end() #define SIZE(x) ll(x.size()) //定数 #define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf #define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf #define MOD 1000000007 //問題による #define F first #define S second //出力(空白区切りで昇順に) #define coutALL(x) for(auto i=x.begin();i!=--x.end();i++)cout<<*i<<" ";cout<<*--x.end()< vec, int number) { auto itr = std::find(vec.begin(), vec.end(), number); size_t index = std::distance( vec.begin(), itr ); if (index != vec.size()) { // 発見できたとき return 1; } else { // 発見できなかったとき return 0; } } vector vector_find_ll(vector vec,ll num){ int N = vec.size(); vector match_num; rep(i,N){ if (vec[i] == num){ match_num.push_back(i); } } return match_num; } ll vector_max(vector vec){ ll ret = -INF64; int N = vec.size(); rep(i,N){ ret = max(ret,vec[i]); } return ret; } ll vector_min(vector vec){ ll ret = INF64; int N = vec.size(); rep(i,N){ ret = min(ret,vec[i]); } return ret; } struct UnionFind{ int N; vector parents; vector sizeg; int group_cnt = N; UnionFind(int N){ rep(i,N) parents.push_back(i); rep(i,N) sizeg.push_back(1); } int find(int x){ if (x == parents[x]){ return x; } else{ parents[x] = find(parents[x]); return parents[x]; } } void unite(int x,int y){ x = find(x); y = find(y); if (x > y){ swap(x,y); } if (x == y){ return; } sizeg[x] += sizeg[y]; parents[y] = x; group_cnt -= 1; } int size(int x){ return sizeg[find(x)]; } bool same(int x,int y){ return find(x) == find(y); } int groupcount(){ return group_cnt; } }; struct SegTree{ ll DEFAULT = 0; ll func(ll x, ll y){ return x ^ y; } int N,K = 0,N2; vector dat; SegTree(vector ls){ N = ls.size(); while ((N-1) >> K){ K += 1; } //K -= 1; N2 = 1 << K; rep(i,1<<(K+1)){ dat.push_back(DEFAULT); } rep(i,N){ dat[N2+i] = ls[i]; } for (int i=N2-1;i>0;i--){ dat[i] = func(dat[i<<1],dat[i<<1|1]); } } ll leafval(int x){ return dat[x+N2]; } void update(int x,ll y){ int i = x + N2; dat[i] = y; while (i > 0){ i >>= 1; dat[i] = func(dat[i<<1],dat[i<<1|1]); } } ll query(int L,int R){ L += N2; R += N2; ll vL = DEFAULT; ll vR = DEFAULT; while (L < R){ if (L & 1){ vL = func(vL,dat[L]); L += 1; } if (R & 1){ R -= 1; vR = func(dat[R],vR); } L >>= 1; R >>= 1; } return func(vL,vR); } }; ll sumvec(vector ls){ ll v = 0; int N = ls.size(); rep(i,N){ v += ls[i]; } return v; } int main(){ ll B; int N; cin >> B >> N; vector ls(N); rep(i,N) cin >> ls[i]; sort(ls.begin(),ls.end()); ll ans = INF64; ll sumls = sumvec(ls); rep(i,N){ int mid = ls[i]; ll v = 0; if (sumls + B < N * mid){ continue; } rep(i,N){ v += abs(ls[i]-mid); } ans = min(ans,v); } cout << ans << endl; }