311015: CF1921D. Very Different Array

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

D. Very Different Arraytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Petya has an array $a_i$ of $n$ integers. His brother Vasya became envious and decided to make his own array of $n$ integers.

To do this, he found $m$ integers $b_i$ ($m\ge n$), and now he wants to choose some $n$ integers of them and arrange them in a certain order to obtain an array $c_i$ of length $n$.

To avoid being similar to his brother, Vasya wants to make his array as different as possible from Petya's array. Specifically, he wants the total difference $D = \sum_{i=1}^{n} |a_i - c_i|$ to be as large as possible.

Help Vasya find the maximum difference $D$ he can obtain.

Input

Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. This is followed by a description of the test cases.

The first line of each test case contains two integers $n$ and $m$ ($1\le n\le m\le 2 \cdot 10^5$).

The second line of each test case contains $n$ integers $a_i$ ($1\le a_i\le 10^9$). The third line of each test case contains $m$ integers $b_i$ ($1\le b_i\le 10^9$).

It is guaranteed that in a test, the sum of $m$ over all test cases does not exceed $2 \cdot 10^5$.

Output

For each test case, output a single integer — the maximum total difference $D$ that can be obtained.

ExampleInput
9
4 6
6 1 2 4
3 5 1 7 2 3
3 4
1 1 1
1 1 1 1
5 5
1 2 3 4 5
1 2 3 4 5
2 6
5 8
8 7 5 8 2 10
2 2
4 1
9 6
4 6
8 10 6 4
3 10 6 1 8 9
3 5
6 5 2
1 7 9 7 2
5 5
9 10 6 3 7
5 9 2 3 9
1 6
3
2 7 10 1 1 5
Output
16
0
12
11
10
23
15
25
7
Note

In the first example, Vasya can, for example, create the array $(1, 5, 7, 2)$. Then the total difference will be $D = |6-1|+|1-5|+|2-7|+|4-2| = 5+4+5+2 = 16$.

In the second example, all the integers available to Vasya are equal to 1, so he can only create the array $(1, 1, 1)$, for which the difference $D = 0$.

In the third example, Vasya can, for example, create the array $(5, 4, 3, 2, 1)$. Then the total difference will be $D = |1-5|+|2-4|+|3-3|+|4-2|+|5-1| = 4+2+0+2+4 = 12$.

Output

**题目大意**:

Petya有一个由n个整数组成的数组\(a_i\)。他的兄弟Vasya羡慕了,决定制作他自己的由n个整数组成的数组。

为了做到这一点,他找到了m个整数\(b_i\)(\(m \ge n\)),现在他想从中选择一些n个整数,并以特定的顺序排列它们,以获得一个长度为n的数组\(c_i\)。

为了避免和他的兄弟相似,Vasya希望他的数组尽可能与Petya的数组不同。具体来说,他希望总差异\(D = \sum_{i=1}^{n} |a_i - c_i|\)尽可能大。

帮助Vasya找到他可以获得的最大差异\(D\)。

**输入数据格式**:

每个测试包含多个测试用例。第一行包含一个整数\(t\)(\(1 \le t \le 100\))——测试用例的数量。接下来是测试用例的描述。

每个测试用例的第一行包含两个整数\(n\)和\(m\)(\(1 \le n \le m \le 2 \cdot 10^5\))。

每个测试用例的第二行包含\(n\)个整数\(a_i\)(\(1 \le a_i \le 10^9\))。第三行包含\(m\)个整数\(b_i\)(\(1 \le b_i \le 10^9\))。

保证在一个测试中,所有测试用例的\(m\)之和不超过\(2 \cdot 10^5\)。

**输出数据格式**:

对于每个测试用例,输出一个整数——可以获得的最大总差异\(D\)。

**示例**:

输入:
```
9
4 6
6 1 2 4
3 5 1 7 2 3
3 4
1 1 1
1 1 1 1
5 5
1 2 3 4 5
1 2 3 4 5
2 6
5 8
8 7 5 8 2 10
2 2
4 1
9 6
4 6
8 10 6 4
3 10 6 1 8 9
3 5
6 5 2
1 7 9 7 2
5 5
9 10 6 3 7
5 9 2 3 9
1 6
3
2 7 10 1 1 5
```

输出:
```
16
0
12
11
10
23
15
25
7
```**题目大意**: Petya有一个由n个整数组成的数组\(a_i\)。他的兄弟Vasya羡慕了,决定制作他自己的由n个整数组成的数组。 为了做到这一点,他找到了m个整数\(b_i\)(\(m \ge n\)),现在他想从中选择一些n个整数,并以特定的顺序排列它们,以获得一个长度为n的数组\(c_i\)。 为了避免和他的兄弟相似,Vasya希望他的数组尽可能与Petya的数组不同。具体来说,他希望总差异\(D = \sum_{i=1}^{n} |a_i - c_i|\)尽可能大。 帮助Vasya找到他可以获得的最大差异\(D\)。 **输入数据格式**: 每个测试包含多个测试用例。第一行包含一个整数\(t\)(\(1 \le t \le 100\))——测试用例的数量。接下来是测试用例的描述。 每个测试用例的第一行包含两个整数\(n\)和\(m\)(\(1 \le n \le m \le 2 \cdot 10^5\))。 每个测试用例的第二行包含\(n\)个整数\(a_i\)(\(1 \le a_i \le 10^9\))。第三行包含\(m\)个整数\(b_i\)(\(1 \le b_i \le 10^9\))。 保证在一个测试中,所有测试用例的\(m\)之和不超过\(2 \cdot 10^5\)。 **输出数据格式**: 对于每个测试用例,输出一个整数——可以获得的最大总差异\(D\)。 **示例**: 输入: ``` 9 4 6 6 1 2 4 3 5 1 7 2 3 3 4 1 1 1 1 1 1 1 5 5 1 2 3 4 5 1 2 3 4 5 2 6 5 8 8 7 5 8 2 10 2 2 4 1 9 6 4 6 8 10 6 4 3 10 6 1 8 9 3 5 6 5 2 1 7 9 7 2 5 5 9 10 6 3 7 5 9 2 3 9 1 6 3 2 7 10 1 1 5 ``` 输出: ``` 16 0 12 11 10 23 15 25 7 ```

加入题单

上一题 下一题 算法标签: