#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; int main(){ int b,n; -scanf("%d%d",&b,&n); vector c(n); int64_t sum=b,average,step=0,minstep=0x7fffffffffffffffL; for(auto &i:c){ -scanf("%d",&i); sum+=i; } average=sum/n; sort(c.begin(),c.end()); for(auto &i:c){ if(i>average){ step+=i-average; b+=i-average; i=average; } } sum=0; for(auto i:c){ sum+=i; } average=sum/n; auto calcstep=[](vector t,int target,int b){ int64_t step=0; for(int i:t){ step+=abs(target-i); b+=target-i; } return b>=0?step:0x7fffffffffffffffL; }; auto t=calcstep(c,c[c.size()/2],b); minstep=min(minstep,t); t=calcstep(c,c[c.size()/2+1],b); minstep=min(minstep,t); printf("%ld\n",step+minstep); return 0; }