#C4118. Minimize Sum of Adjacent Differences and Maximum Distance
Minimize Sum of Adjacent Differences and Maximum Distance
Minimize Sum of Adjacent Differences and Maximum Distance
You are given a sequence of integers. Your task is to rearrange the sequence such that the sum of the absolute differences between every pair of adjacent elements is minimized. Mathematically, if the rearranged sequence is (a_1, a_2, \ldots, a_n), you need to minimize: [ S = \sum_{i=1}^{n-1} |a_{i+1} - a_i| ] It can be proven that sorting the sequence in non-decreasing order achieves this minimum, and in that case, the sum (S) is equal to (a_n - a_1).
In addition, after reordering, compute the maximum possible distance (i.e. the difference in positions) between the minimum and maximum elements in the sequence. For a sequence of length (n), this value is (n - 1).
Note: The input list might contain duplicate elements. You should consider all elements in the order given by sorting.
inputFormat
The input is read from standard input. The first line contains a single integer (n) ((2 \leq n \leq 10^5)), representing the number of elements. The second line contains (n) space-separated integers, which may include duplicates.
outputFormat
Print two space-separated integers: the minimal possible sum of absolute differences between adjacent elements and the maximum distance (index difference) between the minimum and maximum elements in the minimized arrangement.## sample
5
3 1 4 1 5
4 4