311063: CF1929A. Sasha and the Beautiful Array
Description
Sasha decided to give his girlfriend an array $a_1, a_2, \ldots, a_n$. He found out that his girlfriend evaluates the beauty of the array as the sum of the values $(a_i - a_{i - 1})$ for all integers $i$ from $2$ to $n$.
Help Sasha and tell him the maximum beauty of the array $a$ that he can obtain, if he can rearrange its elements in any way.
InputEach test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 100$) — the length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array $a$.
OutputFor each test case, output a single integer — the maximum beauty of the array $a$ that can be obtained.
ExampleInput5 3 2 1 3 3 69 69 69 5 100 54 80 43 90 4 3 4 3 3 2 2 1Output
2 0 57 1 1Note
In the first test case, the elements of the array $a$ can be rearranged to make $a = [1, 2, 3]$. Then its beauty will be equal to $(a_2 - a_1) + (a_3 - a_2) = (2 - 1) + (3 - 2) = 2$.
In the second test case, there is no need to rearrange the elements of the array $a$. Then its beauty will be equal to $0$.
Output
Sasha 想要给他女朋友一个美丽的数组 \(a_1, a_2, \ldots, a_n\)。他发现女朋友认为数组的美丽值是所有整数 \(i\) 从 2 到 \(n\) 的 \((a_i - a_{i - 1})\) 值的总和。请帮助 Sasha 算出在可以任意重新排列数组元素的情况下,他可以获得的最大美丽值。
输入数据格式:
每个测试包含多个测试用例。第一行包含一个整数 \(t\) (\(1 \le t \le 500\))—— 测试用例的数量。接下来是测试用例的描述。
每个测试用例的第一行包含一个整数 \(n\) (\(2 \leq n \leq 100\))—— 数组 \(a\) 的长度。
每个测试用例的第二行包含 \(n\) 个整数 \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\))—— 数组 \(a\) 的元素。
输出数据格式:
对于每个测试用例,输出一个整数——可以获得的数组 \(a\) 的最大美丽值。
示例:
输入
```
5
3
2 1 3
3
69 69 69
5
100 54 80 43 90
4
3 4 3 3
2
2 1
```
输出
```
2
0
57
1
1
```
注意:
在第一个测试案例中,可以重新排列数组 \(a\) 使之成为 \(a = [1, 2, 3]\)。那么它的美丽值将是 \((a_2 - a_1) + (a_3 - a_2) = (2 - 1) + (3 - 2) = 2\)。
在第二个测试案例中,不需要重新排列数组 \(a\) 的元素。那么它的美丽值将是 \(0\)。题目大意: Sasha 想要给他女朋友一个美丽的数组 \(a_1, a_2, \ldots, a_n\)。他发现女朋友认为数组的美丽值是所有整数 \(i\) 从 2 到 \(n\) 的 \((a_i - a_{i - 1})\) 值的总和。请帮助 Sasha 算出在可以任意重新排列数组元素的情况下,他可以获得的最大美丽值。 输入数据格式: 每个测试包含多个测试用例。第一行包含一个整数 \(t\) (\(1 \le t \le 500\))—— 测试用例的数量。接下来是测试用例的描述。 每个测试用例的第一行包含一个整数 \(n\) (\(2 \leq n \leq 100\))—— 数组 \(a\) 的长度。 每个测试用例的第二行包含 \(n\) 个整数 \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\))—— 数组 \(a\) 的元素。 输出数据格式: 对于每个测试用例,输出一个整数——可以获得的数组 \(a\) 的最大美丽值。 示例: 输入 ``` 5 3 2 1 3 3 69 69 69 5 100 54 80 43 90 4 3 4 3 3 2 2 1 ``` 输出 ``` 2 0 57 1 1 ``` 注意: 在第一个测试案例中,可以重新排列数组 \(a\) 使之成为 \(a = [1, 2, 3]\)。那么它的美丽值将是 \((a_2 - a_1) + (a_3 - a_2) = (2 - 1) + (3 - 2) = 2\)。 在第二个测试案例中,不需要重新排列数组 \(a\) 的元素。那么它的美丽值将是 \(0\)。