#C10954. Minimum Subsequence Score
Minimum Subsequence Score
Minimum Subsequence Score
Given an array of integers, your task is to select a subsequence such that the sum of absolute differences between consecutive elements is minimized.
Let (a_1, a_2, \dots, a_n) be the elements of the array. The goal is to find a subsequence (b_1, b_2, \dots, b_k) (with (k \ge 1)) such that the score defined by
[
S = \sum_{i=2}^{k} |b_i - b_{i-1}|,
]
is minimized.
A common strategy is to sort the array. Sorting minimizes the differences between adjacent elements and is optimal for this problem.
inputFormat
The input consists of two lines. The first line contains an integer (n) denoting the number of elements. The second line contains (n) space-separated integers representing the elements of the array.
outputFormat
Output a single integer representing the minimum subsequence score, i.e. the minimal sum of absolute differences between adjacent elements of the optimally chosen subsequence.## sample
1
4
0
</p>