#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include using namespace std; long long int b; int n; unsigned int randxor() { static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned int t; t = (x ^ (x << 11)); x = y; y = z; z = w; return(w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } vector v; long long int countt(long long int a){ long long int r = 0; for (int i = 0; i < v.size(); i++){ r += abs(v[i] - a); } return r; } int main(){ scanf("%lld", &b); scanf("%d", &n); long long int mint = 0; long long int maxt = 0; for (int i = 0; i < n; i++){ long long int a; scanf("%lld", &a); v.push_back(a); maxt += a; } mint = countt(maxt / n); maxt += b; maxt /= (long long int)(n); mint = min(mint, countt(maxt)); while (clock() / (double)(CLOCKS_PER_SEC) < 0.95){ mint = min(mint, countt(rand() % (maxt + 1LL))); } printf("%lld\n", mint); return 0; }