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: Runtime Error

Solution code:
def gcd(x, y):
    while y!=0:
        x, y = y, x % y # x,yの大小が逆でも、入れ替わるので、OK
    return x

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

if n == 2:
    print(max(a))

if n>=3:
    l = [-1] * n
    r = [-1] * n
    l[0] = a[0]  #一番左を左に挿入
    r[-1] = a[-1] #一番右を右に挿入
# print(l)
# print(r)

    for i in range(1, n): #左から最大公約数をn-1回計算していく
        l[i] = gcd(l[i-1], a[i]) # l[0]とa[1]の最大公約数をl[1]に挿入⇒
        # l[1]とa[2]の最大公約数をl[2]に挿入。隣同士素があったら、それ以降右側全て1
        for i in range(n-2, -1, -1): #右から最大公約数を計算していく
        r[i] = gcd(r[i+1], a[i]) # r[n-1]とa[n-2]の最大公約数をr[n-2]に挿入

        # print("l",l) # lはn-1桁のlist
        # print("r",r)

        ans = max(r[1], l[-2]) # rの左から2番目とlの右から2番目の大きい方を、ansに挿入.
        # 右端、左端は、最大公約数ではないため。(元々の数列)

        for i in range(1, n-1): #両端を切り取った中で、lは右側に寄せて、rは左側に寄せているので、
            g = gcd(l[i-1], r[i+1]) #
            ans = max(ans, g)
            # print(i, g, ans)
    print(ans)

Submission status: Runtime Error

Solution code:
import functools
import math

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

gcd = []

for i in range(len(A)):
    B = A[i]
    A.pop(i)
    gcd.extend([functools.reduce(math.gcd,A)])
    A.insert(i,B)

print(max(gcd))

Submission status: Runtime Error

Solution code:
from math import gcd
N = int(input())
aa = list(map(int, input().split()))
front = [0]*N
back = [0]*N
front[0] = aa[0]
back[-1] = aa[-1]
for i in range(1, N):
    front[i] = gcd(front[i-1], aa[i])
    back[-1-i] = gcd(back[-i], aa[-1-i])
ans = back[1]
for i in range(2, N):
    ans = max(gcd(front[i-2], back[i]), ans)
ans = max(front[-2], ans,)
print(ans)

Submission status: Runtime Error

Solution code:
import math
import functools

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

i = 0
list = []
while i < n:
    list.append(int(a[i]))
    i += 1
    
gcd = functools.reduce(math.gcd, list)
print(gcd)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define MAXN 100001

int swap(int a, int b) {
}
int gcd(int a, int b) {
    if(a < b) swap(a, b);
    if (b==0) return a;
    return gcd(b, a%b);
}

int main() {

    int n;
    int a[MAXN];
    int r[MAXN], l[MAXN];
    cin >> n;
    for(int i=0; i<n; i++) {
        cin >> a[i];
    }

    l[0] = 0; r[n] = 0;

    for(int i=1; i<n; i++) {
        l[i] = gcd(l[i-1], a[i-1]);
    }

    for(int i=n-1; i>=0; i--) {
        r[i] = gcd(r[i+1], a[i]);
    }

    int g[MAXN];
    int ans =0;
    for(int i=0; i<n; i++) {
        g[i] = gcd(l[i], r[i+1]);
        ans = max(ans, g[i]);
    }

    cout << ans << endl;

}

Submission status: Runtime Error

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


class SegmentTree():
    def m(self, v1, v2):  # ***monoid***
        return math.gcd(v1, v2)

    def __init__(self, array):
        m = self.m
        e = self.e = 0  # ***identity element***
        size = self.size = 10**6  # ***limit for array size***
        n = self.n = len(array)
        dat = self.dat = [e] * n + array + [e] * (2 * size - 2 * n)
        for i in range(n - 1, 0, -1):  # build
            dat[i] = m(dat[i << 1], dat[i << 1 | 1])

    def modify(self, p, v):  # set value at position p (0-indexed)
        m, n, dat = self.m, self.n, self.dat
        p += n
        dat[p] = v
        while p > 1:
            dat[p >> 1] = m(dat[p], dat[p ^ 1])
            p >>= 1

    def query(self, l, r):  # result on interval [l, r) (0-indexed)
        m, e, n, dat = self.m, self.e, self.n, self.dat
        res = e
        l += n
        r += n
        while l < r:
            if l & 1:
                res = m(res, dat[l])
                l += 1
            if r & 1:
                r -= 1
                res = m(res, dat[r])
            l >>= 1
            r >>= 1
        return res


