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=[i for i in map(int,input().split())]
k=0
while len(A)!=1:
    if A[1]%A[0]!=0:
        A[1]=A[1]%A[0]
        k+=1
    else:
        A.remove(A[1])
    A.sort()
print(A[0])

Submission status: Time Limit Exceeded

Solution code:
import random
N = int(input())
A = input().split(" ")
A = [int(i) for i in A]
list = [min(A)]
A = sorted(A)
iii = 0
while True:
    iii += 1
    if A[N-1] % A[N-2] != 0:
        A[N-1] = A[N-1] % A[N-2]
    elif N >= 3 and A[N-1] % A[N-3] != 0:
        A[N-1] = A[N-1] % A[N-3]
    elif N >= 4 and A[N-1] % A[N-4] != 0:
        A[N-1] = A[N-1] % A[N-4]
    elif N >= 5 and A[N-1] % A[N-5] != 0:
        A[N-1] = A[N-1] % A[N-5]
    elif N >= 6 and A[N-1] % A[N-6] != 0:
        A[N-1] = A[N-1] % A[N-6]
    elif N >= 7 and A[N-1] % A[N-7] != 0:
        A[N-1] = A[N-1] % A[N-7]
    elif N >= 8 and A[N-1] % A[N-8] != 0:
        A[N-1] = A[N-1] % A[N-8]
    elif N >= 9 and A[N-1] % A[N-9] != 0:
        A[N-1] = A[N-1] % A[N-9]
    elif N >= 10 and A[N-1] % A[N-10] != 0:
        A[N-1] = A[N-1] % A[N-10]
    elif N >= 11 and A[N-1] % A[N-11] != 0:
        A[N-1] = A[N-1] % A[N-11]
    elif N >= 12 and A[N-1] % A[N-12] != 0:
        A[N-1] = A[N-1] % A[N-12]
    elif N >= 13 and A[N-1] % A[N-13] != 0:
        A[N-1] = A[N-1] % A[N-13]
    elif N >= 14 and A[N-1] % A[N-14] != 0:
        A[N-1] = A[N-1] % A[N-14]
    elif N >= 15 and A[N-1] % A[N-15] != 0:
        A[N-1] = A[N-1] % A[N-15]
    elif N >= 16 and A[N-1] % A[N-16] != 0:
        A[N-1] = A[N-1] % A[N-16]
    elif N >= 17 and A[N-1] % A[N-17] != 0:
        A[N-1] = A[N-1] % A[N-17]
    elif N >= 18 and A[N-1] % A[N-18] != 0:
        A[N-1] = A[N-1] % A[N-18]
    elif N >= 19 and A[N-1] % A[N-19] != 0:
        A[N-1] = A[N-1] % A[N-19]
    elif N >= 20 and A[N-1] % A[N-20] != 0:
        A[N-1] = A[N-1] % A[N-20]
    elif N >= 21 and A[N-1] % A[N-21] != 0:
        A[N-1] = A[N-1] % A[N-21]
    elif N >= 22 and A[N-1] % A[N-22] != 0:
        A[N-1] = A[N-1] % A[N-22]
    elif N >= 23 and A[N-1] % A[N-23] != 0:
        A[N-1] = A[N-1] % A[N-23]
    elif N >= 24 and A[N-1] % A[N-24] != 0:
        A[N-1] = A[N-1] % A[N-24]
    elif N >= 25 and A[N-1] % A[N-25] != 0:
        A[N-1] = A[N-1] % A[N-25]

    random.shuffle(A)
    list.append(min(A))
    if iii > 100:
        break       
#print(list)
print(min(list))

Submission status: Time Limit Exceeded

Solution code:
import fractions

N = int(input())
Ai = list(map(int, input().split()))
if Ai == [Ai[0]] * len(Ai):
    print(Ai[0])
    quit()

i = 1
min = 1000000000
for a in Ai:
    for a2 in Ai[i:]:
        a_gcd = fractions.gcd(a, a2)
        if min > a_gcd:
            min = a_gcd
            if min <= 2:
                print(min)
                quit()
    i += 1

print(min)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
from functools import reduce
from fractions import gcd
import numpy as np
arr = np.array(list(set(map(int, input().split()))))

