Atto Round 1 (Codeforces Round 1041, Div. 1 + Div. 2)
C
Trip Shopping
Codeforces
1400
原题
← BD →
翻译中…

Ali and Bahamin decided to spend their summer vacation on the beautiful southern coasts of Iran. They also agreed to do some shopping during the trip — but instead of setting a fixed budget, they decided to determine how much they would spend by playing a game.

翻译中…

The game is played on two arrays $a$ and $b$, each containing $n$ integers.

翻译中…

The game will last for $k$ rounds. In one round:

翻译中…
  • First, Ali selects two indices $i$ and $j$ ($1 \leq i \lt j \leq n$);
  • Then, Bahamin rearranges the four integers $a_i$, $a_j$, $b_i$, and $b_j$ arbitrarily. Note that Bahamin can swap numbers between two arrays. He can also keep the two arrays unchanged.
翻译中…

After all the $k$ rounds, the value of the game is defined as $v=\sum\limits_{i=1}^{n} |a_i-b_i|$. Ali and Bahamin will spend exactly $v$ coins during their trip.

翻译中…

However, their goals are quite different:

翻译中…
  • Ali wants to spend as little as possible, that is, to minimize $v$;
  • Bahamin wants to spend as much as possible, that is, to maximize $v$.
翻译中…

You have to find the final amount of coins they will spend if both Ali and Bahamin play optimally.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $k$ ($2 \leq n \leq 2 \cdot 10^5$, $1 \leq k \leq n$) — the length of $a$ and $b$, and the number of rounds.

翻译中…

The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \leq a_i \leq 10^9$) — the elements of $a$.

翻译中…

The third line contains $n$ integers $b_1,b_2,\ldots,b_n$ ($1 \leq b_i \leq 10^9$) — the elements of $b$.

翻译中…

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

翻译中…

Output

翻译中…

For each test case, output a single integer — the final amount of coins they will spend if both Ali and Bahamin play optimally.

翻译中…

Note

翻译中…

In the first test case, Ali can only choose $(i, j) = (1, 2)$, and Bahamin can rearrange all four numbers. Thus, he can assign $a = [5, 1]$ and $b = [3, 7]$. And the value of the game will be $v=|5 - 3| + |1 - 7| = 8$. It can be shown that this is the maximum possible value reachable for Bahamin — Other arrangements like $a = [5, 7],\space b = [1, 3]$ are also possible, but they don't have larger values.

翻译中…

In the second test case, the best strategy for Bahamin is to keep the two arrays unchanged, regardless of what indices Ali selects. And the value of the game will be $v=|1 - 6| + |5 - 2| + |3 - 4| = 9$.

翻译中…

样例

Input #1
5
2 1
1 7
3 5
3 2
1 5 3
6 2 4
5 4
1 16 10 10 16
3 2 2 15 15
4 1
23 1 18 4
19 2 10 3
10 10
4 3 2 100 4 1 2 4 5 5
1 200 4 5 6 1 10 2 3 4
Output #1
8
9
30
16
312
InputCopy OutputCopy
题目信息
题号
2127C
时间限制
2 s
内存限制
256 MB
标签
博弈论
贪心
排序

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