ST = SegmentTree(A)
ans = 0
for i, a in enumerate(A):
    ST.modify(i, ST.e)
    ans = max(ans, ST.query(0, N))
    ST.modify(i, a)
print(ans)

Submission status: Runtime Error

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


def main():
    n = int(input())
    a = list(map(int, input().split()))
    left = [0 for _ in range(n)]
    right = [0 for _ in range(n)]
    for i in range(1, n):
        l = math.gcd(a[i - 1], a[i])
        r = math.gcd(a[n - i - 1], a[n - i - 2])
        left[i] = l
        right[n - i - 2] = r

    m = max(left[0], right[n - 1])
    for i in range(n - 1):
        m = max(m, left[i - 1], right[i + 1])
    print(m)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from fractions import gcd


def main():
    n = int(input())
    a = list(map(int, input().split()))
    left = [0 for _ in range(n)]
    right = [0 for _ in range(n)]
    for i in range(1, n):
        l = math.gcd(a[i - 1], a[i])
        r = math.gcd(a[n - i - 1], a[n - i - 2])
        left[i] = l
        right[n - i - 2] = r

    m = max(left[0], right[n - 1])
    for i in range(n - 1):
        m = max(m, left[i - 1], right[i + 1])
    print(m)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
### C
import math

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

left = [0] * (N+1)
right = [0] *(N+1)

for i in range(N):
    left[i+1] = math.gcd(left[i], A[i])
for i in reversed(range(N)):
    right[i] = math.gcd(right[i+1], A[i])
    
res = 0
for i in range(N):
    l = left[i]
    r = right[i+1]
    res = max(math.gcd(l, r), res)

print(res)

Submission status: Runtime Error

Solution code:
import math

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

left = [0] * (N+1)
right = [0] *(N+1)

for i in range(N):
    left[i+1] = math.gcd(left[i], A[i])
for i in reversed(range(N)):
    right[i] = math.gcd(right[i+1], A[i])
    
res = 0
for i in range(N):
    l = left[i]
    r = right[i+1]
    res = max(math.gcd(l, r), res)

print(res)

Submission status: Runtime Error

