#C6122. Minimize Sum of Absolute Differences

    ID: 49848 Type: Default 1000ms 256MiB

Minimize Sum of Absolute Differences

Minimize Sum of Absolute Differences

You are given N boxes where each box contains a certain number of chocolates. Your goal is to reorder the boxes so that the sum of absolute differences between the number of chocolates in consecutive boxes is minimized.

In mathematical terms, after reordering the boxes such that their counts are a1, a2, ..., aN, you need to minimize the sum:

\( S = \sum_{i=2}^{N} \left|a_i - a_{i-1}\right| \)

Hint: The optimal strategy is to sort the numbers in non-decreasing order.

inputFormat

The first line contains an integer N representing the number of boxes. The second line consists of N integers, where each integer represents the number of chocolates in a box. Input is read from stdin.

outputFormat

Output a single integer representing the minimum possible sum of absolute differences between consecutive boxes. Output is written to stdout.

## sample
4
4 2 1 10
9

</p>