from collections import deque
q = deque([arr])
ans = arr.min()
mi = reduce(gcd, arr)
while len(q) > 0:
    ar = q.popleft()
    ans = min(ar.min(), ans)
    if ans <= mi:
        break
    if ans <= reduce(gcd, ar):
        continue
    for mon in ar:
        tmp = (ar - 1) % mon + 1
        if np.any(ar != tmp):
            q.append(np.unique(tmp))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = input()

monsters_list = list(map(int, input().split()))
monsters_list.sort(reverse=True)

while len(monsters_list) >= 2 and monsters_list[1] != 0:
    for i in range(len(monsters_list[:])):
        try:
            monsters_list[i] %= monsters_list[i+1]
        except IndexError:
            continue
        if 0 in monsters_list:
            monsters_list.remove(0)
        monsters_list.sort(reverse=True)

print(monsters_list[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = set(a)
t = []

while not len(ans) < 2:
    for i in range(n):
        t = [s%min(a) for s in a if s%min(a) != 0]
        t.append(min(a))
    a = t
    ans = set(t)

d = list(ans)
print(d[0])

Submission status: Time Limit Exceeded

Solution code:
def uauau():
    N = int(input())
    aA=sorted(set(int(_) for _ in input().split()))
    N = len(aA)
    def ggg(i,j):
        i %= j
        if i == 0:
            return j
        else:
            return ggg(j,i)

    if len(aA) == 1:
        print(aA[0])
    else:
        iMin = aA[0]
        iMax = aA[-1]
        if iMin  == 1:
            print(1)
        else:
            if False:
                pass
            #if max(i % iMin for i in aA) == 0:
            #   print(iMin)
            #   exit()
            else:
                iM = iMax
                for i in range(N):
                    iA = aA[i]
                    for j in range(i+1,N):
                        iMt = ggg(aA[j],iA)
                        if 0 < iMt and iMt < iM:
                            iM = iMt
                    if iM == 1:
                        print(1)
                        exit()
                print(iM)
uauau()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
An = [int(n) for n in input().split()]
M = sorted(set(An))
mini=M[0]
for i,x in enumerate(M):
    M = [mini] + [x for x in M[1:] if x % mini != 0]
            
while len(M) > 1:
    if 1 in M:
        mini=1
        break
    M = [M[0]] + [x-mini for x in M[1:]]
    M.sort()
    while M[0]<1:
        M.pop(0)
    mini = min(mini,M[0])
print(mini)

Submission status: Time Limit Exceeded

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


for j in range(A[N-1]):
    #0以外の最小値取得
    for i in range(N-1):
        if A[i] >= 1:
            kari = A[i]
            kari2 = i
            break
    for i in range(N-1):
        if A[i+1] > 1 and i >= kari2:
            A[i+1] = A[i+1]-kari
        A.sort()

    if A[N-2] == 0:
        break
       
ans = max(A)    


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import copy

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

com0 = None
while True:
    com = list(itertools.combinations(A,2))
    if com0 == com:
        break
    com0 = copy.copy(com)
    for q in com:
        if q[0] != 0 and q[1] != 0:
            a = 0
            if q[0] > q[1]:
                a = q[0] % q[1]
            else:
                a = q[1] % q[0]
            if a != 0:
                A.add(a)
print(min(A))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
i = input()
N = list(map(int, input().split(' ')))
def one_turn(l):
  l.sort()
  n = l[0]
  
  return np.array([n]+[ i-n for i in l[1:] if i%n != 0])

if len(set(N)) == 1 :
  print(N[0])
else :
  N = np.array(N)
  while True :
    N = one_turn(N)
    if N[0] == 1 :
      print(1)
      break
    if len(N) == 1 :
      print(N[0])
      break

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]


#%%
# n, a = 4, [2, 10, 8, 40]


#%%


#%%

#%%
while len(a) >= 2:
    a.sort()

    for i in range(len(a) -1, 0, -1):
        a[i] = a[i] % a[0]
        if a[i] == 0:
            a.remove(a[i])
    


#%%
print(a[0])

#%%

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
while True:
    a = sorted(list(set(a)))
    tmp = sorted(a[:20])
    for i in tmp:
        if i == a[0]:
            continue
        elif i % a[0] == 0:
            a.remove(i)
        else:
            a.append(i % a[0])
            a.remove(i)
    if len(a) == 1:
        break
print(a[0])

Submission status: Time Limit Exceeded

Solution code:
num_monster = int(input())
hplist = sorted(list(set(map(int, input().split()))))
if not hplist:
  print(0)
while len(hplist) != 1:
  new_hp = hplist[-1] % hplist[-2]
  del hplist[-1]
  if not new_hp in hplist and new_hp != 0:
    hplist.append(new_hp)
  hplist.sort()
print(hplist[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
while True:
    a = sorted(list(set(a)))
    tmp = sorted(a[:500])
    for i in tmp:
        if i == a[0]:
            continue
        elif i % a[0] == 0:
            a.remove(i)
        else:
            a.append(i % a[0])
            a.remove(i)
    if len(a) == 1:
        break
print(a[0])

Submission status: Time Limit Exceeded

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

if len(set(a)) == 1:
    print(a[0])
else:
    a.sort()
    count = 0
    while(1):
        count = count + 1
        for i in range(1, len(a)):
            x = a[i] + count
            for j in range(i):
                x = x % a[i-1-j]
        if x == 0:
            break

    print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
while True:
    a = sorted(list(set(a)))
    tmp = sorted(a[:])
    for i in tmp:
        if i == a[0]:
            continue
        elif i % a[0] == 0:
            a.remove(i)
        else:
            a.append(i % a[0])
            a.remove(i)
    if len(a) == 1:
        break
print(a[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
import numpy as np
arr = np.array(list(set(map(int, input().split()))))

from collections import deque
q = deque([arr])
ans = arr.min()
while len(q) > 0:
    ar = q.popleft()
    ans = min(ar.min(), ans)
    if ans == 1:
        break
    for mon in ar:
        tmp = (ar - 1) % mon + 1
        if np.any(ar != tmp):
            q.append(np.unique(tmp))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
while True:
    a = sorted(list(set(a)))
    tmp = sorted(a[:])
    for i in tmp:
        if i == a[0]:
            continue
        elif i % a[0] == 0:
            a.remove(i)
        else:
            a.append(i % a[0])
            a.remove(i)
    a.sort()
    if len(a) == 1:
        break
print(a[0])

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
list = input().split(" ")
list = [int(i) for i in list]

while len(list) >1:
  if list.count(min(list)) == len(list):
    break
  list2 = [i%min(list) for i in list if i!=min(list)]
  list2.append(min(list))
  list = [i for i in list2 if i >0]
print(min(list))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
import numpy as np
arr = np.array(list(set(map(int, input().split()))))

from collections import deque
q = deque([arr])
ans = arr.min()
while len(q) > 0:
    ar = q.popleft()
    ans = min(ar.min(), ans)
    if ans == 1:
        break
    for mon in ar:
        tmp = (ar - 1) % mon + 1
        if np.any(ar != tmp):
            q.append(np.array(list(set(tmp))))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
aA=sorted(set(int(_) for _ in input().split()))
N = len(aA)
def ggg(i,j):
    i %= j
    if i == 0:
        return j
    else:
        return ggg(j,i)

if len(aA) == 1:
    print(aA[0])
else:
    iMin = aA[0]
    iMax = aA[-1]
    if iMin  == 1:
        print(1)
    else:
        if max(i % iMin for i in aA) == 0:
            print(iMin)
            exit()
        else:
            iM = iMax
            for i in range(N):
                iA = aA[i]
                for j in range(i+1,N):
                    iMt = ggg(aA[j],iA)
                    if 0 < iMt and iMt < iM:
                        iM = iMt
                if iM == 1:
                    print(1)
                    exit()
            print(iM)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]


#%%
# n, a = 4, [2, 10, 8, 40]


#%%

a.sort()

#%%

#%%
while len(a) >= 2:
    if a[0] > a[1]:
        a[0], a[1] = a[1], a[0]

    for i in range(len(a) -1, 0, -1):
        a[i] = a[i] % a[0]
        if a[i] == 0:
            a.remove(a[i])
    


#%%
print(a[0])

#%%

Submission status: Time Limit Exceeded

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

while(1):
    s = min(box)
    num = box.index(s)

    for index,item in enumerate(box):
    
        if num != index:
        
            if item % s == 0:
                box.remove(item)
            else:
                box[index] = item % s

    if len(box) == 1:
        break

print(box[0],end='')

Submission status: Time Limit Exceeded

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

if all(A[0]==a for a in A):
    print(A[0])
else:
    while len(A) != 1:
        B = []
        for a in A:
            for i in range(len(A)):
                if A[i]%a != 0:
                    B.append(A[i]%a)
            if B is not None:
                B.sort()
                C = list(set(B))
                C.sort()
        if B == []:
            break
        else:
            A = C
    print(C[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
while True:
    a = list(set(a))
    tmp = a[:]
    for i in tmp:
        if i == a[0]:
            continue
        elif i % a[0] == 0:
            a.remove(i)
        else:
            a.append(i % a[0])
    a.sort()
    if len(a) == 1:
        break
print(a[0])

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=sorted(map(int,input().split()))[::-1]
ans=A[0]
while True:
    M=A[0]
    m=A[1]
    del A[0]
    if M==1:ans=1;break
    if m==0:break
    A.append(M%m)
    A=sorted(A)[::-1]
    ans=min(ans,A[0])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
aA=sorted(set(int(_) for _ in input().split()))
N = len(aA)
def ggg(i,j):
    if i < j :
        i,j = j,i
    i %= j
    if i == 0:
        return j
    else:
        return ggg(j,i)

if len(aA) == 1:
    print(aA[0])
else:
    iMin = aA[0]
    iMax = aA[-1]
    if iMin  == 1:
        print(1)
    else:
        if max(i % iMin for i in aA) == 0:
            print(iMin)
            exit()
        else:
            iM = iMax
            for i in range(N):
                iA = aA[i]
                for j in range(i+1,N):
                    iMt = ggg(iA,aA[j])
                    if 0 < iMt and iMt < iM:
                        iM = iMt
                if iM == 1:
                    print(1)
                    exit()
            print(iM)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
import numpy as np
arr = np.array(list(set(map(int, input().split()))))

from collections import deque
q = deque([arr])
ans = arr.min()
while len(q) > 0:
    ar = q.popleft()
    ans = min(ar.min(), ans)
    if ans == 1:
        break
    for mon in ar:
        tmp = (ar - 1) % mon + 1
        if np.any(ar != tmp):
            q.append(tmp)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = list(map(int,input().split()))

mi = min(A)

def mak(num):
    if num < 1:
        return []
    elif num == 1:
        return [1]
    else:
        divisor_list = []
        divisor_list.append(1)
        for i in range(2, num // 2 + 1):
            if num % i == 0:
                divisor_list.append(i)
        divisor_list.append(num)

        return divisor_list
    
P = mak(mi)[1:][::-1]
lenp = len(P)

Q = np.array(A)

an = 1
flag = 0
for i in range(lenp):
    R = Q % P[i]
    if any(R) == 0:
        if flag == 1:
            flag = 0
            continue
        an = P[i]
        break
    else:
        if i != lenp - 1:
            if max(R) < P[i+1]:
                flag = 1
        Q = R
print(an)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int,input().split())))
while len(A) > 1:
    min_A = min(A)
    A = A-min_A
    A = A[A>0]
    A = np.append(A,[min_A])
    answer = min_A
    if answer == 1:
        break
print(min_A)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
An = [int(n) for n in input().split()]
M = sorted(set(An))
mini=M[0]
while len(M) > 1:
    if 1 in M:
        mini=1
        break
    M = [M[0]] + [x-mini for x in M[1:]]
    M.sort()
    while M[0]<1:
        M.pop(0)
    mini = min(mini,M[0])
print(mini)

Submission status: Time Limit Exceeded

Solution code:
#ABC118C-MonstersBattleRoyale.py

from sys import stdin

input = stdin.readline

N = int(input())

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

A = set(A)
A = list(A)

a = 0

while len(A) != 1:
    A.sort()
    for i in range(1,len(A)):
        A[i] = A[i] - A[0]
    if A[0] == 0:
        A.pop(0)
    if len(A) <= N/10000:
        break

i = 0
while True:
    if A[i] != 0:
        print(A[i])
        break
    i += 1

Submission status: Time Limit Exceeded

Solution code:
#ABC118C-MonstersBattleRoyale.py

from sys import stdin

input = stdin.readline

N = int(input())

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

A = set(A)
A = list(A)

a = 0

while len(A) != 1:
    A.sort()
    for i in range(1,len(A)):
        A[i] = A[i] - A[0]
    if A[0] == 0:
        A.pop(0)
    if len(A) <= N/100:
        break

i = 0
while True:
    if A[i] != 0:
        print(A[i])
        break
    i += 1

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())))
i=0
while(a[-2] != 0):
    a[-1]=a[-1]-(a[-1]//a[i])*a[i]
    if(a[-1]==0):
        i+=1
        a=sorted(a)
print(a[-1])

Submission status: Time Limit Exceeded

Solution code:
#ABC118C-MonstersBattleRoyale.py

from sys import stdin

input = stdin.readline

N = int(input())

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

A = set(A)
A = list(A)

a = 0

while len(A) != 1:
    A.sort()
    for i in range(1,len(A)):
        A[i] = A[i] - A[0]
    if A[0] == 0:
        A.pop(0)
    if len(A) <= N/2:
        break

i = 0
while True:
    if A[i] != 0:
        print(A[i])
        break
    i += 1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
life = list(map(int, input().split()))
while len(life) >1:
    life.sort()
    if life[0] == 1 : break
    life_array = [life[0]] + [i%life[0] for i in life if i%life[0] !=0]
    life = []
    for i in life_array:
        if i not in life:
            life.append(i)
 
print(life[0])

Submission status: Time Limit Exceeded

Solution code:
import fractions

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

if Ai == [Ai[0]] * len(Ai):
    print(Ai[0])
    quit()

Ai.sort()

i = 1
diff_list = []
min = 1000000000
for a in Ai:
    for a2 in Ai[i:]:
        a_gcd = fractions.gcd(a, a2)
        if min > a_gcd:
            min = a_gcd
        if min <= 2:
            print(min)
            quit()
    i += 1

print(min)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
while len(a)!=1:
    a = sorted(a)
    a = [i%min(a) for i in a[1:]]+[min(a)]
    a = [i for i in a if i!=0]
print(min(a))

Submission status: Time Limit Exceeded

Solution code:
#-*- coding: utf-8 -*-
"""
https://yahoo-procon2019-qual.contest.atcoder.jp/tasks/yahoo_procon2019_qual_a
"""

from collections import defaultdict
import sys
def rec2(S):
    if len([s for s in S if s > 0]) == 1:
        return max(S)
    else:
        mini = sys.maxsize
        for i in range(len(S)):
            for j in range(len(S)):
                if i == j: continue
                S2 = []
                for s in S:
                    S2.append(S[j] - S[i])
                mini = min(mini, rec2(S2))
        return mini



def sol2(n, S):
    return rec2(S)


def sol(n, S):

    while len(S) >= 2:
        mini = min(S)
        maxi = max(S)
        if maxi % mini == 0:
            S.remove(maxi)
        else:
            tmp = maxi % mini
            S.remove(maxi)
            S.append(tmp )

    return mini



test = False
#test = True
if test:
    n = 4
    S = [2, 10, 8, 40]
    #S =list( map(int, "1 6 3".split()))
    print(sol(n, S))

    n = 4
    S = [5, 13, 8, 1000000000]
    #S =list( map(int, "1 6 3".split()))
    print(sol(n, S))

    n = 3
    S = [1000000000, 1000000000, 1000000000]
    print(sol(n, S))

else:
    n = int(input().strip())
    #n, k = map(int, input().split())
    #print("%s %s %s %s" % (type(n), type(k), n, k))
    S =list( map(int, input().split()))
    print(sol(n, S))

Submission status: Time Limit Exceeded

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

    while True:
        m = min(a)
        new = [0 for i in range(n)]
        new[0] = m

        ind=1
        for i in range(len(a)):
            if a.index(m) != i:
                num = a[i]%m
                if num != 0:
                    new[ind] = a[i]%m
                    ind+=1

        a = new[:ind]

        if len(a) == 1:
            print(min(a))
            exit()



if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

while(1):

    num = box.index(min(box))#最小値のインデックス
    #print(num,box)
    for index,item in enumerate(box):
    
        if num != index:
        
            if item % min(box) == 0:
                box.remove(item)
            else:
                box[index] = item % min(box)

    if len(box) == 1:
        break

print(box[0],end='')

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
lst = list(map(int, input().split()))
while len(lst) > 1:
    Max = max(lst)
    Min = min(lst)
    if Max % Min == 0 :
        lst.remove(Max)
    else:
        lst.remove(Max)
        newcomer = Max % Min
        lst.append(newcomer)
ans = lst[0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())))
i=0
while(a[-2] != 0):
    a[-1]=a[-1]-(a[-1]//a[i])*a[i]
    if(a[-1]==0):
        i+=1
    a=sorted(a)
print(a[-1])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
while len(a) >= 2 :
    k = a[1] % a[0]
    if k == 0 :
        a.pop(1)
    else :
        a[1] = k
    a.sort()
    if a[0] == 1 :
        break
print(a[0])

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
def li(): return map(int, stdin.readline().split())
N = li()
A = list(li())

def gcd(A, B):
    # fast return
    if B == 1 or A == 1:
        return 1

    # normal gcd logic
    while B not in (1, 0):
        A, B = min(A, B), abs(A - B)
    if B == 0:
        return A
    elif B == 1:
        return 1

A = list(filter(lambda x:x>0, A))
A.sort()
if len(A) == 0:
    print(0)
else:
    res = A[0]
    for a in A:
        res = gcd(a, res)
        if res == 1:
            break

    print(res)

Submission status: Time Limit Exceeded

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

N = int(input())
ALL = sorted(list(map(int,input().split())))

while len(ALL) > 1:
  ALL.append(gcd(ALL[0],ALL[1]))
  del ALL[0]
  del ALL[0]
  
print(ALL[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
life = list(map(int, input().split()))
min = 1000000000000
while len(life) >1:
    life = list(set(life))
    for i in life:
        if i < min: min = i
    
    life_array=[]
    for i in life:
        if i%min != 0 and i not in life_array: life_array.append(i%min)
    life = [min] + life_array

print(life[0])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = list(map(int,input().split()))

mi = min(A)

def mak(num):
    if num < 1:
        return []
    elif num == 1:
        return [1]
    else:
        divisor_list = []
        divisor_list.append(1)
        for i in range(2, num // 2 + 1):
            if num % i == 0:
                divisor_list.append(i)
        divisor_list.append(num)

        return divisor_list
    
P = mak(mi)[1:][::-1]
lenp = len(P)

Q = np.array(A)

an = 1
for i in range(lenp):
    R = Q % P[i]
    if any(R) == 0:
        an = P[i]
        break
    else:
        Q = R[R!=0]

print(an)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=[i for i in map(int,input().split())]
k=0
while k!=len(A):
    k=0
    for i in range(len(A)):
        if A[i]%min(A)!=0:
            A[i]=A[i]%min(A)
        else:
            k+=1  
print(min(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
aA=list(set(int(_) for _ in input().split()))
N = len(aA)
def ggg(i,j):
    if i < j :
        i,j = j,i
    i %= j
    if i == 0:
        return j
    else:
        return ggg(j,i)

if len(aA) == 1:
    print(aA[0])
else:
    iMin = min(aA)
    iMax = max(aA)
    if iMin  == 1:
        print(1)
    else:
        if max(i % iMin for i in aA) == 0:
            print(iMin)
            exit()
        else:
            iM = iMax
            for i in range(N):
                iA = aA[i]
                for j in range(i+1,N):
                    iMt = ggg(iA,aA[j])
                    if 0 < iMt and iMt < iM:
                        iM = iMt
                    if iM == 1:
                        print(1)
                        exit()
            print(iM)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
A = list(set(A))
A.sort(reverse=True)

result = 0
while True:
    if len(A) == 1:
        break
    if A[-1] == 1:
        result = 1
        break
    A[0] = A[0] % A[1]
    if A[0] == 0:
        A.pop(0)
    A = list(set(A))
    A.sort(reverse=True)

if result != 0:
    print(result)
else:
    print(A[0])

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())))
i=0
while(a[-2] != 0):
    if(a[-1]%a[i]==0):
        a[-1]=0
        i+=1
    else:
        a[-1]=a[-1]-(a[-1]//a[i])*a[i]
    a=sorted(a)
print(a[-1])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = list(map(int,input().split()))

mi = min(A)

def mak(num):
    if num < 1:
        return []
    elif num == 1:
        return [1]
    else:
        divisor_list = []
        divisor_list.append(1)
        for i in range(2, num // 2 + 1):
            if num % i == 0:
                divisor_list.append(i)
        divisor_list.append(num)

        return divisor_list
    
P = mak(mi)[1:][::-1]
lenp = len(P)

Q = np.array(A)

an = 1
for i in range(lenp):
    R = Q % P[i]
    if any(R) == 0:
        an = P[i]
        break
    else:
        Q = R

print(an)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
An = [int(n) for n in input().split()]
M = sorted(set(An))
mini=M[0]
while len(M) > 1:
    if 1 in M:
        mini=1
        break
    M = [M[0]] + [M[x]-mini for x in range(1,len(M))]
    M.sort()
    M = [x for x in M if x >= 1]
    mini = min(mini,M[0])
print(mini)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
aA=list(set(int(_) for _ in input().split()))
N = len(aA)
def ggg(i,j):
    if i < j :
        i,j = j,i
    i %= j
    if i == 0:
        return j
    else:
        return ggg(j,i)

if len(aA) == 1:
    print(aA[0])
else:
    iMin = min(aA)
    iMax = max(aA)
    if iMin  == 1:
        print(1)
    else:
        iDm = [i % iMin for i in aA]
        if max(iDm) == 0:
            print(iMin)
            exit()
        else:
            iM = iMax
            for i in range(N):
                iA = aA[i]
                for j in range(i+1,N):
                    iMt = ggg(iA,aA[j])
                    if 0 < iMt and iMt < iM:
                        iM = iMt
                    if iM == 1:
                        print(1)
                        exit()
            print(iM)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
life = list(map(int, input().split()))
min = 1000000000000
while len(life) >1:
    for i in life:
        if i < min: min = i
    life_array=[]
    for i in life:
        if i%min != 0 and i not in life_array: life_array.append(i%min)
    life = [min] + life_array

print(life[0])

Submission status: Time Limit Exceeded

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

while True:
    t = max(arr) % min(arr)
    if t == 0:
        arr.pop(arr.index(max(arr)))
    else:
        arr[arr.index(max(arr))] = t
    
    if len(arr) == 1 or max(arr) == min(arr):
        print(min(arr))
        break

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a_s=list(map(int,input().split()))
while True:
    a_s=set(a_s)
    a_s.discard(0)
    a_s=list(a_s)
    index=a_s.index(min(a_s))
    for i in range(len(a_s)):
        if i!=index and min(a_s)!=0:
            a_s[i]=a_s[i]%min(a_s)
    if len(a_s)==1:
        break
print(sum(a_s))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = map(int, input())
A = list(map(int, input().split()))

while len(A) >= 2:
    x = A.pop(A.index(max(A)))
    y = min(A)
    n = x % y
    if n == 0:
        pass
    else:
        A.append(n)

print(A[0])

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())))
i=0
while(a[-2] != 0):
    if(a[-1]%a[i]==0):
        a[-1]=0
        i+=1
    else:
        a[-1]=a[-1]-a[i]
    a=sorted(a)
print(a[-1])

Submission status: Time Limit Exceeded

Solution code:
# import math
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


input()
a_list = [int(s) for s in input().split()]
N = len(a_list)

min_last_hp = 10 ** 9

for start in range(N):
    for c in range(start+1, N):
        last_hp = gcd(a_list[start], a_list[c])
        # if min_last_hp == None:
        #     min_last_hp = last_hp
        if last_hp < min_last_hp:
            min_last_hp = last_hp

print(min_last_hp)

Submission status: Time Limit Exceeded

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

A.sort()


if((A[1] - A[0]) == 0):
    min_HP = A[0]
    print(min_HP)
else:
    min_HP = A[1] - A[0]
    while((A[1] - A[0]) > 0):
        A.insert(0, A[1] - A[0])
        A.sort()
        if((A[1] - A[0]) == 0):
            break
        min_HP = A[1] - A[0]

    # そのループでまだmin_HPが更新されていない
    min_flag = False
    while(True):
        for a in A:
            if ((a % min_HP) < min_HP and (a % min_HP) > 0):
                min_HP = a % min_HP
                min_flag = True
        if( min_HP > 0 and min_flag ):
            continue
        else:
            break
    print(min_HP)