Solution code:
# @profile
def main():
    import copy
    import math

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

    # N=5
    # a = [12,15,18,36,42]

    # N = 10**5
    # a = [1]*N

    # N=11
    # a = [56,58,82,46,67,36,98,68,24,46,52]

    #a,bの最大公約数
    def gcd(a, b):
        while b:
            a, b = b, a % b
        return a

    # 一番近い大きい側の2の累乗の数が，segtreeの葉の数になる．
    N_ADJUST = 2**int(math.ceil(math.log2(N)))
    # segtreeのサイズはN_ADJUSTを葉とする木のノード数．
    ST_SIZE = N_ADJUST*2-1
    # binopに合わせて単位元として決める．maxならINT_MAX(問題文の制限)
    INT_IDENTITY = 0
    li = [INT_IDENTITY]*ST_SIZE
    # li_initは，葉の配列．
    # 節点kが [l,r) にbinopという演算で結びついている．
    def init_segtree(li, k, l, r, binop, li_init):
        if r-l==1:
            li[k] = INT_IDENTITY if l >= len(li_init) else li_init[l]
        elif r-l>1:
            k_l, k_r = k*2+1, k*2+2
            init_segtree(li, k_l, l, (l+r)//2, binop, li_init)
            init_segtree(li, k_r, (l+r)//2, r, binop, li_init)
            li[k] = binop(li[k_l],li[k_r])

    init_segtree(li,0,0,N_ADJUST,gcd,a)

    # print(li)
    # 下からupdateする．
    def update_segtree(li,k,binop):
        if k > 0:
            k_2 = (k-1)//2
            k_l = k_2*2+1
            k_r = k_l+1
            li[k_2] = binop(li[k_l],li[k_r])
            update_segtree(li,k_2,binop)

    val = []
    for i in range(N_ADJUST-1,N_ADJUST+N-1):
        tmp = li[i]
        li[i] = 0
        update_segtree(li,i,gcd)
        # print(i, li)
        val.append(li[0])
        li[i] = tmp
        update_segtree(li,i,gcd)

    print(max(val))

main()

Submission status: Runtime Error

Solution code:
def main():
    import copy
    import math

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

    # N=5
    # a = [12,15,18,36,42]

    N = 10**5
    a = [1]*N

    # N=11
    # a = [56,58,82,46,67,36,98,68,24,46,52]

    #a,bの最大公約数
    def gcd(a, b):
        while b:
            a, b = b, a % b
        return a

    # 一番近い大きい側の2の累乗の数が，segtreeの葉の数になる．
    N_ADJUST = 2**int(math.ceil(math.log2(N)))
    # segtreeのサイズはN_ADJUSTを葉とする木のノード数．
    ST_SIZE = N_ADJUST*2-1
    # binopに合わせて単位元として決める．maxならINT_MAX(問題文の制限)
    INT_IDENTITY = 0
    li = [INT_IDENTITY]*ST_SIZE
    # li_initは，葉の配列．
    # 節点kが [l,r) にbinopという演算で結びついている．
    def init_segtree(li, k, l, r, binop, li_init):
        if r-l==1:
            li[k] = INT_IDENTITY if l >= len(li_init) else li_init[l]
        elif r-l>1:
            k_l, k_r = k*2+1, k*2+2
            init_segtree(li, k_l, l, (l+r)//2, binop, li_init)
            init_segtree(li, k_r, (l+r)//2, r, binop, li_init)
            li[k] = binop(li[k_l],li[k_r])

    init_segtree(li,0,0,N_ADJUST,gcd,a)

    # print(li)
    # 下からupdateする．
    def update_segtree(li,k,binop):
        if k > 0:
            k_2 = (k-1)//2
            k_l = k_2*2+1
            k_r = k_l+1
            li[k_2] = binop(li[k_l],li[k_r])
            update_segtree(li,k_2,binop)

    val = []
    for i in range(N_ADJUST-1,N_ADJUST+N-1):
        tmp = li[i]
        li[i] = 0
        update_segtree(li,i,gcd)
        # print(i, li)
        val.append(li[0])
        li[i] = tmp
        update_segtree(li,i,gcd)

    print(max(val))

main()

Submission status: Runtime Error

Solution code:
#include <iostream>
//#include <cmath>
//#include <numeric>
#include <algorithm>
using namespace std;

int main() {
    long long N;
    cin >> N;
    long long A[N];
    for(int i = 0; i < N; i++) {
        cin >> A[i];
    }

    long long gcdlistL[N + 1];
    long long gcdlistR[N + 1];
    gcdlistL[0] = 0;
    gcdlistR[N] = 0;
    for(long long i = 0; i < N; i++) {
        gcdlistL[i + 1] = __gcd(gcdlistL[i], A[i]);
        gcdlistR[N - 1 - i] = __gcd(A[N - 1 - i], gcdlistR[N - i]);
    }
    long long gcdmax = 0;
    for(long long i = 0; i < N; i++) {
        gcdmax = max(gcdmax, __gcd(gcdlistL[i], gcdlistR[i+1]));
    }

    cout << gcdmax << endl;
}

Submission status: Runtime Error

Solution code:
# @profile
def main():
    import copy
    import math

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

    # N=5
    # a = [12,15,18,36,42]

    N = 10**5
    a = [1]*N

    # N=10
    # a = [56,58,82,46,67,36,98,68,24,46]

    #a,bの最大公約数
    def gcd(a, b):
        while b:
            a, b = b, a % b
        return a

    # 一番近い大きい側の2の累乗の数が，segtreeの葉の数になる．
    N_ADJUST = 2**int(math.ceil(math.log2(N)))
    # segtreeのサイズはN_ADJUSTを葉とする木のノード数．
    ST_SIZE = N_ADJUST*2-1
    # binopに合わせて単位元として決める．maxならINT_MAX(問題文の制限)
    INT_IDENTITY = 0
    li = [INT_IDENTITY]*ST_SIZE
    # li_initは，葉の配列．
    # 節点kが [l,r) にbinopという演算で結びついている．
    def init_segtree(li, k, l, r, binop, li_init):
        if r-l==1:
            li[k] = INT_IDENTITY if l >= len(li_init) else li_init[l]
        elif r-l>1:
            k_l, k_r = k*2+1, k*2+2
            init_segtree(li, k_l, l, (l+r)//2, binop, li_init)
            init_segtree(li, k_r, (l+r)//2, r, binop, li_init)
            li[k] = binop(li[k_l],li[k_r])

    init_segtree(li,0,0,N_ADJUST,gcd,a)

    # print(li)
    # 下からupdateする．
    def update_segtree(li,k,binop):
        if k > 0:
            k_l, k_r = ((k-1)//2)*2+1, ((k-1)//2)*2+2
            li[(k-1)//2] = binop(li[k_l],li[k_r])
            update_segtree(li,k//2,binop)

    val = []
    for i in range(N_ADJUST-1,N_ADJUST+N-1):
        tmp = li[i]
        li[i] = 0
        update_segtree(li,i,gcd)
        # print(i, li)
        val.append(li[0])
        li[i] = tmp
        update_segtree(li,i,gcd)

    print(max(val))

main()

Submission status: Runtime Error

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))
C=A[:]
C=list(set(C))

#print(A)
#print(C)
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

anslist=[]

if len(C)==1:
  ans=C[0]

elif len(C)==2:
  sol=max(C[0],C[1])

else:
  for i in range(3):
    B=A[:]
    B.remove(C[i])
    sol=0
    for b in B:
        sol=gcd(sol,b)
    anslist.append(sol)
    ans=max(anslist)

#print(anslist)
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))
C=list(set(A))

#print(A)
#print(C)
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

anslist=[]

if len(C)==1:
  ans=C[0]

elif len(C)==2:
  sol=max(A[0],A[1])

else:
  for i in range(3):
    B=A[:]
    B.remove(C[i])
    sol=0
    for b in B:
        sol=gcd(sol,b)
    anslist.append(sol)
    ans=max(anslist)

#print(anslist)
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))
C=list(set(A))

#print(A)
print(C)
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

anslist=[]

if len(C)==1:
  ans=C[0]

elif len(C)==2:
  sol=max(A[0],A[1])

else:
  for i in range(3):
    B=A[:]
    B.remove(C[i])
    sol=0
    for b in B:
        sol=gcd(sol,b)
    anslist.append(sol)
    ans=max(anslist)

print(anslist)
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))


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

ans=[]

for i in range(3):
    B=A[:]
    del B[i]
    sol=0
    for b in B:
        sol=gcd(sol,b)
    ans.append(sol)

print(max(ans))

Submission status: Runtime Error

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))


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

