AmazonOA #391
Warehouse Allocation (Distribute)
Estimated MediumJudge-ready
Topics (AI-labelled)GreedyBits & Math
One move takes a box from a non-empty pile and puts it on another pile. First make the difference between the largest and smallest final piles as small as possible; among plans that achieve this minimum difference, find the minimum number of moves. Initial counts may repeat.
Input
The first line contains n, and the second line contains n box counts. Original bounds: 1≤n≤100000, 1≤boxes[i]≤10^9; the total number of boxes stays the same, and no external difference parameter is given.
Output
Output a single integer answer.
Example 1
Input:
3
1 3 5
Output:
2
Example 2
Input:
2
5 5
Output:
0
Example 3
Input:
3
1 1 10
Output:
6
Example notes
The outputs of the three public examples are, in order: 2; 0; 6. Verified by independent enumeration or direct simulation.
Editorial
Loading solution…