#include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(x, to) for (int x = 0; x < (to); x++) #define REP(x, a, to) for (int x = (a); x < (to); x++) #define foreach(itr, x) for (typeof((x).begin()) itr = (x).begin(); itr != (x).end(); itr++) using namespace std; typedef long long ll; typedef pair PII; typedef pair PLL; ll B, N; ll c[15]; ll mx; //均一化できる最大数 const ll INF = (ll)(1e+18 + 7); ll f(ll x) { ll cost = 0; //箱のキャンディをx個にするときの作業数 rep(i, N) { cost += abs(c[i] - x); } return cost; } void solve() { ll left = 0; ll right = mx; while (right - left > 2) { ll a = (2 * left + right) / 3; ll b = (left + 2 * right) / 3; if (f(a) < f(b)) { right = b; } else { left = a; } //printf("f(%lld)=%lld f(%lld)=%lld\n", left, f(left), right, f(right)); } cout << min(f(left), min(f(left+1), f(right))) << endl; } int main() { cin >> B; cin >> N; rep(i, N) { cin >> c[i]; } rep(i, N) mx += c[i]; mx += B; mx /= N; solve(); return 0; }