33OJ
登录 注册

#P17537. [JAG 2026 Summer Camp #1] Rabbit, Rabbit, Rabbit

[JAG 2026 Summer Camp #1] Rabbit, Rabbit, Rabbit

Problem Description

There are NN rabbits and NN watering holes on a number line, where the positive direction is to the right. The ii-th rabbit is initially at coordinate XiX_i, and the ii-th watering hole is at coordinate YiY_i. You may perform the following operation zero or more times:

  • For each of the NN rabbits, independently move it by a distance of 11 to either the right or the left. If RR rabbits move to the right and LL rabbits move to the left, this operation incurs a cost of R×LR\times L.

Note that multiple rabbits may occupy the same coordinate simultaneously.

Your objective is to reach a state where the coordinates of the NN rabbits match the coordinates of the NN 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 TT (1≤T≤3×1051\le T\le 3\times 10^5), which is the number of test cases. The descriptions of the TT 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 NN (1≤N≤3×1051\le N\le 3\times 10^5).

The second line contains NN integers X1,X2,…,XNX_1,X_2,\ldots,X_N (−107≤Xi≤107-10^7\le X_i\le 10^7). XiX_i represents the initial coordinate of the ii-th rabbit.

The third line contains NN integers Y1,Y2,…,YNY_1,Y_2,\ldots,Y_N (−107≤Yi≤107-10^7\le Y_i\le 10^7). YiY_i represents the coordinate of the ii-th watering hole. It is guaranteed that Yi≠YjY_i\ne Y_j for all i≠ji\ne j.

The sum of NN over all test cases does not exceed 3×1053\times 10^5.

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