Problem:


Score : 300 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on the blackboard.

You will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written.

Find the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Output

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Sample Input 1

3
7 6 8

Sample Output 1

2

If we replace 7 with 4, the greatest common divisor of the three integers on the blackboard will be 2, which is the maximum possible value.

Sample Input 2

3
12 15 18

Sample Output 2

6

Sample Input 3

2
1000000000 1000000000

Sample Output 3

1000000000

We can replace an integer with itself.



Sample input:
3
7 6 8


Sample output:
2

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

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

m = 0
for i in range(n):
  now = gcd_list(a[:i] + a[(i + 1):])          
  if m < now:
    m = now

print(m)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
import fractions
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

N=int(input())
A = list(map(int,input().split()))
mx=0
import itertools
for J in itertools.combinations(A, N-1):
  z = gcd_list(J)
  if z >= mx:
     mx = z
print(mx)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
import fractions

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)


n = int(input())
a = list(map(int, input().split(" ")))
res = []
for i in range(n):
    tmp = a[:]
    del tmp[i]
    res.append(gcd_list(tmp))

print(max(res))

Submission status: Time Limit Exceeded

Solution code:
import collections

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

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

divisors = []
for i in range(N):
    divisors.extend(make_divisors(A[i]))
clist = collections.Counter(divisors)

anslist = []
for a, b in clist.most_common():
    if N - 1 <= b:
        anslist.append(a)

print(max(anslist))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

def gcd_mul(*numbers):
    return reduce(gcd, numbers)

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

l = []
for i in range(n):
    a_reduced = a[:i] + a[i+1:]
    l.append(gcd_mul(*a_reduced))

print(max(l))

Submission status: Time Limit Exceeded

Solution code:
N =int(input())

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

#ユークリットの互除法の関数定義
def gcd(a, b):
    if b == 0: return a
    return gcd(b, a%b)

ans_lst = []
n = N - 1
if N == 1:
    print(A_lst[0])
elif N == 2:
    print(max(A_lst[0], A_lst[1]))
else:
    for i in range(N):
        B_lst = []
        B_lst +=  A_lst
        del B_lst[i]
        element = gcd(B_lst[0], B_lst[1])
        if n == 2:
            ans_lst.append(element)
        else:
            for i in range(2, n):
                element = gcd(element, B_lst[i])
            ans_lst.append(element)
    print(max(ans_lst))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors


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

cnt_div = dict()
for _a in a:
    for d in make_divisors(_a):
        if d in cnt_div:
            cnt_div[d] += 1
        else:
            cnt_div[d] = 1

max_div = 1
for r, c in cnt_div.items():
    if c >= n - 1:
        max_div = max(max_div, r)

print(max_div)

Submission status: Time Limit Exceeded

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


def calc(a, b):
  while a != 0 and b != 0:
    if a < b:
      b -= a
    else:
      a -= b

  return a if a != 0 else b


def main():
  input()
  a = list(map(int, input().split()))
  p = [a[0], a[1], calc(a[0], a[1])]

  for v in a[2:]:
    for j in range(len(p) - 1):
      p[j] = calc(p[j], v)

    p.append(calc(p[-1], v))

  print(max(p), flush=True)


if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted([int(i) for i in input().split()])
ma = A[1]
minus = 1
if sum([a%2!=0 for a in A])>1:
    minus = 2
    if ma%2==0:
        ma -= 1

