#C7179. Minimizing Difficulty Difference Sum
Minimizing Difficulty Difference Sum
Minimizing Difficulty Difference Sum
Alice is organizing a hiking trip with her friends. There are n checkpoints and each checkpoint has a unique difficulty level. In order to make the journey as smooth as possible, Alice wants to plan the sequence of visiting these checkpoints such that the sum of the absolute differences between the difficulties of consecutive checkpoints is minimized.
Formally, given an array of difficulty levels \(a_1, a_2, \dots, a_n\), you are required to find a permutation that minimizes the following sum:
\( \sum_{i=1}^{n-1} \left|a_{i+1} - a_i\right| \)
Your task is to compute and output this minimum possible sum.
inputFormat
The input consists of two lines:
- The first line contains an integer
n
representing the number of checkpoints. - The second line contains
n
space-separated integers which represent the difficulty levels of the checkpoints.
outputFormat
Output a single integer which is the minimum possible sum of the absolute differences between the difficulties of consecutive checkpoints.
## sample4
10 1 4 8
9