#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define LOG(...) printf(__VA_ARGS__) //#define LOG(...) #define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i) #define REP(i,n) for(int i=0;i<(int)(n);++i) #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() #define EXIST(s,e) ((s).find(e)!=(s).end()) #define SORT(c) sort((c).begin(),(c).end()) #define RSORT(c) sort((c).rbegin(),(c).rend()) #define CLR(a) memset((a), 0 ,sizeof(a)) typedef long long ll; typedef unsigned long long ull; typedef vector vb; typedef vector vi; typedef vector vll; typedef vector vvb; typedef vector vvi; typedef vector vvll; typedef pair pii; typedef pair pll; const int dx[] = {-1,0,1,0}; const int dy[] = {0,1,0,-1}; int main() { ll have, box_n; cin >> have >> box_n; vll boxes(box_n); REP(i, box_n){ cin >> boxes[i]; } SORT(boxes); ll trg = boxes[min(boxes.size()/2, boxes.size())]; ll able = -1, cnt = 0; while(able < 0) { able = have; cnt = 0; for (auto n:boxes) { ll d = n - trg; able += d; cnt += abs(d); } // LOG("%d %d %d\n", able, cnt, trg); trg+=able/box_n-1; } cout << cnt << endl; }