ans=[]

for i in range(3):
    B=A[:]
    del B[i]
    sol=0
    for b in B:
        sol=gcd(sol,b)
    ans.append(sol)

print(ans)
print(max(ans))

Submission status: Runtime Error

Solution code:
import copy
import math
# N=int(input())
# a = list(map(int,input().split()))

# N=3
# a = [7,6,8]

# N = 40
# a = np.array([4,8,6,12]*(N//4), dtype=np.int)

N=10
a = [56,58,82,46,67,36,98,68,24,46]

#a,bの最大公約数
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

# 一番近い大きい側の2の累乗の数が，segtreeの葉の数になる．
N_ADJUST = 2**int(math.ceil(math.log2(N)))
# segtreeのサイズはN_ADJUSTを葉とする木のノード数．
ST_SIZE = N_ADJUST*2-1
# binopに合わせて単位元として決める．maxならINT_MAX(問題文の制限)
INT_IDENTITY = 0
li = [INT_IDENTITY]*ST_SIZE
# li_initは，葉の配列．
# 節点kが [l,r) にbinopという演算で結びついている．
def init_segtree(li, k, l, r, binop, li_init):
    if r-l==1:
        li[k] = INT_IDENTITY if l >= len(li_init) else li_init[l]
    elif r-l>1:
        k_l, k_r = k*2+1, k*2+2
        init_segtree(li, k_l, l, (l+r)//2, binop, li_init)
        init_segtree(li, k_r, (l+r)//2, r, binop, li_init)
        li[k] = binop(li[k_l],li[k_r])

init_segtree(li,0,0,N_ADJUST,gcd,a)

# print(li)

# 下からupdateする．
def update_segtree(li,k,binop):
    if k > 0:
        k_l, k_r = ((k-1)//2)*2+1, ((k-1)//2)*2+2
        li[(k-1)//2] = binop(li[k_l],li[k_r])
        update_segtree(li,k//2,binop)

val = []
l2 = copy.deepcopy(li)
for i in range(N_ADJUST-1,N_ADJUST+N-1):
    l2[i] = 0
    update_segtree(l2,i,gcd)
    # print(i, l2)
    val.append(l2[0])
    l2[i] = li[i]
    update_segtree(l2,i,gcd)

print(max(val))

Submission status: Runtime Error

Solution code:
#3.7.3 -> import math
from fractions import gcd

# [0,--list--,0]
N = int(input())
A = [0]+list(map(int,input().split()))+[0]
L = [0]*(N+2)
R = [0]*(N+2)
#0,A[1]->L[i-1],A[i]->L[N-1],A[N]
for i in range(1,N+1):
    L[i] = math.gcd(L[i-1],A[i])
#0,A[N]->R[i+1],A[i]->R[2],A[1]
for i in range(N,0,-1):
    R[i] = math.gcd(R[i+1],A[i])
#A[i]を含めないL,Rのgcdをすべて並べてmaxをとる
print(max(math.gcd(L[i-1],R[i+1]) for i in range(1,N+1)))

Submission status: Runtime Error

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

for i in range(N):
    for j in range(N):
        if i == j:
            pass
        else:
            G.append(gcd(A[i], A[j]))
            

print(max(G))

Submission status: Runtime Error

Solution code:
from math import gcd

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

left = [0] * (n+1)
right = [0] * (n+1)
for i in range(n):
    left[i+1] = gcd(left[i], a[i])
for j in range(n-1, -1, -1):
    right[j] = gcd(right[j+1], a[j])
for i in range(n):
    result.append(gcd(left[i], right[i+1]))

print(max(result))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
from math import gcd
pr = []
for c in a:
    if not len(pr): pr.append(c)
    else: pr.append(gcd(pr[-1],c))
sf = []
a.reverse()
for c in a:
    if not len(sf): sf.append(c)
    else: sf.append(gcd(sf[-1],c))
sf.reverse()
ans = max(pr[-2],sf[1])
for i in range(1,n-1):
    u = gcd(pr[i-1],sf[i+1])
    ans = max(ans, u)
print(ans)

Submission status: Runtime Error

Solution code:
import math
import numpy as np
from itertools import accumulate, chain, islice, starmap
N = int(input())
numbers = list(map(int, input().split()))
# numbers = [2, 4, 8, 8]
# N = len(numbers)


def calculate_maximum_subgcd_with_np_ufunc(numbers):
    gcd = np.frompyfunc(math.gcd, 2, 1)
    cumgcds = gcd.accumulate(numbers, dtype=np.object).astype(np.int)
    sub_cumgcds = np.roll(cumgcds, 1)
    sub_cumgcds[0] = 0
    reverse_cumgcds = gcd.accumulate(
        numbers[::-1], dtype=np.object).astype(np.int)
    sub_reverse_cumgcds = np.roll(reverse_cumgcds, 1)
    sub_reverse_cumgcds[0] = 0
    return np.max(gcd(sub_cumgcds, sub_reverse_cumgcds[::-1]))


def calculate_maximum_subgcd_with_func(numbers):
    operation = math.gcd
    cumgcds = accumulate(numbers, operation)
    sub_cumgcds = islice(chain([0], cumgcds), len(numbers))
    reverse_cumgcds = accumulate(numbers[::-1], operation)
    sub_reverse_cumgcds = islice(chain([0], reverse_cumgcds), len(numbers))
    sub_gcds = starmap(operation, zip(sub_cumgcds, list(sub_reverse_cumgcds)[::-1]))
    return max(sub_gcds)


# calculate_maximum_subgcd = calculate_maximum_subgcd_with_np_ufunc
calculate_maximum_subgcd = calculate_maximum_subgcd_with_func

print(calculate_maximum_subgcd(numbers))

Submission status: Runtime Error

Solution code:
import math
import typing
from typing import List
import numpy as np
from itertools import accumulate, chain, islice, starmap
N = int(input())
numbers = list(map(int, input().split()))
# numbers = [2, 4, 8, 8]
# N = len(numbers)


def calculate_maximum_subgcd_with_np_ufunc(numbers: np.ndarray) -> int:
    gcd = np.frompyfunc(math.gcd, 2, 1)
    cumgcds = gcd.accumulate(numbers, dtype=np.object).astype(np.int)
    sub_cumgcds = np.roll(cumgcds, 1)
    sub_cumgcds[0] = 0
    reverse_cumgcds = gcd.accumulate(
        numbers[::-1], dtype=np.object).astype(np.int)
    sub_reverse_cumgcds = np.roll(reverse_cumgcds, 1)
    sub_reverse_cumgcds[0] = 0
    return np.max(gcd(sub_cumgcds, sub_reverse_cumgcds[::-1]))


def calculate_maximum_subgcd_with_func(numbers: List[int]) -> int:
    operation = math.gcd
    cumgcds = accumulate(numbers, operation)
    sub_cumgcds = islice(chain([0], cumgcds), len(numbers))
    reverse_cumgcds = accumulate(numbers[::-1], operation)
    sub_reverse_cumgcds = islice(chain([0], reverse_cumgcds), len(numbers))
    sub_gcds = starmap(operation, zip(sub_cumgcds, list(sub_reverse_cumgcds)[::-1]))
    return max(sub_gcds)


# calculate_maximum_subgcd = calculate_maximum_subgcd_with_np_ufunc
calculate_maximum_subgcd = calculate_maximum_subgcd_with_func

print(calculate_maximum_subgcd(numbers))

Submission status: Runtime Error

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

gcd_list=[A[0]]
rev_gcd_list=[A[-1]]

for i in range(1,N):
    gcd_list.append(gcd([gcd_list[-1],A[i]]))
    rev_gcd_list.append(gcd([rev_gcd_list[-1],A[-i-1]]))
rev_gcd_list=rev_gcd_list[::-1]

ans=max(gcd_list[N-2],rev_gcd_list[1])
for i in range(1,N-1):
    ans=max(ans,gcd([gcd_list[i-1],rev_gcd_list[i+1]]))
print(ans)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

N = int(input())
A = list(map(int,input().split()))
max = 0
L = []
R = []
for i in range(N):
    L = A[:i]
    R = A[i:]
    if i > 0 :
        L.pop()
    while 2>len(L) or 2>len(R):
        L.append(0)
        R.append(0)
    x = reduce(math.gcd,L)
    y = reduce(math.gcd,R)
    gcdmax = math.gcd(x,y)
    if max < gcdmax:
        max = gcdmax
print(max)

Submission status: Runtime Error

Solution code:
import math
 
N = int(input())
A = list(map(int,input().split()))
L = [0] * N
R = [0] * N

for i in range(1, N):
    L[i] = math.gcd(L[i-1], A[i-1])
    R[-i-1] = math.gcd(R[-i], A[-i])

ans = [0] * N

for i in range(0, N):
    ans[i] = math.gcd(L[i], R[i])

print(max(ans))

Submission status: Runtime Error

Solution code:
import math
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
L = [math.gcd(A[-1],A[-2])]
flag = 0
for i in range(N-2,-1,-1):
    L.append(math.gcd(A[i],A[i-1]))
    flag += 1 if L[-1]%L[-2] != 0 else 0
if flag >= 2:
    L2 = []
    for j in range(len(L)-1,-1,-1):
        L2.append(math.gcd(L[i],L[i-1]))
    L = L2
print(max(L))

Submission status: Runtime Error

Solution code:
import math
 
N = int(input())
A = list(map(int,input().split()))
L = [0] * N
R = [0] * N

for i in range(1, N):
    L[i] = math.gcd(L[i-1], A[i-1])
    R[-i-1] = math.gcd(R[-i], A[-i])

ans = [0] * N

for i in range(0, N):
    ans[i] = math. gcd(L[i], R[i])

print(max(ans))

Submission status: Runtime Error

Solution code:
import fractions
 
N = int(input())
A = list(map(int,input().split()))
L = [0] * N
R = [0] * N

for i in range(1, N):
    L[i] = math.gcd(L[i-1], A[i-1])
    R[-i-1] = math.gcd(R[-i], A[-i])

ans = [0] * N

for i in range(0, N):
    ans[i] = math. gcd(L[i], R[i])

print(max(ans))

Submission status: Runtime Error

Solution code:
import math
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
L = [0]*(N-3)
for i in range(N-1,-1,-1):
    L.append(math.gcd(A[i],A[i-1]))
if L.count(min(L)) == 2 and len(L) > 2:
    L.sort()
    ans = L[2]
else:
    ans = L[0]
print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
try:
    from math import gcd
except:
    from fractions import gcd
N = int(input())
A = list(map(int, input().split()))
A.sort()
l = []
for i in range(2, A[0]+1):
    if A[0] % i == 0:
        l.append(i)
for i in range(2, A[1]+1):
    if A[1] % i == 0:
        l.append(i)
l = list(set(l))
ans = []
for i in l:
    cnt = 0
    for a in A:
        if a % i != 0:
            cnt += 1
    if cnt == 1:
        ans.append(i)
print(max(ans))

Submission status: Runtime Error

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

def gcd(x, y):
    if x < y:
        x = x ^ y
        y = x ^ y
        x = x ^ y
    div = x % y
    if (div == 0):
        return y
    while div != 0:
        div = x % y
        x = y
        y = div
    return x

memo = {}
memo[(0, 1)] = nums[0]
memo[(N-1, N)] = nums[N-1]

def solve(left, right):
    if (left, right) in memo:
        return memo[(left, right)]
    if left == 0:
        sol = gcd(nums[right - 1], solve(left, right - 1))
        memo[(left, right)] = sol
        return sol
    if right == N:
        sol = gcd(nums[left], solve(left+1, right))
        memo[(left, right)] = sol
        return sol

res = 1
for i in range(1, N-1):
    res = max(res, gcd(solve(0, i), solve(i+1, N)))

res = max(res, solve(0, N-1))
res = max(res, solve(1, N))

print(res)

Submission status: Runtime Error

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

def gcd(x,y):
    if x < y:
        x = x ^ y
        y = x ^ y
        x = x ^ y
    div = x % y
    if div:
        return gcd(div, y)
    else:
        return y

memo = {}
memo[(0, 1)] = nums[0]
memo[(N-1, N)] = nums[N-1]

def solve(left, right):
    if (left, right) in memo:
        return memo[(left, right)]
    if left == 0:
        sol = gcd(nums[right - 1], solve(left, right - 1))
        memo[(left, right)] = sol
        return sol
    if right == N:
        sol = gcd(nums[left], solve(left+1, right))
        memo[(left, right)] = sol
        return sol

res = 1
for i in range(1, N-1):
    res = max(res, gcd(solve(0, i), solve(i+1, N)))

res = max(res, solve(0, N-1))
res = max(res, solve(1, N))

print(res)

Submission status: Runtime Error

Solution code:
from math import gcd
N=int(input())
a=list(map(int,input().split()))

l=[0]*N
l[0]=a[0]
for i in range(1,N):
    l[i]=gcd(l[i-1],a[i])

r=[0]*N
r[N-1]=a[N-1]
for i in range(N-2,-1,-1):
    r[i]=gcd(r[i+1],a[i])

ans=0
for i in range(N):
    if i==0: tmp=r[1]
    elif i==N-1: tmp=l[N-2]
    else: tmp=gcd(l[i-1],r[i+1])
    ans=max(tmp,ans)

print(ans)

Submission status: Runtime Error

Solution code:
import functools
import copy

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

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

def gcd_l(l):
    return functools.reduce(lambda x, y: gcd(x, y), l)


l = [0] * N  #L_i, i = 0, 1, 2, ..., N-1
for i in range(N):
    if i == 0:
        continue
    else:
        l[i] = gcd_l(l[i-1] + A[i-1])

r = [0] * N #R_i, i = 0, 1, 2, ..., N-1
for j in range(N):
    if j == 0:
        continue
    else:
        r[N-j] = gcd_l(r[N-j] + A[N-j-1])

m = [0] * N
for i in range(N):
    m[i] = gcd(l[i], r[i])

print(max(m))

Submission status: Runtime Error

Solution code:
# 解けなかった...orz
from math import gcd

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

def gcd(i, j):
    if i <= 0:
        return j
    elif j <= 0:
        return i

    while i != j:
        if i < j:
            j %= i
            if j == 0: return i
        else:
            i %= j
            if i == 0: return j
    return i

# M = [0] * N
L = [0] * (N+1)
R = [0] * (N+1)
# L[0] = 0
# R[N] = 0

for i in range(N):
    L[i+1] = gcd(L[i], A[i])
    R[N-i-1] = gcd(R[N-i], A[N-i-1])

res = max(gcd(L[i], R[i+1]) for i in range(N))
print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))
A=list(set(A))

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

l1=[]
l2=[]
l3=[]

if len(A)==1:
    ans=A[0]

elif len(A)==2:
    ans=max(A)

elif len(A)==2:
    a=gcd(A[0],A[1])
    b=gcd(A[0],A[2])
    c=gcd(A[2],A[1])
    ans=max([a,b,c])

else:
    a=A[0]
    b=A[1]
    if gcd(a,b)==1:
        a=A[2]
        b=A[4]
    
    for x in A:
        l1.append(gcd(gcd(x,a),b))

print(max(l1))

Submission status: Runtime Error

Solution code:
import math
 
n = int(input())
li = list(map(int,input().split()))
 
leftgcd = [0 for i in range(n)]
rightgcd = [0 for i in range(n)]
 
leftgcd[0] = li[0]
rightgcd[n - 1] = li[n - 1]
 
if n == 2:
  print(max(li[0], li[1]))
else:
  for j in range(1, n):
    leftgcd[j] = math.gcd(li[j], leftgcd[j - 1])
    rightgcd[n - 1 - j] = math.gcd(li[n - 1 - j], rightgcd[n - j])

  ans = max(leftgcd[n - 2], rightgcd[1])
  for k in range(1, n - 1):
    g = math.gcd(leftgcd[k - 1], rightgcd[k + 1])
    if g > ans:
      ans = g
  print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))
A=list(set(A))

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

l1=[]
l2=[]
l3=[]


if len(A)==2:
    ans=max(A)

else:
    a=A[0]
    b=A[1]
    if gcd(a,b)==1:
        a=A[2]
        b=A[4]
    
    for x in A:
        l1.append(gcd(gcd(x,a),b))

print(max(l1))

Submission status: Runtime Error

Solution code:
import math

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

leftgcd = [0 for i in range(n)]
rightgcd = [0 for i in range(n)]

leftgcd[0] = li[0]
rightgcd[n - 1] = li[n - 1]

for j in range(1, n):
  leftgcd[j] = math.gcd(li[j], leftgcd[j - 1])
  rightgcd[n - 1 - j] = math.gcd(li[n - 1 - j], rightgcd[n - j])
  
ans = max(leftgcd[n - 2], rightgcd[1])
for k in range(1, n - 1):
  g = math.gcd(leftgcd[k - 1], rightgcd[k + 1])
  if g > ans:
    ans = g
print(ans)

Submission status: Runtime Error

Solution code:
import fraction
from functools import reduce

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

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

for i in range(n):
    max = gcd_list(a[-i])
    if max > ans:
        ans = max
        
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from functools import reduce


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))

