Fox and Number Game

Fox Ciel is playing a game with numbers now.

Ciel has n positive integers: x1x2, ..., xn. She can do the following operation as many times as needed: select two different indexes i and j such that xi > xj hold, and then apply assignment xi = xi - xj. The goal is to make the sum of all numbers as small as possible.

Please help Ciel to find this minimal sum.

Input

The first line contains an integer n (2 ≤ n ≤ 100). Then the second line contains nintegers: x1x2, ..., xn (1 ≤ xi ≤ 100).

Output

Output a single integer — the required minimal sum.

Examples

Input
2
1 2
Output
2
Input
3
2 4 6
Output
6
Input
2
12 18
Output
12
Input
5
45 12 27 30 18
Output
15

Note

In the first example the optimal way is to do the assignment: x2 = x2 - x1.

In the second example the optimal sequence of operations is: x3 = x3 - x2x2 = x2 -x1.

 

 

The meaning of the title

Problem-solving idea: I saw that the amount of data is very small, so I can choose to use the simulation method to simulate the process. In fact, I can find that there is a law in this problem. In the end, all numbers will become the greatest common divisor of all numbers. .

 

Above code:

Simulation method:

 1 #include<stdio.h>
 2 #include<algorithm>
 3 using namespace std;
 4 int main()
 5 {
 6     int n,i,j,ans;
 7     int a[1010];
 8     scanf("%d",&n);
 9     for(i=0; i<n; i++)
10     {
11         scanf("%d",&a[i]);
12     }
13     while(1)
 14      {
 15          sort(a,a+ n);
 16          if (a[n- 1 ]==a[ 0 ]) /// Subtract each other until they are equal 
17          {
 18              break ;
 19          }
 20          for (j=n - 1 ; j> 0 ; j-- )
 21          {
 22              if (a[j]!=a[j- 1 ])
 23              {
 24                  a[j]=a[j]-a[j- 1 ];
 25              }
 26              else 
27             {
28                 continue;
29             }
30         }
31     }
32     ans=a[0]*n;
33     printf("%d",ans);
34     return 0;
35 }

 

 

To find the pattern, use GCD:

 

 1 #include<stdio.h>
 2 #include<algorithm>
 3 using namespace std;
 4 int gcd(int a,int b)
 5 {
 6     int r;
 7     while(b>0)
 8     {
 9          r=a%b;
10          a=b;
11          b=r;
12     }
13     return a;
14 }
15 int main()
16 {
17     int i,k,n,ans;
18     int a[1010];
19     scanf("%d",&n);
20     for(i=0;i<n;i++)
21     {
22         scanf("%d",&a[i]);
23     }
24     k=a[0];
25     for(i=1;i<n;i++)
26     {
27         ans=gcd(k,a[i]);
28         k=ans;
29     }
30     printf("%d\n",ans*n);
31     return 0;
32 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324868285&siteId=291194637