#abc254h. Ex - Multiply or Divide by 2

Ex - Multiply or Divide by 2

Score : 600600 points

问题陈述

给定两个包含 NN 个非负整数的多重集:A={a1,,aN}A=\{ a_1,\ldots,a_N \}B={b1,,bN}B=\{ b_1,\ldots,b_N \}

您可以按照任意顺序执行以下操作任意次数:

  • AA 中选择一个非负整数 xx。从 AA 中删除一个 xx 的实例,并添加一个 2x2x 的实例。
  • AA 中选择一个非负整数 xx。从 AA 中删除一个 xx 的实例,并添加一个 x2\left\lfloor \frac{x}{2} \right\rfloor 的实例。(x\lfloor x \rfloor 表示不超过 xx 的最大整数。)

您的目标是使 AABB 相等(作为多重集)。

确定这一目标是否可以实现,并在可实现的情况下找到实现所需的最小操作次数。

以上为通义千问 qwen-max 翻译,仅供参考。

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.

Sample Input 1

3
3 4 5
2 4 6

Sample Output 1

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\}.

Sample Input 2

1
0
1

Sample Output 2

-1

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

update @ 2024/3/10 10:50:50