#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; //MT random generator using namespace std; typedef unsigned int uint; typedef long long LL; typedef pair pii; typedef pair pli; typedef vector vi; typedef vector vpii; #define FI(n) FastIO::read_int(n) #define FO(n) FastIO::write_int(n) #define CLK CLOCKS_PER_SEC #define pb push_back #define X first #define Y second #define MT_N 624 #define MT_M 397 #define MT_MSB 0x80000000U #define MT_LS31B 0x7FFFFFFFU #define MT_A 2567483615U class MersenneTwister { /* pseudo-random number generator */ uint twistory[MT_N]; // history (i.e., previous states) of the generator int pos; public: MersenneTwister(uint seed = 0) { twistory[0] = seed; for (int i = 1; i < MT_N; i++) twistory[i] = 1812433253U * (twistory[i - 1] ^ (twistory[i - 1] >> 30)) + i; pos = 0; } void reset(uint seed = 0) { twistory[0] = seed; for (int i = 1; i < MT_N; i++) twistory[i] = 1812433253U * (twistory[i - 1] ^ (twistory[i - 1] >> 30)) + i; pos = 0; } void generate(void) { uint tmp; int i; for (i = 0; i < MT_N - MT_M; i++) { tmp = (twistory[i] & MT_MSB) + (twistory[i + 1] & MT_LS31B); twistory[i] = twistory[i + MT_M] ^ (tmp >> 1) ^ (MT_A & -(tmp & 1)); } for (; i < MT_N - 1; i++) { tmp = (twistory[i] & MT_MSB) + (twistory[i + 1] & MT_LS31B); twistory[i] = twistory[i + MT_M - MT_N] ^ (tmp >> 1) ^ (MT_A & -(tmp & 1)); } tmp = (twistory[i] & MT_MSB) + (twistory[0] & MT_LS31B); twistory[i] = twistory[MT_M - 1] ^ (tmp >> 1) ^ (MT_A & -(tmp & 1)); } uint rand_unsigned() { if (!pos) generate(); uint ans = twistory[pos++]; pos &= -(pos != 624); ans ^= ans >> 11; ans ^= (ans << 7) & 2636928640U; ans ^= (ans << 15) & 4022730752U; ans ^= ans >> 18; return ans; } double next_double() { return 1.0 * rand_unsigned() / MT_LS31B; } int rand_signed() { return rand_unsigned() >> 1; } int next_int(int n) { if (n == 1) return 0; return rand_unsigned() % n; } int next_int(int a, int b) { if (a == b) return a; return rand_unsigned() % (b - a + 1) + a; } } rnd(1028); inline int next_int(int a, int b) { return rnd.next_int(a, b); } inline int next_int(int n) { return rnd.next_int(n); } 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(next_int(maxt))); } printf("%lld\n", mint); return 0; }