#K33607. Minimize Absolute Difference Sum Rearrangement

    ID: 25125 Type: Default 1000ms 256MiB

Minimize Absolute Difference Sum Rearrangement

Minimize Absolute Difference Sum Rearrangement

You are given an array of n integers. Your task is to rearrange the array such that the sum of the absolute differences between consecutive elements is minimized. In other words, you need to minimize the value defined by the formula:

$$S = \sum_{i=1}^{n-1}\left|a_{i+1} - a_i\right|$$

A straightforward strategy to achieve the minimum sum is to sort the array in non-decreasing order. After sorting, the sum becomes:

$$S = \sum_{i=1}^{n-1}(a_{i+1} - a_i)$$

Note that since the array is sorted, each difference is non-negative. Your solution should read the input from standard input and print the result to standard output.

inputFormat

The first line contains an integer n representing the number of elements in the array. The second line contains n space-separated integers representing the elements of the array.

outputFormat

Output a single integer which is the minimized sum of the absolute differences between consecutive elements after rearranging the array.

## sample
4
4 2 1 3
3

</p>