#C2553. Minimum Sum of Absolute Differences
Minimum Sum of Absolute Differences
Minimum Sum of Absolute Differences
In this problem, you are given k categories. Each category represents a collection of book IDs, where the first number in the category is the count of books, followed by the respective book ID's. Your task is to compute, for each category, the minimum possible sum of absolute differences between successive book IDs when they are arranged in sorted order.
More formally, suppose a category has \( n \) books with IDs \( a_1, a_2, \dots, a_n \). After sorting them in ascending order to obtain \( b_1 \leq b_2 \leq \cdots \leq b_n \), you are required to calculate \[ S = \sum_{i=2}^{n} \lvert b_i - b_{i-1} \rvert. \] Output the computed sum for each category on a separate line.
This problem tests your ability to process input and implement basic sorting and summation logic efficiently.
inputFormat
The input is given via standard input (stdin) with the following format:
- The first line contains a single integer \( k \) which is the number of categories.
- Each of the next \( k \) lines describes a category. Each line begins with an integer \( n \), representing the number of book IDs in that category, followed by \( n \) space-separated integers which are the IDs of the books.
For instance:
2 3 10 20 30 4 7 1 3 10
outputFormat
For each category, output a single line to standard output (stdout) containing the minimum sum of absolute differences between consecutive book IDs after sorting them in ascending order.
For the sample input above, the expected output is:
20 9## sample
2
3 10 20 30
4 7 1 3 10
20
9
</p>