Section: 2, Score: 19, Time limit per test: 30 seconds, Memory limit per test: 512MB, Input: stdin, Output: stdout
When an employee joins Agoda, they receive a box containing a set number of goodies, each with a value tag attached. All boxes are equivalent and contain an equal number of goodies.
However, on one occasion, the delivery team makes a mistake and sends two boxes with an equal number of goodies, but with different values.
You are given the task of making both the boxes equivalent with the following operation:
Choose two indices
Two goodies are equivalent if they have the same value. Two boxes are equivalent if for every goodie in the first box, there is a unique equivalent goodie in the second box.
Help calculate the minimum cost to make the boxes equivalent.
The first line contains an integer
The next line contains
The next line contains
Print the minimum cost to make the boxes equivalent. If it is not possible to make both boxes equivalent, print -1.
4
4 2 2 2
1 4 1 2
1
Swap index 1 of box1 with index 0 of box2, which has cost 1. Now box1 = [4,1,2,2]
and box2 = [2,4,1,2]
. Rearranging both the boxes makes them equal.
4
2 3 4 1
3 2 5 1
-1