QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 1024 MB Total points: 100

#13347. New Year's Repeater

統計

On the eve of the Spring Festival, Mickey found a repeater on the clock tower. This repeater can not only repeat audio automatically but can also be used to find the greatest common divisor of all numbers in a sequence.

The machine is used as follows: initially, input a sequence of length $n$, where the $i$-th number is $a_i$ ($1 \le i \le n$).

Each time, Mickey can choose two adjacent numbers $a_i$ and $a_{i+1}$ and insert a number of gold coins exactly equal to the sum of these two numbers, i.e., $a_i + a_{i+1}$, into the machine. The machine then automatically calculates the greatest common divisor of these two numbers and replaces the two adjacent numbers with this result at their original position. This operation is repeated until only one number remains in the machine, which is the answer.

As the saying goes, a wealthy mouse who doesn't want to be a repeater repairman is not a good mathematician. Mickey wants to know the minimum number of gold coins required to calculate the answer.

Input

The first line contains a positive integer $n$, representing the length of the sequence.

The second line contains $n$ positive integers $a_i$, representing the sequence.

Output

A single integer representing the minimum number of gold coins used.

Examples

Input 1

7
33 33 66 6 66 22 22

Output 1

260

Note 1

Initially, the sequence is $[33, 33, 66, 6, 66, 22, 22]$.

Step 1: Merge $a_4, a_5$ to get $[33, 33, 66, 6, 22, 22]$.

Step 2: Merge $a_4, a_5$ to get $[33, 33, 66, 2, 22]$.

Step 3: Merge $a_3, a_4$ to get $[33, 33, 2, 22]$.

Step 4: Merge $a_2, a_3$ to get $[33, 1, 22]$.

Step 5: Merge $a_1, a_2$ to get $[1, 22]$.

Step 6: Merge $a_1, a_2$ to get $[1]$.

The total cost is $(6 + 66) + (6 + 22) + (66 + 2) + (33 + 2) + (33 + 1) + (1 + 22) = 260$.

Input 2

See sample data download.

Constraints

Subtask ID $n$ Score
$1$ $\le 500$ $5$
$2$ $\le 1000$ $15$
$3$ $\le 3000$ $15$
$4$ $\le 3\times 10^4$ $30$
$5$ $\le 2\times 10^5$ $35$

For all data, it is guaranteed that $1\le n\le 2\times 10^5$ and $1\le a_i \le 10^{12}$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.