atcoder#ABC254H. [ABC254Ex] Multiply or Divide by 2

[ABC254Ex] Multiply or Divide by 2

Score : 600600 points

Problem Statement

You are given multisets with NN non-negative integers each: A={a1,,aN}A=\{ a_1,\ldots,a_N \} and B={b1,,bN}B=\{ b_1,\ldots,b_N \}. You can perform the operations below any number of times in any order.

  • Choose a non-negative integer xx in AA. Delete one instance of xx from AA and add one instance of 2x2x instead.
  • Choose a non-negative integer xx in AA. Delete one instance of xx from AA and add one instance of x2\left\lfloor \frac{x}{2} \right\rfloor instead. (x\lfloor x \rfloor is the greatest integer not exceeding xx.)

Your objective is to make AA and BB equal (as multisets). Determine whether it is achievable, and find the minimum number of operations needed to achieve it if it is achievable.

Constraints

  • 1N1051 \leq N \leq 10^5
  • 0a1aN1090 \leq a_1 \leq \ldots \leq a_N \leq 10^9
  • 0b1bN1090 \leq b_1 \leq \ldots \leq b_N \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

a1a_1 \ldots aNa_N

b1b_1 \ldots bNb_N

Output

If the objective is achievable, print the minimum number of operations needed to achieve it; otherwise, print -1.

3
3 4 5
2 4 6
2

You can achieve the objective in two operations as follows.

  • Choose x=3x=3 to delete one instance of x(=3)x\, (=3) from AA and add one instance of 2x(=6)2x\, (=6) instead. Now we have A={4,5,6}A=\{4,5,6\}.
  • Choose x=5x=5 to delete one instance of x(=5)x\, (=5) from AA and add one instance of x2(=2)\left\lfloor \frac{x}{2} \right\rfloor \, (=2) instead. Now we have A={2,4,6}A=\{2,4,6\}.
1
0
1
-1

You cannot turn {0}\{ 0 \} into {1}\{ 1 \}.