cswork / OAOpen practice workspace

AmazonOA #40

Get Fewest Operations (Box Balancing)

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, the difference between the largest and smallest final piles must reach its smallest possible value; then find the minimum number of moves needed to achieve it.

Input

The first line contains n (1..100000), and the second line contains n box counts (1..10⁹).

Output

Output a single integer answer.

Example 1

Input:

4
5 5 8 7

Output:

2

Example 2

Input:

2
1 1

Output:

0

Example 3

Input:

2
1 5

Output:

2

Example notes

Example 1: the total is 25 and the sorted target is 6, 6, 6, 7; move 2 boxes out of the pile of 8, one to each 5, for 2 moves in total. Example 2: already balanced, 0 moves. Example 3: move 2 boxes from 5 to 1, making the two piles 3, 3, for 2 moves.

Editorial

Loading solution…