#K68432. Optimal Coin Arrangement
Optimal Coin Arrangement
Optimal Coin Arrangement
You are given N coins, each minted in a certain year. Your task is to arrange the coins in an optimal order such that the total year differences between consecutive coins is minimized. In other words, if the sorted order of years is \(y_1, y_2, \dots, y_N\), then the sum of differences is
\(S = \sum_{i=1}^{N-1} (y_{i+1} - y_i) = y_N - y_1\).
Note that if there is only one coin, the total difference is \(0\).
The input will be provided in two lines: the first line contains the integer \(N\), and the second line contains \(N\) space-separated integers representing the minting years. Print the computed total difference to stdout.
inputFormat
The first line contains an integer \(N\) representing the number of coins. The second line contains \(N\) space-separated integers, each representing the minting year of a coin.
outputFormat
Output a single integer — the minimum total year difference between adjacent coins when they are arranged in optimal order.
## sample5
1987 2003 1995 2019 2010
32
</p>