CF1843A.Sasha and Array Coloring

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Sasha found an array aa consisting of nn integers and asked you to paint elements.

You have to paint each element of the array. You can use as many colors as you want, but each element should be painted into exactly one color, and for each color, there should be at least one element of that color.

The cost of one color is the value of max(S)min(S)\max(S) - \min(S) , where SS is the sequence of elements of that color. The cost of the whole coloring is the sum of costs over all colors.

For example, suppose you have an array a=[1,5,6,3,4]a = [\color{red}{1}, \color{red}{5}, \color{blue}{6}, \color{blue}{3}, \color{red}{4}] , and you painted its elements into two colors as follows: elements on positions 11 , 22 and 55 have color 11 ; elements on positions 33 and 44 have color 22 . Then:

  • the cost of the color 11 is max([1,5,4])min([1,5,4])=51=4\max([1, 5, 4]) - \min([1, 5, 4]) = 5 - 1 = 4 ;
  • the cost of the color 22 is max([6,3])min([6,3])=63=3\max([6, 3]) - \min([6, 3]) = 6 - 3 = 3 ;
  • the total cost of the coloring is 77 .

For the given array aa , you have to calculate the maximum possible cost of the coloring.

输入格式

The first line contains one integer tt ( 1t10001 \leq t \leq 1000 ) — the number of test cases.

The first line of each test case contains a single integer nn ( 1n501 \le n \le 50 ) — length of aa .

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n ( 1ai501 \leq a_i \leq 50 ) — array aa .

输出格式

For each test case output the maximum possible cost of the coloring.

输入输出样例

  • 输入#1

    6
    5
    1 5 6 3 4
    1
    5
    4
    1 6 3 9
    6
    1 13 9 3 7 2
    4
    2 2 2 2
    5
    4 5 2 2 3

    输出#1

    7
    0
    11
    23
    0
    5

说明/提示

In the first example one of the optimal coloring is [1,5,6,3,4][\color{red}{1}, \color{red}{5}, \color{blue}{6}, \color{blue}{3}, \color{red}{4}] . The answer is (51)+(63)=7(5 - 1) + (6 - 3) = 7 .

In the second example, the only possible coloring is [5][\color{blue}{5}] , for which the answer is 55=05 - 5 = 0 .

In the third example, the optimal coloring is [1,6,3,9][\color{blue}{1}, \color{red}{6}, \color{red}{3}, \color{blue}{9}] , the answer is (91)+(63)=11(9 - 1) + (6 - 3) = 11 .

首页