#abc276d. D - Divide by 2 or 3

D - Divide by 2 or 3

Score : 400400 points

问题描述

给定一个正整数序列:A=(a1,a2,,aN)A=(a_1,a_2,\ldots,a_N)

你可以任意次数(包括零次)选择并执行以下操作之一:

  • 选择一个满足 1iN1 \leq i \leq Naia_i 是2的倍数的整数 ii,并将 aia_i 替换为 ai2\frac{a_i}{2}
  • 选择一个满足 1iN1 \leq i \leq Naia_i 是3的倍数的整数 ii,并将 aia_i 替换为 ai3\frac{a_i}{3}

你的目标是使序列 AA 满足 a1=a2==aNa_1=a_2=\ldots=a_N

找出实现目标所需的最小操作总次数。如果无法实现目标,则输出 -1

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

Problem Statement

You are given a sequence of positive integers: A=(a1,a2,,aN)A=(a_1,a_2,\ldots,a_N).
You can choose and perform one of the following operations any number of times, possibly zero.

  • Choose an integer ii such that 1iN1 \leq i \leq N and aia_i is a multiple of 22, and replace aia_i with ai2\frac{a_i}{2}.
  • Choose an integer ii such that 1iN1 \leq i \leq N and aia_i is a multiple of 33, and replace aia_i with ai3\frac{a_i}{3}.

Your objective is to make AA satisfy a1=a2==aNa_1=a_2=\ldots=a_N.
Find the minimum total number of times you need to perform an operation to achieve the objective. If there is no way to achieve the objective, print -1 instead.

Constraints

  • 2N10002 \leq N \leq 1000
  • 1ai1091 \leq a_i \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN

a1a_1 a2a_2 \ldots aNa_N

Output

Print the answer.

Sample Input 1

3
1 4 3

Sample Output 1

3

Here is a way to achieve the objective in three operations, which is the minimum needed.

  • Choose an integer i=2i=2 such that aia_i is a multiple of 22, and replace a2a_2 with a22\frac{a_2}{2}. AA becomes (1,2,3)(1,2,3).
  • Choose an integer i=2i=2 such that aia_i is a multiple of 22, and replace a2a_2 with a22\frac{a_2}{2}. AA becomes (1,1,3)(1,1,3).
  • Choose an integer i=3i=3 such that aia_i is a multiple of 33, and replace a3a_3 with a33\frac{a_3}{3}. AA becomes (1,1,1)(1,1,1).

Sample Input 2

3
2 7 6

Sample Output 2

-1

There is no way to achieve the objective.

Sample Input 3

6
1 1 1 1 1 1

Sample Output 3

0

update @ 2024/3/10 11:37:21

}