Problem:
Score : 300 points

Problem Statement

There are N monsters, numbered 1, 2, ..., N.

Initially, the health of Monster i is A_i.

Below, a monster with at least 1 health is called alive.

Until there is only one alive monster, the following is repeated:

A random alive monster attacks another random alive monster.

As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.

Find the minimum possible final health of the last monster alive.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible final health of the last monster alive.

Sample Input 1

4
2 10 8 40

Sample Output 1

2

When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.

Sample Input 2

4
5 13 8 1000000000

Sample Output 2

1

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

1000000000

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();

		int min = Integer.MAX_VALUE;
		Set<Integer> monsters = new HashSet<Integer>();
		for (int i = 0; i < n; i++) {
			int a = scanner.nextInt();
			if (a < min) {
				min = a;
			}
			monsters.add(a);
		}

		while (min != 1 && 1 < monsters.size()) {
			List<Integer> listMonsters = new ArrayList<Integer>(monsters);
			monsters.clear();

			for (int i = 0; i < listMonsters.size() - 1; i++) {
				int mi = listMonsters.get(i);
				for (int j = i+1; j < listMonsters.size(); j++) {
					int mj = listMonsters.get(j);

					int next = mi < mj ? mj % mi : mi % mj;
					int next2 = mi < mj ? mi : mj;
					monsters.add(next2);
					if (next != 0) {
						monsters.add(next);
						if (next < min) {
							min = next;
						}
					}
				}
			}
		}

		System.out.println(min);

	}

}