#C2849. Sequence Reduction Game

    ID: 46210 Type: Default 1000ms 256MiB

Sequence Reduction Game

Sequence Reduction Game

You are given a sequence of n integers. You can perform the following operation any number of times: replace any element in the sequence with the absolute difference of two (possibly the same or different) elements in the sequence.

It can be shown that no matter how you perform the operations, the sequence can always be reduced such that all its elements become equal to the greatest common divisor (GCD) of the original numbers. The answer to the problem is the value of the GCD.

In mathematical terms, given a sequence \(a_1,a_2,\dots,a_n\), the final value is \(\gcd(a_1,a_2,\dots,a_n)\). Recall that the Euclidean algorithm for two numbers is defined as \(\gcd(a,b)=\gcd(b,a\bmod b)\), with \(\gcd(a,0)=a\).

inputFormat

The input consists of two lines:

  1. The first line contains an integer n (1 ≤ n ≤ 105), the number of elements in the sequence.
  2. The second line contains n space-separated integers representing the elements of the sequence. Each integer is between 1 and 109.

outputFormat

Output a single integer — the smallest possible sum achievable from the sequence. Based on the operations described, this sum is equal to the greatest common divisor (GCD) of all the numbers in the sequence.

## sample
1
5
5