#include using namespace std; typedef long long ll; const int MOD = 1000000007; const int INF = 1000000000; #define REP(i,n) for((i)=0;(i)<(int)(n);(i)++) int n, b; int a[20]; ll func(int x){ ll ret = 0; ll stock = b; for (int i = 0; i < n; i++){ if (a[i] < x) stock -= (x - a[i]); else stock += (a[i] - x); ret += abs(a[i] - x); } if (stock < 0) return INF; return ret; } int main(){ cin >> b >> n; for (int i = 0; i < n; i++) cin >> a[i]; int ng = -1; int ok = INF; while(abs(ok - ng) > 1){ int mid = (ok + ng) / 2; if (func(mid) <= func(mid + 1)) ok = mid; else ng = mid; } cout << func(ok) << endl; return 0; }