#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]; long long ave = std::accumulate(boxes.begin(), boxes.end(), 0) / box_num; long long count = 0; for (size_t i = 0; i < boxes.size(); i++) { if (boxes[i] > ave) { for (size_t j = 0; j < boxes.size(); j++) { while(boxes[i] != ave && boxes[j] < ave){ boxes[i]--; boxes[j]++; count += 2; } } if (boxes[i] != ave) count += boxes[i] - ave; } else if(boxes[i] < ave) { for (size_t j = 0; j < boxes.size(); j++) { while (boxes[i] != ave && boxes[j] > ave) { boxes[i]++; boxes[j]--; count+=2; } } if (boxes[i] != ave) count += ave - boxes[i]; } } std::cout << count << std::endl; return 0; }