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



Sample input:
4
2 10 8 40


Sample output:
2

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]

def attack(A):
  A = list(set(A))
  if min(A) ==0:
    A.pop(A.index(min(A)))
  attack_mons = min(A)
  attack_mons_index = A.index(min(A))
  new_A = []
  for i in range(len(A)):
    if i != attack_mons_index:
      new_A.append(A[i]%min(A))
    else:
      new_A.append(A[i])
  return new_A

new_A = attack(A)
while A!=new_A:
  A = new_A
  new_A = attack(new_A)

print(min(new_A))

Submission status: Time Limit Exceeded

Solution code:
N = input()
A = {int(x) for x in input().split()}

while True:
    if len(A) == 1:
        break
    mn = min(A)
    A = {x for x in A if x % mn != 0 or x == mn}

print(A.pop())

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))

def gcd(a,b):
    a,b=max(a,b),min(a,b)
    while b>=1:
        a,b=b,a%b
    return a

def multigcd(list):
    L=list[:]
    for _ in range(len(list)-1):
        L=[gcd(L[0],L[1])]+L[2:]
    return L[0]

print(multigcd(A))

Submission status: Time Limit Exceeded

Solution code:
from __future__ import print_function
# import numpy as np
# import numpypy as np
import sys
import math
import string
import fractions
import re
import array
import copy
import functools

import collections
import itertools
import bisect
import heapq


from heapq import heappush
from heapq import heappop

# from itertools import accumulate
# from collections import deque
import random

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)


def main():
    n = int(input())
    l = list(map(int, input().split()))

    # eprint("=======================")
    while True:
        xx = min([x for x in l if x > 0])
        l = [(tt if tt==xx else tt%xx) for tt in l]

        # eprint(l)
        if all(x==l[0] for x in l):
            print(l[0])
            return 0
        if 1 in l:
            print(1)
            return 0
        if sum(l)==xx:
            print(xx)
            return 0
        # eprint("=======================")

    print(xx)
    return 0


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from __future__ import print_function
# import numpy as np
# import numpypy as np
import sys
import math
import string
import fractions
import re
import array
import copy
import functools

import collections
import itertools
import bisect
import heapq


from heapq import heappush
from heapq import heappop

# from itertools import accumulate
# from collections import deque
import random

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)


def main():
    n = int(input())
    l = list(map(int, input().split()))

    # eprint("=======================")
    while True:
        xx = min([x for x in l if x > 0])
        l = [(tt if tt==xx else tt%xx) for tt in l]

        # eprint(l)
        if all(x==l[0] for x in l):
            print(l[0])
            return 0
        if 1 in l:
            print(1)
            return 0
        if sum(l)==xx:
            print(xx)
            return 0
        # eprint("=======================")

    print(xx)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from __future__ import print_function
# import numpy as np
# import numpypy as np
import sys
import math
import string
import fractions
import re
import array
import copy
import functools

import collections
import itertools
import bisect
import heapq


from heapq import heappush
from heapq import heappop

# from itertools import accumulate
# from collections import deque
import random

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)


def main():
    n = int(input())
    l = list(map(int, input().split()))

    # eprint("=======================")
    while True:
        xx = min([x for x in l if x > 0])
        l = [(tt if tt==xx else tt%xx) for tt in l]

        # eprint(l)
        if all(x==l[0] for x in l):
            print(l[0])
            return
        if 1 in l:
            print(1)
            return 0
        if sum(l)==xx:
            print(xx)
            return 0
        # eprint("=======================")

    print(xx)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N=int(input())
A=[i for i in map(int,input().split())]

def gcd(x,y):
    while y!=0:
        x , y = y , x%y
    return x

