#P17537. [JAG 2026 Summer Camp #1] Rabbit, Rabbit, Rabbit
[JAG 2026 Summer Camp #1] Rabbit, Rabbit, Rabbit
Problem Description
There are rabbits and watering holes on a number line, where the positive direction is to the right. The -th rabbit is initially at coordinate , and the -th watering hole is at coordinate . You may perform the following operation zero or more times:
- For each of the rabbits, independently move it by a distance of to either the right or the left. If rabbits move to the right and rabbits move to the left, this operation incurs a cost of .
Note that multiple rabbits may occupy the same coordinate simultaneously.
Your objective is to reach a state where the coordinates of the rabbits match the coordinates of the watering holes in some order. Find the minimum total cost required to achieve this objective. If it is impossible, output -1.
Input Format
The input contains one or more test cases. The first line of the input contains an integer (), which is the number of test cases. The descriptions of the test cases follow, each in the following format.
N
X_1 X_2 ... X_N
Y_1 Y_2 ... Y_N
The first line contains an integer ().
The second line contains integers (). represents the initial coordinate of the -th rabbit.
The third line contains integers (). represents the coordinate of the -th watering hole. It is guaranteed that for all .
The sum of over all test cases does not exceed .
Output Format
For each test case, output the minimum total cost required to achieve the objective. If it is impossible, output -1.
3
3
2 4 8
-2 6 10
2
0 2
0 1
10
37 -82 14 91 -5 68 -47 22 55 -16
-73 44 19 -88 67 2 95 -31 58 10
6
-1
467