#include #include #include #include #include int main() { int box_num, having_candy_num; std::cin >> having_candy_num >> box_num; std::vector boxes(box_num); for (int i = 0; i < box_num; i++) std::cin >> boxes[i]; std::sort(boxes.begin(), boxes.end()); long long median = boxes.size() / 2; long long count = 0; for (int i = median; i >= 0; i--) { long long h = having_candy_num; for (size_t j = 0; j < boxes.size(); j++) { if (boxes[j] > boxes[i]) { count += boxes[j] - boxes[i]; h += boxes[j] - boxes[i]; } else if (boxes[j] < boxes[i]) { count += boxes[i] - boxes[j]; h -= boxes[i] - boxes[j]; } } if (h >= 0)break; count = 0; } std::cout << count << std::endl; return 0; }