#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; #define MOD 1000000007 namespace math{ long long int __gcd(long long int a, long long int b){ if (a > b){ swap(a, b); } while (a){ swap(a, b); a %= b; } return b; } long long int lcm(long long int a, long long int b){ long long int g = __gcd(a, b); a /= g; return a*b; } long long int ppow(long long int i, long long int j){ long long int res = 1LL; while (j){ if (j & 1LL){ res *= i; res %= MOD; } i *= i; i %= MOD; j >>= 1LL; } return res; } namespace factorial{ vector lo; vector l2; void set_long(long long int b){ if (lo.size()){ } else{ lo.push_back(1); } for (long long int i = lo.size(); i <= b; i++){ lo.push_back(lo.back()); lo.back() *= i; if (lo.back() >= MOD){ lo.back() %= MOD; } } } void set_log(long long int b){ if (l2.size()){ } else{ l2.push_back(log(0.0)); } for (long long int i = l2.size(); i <= b; i++){ l2.push_back(l2.back()); l2.back() += log((double)(i)); } } long long int get_long(int b){ if (lo.size() <= b){ set_long(b); } return lo[b]; } double get_log(int b){ if (l2.size() <= b){ set_log(b); } return l2[b]; } } namespace combination{ long long int simpleC(long long int a, long long int b){ if (a < b){ return 0; } if (a - b < b){ b = a - b; } long long int u = 1LL; for (long long int j = a; j >= a - b + 1LL; j--){ u *= j; if (u >= MOD){ u %= MOD; } } long long int s = 1LL; for (long long int i = 1LL; i <= b; i++){ s *= i; if (s >= MOD){ s %= MOD; } } return (u*ppow(s, MOD - 2)) % MOD; } long long int C(long long int a, long long int b){ if (a < b){ return 0; } long long int u = math::factorial::get_long(a); long long int s = math::factorial::get_long(b)*math::factorial::get_long(a - b); u %= MOD; s %= MOD; return (u*ppow(s, MOD - 2)) % MOD; } double logC(int a, int b){ double u = math::factorial::get_log(a); double s = math::factorial::get_log(b) + math::factorial::get_log(a - b); return u - s; } long long int H(long long int a, long long int b){ return math::combination::C(a + b - 1LL, b); } long long int simpleH(long long int a, long long int b){ return math::combination::simpleC(a + b - 1LL, b); } } namespace prime{ vector prime; vector use; //smallest divisor void init(int b){ use.assign(b + 1, 0); prime.clear(); prime.push_back(2); use[2] = 2; for (int i = 3; i < use.size(); i += 2){ if (use[i] == 0LL){ prime.push_back(i); use[i] = i; for (int j = i * 2; j < use.size(); j += i){ use[j] = i; } } } } vector factorizetion(long long int num){ vector r; r.clear(); for (int i = 0; i 1LL){ r.push_back(num); } return r; } int size_of_factorization(long long int num){ int cnt = 0; for (int i = 0; i 1LL){ cnt++; } return cnt; } long long int number_of_div(long long int num){ long long int way = 1LL; long long int cnt = 0; for (int i = 0; i < prime.size() && prime[i] * prime[i] <= num; i++){ cnt = 0; while (num%prime[i] == 0){ cnt++; num /= prime[i]; } way *= (cnt + 1LL); } if (num > 1LL){ way *= 2LL; } return way; } } } using namespace math; 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; } maxt /= (long long int)(n); mint = countt(maxt); while (clock() / (double)(CLOCKS_PER_SEC) < 0.95){ mint = min(mint, countt(randxor() % (maxt+1LL))); } printf("%lld\n", mint); return 0; }