C. Trip Shopping
games; greedy; sortings
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 < 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.
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\).
For each test case, output a single integer — the final amount of coins they will spend if both Ali and Bahamin play optimally.
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\).