← 返回 amazon 的题目列表Minimum Operations to Minimize Maximum Box Pile Difference
类型:online_judge
amazon
Amazon has several warehouses that store piles of boxes containing goods to be shipped. Each warehouse contains n piles, numbered from 1 to n, where pile i has boxes[i] boxes. The caretaker can perform the following operation an unlimited number of times to even out the distribution: choose two distinct piles and move one box from one pile to another. Design an algorithm to find the minimum operations required to achieve the possible smallest maximum difference.
Input Format
An integer n (1 ≤ n ≤ 10^5), the number of piles.
An integer array boxes, of length n, where boxes[i] denotes the number of boxes in pile i.
Output Format
An integer, the minimum number of operations required to achieve the minimum possible difference.
Sample Input
5
5 8 2 7 4
Sample Output
2
Explanation
You can move a box from the second pile to the third and then from the second pile to the third again, [5, 8, 2, 7, 4] -> [6, 6, 7, 6, 4]
Constraints
1 ≤ n ≤ 10^5
1 ≤ boxes[i] ≤ 10^9
Example
Input
5
5 8 2 7 4