#CF2237B. 烦人的幽灵 / B. Annoying the Ghost
烦人的幽灵 / B. Annoying the Ghost
B. Annoying the Ghost
Source: Codeforces 2237B
Contest: Order Capital Round 2 (Codeforces Round 1104, Div. 1 + Div. 2)
Time limit: 1.5 seconds
Memory limit: 256 megabytes
Statement
Ja the Ghost is playing with rubber ducks. He has piles of rubber ducks arranged in a row, where the -th pile contains ducks. Quack the Duck gives Ja a strictly increasing sequence and commands him to make the piles become exactly this sequence.
Ja performs the process in the following two stages:
- Ja may add any number of ducks to each pile. Formally, for each pile , he chooses a non-negative integer and replaces with .
- Ja may repeatedly swap two adjacent piles. Formally, he may perform the following operation any number of times, possibly zero: choose an index such that , and swap the values of and .
A process is called valid if, after both stages end, the sequence of pile sizes is exactly .
Find the minimum possible number of operations performed in the second stage among all valid processes. If there is no valid process, output .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains an integer () — the number of piles of rubber ducks.
The second line contains integers () — the initial number of rubber ducks in each pile.
The third line contains integers () — the final number of rubber ducks in each pile.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output a single integer — the minimum possible number of operations performed in the second stage among all valid processes. If there is no valid process, output .
Note
In the first test case, Ja only needs the first stage. He can set , so the piles become . No swaps are needed, so the answer is .
In the second test case, Ja needs both stages. He can set , so the piles become . Then he can perform two swaps: $$ [2,3,1]\to [2,1,3]\to [1,2,3]. $$ The pile with duck must move from the third position to the first position, so at least two swaps are necessary. Therefore the answer is .
In the third test case, it is impossible. The first pile initially contains ducks, but every number in the target sequence is at most . Since Ja can only add ducks and cannot remove them, this pile cannot become equal to any number in the target sequence. Therefore the answer is .
In the fourth test case, no ducks need to be added. Ja only needs to reorder the piles into increasing order. The minimum number of adjacent swaps is .
In the fifth test case, no ducks need to be added. Again, Ja only needs to reorder the piles into increasing order. The minimum number of adjacent swaps is .
Examples
10
3
1 2 2
1 3 5
3
2 2 1
1 2 3
2
5 1
2 4
6
6 5 4 3 2 1
1 2 3 4 5 6
7
4 7 1 6 2 5 3
1 2 3 4 5 6 7
2
2 1
2 3
4
3 2 2 1
1 2 3 4
4
4 3 2 1
1 3 4 5
5
1 5 4 3 2
2 3 4 5 6
5
10 3 8 6 9
3 6 8 9 10
0
2
-1
15
12
0
4
4
3
5