#include #include #include #include using namespace std; long long turns(long long targetCount, const vector& boxes) { long long result = 0; for (size_t i = 0; i < boxes.size(); ++i) { result += abs(boxes[i] - targetCount); } return result; } int main() { long long b; int n; cin >> b >> n; vector boxes(n); for (int i = 0; i < n; ++i) { cin >> boxes[i]; } long long left = 0; long long right = (b + std::accumulate(boxes.begin(), boxes.end(), 0LL)) / boxes.size(); while (left < right) { long long middleLeft = (2 * left + right) / 3; long long middleRight = (left + 2 * right + 2) / 3; if (turns(middleLeft, boxes) < turns(middleRight, boxes)) { right = middleRight - 1; } else { left = middleLeft + 1; } } cout << turns(left, boxes) << endl; return 0; }