/* -*- coding: utf-8 -*- * * 198.cc: No.198 キャンディー・ボックス2 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 10; /* typedef */ typedef long long ll; /* global variables */ ll cs[MAX_N]; /* subroutines */ ll steps(int n, ll a) { ll d = 0; for (int i = 0; i < n; i++) d += (cs[i] > a) ? cs[i] - a : a - cs[i]; return d; } /* main */ int main() { ll b; int n; cin >> b >> n; ll sum = 0; for (int i = 0; i < n; i++) { cin >> cs[i]; sum += cs[i]; } ll c0 = -1, c1 = (sum + b) / n + 1; while (c0 + 2 < c1) { ll a0 = (c0 * 2 + c1) / 3; ll a1 = (c0 + c1 * 2) / 3; if (steps(n, a0) <= steps(n, a1)) c1 = a1; else c0 = a0; } //printf("steps(%lld)=%lld, steps(%lld)=%lld\n", //c0, steps(n, c0), c1, steps(n, c1)); ll mins = steps(n, c0); ll s1 = steps(n, c0 + 1); ll s2 = steps(n, c0 + 2); if (mins > s1) mins = s1; if (mins > s2) mins = s2; printf("%lld\n", mins); return 0; }