Codeforces Round 979 (Div. 2)
A
A Gift From Orangutan
Codeforces
800
原题
←B →
翻译中…

While exploring the jungle, you have bumped into a rare orangutan with a bow tie! You shake hands with the orangutan and offer him some food and water. In return...

翻译中…

The orangutan has gifted you an array $a$ of length $n$. Using $a$, you will construct two arrays $b$ and $c$, both containing $n$ elements, in the following manner:

翻译中…
  • $b_i = \min(a_1, a_2, \ldots, a_i)$ for each $1 \leq i \leq n$.
  • $c_i = \max(a_1, a_2, \ldots, a_i)$ for each $1 \leq i \leq n$.
翻译中…

Define the score of $a$ as $\sum_{i=1}^n c_i - b_i$ (i.e. the sum of $c_i - b_i$ over all $1 \leq i \leq n$). Before you calculate the score, you can shuffle the elements of $a$ however you want.

翻译中…

Find the maximum score that you can get if you shuffle the elements of $a$ optimally.

翻译中…

Input

翻译中…

The first line contains $t$ ($1 \leq t \leq 100$) — the number of test cases.

翻译中…

The first line of each test case contains an integer $n$ ($1 \leq n \leq 1000$) — the number of elements in $a$.

翻译中…

The following line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 1000$) — the elements of the array $a$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$.

翻译中…

Output

翻译中…

For each test case, output the maximum score that you can get.

翻译中…

Note

翻译中…

In the first test case, there is no other way to rearrange $a$. So, $b = [69]$ and $c = [69]$. The only possible score is $69 - 69 = 0$.

翻译中…

In the second test case, you can rearrange $a$ as $[7, 5, 6]$. Here, $b = [7, 5, 5]$ and $c = [7, 7, 7]$. The score in this case is $(7 - 7) + (7 - 5) + (7 - 5) = 4$. It can be shown this is the maximum possible score.

翻译中…

样例

Input #1
3
1
69
3
7 6 5
5
1 1 1 2 2
Output #1
0
4
4
InputCopy OutputCopy
题目信息
题号
2030A
时间限制
1 s
内存限制
256 MB
标签
构造
贪心
数学
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。