def SUM(x):return sum([abs(x) for x in a ])



sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

n = INT()

a = LIST()

g=[]

for i in range(n-1):
    for j in range(i,n):
        g.append(gcd(a[i], a[j]))
    
g.sort()
g=[x for x in g if x!=min(g)]

print(g[0])

Submission status: Runtime Error

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

from functools import lru_cache
@lru_cache(maxsize = None)
def gcd(x, y):
    if x == 0 or y == 0:
        return max(x, y)
    if y == 0:
        return x
    return gcd(y, x % y)


def L(x):
    if x <= 0:
        return 0
    else:
        return gcd(L(x-1), A[x-1])

def R(x):
    if x >= N - 1:
        return 0
    else:
        return gcd(A[x+1], R(x+1))

for i in range(N-1, -1, -1):
    R(i)

w = 1
for i in range(N):
    w = max(w, gcd(L(i), R(i)))

print(w)

Submission status: Runtime Error

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

import functools
from functools import reduce

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

score = 0
for i in range(N):
    B = A
    keep = B.pop(0)
    n = gcd_list(B)
    if n > score:
        score = n
    A.append(keep)


print(score)

Submission status: Runtime Error

Solution code:
import math
N=int(input())
A=list(map(int,input().split()))
L=[0]
R=[0]
for i in range(N-1):
    L=L+[math.gcd(L[-1],A[i])]
    R=[math.gcd(R[0],A[N-i-1])]+R
M=[math.gcd(L[n],R[n]) for n in range(N)]
print(max(M))

Submission status: Runtime Error

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

import math
from functools import reduce

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

score = 0
for i in range(N):
    B = A
    keep = B.pop(0)
    n = gcd_list(B)
    if n > score:
        score = n
    A.append(keep)


print(score)