#K56812. Minimal Painting Cost
Minimal Painting Cost
Minimal Painting Cost
You are given a collection of blocks, each with a certain height. You can rearrange the blocks in any order. When the blocks are arranged, the painting cost is defined as the sum of the absolute differences between the heights of consecutive blocks. Formally, if the blocks are rearranged into an array \(a_1, a_2, \dots, a_n\), then the cost is given by:
\[ \text{cost} = \sum_{i=2}^{n} |a_i - a_{i-1}|, \]
Your task is to compute the minimal possible painting cost after rearranging the blocks. It can be proven that the optimal solution is to sort the blocks in non-decreasing order, in which case the cost simplifies to \(a_n - a_1\). Note that if there is only one block, the cost is 0.
inputFormat
The input begins with an integer \(T\) representing the number of test cases. Each test case consists of two lines:
- The first line contains an integer \(n\) (\(1 \leq n \leq 10^5\)), the number of blocks.
- The second line contains \(n\) space-separated integers representing the heights of the blocks.
outputFormat
For each test case, output a single integer which is the minimal painting cost after rearranging the blocks. Each answer should be printed on a new line.
## sample1
5
5
0
</p>