#C1307. Minimum Dissatisfaction Score

    ID: 42567 Type: Default 1000ms 256MiB

Minimum Dissatisfaction Score

Minimum Dissatisfaction Score

Problem Statement:

You are given an array of n integers. The dissatisfaction score of an arrangement of the array is defined as

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

Your task is to find the minimum possible dissatisfaction score by rearranging the elements of the array. It can be shown that sorting the array in non-decreasing order produces the minimum score.

Note: When n = 1, the dissatisfaction score is 0 since there is only one element.

inputFormat

The input is given via standard input (stdin). The first line contains an integer n (the number of elements in the array). The second line contains n space-separated integers representing the array.

outputFormat

Output via standard output (stdout) a single integer: the minimum possible dissatisfaction score that can be achieved.## sample

4
2 4 1 3
3