for i in range(ma):
    tof = [a%ma==0 for a in A]
    if sum(tof)>=N-1:
        break
    if A[2]//2 > ma-minus:
        ma -= minus
    else:
        ma = int((ma+1)//2)
        
print(ma)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
A = [int(n) for n in input().split()]
enh = [1 for _ in range(N)]
maxn = 1

dev = 2
while True:
    cnt = [0 for _ in range(N)]
    endf = 0

    for i in range(N):
        while A[i] % dev == 0:
            A[i] /= dev
            cnt[i] += 1
        if A[i] == 1:
            endf += 1
            continue

    arg = np.argsort(np.array(cnt))
    maxn *= max([1, dev ** cnt[arg[0]]])
    d = cnt[arg[1]] - cnt[arg[0]]
    if d != 0:
        enh[arg[0]] *= d * dev

    if dev == 2:
        dev = 3
    else:
        dev += 2

    if endf == N:
        break

print(maxn * max(enh))

Submission status: Time Limit Exceeded

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

def list_gcd(v_list,max):
    for i in range(len(v_list)):
        v_list[0] = gcd(v_list[0],v_list[i])
        if max > v_list[0]:
            return max
    return v_list[0]

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

max = 0
for i in range(n):
    tmp = v_list[:i]
    tmp.extend(v_list[i+1:])
    tmp_v = list_gcd(tmp,max)
    if tmp_v > max:
        max = tmp_v

print(max)

Submission status: Time Limit Exceeded

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

def gcd(x, y):
    g = x % y
    if g == 0:
        return y
    return gcd(y, g)

sw = 0
for i in range(N):
    w = A[0 if i != 0 else 1]
    for j in range(N):
        if j != i:
            w = gcd(w, A[j])
    sw = max(sw, w)

print(sw)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

A.sort()

gcd_left = []
gcd_left.append(A[0])
for i in range(N-1):
    gcd_left.append(fractions.gcd(gcd_left[i],A[i+1]))


A.reverse()
gcd_right = []
gcd_right.append(A[0])
for i in range(N-1):
    gcd_right.insert(0,fractions.gcd(gcd_right[0],A[i+1]))


min_gcd = 1
if min_gcd < gcd_right[1]:
    min_gcd = gcd_right[1]
if min_gcd < gcd_left[N-2]:
    min_gcd = gcd_left[N-2]
for i in range(0,N-2):
    tmp = fractions.gcd(gcd_left[i],gcd_right[i+2])
    if tmp > min_gcd:
        min_gcd = tmp
        
print(min_gcd)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
def gcd(numbers):
    return reduce(fractions.gcd, numbers)
N = int(input()); A = list(map(int, input().split())) 
ret = 0
for i in range(N):
  ret = max(ret, gcd([A[j] for j in range(N) if i != j]))
print(ret)

Submission status: Time Limit Exceeded

Solution code:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

N = int(input())
A = list(map(int, input().split()))
ans = 0
if len(A) == 1:
    ans = A[0]
elif len(A) == 2:
    ans = max(A)
else:
    d = {}
    for a in A:
        T = make_divisors(a)
        for t in T:
            if t in d:
                d[t] += 1
            else:
                d[t] = 1
    for k, v in d.items():
        if v >= N - 1:
            ans = max(ans, k)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
memo = {}

def gcd(a, b):
    if (a, b) in memo.keys():
        return memo[(a, b)]
    if b == 0:
        return a
    return gcd(b, a % b)

n = input()
a_list = map(int, raw_input().split())
max_g = 0
for exclude in reversed(xrange(n)):
    g = a_list[0] if exclude > 0 else a_list[1]
    for i, a in enumerate(a_list):
        if i == 0 or i == exclude:
            continue
        g_new = gcd(g, a)
        if (g, a) not in memo:
            memo[(g, a)] = g_new
        g = g_new
        if g <= max_g:
            break
    if g > max_g:
        max_g = g
print(max_g)

Submission status: Time Limit Exceeded

Solution code:
import copy
from functools import reduce

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

def gcd_(list):
  return reduce(gcd, list)

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

b = copy.copy(a)
c = []

for i in range(n):
  del b[i]
  c.append(gcd_(b))
  b = copy.copy(a)
  
print(max(c))

Submission status: Time Limit Exceeded

Solution code:
import fractions
import functools
n=input()
n=int(n)
a=input().split()
a=[int(i) for i in a]
gll=list()
for i in range(n):
    s=a[0]
    del a[0]
    gcd = functools.reduce(fractions.gcd, a)
    gll.append(gcd)
    a.append(s)
print(max(gll))

Submission status: Time Limit Exceeded

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


def gcd(l):
    flag = {'gcd':True}
    g = 0
    m = min(l)
    for i in range(1,m+1):
        flag['gcd'] = True
        for j in l:
            if j % i != 0:
                flag['gcd'] = False
                break

        if flag['gcd']:
            g = i
    return g

memo = 1
for i in range(N):
    g = gcd(l[:i]+l[i+1:])
    if g > memo:
        memo = g

print(memo)

Submission status: Time Limit Exceeded

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

wari_num = []
for i in range(1, max(a)+1):
    if max(a) % i == 0:
        wari_num.append(i)

one = 0
for w in wari_num[::-1]:
    for j in a:
        if j % w != 0:
            one += 1

        if one == 2:
            one = 0
            break

    if one == 1:
        print(w)
        exit()
    else:
        one = 0

print(a[0])

Submission status: Time Limit Exceeded

Solution code:
def gcd(num_list,a):
  for i in range(min(num_list),a,-1):
    D = [o%i for o in num_list]
    if sum(D) == 0:
      return i
    elif D.count(0) == len(D)-1:
      return i
  return 1

def gcd2(num_list,a):
  for i in range(min(num_list),a,-1):
    D = [o%i for o in num_list]
    if sum(D) == 0:
      return i
  return 1

N = input()
A = list(map(int,input().split()))
ans1 = gcd(A,1)
temp = min(A)
A.remove(min(A))
ans2 = gcd2(A,temp)
print(max(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
import sys
import os
import math
import itertools
import math
import fractions
from functools import reduce

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

n = int(input())
nums = [int(i) for i in input().split()]
gcds = []
for num in nums:
    tmp = nums.copy()
    tmp.remove(num)
    gcds.append(gcd(*tmp))
print(max(gcds))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding:utf-8 -*-

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

    # 他のAiにはあって、あるAiに唯一ない約数が答え
    
    candidates = []

    if N == 2:
        print(max(A[0], A[1]))
        return

    candidates = {}
    # まずは0番目の奴の候補を考える
    for i in range(1, A[0]+1):
        if A[0]%i == 0:
            candidates[i] = 1

    for key in candidates.keys():
        if A[1]%key != 0:
            candidates[key] -= 1

    for i in range(1, A[1]+1):
        if A[1]%i == 0:
            if not i in candidates:
                candidates[i] = 0

    import copy
    for i in range(2, N):
        copied_candidates = copy.deepcopy(candidates)
        for key in copied_candidates.keys():
            if A[i]%key == 0:
                pass
            else:
                candidates[key] -= 1
                if candidates[key] < 0:
                    candidates.pop(key)
    
    ans = 0
    for key in candidates.keys():
        ans = max(ans, key)
    
    print(ans)


    

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
def gcd(a,b):
  if b == 0:
    return a
  return gcd(b, a%b)

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

B = A.copy()
ans = 0

for n in range(N):
  del A[n]
  agcd = A[0]
  for j in A[1:]:
    agcd = gcd(agcd, j)
  if ans < agcd:
    ans = agcd
  A = B.copy()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a, b):
	if a < b:
		a, b = b, a
	if b == 0:
		return a
	c = a % b
	return gcd(b, c)

n = int(input())
a = list(map(int,input().split()))
gcds = []

maxt = 0
for i in range(n):

	b = a[0:i]
	b += (a[i+1:n])
	tmp = b[0]

	for bi in b[1::]:
		tmp = gcd(tmp,bi)
		
	if maxt < tmp:
		maxt = tmp
 
print (maxt)

Submission status: Time Limit Exceeded

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

from fractions import gcd
from functools import reduce
import copy

max_g = 1
for i in range(n):
  bn = copy.copy(an)
  bn.pop(i)
  g = reduce(gcd, bn)
  max_g = max(g, max_g)
print(max_g)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import copy
import fractions
from functools import reduce


def gcd(*numbers):
    return reduce(fractions.gcd, numbers)


n = input()
a = list(map(int, input().split()))
a.sort()
b = []
c = 0
for i in range(len(a)):
    b = copy.copy(a)
    b[i] = max(a)
    t = gcd(*b)
    if c < t:
        c = t

print(c)

Submission status: Time Limit Exceeded

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

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors
    
    
kosuu = int(input())
nums = list(map(int,input().split()))

temp_arr=[]
for temp in nums:
    temp_arr.append(make_divisors(temp))
    
temp_arr2 = sum(temp_arr,[])
temp_arr2.sort(reverse=True)
for temp in temp_arr2:
    if temp_arr2.count(temp) == kosuu -1 or temp_arr2.count(temp) == kosuu:
        print(temp)
        break

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
import random
import copy

def gcd(numbers):
    return reduce(fractions.gcd, numbers)

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

for i in range(n):
  b = copy.copy(a)
  b.pop(i)
  t = gcd(b)
  if ans < t:
    ans = t

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# C
import functools
def gcd(a,b):
    if a<b:a,b=b,a
    if a%b==0:return b
    return gcd(b,a%b)

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

for i in range(n):
    values = a[:i] + a[i+1:]
    res = functools.reduce(gcd, values)
    ans = max(ans, res)
print(ans)

Submission status: Time Limit Exceeded

Solution code:

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


n = int(input())

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

ans = 0

for i in range(n):
    # print('i=',i)
    f = 0

    for j in range(n):
        # print('j=',j)

        if j!= i:
            if f == 0:
                f = a[j]
            else:
                # print(f,a[j])
                f=gcd(a[j],f)
    ans = max(ans,f)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

dic = {}
def gcd(x, y):
  if (x, y) in dic:
    return dic[(x, y)]
  if y == 0:
    dic[(x, y)] = x
  else:
    dic[(x, y)] = gcd(y, x % y)
  return dic[(x, y)]

def gcd_list(numbers):
  return reduce(gcd, numbers)
 
max = 0
n = int(input())
li = list(map(int, input().split()))
 
for i in range(n):
  x = li.copy()
  x.pop(i)
  g = gcd_list(x)
  if max < g:
    max = g

print(max)

Submission status: Time Limit Exceeded

Solution code:
N = raw_input()
Ai = map(int, raw_input().split(" "))

def get_gcd(num):
    while min(num) != max(num):
        num = sorted(num)
        for i in range(0, (len(num)+1)/2):
            tmp = num[i+1] % num[i]
            if tmp == 1:
                return 1
            num[i+1] = num[i] if tmp == 0 else tmp
    return num[0]

def get_list(lt, i):
    tmp = lt[:]
    tmp.remove(i)
    return tmp

print max(map(lambda x: get_gcd(get_list(Ai, x)), Ai))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted([int(i) for i in input().split()])
ma = A[1]
for i in range(ma):
    tof = [a%ma==0 for a in A]
    if sum(tof)>=N-1:
        break
    if A[2]//2 > ma-1:
        ma -= 1
    else:
        ma = int((ma+1)//2)       
print(ma)

Submission status: Time Limit Exceeded

Solution code:
import functools
# import math


def solve():
    N = int(input())
    A = [int(i) for i in input().split()]
    # gcd = functools.reduce(math.gcd, A)
    max_gcd = 1
    for i in range(N):
        A_ = A.copy()
        A_.remove(A[i])
        max_gcd = max(max_gcd, functools.reduce(gcd, A_))
    print(max_gcd)


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


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
import copy
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
n=int(input())
a_o=list(map(int,input().split()))
g=[1]*n
for i in range(n):
  a=copy.copy(a_o)
  a.pop(i)
  g[i]=gcd_list(a)
print(max(g))

Submission status: Time Limit Exceeded

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

#ユークリッド互除法の関数の定義
def gcd(a,b):
    if b==0:return a
    return gcd(b,a%b)


gcds = [-1]*n

alist = [a]*n

for i in range(n):
    save = a[i]
    a[i] = 0
    if n==1:
        gcds[i]=a[0]
    else:
        element = gcd(a[0], a[1])
        if n==2:
            gcds[i]=element
        else:
            for j in range(2,n):
                element=gcd(element,a[j])
            gcds[i] = element
    a[i] = save

# print(gcds)
print(max(gcds))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

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

m = 1
l = 0
while m < n:
    m *= 2
    l += 1
tr = [0] * (2 * m)

tr[m:m + n] = a


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


def init_tr():
    for d in range(l - 1, -1, -1):
        for i in range(1 << d, 1 << (d + 1)):
            tr[i] = gcd(tr[i * 2], tr[i * 2 + 1])


def upd_tr(j):
    i = j // 2
    if i == 0:
        return
    tr[i] = gcd(tr[i * 2], tr[i * 2 + 1])
    upd_tr(i)


init_tr()

ans = 0

for i in range(n):
    tmp = tr[m + i]
    tr[m + i] = 0
    upd_tr(m + i)
    ans = max(ans, tr[1])
    tr[m + i] = tmp
    upd_tr(m + i)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a

def gcd_list(a):
  s = a[0]
  for i in range(len(a)):
    s = gcd(s,a[i])
  return s

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

s = gcd_list(a)
a2 = []
for i in range(n):
  a2 = a[0:i]+a[i+1:n]
  l = gcd_list(a2)
  if s < l:
    s = l
print(s)

Submission status: Time Limit Exceeded

Solution code:
def gcd(m,n):
  x=max(m,n)
  y=min(m,n)
  if x==0:
    return 0
  while y!=0:
    x,y=y,x%y
  return x

n = int(input())
a = list(map(int, input().split()))
b = []

for i in range(n):
  a[i],a[n-1]=a[n-1],a[i]
  temp = a[0]
  for j in range(n-1):
    temp=gcd(temp,a[j])
  b.append(temp)
  a[i],a[n-1]=a[n-1],a[i]
  
print(max(b))

Submission status: Time Limit Exceeded

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

if n == 2 and a[0] == a[1]:
  ans = a[0]
else:
  ans = 0

def gcd(*args):
    if len(args) == 1:
        return args[0]

    L = list(args)

    while len(L) > 1:
        a = L[len(L) - 2]
        b = L[len(L) - 1]
        L = L[:len(L) - 2]
        
        while a:
            a, b = b%a, a

        L.append(b)
        
    return abs(b)

a = sorted(a)
r = gcd(*a)
for i in range(len(a)):
  a[i] = a[i]/r

for i in range(len(a)):
  c = list(a)
  del(c[i])
  r2 = gcd(*c)
  if r2 > 1 and ans == 0:
    ans = int(r*r2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:

def gcd(a, b):
    while a>0 and b >0:
        if a > b:
            a %= b
        else:
            b %= a
    return a + b

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

ans = 0
for i in range(N):
    a = A[1] if i == 0 else A[0]
    for j in range(N):
        if i == j:
            continue
        a = gcd(a, A[j])
    if a > ans:
        ans = a

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd  # in math module python >= 3.5
from functools import reduce


def gcdall(numlist):
    return reduce(gcd, numlist)


def main():
    _ = int(input())
    a = list(map(int, input().split()))
    if len(a) < 3:
        print(max(a))
        return

    gcds = []
    calced = []
    for i in range(len(a)):
        target = set([a[j] for j in range(len(a)) if j != i])
        if (any([c == target for c in calced])):
            continue
        excepted = list(target)
        b = 0
        while all([k % 2 == 0 for k in excepted]):
            b += 1
            excepted = [k // 2 for k in excepted]

        if b > 0:
            gcds.append(gcdall(excepted) * 2 * b)
        else:
            gcds.append(gcdall(excepted))

        calced.append(excepted)
    print(max(gcds))


main()

Submission status: Time Limit Exceeded

Solution code:
import functools
n=int(input())
a=list(map(int,input().split()))
def gcd(j,k):
  if j<k:j,k=k,j
  if j%k==0:return k
  return gcd(k,j%k)
A=functools.reduce(gcd,a)
for i in range(n):
    c=a[i]
    del a[i]
    B=functools.reduce(gcd,a)
    if A<B:
        A=B
    else:
        A=A
    a.insert(i,c)
print(A)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
 
def gcd(a,b):
  if b==0:
    return a
  else:
  	return gcd(b, a%b)
 
def gcd_list(X):
  dmy=gcd(X[0],X[1])
  dmy=X[0]*X[1]
  for i in range(len(X)):
    dmy=gcd(dmy,X[i])
  return dmy
 
if N==2:
  print(max(A))
else:
  R=[]
  for i in range(N):
    C=A[:i]
    C.extend(A[i+1:])
    R.append(gcd_list(C))
 
  print(int(max(R)))

Submission status: Time Limit Exceeded

Solution code:
import copy

N = int(input())
As = [int(i) for i in input().split()]
A = copy.copy(As)
max_gcd = 0

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

for i in range (N):
    for j in range (max(A)):
        A[i] = j
        for k in range (N-1):
            A[k+1] = gcd(A[k], A[k+1])
        #print (A)
        if A[N-1] > max_gcd:
            max_gcd = A[N-1]
        #print (max_gcd)
        A = copy.copy(As) 
        
print (max_gcd)

Submission status: Time Limit Exceeded

Solution code:
memo = {}

def gcd(a, b):
    if (a, b) in memo.keys():
        return memo[(a, b)]
    if b == 0:
        return a
    return gcd(b, a % b)

n = input()
a_list = map(int, raw_input().split())
max_g = 0
for exclude in xrange(n):
    g = a_list[0] if exclude > 0 else a_list[1]
    for i, a in enumerate(a_list):
        if i == 0 or i == exclude:
            continue
        g_new = gcd(g, a)
        if (g, a) not in memo:
            memo[(g, a)] = g_new
        g = g_new
        if g <= max_g:
            break
    if g > max_g:
        max_g = g
print(max_g)

Submission status: Time Limit Exceeded

Solution code:
import fractions

N=int(input())
a_list = [int(i) for i in input().split()]
result = 0
for i in range(N):
	b_list = a_list[:]
	del b_list[i]
	ans=b_list[0]
	for j in range(N-1):
		ans = fractions.gcd(ans, b_list[j])
	if ans > result:
		result = ans
print(result)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

# -*- coding: utf-8 -*-
"""
Created on Sat Apr 13 09:53:12 2019

@author: Owner
"""

import numpy as np
import sys
""""
import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy
import bisect
"""

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default
# 約数を高速で列挙するコード
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort(reverse = True)
    return divisors
"""
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())

# 多次元配列の宣言（あとでintにすること。）（タプルにすること。）
dp = np.zeros((N+1, 4,4,4))
    
all(nstr.count(c) for c in '753')

# 複数配列を並び替え
ABT = zip(A, B, totAB)
result = 0
# itemgetterには何番目の配列をキーにしたいか渡します
sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)

# 2進数のbit判定
(x >> i) & 1

# dp最小化問題
dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break
# 累積和
add = 1 # 問題によって決まる
res = 0
sums = [0]*(len(nums)+1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]
for i in range(0, len(nums), 2):
    left = i
    right = min(i+add, len(nums))
    tmp = sums[right] - sums[left]
    res = max(tmp, res)

#２分探索
li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    

#ソート関数
org_list = [3, 1, 4, 5, 2]
new_list = sorted(org_list)
print(org_list)
print(new_list)
# [3, 1, 4, 5, 2]
# [1, 2, 3, 4, 5]

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


def main():
    A.sort()
    CNT = dict()    
    for a in A:
        ads = make_divisors(a)
        for ad in ads:
            if ad in CNT:
                pass
            else:
                CNT[ad]=0
            CNT[ad] +=1
    l_kouho = [k for k, v in CNT.items() if v == N or v == N-1]
    print(max(l_kouho))
            
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy
import fractions
from functools import reduce

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)


n = int(input())

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

ans = 0

for i in range(n):
    # print('i=',i)
    f = 0

    for j in range(n):
        # print('j=',j)

        if j!= i:
            if f == 0:
                f = a[j]
            else:
                # print(f,a[j])
                f=fractions.gcd(a[j],f)
    ans = max(ans,f)

print(ans)

Submission status: Time Limit Exceeded

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

# 全てのパターンをしらべる
result=0
for i in range(n[0]):
  a_=a.copy()
  a_.pop(i)
  
  firstFlag = True
  u=0
  v=0
  for j in a_:
    if firstFlag:
      u=j
      firstFlag=False
    else:
      v=j
      # ユークリッドの互除法
      r=0
      while v !=0:
        r=u%v
        u=v
        v=r
  if result<u:
    result=u
    

print(result)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import numpy as np
from functools import reduce

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)


N = int(input())
A_list = list(map(int,input().split()))
temp_max= 0
for i in range(N):
        temp_list = A_list.copy()
        temp_list.pop(i)
        gcd_temp = gcd(*temp_list)
        if type(gcd_temp)== int:
                gcd_temp = [gcd_temp] 
        if max(gcd_temp)>temp_max:
                temp_max = max(gcd_temp)

print(temp_max)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

def listgcd(l):
  while len(l) > 1:
    x = l.pop(0)
    y = l.pop(0)
    l.append(gcd(x,y))
  return l[0]
    
def main():
  cnt = int(input())
  numbers = list(map(int, input().split(" ")))
  
  ret_list = []
  for i in (range(len(numbers))):
    l = copy.deepcopy(numbers)
    l.pop(i)
    ret_list.append(listgcd(l))
  print (max(ret_list))

  


main()

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
import itertools


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

ans = []

for a in list(itertools.combinations(A, N-1)):
    ans.append(reduce(fractions.gcd, a))
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

import fractions as f
import copy

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

max = 1
for i in range(N):
    temp = copy.deepcopy(a)
    del temp[i]
    if len(temp) == 1:
        gcd_2 = temp[0]
    else:
        for j in range(N-2):
            if j == 0:
                gcd_2 = f.gcd(temp[j], temp[j+1])
            else:
                gcd_2 = f.gcd(gcd_2, temp[j+1])
            if gcd_2 < max:
                break
    if gcd_2 > max:
        max = gcd_2

print(max)

Submission status: Time Limit Exceeded

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

import fractions
from functools import reduce

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)


ans=0
GCD_target=gcd_list(A)
#print("GCD_target = {}".format(GCD_target))

for i in range(N):
   copied_A = A[:]
   copied_A.pop(i)
   GCD_current=gcd_list(copied_A)
   #print(GCD_current)
   ans=max(ans,GCD_current)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
from collections import defaultdict
d=defaultdict(int)
for ai in a:
    for i in range(1, int(ai**0.5)+1):
        if ai % i == 0:
            d[i]+=1
            if i != ai // i:
                d[ai//i]+=1
print(max([x for x in d.keys() if d[x]==n or d[x]==n-1]))

Submission status: Time Limit Exceeded

Solution code:
import functools
import math

def GCD(m, n):
    while n:
        m, n = n, m % n
    return m



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

gcda = 0
for i in range(N):
    list2 = list1[0:i]
    list3 = list1[i+1:]
    gcd = functools.reduce(GCD, list2+list3)
    if gcd > gcda:
        gcda = gcd

print(gcda)