ans=1e10
for comb in combinations(A,2):
    ans=min(ans,gcd(comb[0],comb[1]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
monsters = list(map(int, input().split()))

monsters.sort(reverse=True)
#print(monsters)
ans = min(monsters)

while ans > 0:
    maxnum = max(monsters)
    minnum = min(monsters)
    maxidx = monsters.index(maxnum)

    if maxnum % minnum == 0:
        del monsters[maxidx]
        if len(monsters) == 1:
            print(monsters[0])
            quit()
    else:
        ans = maxnum % minnum
        monsters[maxidx] = ans

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = [0]*n
ans = min(a)
for j in range(min(a)):
	for i in range(n):
		b[i] = a[i]%ans
	if max(b) == 0:
		break
	else:
		ans = max(b)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)

def multigcd(n,arr):
  a,b=max(n,arr[0]),min(n,arr[0])
  while b!=0:
    a,b=b,a%b
  if len(arr)==1:
    return a
  else:
    return multigcd(a,arr[1:])

n=int(input())
arr=list(map(int,input().split()))
print(multigcd(arr[0],arr[1:]))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()

while A[-2] != 0:
    A[-1] -= A[-2] * (A[-1] // A[-2])
    A.sort()

print(A[-1])

Submission status: Time Limit Exceeded

Solution code:
input()
monsters = list(map(int, input().split()))
val = min(monsters)
min_monster = val
while val > 0:
    if min_monster % val:
        continue
    if not any([m % val for m in monsters]):
        break
    val -= 1

print(val)

Submission status: Time Limit Exceeded

Solution code:
input()
monsters = list(map(int, input().split()))
val = min(monsters)
while val > 0:
    if not any([m % val for m in monsters]):
        break
    val -= 1

print(val)

Submission status: Time Limit Exceeded

Solution code:
input()
monsters = sorted(map(int, input().split()))
val = 0
candidates = monsters
min_val = min(candidates) + 1
while min_val != min(candidates):
    min_val = min(candidates)
    next_candidates = []
    for n in range(len(candidates)):
        div_val = candidates[n]
        next_candidates.append(
            min(m % div_val if m % div_val else div_val for m in monsters)
        )
    candidates = next_candidates

print(min(candidates))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
hoge=list(map(int,input().split()))
###
def huga(a,b):
  x=max(a,b)
  y=min(a,b)
  z=1
  for i in range(y):
    wa=y-i
    if(x%wa==0 and y%wa==0):
      z=wa
      break
  return z
###
hoge.sort(reverse=True)
ans=hoge[0]
for i in range(n-1):
  ans=huga(ans,hoge[i+1])
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
hoge=list(map(int,input().split()))
###
def huga(a,b):
  x=max(a,b)
  y=min(a,b)
  z=1
  for i in range(y):
    wa=y-i
    if(x%wa==0 and y%wa==0):
      z=wa
      break
  return z
###
hoge.sort()
ans=hoge[0]
for i in range(n-1):
  ans=huga(ans,hoge[i+1])
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
hoge=list(map(int,input().split()))
###
def huga(a,b):
  x=max(a,b)
  y=min(a,b)
  z=1
  for i in range(y):
    wa=i+1
    if(x%wa==0 and y%wa==0):
      z=wa
  return z
###
hoge.sort()
ans=hoge[0]
for i in range(n-1):
  ans=huga(ans,hoge[i+1])
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def del_list(a):
  x = []
  for i in a:
    if i != 0:
      x.append(i)
      
  return x
 
 
 
n = int(input())
m = list(map(int, input().split()))
 
 
while True:
  mil = min(m)
  c = 0
  for i in range(len(m)):
    if m[i] == mil and c == 0:
      c += 1
      continue
    m[i] -= mil
    
  if 0 in m:
    m = del_list(m)
  
  if len(m) == 1:
    break
    
print(m[0])

Submission status: Time Limit Exceeded

Solution code:
import fractions
n=int(input())
a=list(map(int,input().split()))

while (len(a)>1):
    b=fractions.gcd(a[0],a[1])
    if b==1:
        print(1)
        exit()
    del a[1]
    del a[0]
    a.append(b)
    a=list(set(a))
print(a[0])

Submission status: Time Limit Exceeded

Solution code:
import fractions
n=int(input())
a=list(map(int,input().split()))

while (len(a)>1):
    b=fractions.gcd(a[0],a[1])
    del a[1]
    del a[0]
    a.append(b)
print(a[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
res1 = 0
res2 = pow(10,9)
rmA = 0
index = 0
reslen = N
A.sort()
while reslen > 1:
    for i in range(N-1):
        res1 = A[i+1] % A[0]
        if res1 < res2:
            res2 = res1
            index = i+1
    if res2 != 0:
        A[index] = res2
        A.sort()
    else:
        A.remove(A[index])
        N = N - 1
    reslen = len(A)
    res2 = pow(10,9)
print(A[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
hp_list = list(map(int, input().split()))
i = min(hp_list)
while True:
    dividable = True
    for j in hp_list:
        if j % i != 0:
            dividable = False
    if dividable:
        break
    else:
        i -= 1
print(i)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))

living = N
while(living > 1):
    A = sorted(A)
    if A[N-1] % A[N-2] == 0:
        living -= 1
    A[N-1] = A[N-1] % A[N-2]
    
print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(min(A),0,-1):
    if all( A[c] % i == 0 for c in range(N)):
        ans = i
        break   
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import numpy as np

N = int(stdin.readline().rstrip().split()[0])
A = [int(x) for x in stdin.readline().rstrip().split()]
# N, X = [int(x) for x in stdin.readline().rstrip().split()]

def G(_a, _b):
  if _a>=_b:
    a, b = _a, _b
  else:
    a, b = _b, _a
  
  while True:
    c = a%b
    if c == 0:
      break
    a,b = b,c
  
  return b
ans=0
L=list(set(A))
L_array = np.array(L)
if np.sum(L%np.min(L))==0:
  ans = min(L)
while ans==0:
  if len(L)==1:
    ans = L[0]
    break
  if min(L)==1:
    ans = 1
    break
  g = G(L[-2], L[-1])
  L[-2] = g
  L.pop()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import numpy as np

N = int(stdin.readline().rstrip().split()[0])
A = [int(x) for x in stdin.readline().rstrip().split()]
# N, X = [int(x) for x in stdin.readline().rstrip().split()]

def G(_a, _b):
  if _a>=_b:
    a, b = _a, _b
  else:
    a, b = _b, _a
  
  while True:
    c = a%b
    if c == 0:
      break
    a,b = b,c
  
  return b
L=list(set(A))
while True:
  if len(L)==1:
    ans = L[0]
    break
  elif min(L)==1:
    ans = 1
    break
  else:
    g = G(L[-2], L[-1])
    L[-2] = g
    L.pop()
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import numpy as np

N = int(stdin.readline().rstrip().split()[0])
A = [int(x) for x in stdin.readline().rstrip().split()]
# N, X = [int(x) for x in stdin.readline().rstrip().split()]

def G(_a, _b):
  if _a>=_b:
    a, b = _a, _b
  else:
    a, b = _b, _a
  
  while True:
    c = a%b
    if c == 0:
      break
    a,b = b,c
  
  return b
L=A
while True:
  if len(L)==1:
    ans = L[0]
    break
  elif min(L)==1:
    ans = 1
    break
  else:
    g = G(L[-2], L[-1])
    L[-2] = g
    L.pop()
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
m = 1
c = 0
for i in range(1, int(min(A))+1):
    c = 1
    for j in range(N):
        if A[j] % i != 0:
            c = 0
            break
    if c == 1:
        m = max(i, m)
print(m)

Submission status: Time Limit Exceeded

Solution code:
n,*h=map(int,open(0).read().split())
while len(h)>1:
    kh=set()
    for x in h:
        mn = min(h)
        md=x%mn
        if md!=0:
            kh.add(md)
        if len(kh)==mn-1:
            break
    kh.add(mn)
    h=kh
print(mn)

Submission status: Time Limit Exceeded

Solution code:
n,*h=map(int,open(0).read().split())
while len(h)>1:
    kh=set()
    for x in h:
        mn = min(h)
        md=x%mn
        if md!=0:
            kh.add(md)
        kh.add(mn)
    h=kh
print(mn)

Submission status: Time Limit Exceeded

Solution code:
n,*h=map(int,open(0).read().split())
while len(h)>1:
    kh=[]
    for i in range(len(h)):
        mn = min(h)
        md=h[i]%mn
        if md!=0:
            kh.append(md)
        kh.append(mn)
    h=list(set(kh))
print(mn)

Submission status: Time Limit Exceeded

Solution code:
n,*h=map(int,open(0).read().split())
while len(h)>1:
    kh=[]
    for i in range(len(h)):
        mn=min(h)
        md=h[i]%mn
        if md!=0:
            kh.append(md)
        else:
            kh.append(mn)
    h=list(set(kh))
print(mn)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
HP_list = [int(i) for i in input().split()]
while len(set(HP_list))>1:
    for i in range(0, len(set(HP_list))):
        min_HP = min(HP_list)
        if HP_list[i] > min_HP:
            HP_list[i] = HP_list[i]%min_HP
            if HP_list[i]==0:
                HP_list[i] += min_HP
print(list(set(HP_list))[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
HP_list = [int(i) for i in input().split()]
while len(set(HP_list))>1:
    HP_list[-1] = min([HP_list[-1]%i for i in HP_list[:-1]])
    if HP_list[-1]== 0:
        HP_list.pop(-1)
    else:
        HP_list.sort()
print(HP_list[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
HP_list = [int(i) for i in input().split()]
HP_list.sort()
while len(HP_list)>1:
    HP_list[-1] = min([HP_list[-1]%i for i in HP_list[:-1]])
    if HP_list[-1]== 0:
        HP_list.pop(-1)
    else:
        HP_list.sort()
print(HP_list[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
HP_list = [int(i) for i in input().split()]

while len(HP_list)>1:
    HP_list.sort()
    #attacked_list = [HP_list[-1]%i for i in HP_list[:-1]]
    HP_list[-1] = min([HP_list[-1]%i for i in HP_list[:-1]])
    HP_list.sort()
    if HP_list[0] == 0:
        HP_list=HP_list[1:]
print(HP_list[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
HP_list = [int(i) for i in input().split()]
while len(HP_list)>1:
    HP_list.sort()
    HP_max = HP_list.pop(-1)
    attacked_list = [HP_max%i for i in HP_list]
    HP_list.append(min(attacked_list))
    HP_list.sort()
    if HP_list[0] == 0:
        HP_list.pop(0)
print(HP_list[0])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8-*-

n = int(input())
a = list(map(int, input().split(" ")))

a.sort()
m = a[0]
a = a[1:]
while True:
    a = list(map(lambda x : x % m, a))
    a.sort()
    if a[-1] == 0:
        break
    else:
        for i in a:
            if i != 0:
                t = i
        a[0] = m
        m = t
print(m)

Submission status: Time Limit Exceeded

Solution code:
###C - Monsters Battle Royale
N = int(input())
A = list(map(int, input().split()))

while(len(set(A)) != 1):
    for i in range(N):
        minA  = min(A)
        if A[i] > minA:
            time = A[i] // minA
            if time == 1:
                A[i] =A[i] -  time * minA
            else:
                A[i] =A[i] -  (time - 1) * minA
                
print(min(A))

Submission status: Time Limit Exceeded

Solution code:
###C - Monsters Battle Royale
N = int(input())
A = list(map(int, input().split()))
jlist=[True]
cnt = 0
while(min(A) !=1 and len(set(A)) != 1):
    cnt = 0
    for i in range(N):
        if A[i] - min(A) > 0:
            A[i] = A[i] - min(A)
print(min(A))

Submission status: Time Limit Exceeded

Solution code:
###C - Monsters Battle Royale
N = int(input())
A = list(map(int, input().split()))
jlist=[True]
while(any(jlist) and len(set(A)) != 1):
    for i in range(N):
        if A[i] - min(A) > 0:
            A[i] = A[i] - min(A)
            jlist.append(False)
        else:
            jlist.append(True)
print(min(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))


def bubble_sort(lst):
    for i in reversed(range(len(lst))):
        for j in range(i):
            if lst[j] > lst[j + 1]:
                lst[j], lst[j + 1] = lst[j + 1], lst[j]


bubble_sort(a)
zero_monster_indexes = []

while True:
    for monster_i in range(1, len(a)):
        if a[monster_i] == 0:
            zero_monster_indexes.append(monster_i)
        else:
            a[monster_i] %= a[0]

            if a[monster_i] == 0:
                zero_monster_indexes.append(monster_i)

    for zero_monster_index in reversed(zero_monster_indexes):
        a.pop(zero_monster_index)
    zero_monster_indexes.clear()

    bubble_sort(a)

    if len(a) == 1:
        break

print(a[0])

Submission status: Time Limit Exceeded

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

N = input()
ls = [int(x) for x in input().split()]
ls.sort()
while len(ls) != 1:
    if ls[1] % ls[0] == 0:
        ls.remove(ls[1])
    else:
        ls[1] = ls[1] - ls[0]
        ls.sort()

print(ls[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(int(m) for m in input().split())
ans = 1
for t in reversed(range(min(A) - 1)):
  B = list()
  for x in range(N):
    B.append(A[x] % (t+2))
    if B.count(0) != x+1:
      break
  if B.count(0) == N:
    ans = t+2
    break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
while True:
    a[0] = a[0] % a[1]
    if a[0] <= 0:
        a = a[1:]
    a.sort(reverse=True)
    if len(a) == 1:
        print(a[0])
        break

Submission status: Time Limit Exceeded

Solution code:

def gcd(a, b):
    lower = min(a, b)
    bigger = max(a, b)

    for i in range(lower, 0, -1):
        if a%i==0 and b%i==0:
            return i


def solve():
    N = int(input())
    A = [int(x) for x in input().split(" ")]

    d = 10**9
    for i in range(1, N):
        d = min(gcd(A[0], A[i]), d)

    print(d)


if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
xs = [int(x) for x in input().split()]

def gcd(a,b):
  return a if b==0 else gcd(b, abs(a%b))

min_gcd = 10**18
for i in range(n):
  for j in range(i+1,n):
    min_gcd = min(min_gcd, gcd(xs[i], xs[j]))
print(min_gcd)

Submission status: Time Limit Exceeded

Solution code:
from queue import PriorityQueue
N = int(input())
A = map(int, input().split())
q = PriorityQueue()
for a in A:
  q.put(a)
x = q.get()
while not q.empty():
  y=q.get()
  if y == 0:
    continue
  q.put(x % y)
  x = y
print(x)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))

A.sort()

R = A[0]

while True:
    C = A.copy()
    B = [A[0]] + [A[i+1] - A[i] for i in range(len(A)-1) if A[i+1] > A[i]]
    B = list(set(B))
    B.sort()
    R = min(R, B[0])
    if C == B:
        break
    elif R == 1:
        break
    else:
        A = B

print(R)

Submission status: Time Limit Exceeded

Solution code:
from sys import setrecursionlimit

setrecursionlimit(10000)

N = int(raw_input())
health = map(int, raw_input().strip().split())

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def gcd_driver(lst):
	if len(lst) <= 1:
		return lst[0]

	if len(lst) > 2:
		lst[1] = gcd(lst[0], lst[1])
		return gcd_driver(lst[1:])

	return gcd(lst[0], lst[1])

print(gcd_driver(health))

Submission status: Time Limit Exceeded

Solution code:
import random
from copy import deepcopy

random.seed(12345)

N = int(raw_input().split()[0])
inp = map(int, raw_input().split())

health = {}
for k, v in enumerate(inp):
  health[k] = v


l = len(health.keys())
while l > 1:
  available = health.keys()
  
  remaining = deepcopy(health.keys())

  attacker = random.choice(available)
  remaining.remove(attacker)
  defender = random.choice(remaining)

  rem_health = health[defender] - health[attacker]

  if rem_health == 0:
    health.pop(defender, None)
    l -= 1
  elif rem_health < 0:
  	continue

  else:
  	health[defender] = rem_health

print health.values()[0]

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

N = int(input())
A = list(map(int, input().split()))

from functools import reduce

def gcd(a, b):
    if a<b:
      a,b = b,a
    while b != 0:
        a, b = b, a % b
    return a

def lcm(a, b):
    return (a * b) // gcd(a, b)

G,L = A[0], 1
for i in range(N-1):
  G = gcd(G,A[i+1])
  L = L * A[i+1] // G
  #print(G,L)
  
print(G)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]

def t(A):
    a = min(A)
    while a == 0:
        A.remove(0)
        a = min(A)
    f = A.index(a)
    for i in range(0,len(A)):
        A[i] = A[i] % a
    A[f] = A[f] + a
    return A

while len(A)>1:
    A = t(A)

print(max(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = []
A = input().split()
A = [int(i) for i in A]
A.sort()
A.reverse()
while True:
    A[0] = A[0] % A[1]
    A.sort()
    A.reverse()
    if A[1] == 0:
        break
print(A[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = []
A = input().split()
A = [int(i) for i in A]
A.sort()
A.reverse()
while True:
    A[0] = A[0] - A[1]
    A.sort()
    A.reverse()
    if A[1] == 0:
        break
print(A[0])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

def fun():
    n = (int(i) for i in input().split())

    l = [int(x) for x in stdin.readline().rstrip().split()]

    l = list(set(l))

    while 1 < len(l):
        for i in range(1, len(l)):
            if l[i]%l[0] == 0:
                l[i] = l[0]
            elif l[i] - l[i-1] == 1:
                print(1)
                return
            else:
                l[i] = l[i]%l[0]
        #l = [e for e in l if e != 0]
        l = list(set(l))
        #l.sort()

        if l[0]==1:
            print(1)
            return

    print(l[0])
    return

if __name__ == '__main__':
    fun()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

def fun():
    n = (int(i) for i in input().split())

    l = [int(x) for x in stdin.readline().rstrip().split()]

    l.sort()

    while 1 < len(l):
        for i in range(1, len(l)):
            if l[i]%l[0] == 0:
                l[i] = l[0]
            elif l[i] - l[i-1] == 1:
                print(1)
                return
            else:
                l[i] = l[i]%l[0]
        #l = [e for e in l if e != 0]
        l = list(set(l))
        #l.sort()

        if l[0]==1:
            print(1)
            return

    print(l[0])
    return

if __name__ == '__main__':
    fun()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

def fun():
    n = (int(i) for i in input().split())

    l = [int(x) for x in stdin.readline().rstrip().split()]

    l.sort()

    while 1 < len(l):
        for i in range(1, len(l)):
            if l[i]%l[0] == 0:
                l[i] = l[0]
            elif l[i] - l[i-1] == 1:
                print(1)
                return
            else:
                l[i] = l[i]%l[0]
        l = [e for e in l if e != 0]
        l.sort()

        if l[0]==1:
            print(1)
            return

    print(l[0])
    return

if __name__ == '__main__':
    fun()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

def fun():
    n = (int(i) for i in input().split())

    l = [int(x) for x in stdin.readline().rstrip().split()]

    l.sort()

    while 1 < len(l):
        for i in range(1, len(l)):
            if l[i]%l[0] == 0:
                l[i] = l[i] - l[0]
            elif l[i] - l[i-1] == 1:
                print(1)
                return
            else:
                l[i] = l[i]%l[0]
        l = [e for e in l if e != 0]
        l.sort()

    print(l[0])
    return

if __name__ == '__main__':
    fun()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

n = (int(i) for i in input().split())

l = [int(x) for x in stdin.readline().rstrip().split()]

l.sort()

while 1 < len(l):
    for i in range(1, len(l)):
        if l[i]%l[0] == 0:
            l[i] = l[i] - l[0]
        else:
            l[i] = l[i]%l[0]
    l = [e for e in l if e != 0]
    l.sort()

print(l[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
# print(N,A,sep="\n")
data = set(A)

def culc(data):
  A_min=min(data)
  if len(set(map(lambda x: x%A_min,data)))==1:
    return A_min
  for i in range(A_min,1,-1):
    if len(set(map(lambda x: x%i,data)))==1:
      return i
  return 1


print(culc(data))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
mon = input().split(" ")
for i in range(N):
    a = int(mon[i])
    mon[i]=a
def gcd(x,y):
    if y ==0:
        return x
    else:
        return gcd(y,x%y)
for i in range(N):
    for m in range(N):
        if i !=m:
            k = gcd(mon[i],mon[m])
            mon[i] =k
            mon[m] = k

print(min(mon))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
mini=min(A)
while mini>0:
    for i in range(0,len(A)):
        if A[i]!=mini:
            A[i]=A[i]%mini
    z=A.count(0)
    A=sorted(A)[z:]
    m=A[0]
    if m==1:
        break
    else:
        if [i%m for i in A]==[0]*len(A):
            break
print(A[0])

Submission status: Time Limit Exceeded

Solution code:
#C Monsters Battle Rpyale

N = int(input())
monster_life_lst = list(map(int, input().split()))
flag = False 
while True:
    monster_life_lst.sort()
    if len(monster_life_lst) == 1:
        flag = True
        out_flag = monster_life_lst[0]
        break
    else:
        monster_life_lst[1] -= monster_life_lst[0]
        if monster_life_lst[1] == 0:
            monster_life_lst.remove(0)

def out(out_flag):
    print(out_flag)

out(out_flag)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))

# A内の整数値を用いて作られる最小の整数を求めよ、という問題


def gcd(a, b):
    """
    自然数a, b の最大公約数を求める
    """
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


ans = float('inf')
for i in range(N):
    for j in range(i, N):
        ans = min(ans, gcd(A[i], A[j]))


print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
while len(a)>1:
    m=min(a)
    def f(n):
        return n-m
    a=map(f,a)
    a=set(a)
    a.add(m)
    a.remove(0)
    a=list(a)
print(a[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = [int(s) for s in input().split(' ')]

while len(L) > 1:
  L.sort()
  if L[-1] % L[0] == 0:
    L.pop()
  else:
    L[-1] = L[-1] % L[0]
print(L[0])

Submission status: Time Limit Exceeded

Solution code:
import fractions
from itertools import combinations
N=int(input())
A=list(map(int,input().split()))
comb = list(combinations(A, 2))
ans = max(A)
for c in comb:
    gcd = fractions.gcd(c[0],c[1])
    if ans > gcd:
        ans = gcd
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
N=int(input())
A=list(map(int,input().split()))
ans = max(A)
for a in A:
    temp_A = [temp_a for temp_a in A if temp_a != a]
    for ta in temp_A:
        gcd = fractions.gcd(a,ta)
        if ans > gcd:
            ans = gcd
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
def mon():
    n=int(input())
    a=[int(i) for i in input().split()]
    a.sort()
    za=a[0]

    b=[i for i in a[1:] if i%za!=0]
    if b==[]:
        print(za)
        exit()
    c=[za]

    for i in b:
        c+=[i%za]

    while not len(c)==1:
        c.sort(reverse=True)
        c[0]=c[0]-c[1]
        if c[0]<=0:
            c.pop(0)

    return(c[0])


print(mon())

Submission status: Time Limit Exceeded

Solution code:
def gcd(a, b):
    while b != a:
        if a < b:
            while a < b:
                b = b - a
        elif b < a:
            while b < a:
                a = a - b
    return a

def m(a):
    a.sort()
    l = len(a)
    b = []
    for i in range(l // 2):
        b.append(gcd(a[i], a[l - i - 1]))
    if l % 2 == 1:
        b.append(a[l // 2])
    return list(set(b))

    
n = int(input())
a = [int(x) for x in input().split(" ")]

while len(a) > 1:
    a = m(a)

print(a[0])

Submission status: Time Limit Exceeded

Solution code:
def gcd(a, b):
    while a != b:
        if a < b:
            b = b - a
        else:
            a = a - b
    return a

def m(a):
    a.sort()
    l = len(a)
    b = []
    for i in range(len(a) // 2):
        b.append(gcd(a[i], a[i + 1]))
    if l % 2 == 1:
        b.append(a[l - 1])
    return b

    
n = int(input())
a = [int(x) for x in input().split(" ")]

while len(a) > 1:
    a = m(a)

print(a[0])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)

def gcd(i,j):
  if i>j:
    i,j=j,i
  if j%i==0:
    return i
  return gcd(j-i,i)

n=int(input())
l=[int(i) for i in input().split()]
g=gcd(l[0],l[1])
for i in range(2,len(l)):
  g=gcd(g,l[i])
print(g)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)

def gcd(i,j):
  if i>j:
    i,j=j,i
  if j%i==0:
    return i
  return gcd(i,j-i)

n=int(input())
l=[int(i) for i in input().split()]
g=gcd(l[0],l[1])
for i in range(2,len(l)):
  g=gcd(g,l[i])
print(g)

Submission status: Time Limit Exceeded

Solution code:
#-*-coding:utf8;-*-
#qpy:3
#qpy:console
n = int(input())
hp = list(map(int, input().split()))
while len(hp) != 1:
    minhp = min(hp)
    hp = [i % minhp if not i == minhp else i for i in hp]
    hp = [i for i in hp if not i == 0]
    if min(hp) == 1:
        minhp = min(hp)
        break
print(minhp)