#include #include #include #include #include #include #include #include #include #include #include #define repd(i,a,b) for (int i=(a);i<(b);i++) #define rep(i,n) repd(i,0,n) typedef long long ll; using namespace std; int inputValue(){ int a; cin >> a; return a; }; template void output(T a, int precision) { if(precision > 0){ cout << setprecision(precision) << a << "\n"; } else{ cout << a << "\n"; } } int main() { // source code int B = inputValue(); // 手持ち ll N; // 箱の数 int num = B; // 合計の数 cin >> N; vector C(N); rep(i, N){ C[i] = inputValue(); // 各箱の中の数 num += C[i]; } ll low = 0; ll high = num / N; ll lcnt = 0; ll hcnt = 0; while (low + 2 < high) { ll lmid = (2 * low + high) / 3; ll hmid = (low + 2 * high) / 3; lcnt = 0; hcnt = 0; rep(i, N){ lcnt += abs(C[i] - lmid); hcnt += abs(C[i] - hmid); } if (lcnt > hcnt) { low = lmid; } else{ high = hmid; } } ll mid = low + 1; lcnt = 0; ll mcnt = 0; hcnt = 0; rep(i, N){ lcnt += abs(C[i] - low); mcnt += abs(C[i] - mid); hcnt += abs(C[i] - high); } output(min(lcnt, min(mcnt, hcnt)), 0); return 0; }