#K86647. Minimum Moves to Equalize Apples

    ID: 36911 Type: Default 1000ms 256MiB

Minimum Moves to Equalize Apples

Minimum Moves to Equalize Apples

You are given n baskets, each containing a certain number of apples. In one move, you can transfer one apple from one basket to another. Your task is to determine the minimum number of moves required to make every basket contain the same number of apples. It is guaranteed that by transferring apples it is possible to achieve this equalization.

Let \( a_1, a_2, \dots, a_n \) be the number of apples in each basket. The total number of apples is \( T = \sum_{i=1}^{n} a_i \). Let \( m = \lfloor T/n \rfloor \) be the target number of apples per basket. The minimum number of moves is computed by the formula:

\( \text{moves} = \frac{1}{2} \sum_{i=1}^{n} \left|a_i - m\right| \)

Note that the division by 2 accounts for the fact that moving one apple from a basket above the target to a basket below the target fixes the imbalance in both baskets simultaneously.

inputFormat

The input is read from stdin and consists of two lines:

  • The first line contains a single integer n (the number of baskets).
  • The second line contains n space-separated integers representing the number of apples in each basket.

outputFormat

Output a single integer on stdout representing the minimum number of moves required to make all baskets contain the same number of apples.

## sample
4
1 2 3 4
2

</p>