-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path198.cpp
43 lines (39 loc) · 801 Bytes
/
198.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <sstream>
#include <algorithm>
#include <numeric>
using namespace std;
typedef long long LL;
int N;
LL B;
LL C[10];
LL cost(LL t) {
LL cost = 0;
for (int i = 0; i < N; ++i) {
cost += abs(t - C[i]);
}
return cost;
}
int main(int argc, char *argv[]) {
cin >> B;
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> C[i];
}
LL m = accumulate(C, C + N, B) / N;
LL left = *min_element(C, C + N);
LL right = min(m, *max_element(C, C + N));
for (LL a = 0; a < 100; ++a) {
if (cost((left * 2 + right) / 3) <= cost((left + right * 2) / 3)) {
right = (left + right * 2) / 3;
} else {
left = (left * 2 + right) / 3;
}
}
LL ans = 1LL << 60;
for (LL a = left; a <= min(m, right + 2); ++a) {
ans = min(ans, cost(a));
}
cout << ans << endl;
return 0;
}