Problem p03061 (python)

Fixed submission:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools, bisect
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007
INF = float("inf")

def main():
    N = I()
    A = LI()

    forward = list(itertools.accumulate(A, math.gcd))
    backward = list(itertools.accumulate(A[::-1], math.gcd))[::-1]

    ans = max(backward[1], forward[-2])

    for i in range(1, N - 1):
        ans = max(ans, math.gcd(forward[i - 1], backward[i + 1]))
    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import fractions
"""
import math
import queue

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

backet = [1 for i in range(As[-1]+1)]
ans = 1

for i, Ai in enumerate(As):
    for j in range(2,int(Ai/2)+1) :
        if (Ai % j == 0) :
            backet[j] += 1

flag = True
while flag :
    flag = False
    for i, bc in enumerate(backet):
        if (bc >= N) :
            backet[i] -= N
            ans *= (bc-1)
            flag = True
            continue

print(ans)
"""

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


def L(i) :
    if (i-1 < 0) :
        return 0
    return fractions.gcd(L(i-1), A[i-1])

def R(i) :
    if (i >= N) :
        return 0
    return fractions.gcd(R(i+1), A[i])

M = 0
for i in range(N) :
    M = max(M, fractions.gcd(L(i), R(i+1)))


print(M)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))

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

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

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

M = 0
for i in range(N) :
    M = max(M, gcd(L(i), R(i+1)))


print(M)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))

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

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

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

M = 0
for i in range(N) :
    M = max(M, gcd(L(i), R(i)))


print(M)

Buggy submission (verdict: Runtime Error):
import math
import queue

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

ques = [queue.Queue() for i in range(N)]

for i, Ai in enumerate(As):
    qi = ques[i]
    for j in range(2,int(math.sqrt(Ai))+1) :
        if (Ai % j == 0) :
            qi.put(j)
    
flag = True
while flag:
    for i, qi in enumerate(ques):
        if(qi.empty()) :
            As[i] = As[-1]
            flag = False
            break
        else :
            qi.get()

min_num = As[0]
gcd_num = math.gcd(As[0], As[1])
for i in range(2,N) :
    #改善の余地あり
    gcd_num = math.gcd(gcd_num, As[i])
    if (gcd_num==1) :
        break

print(gcd_num)

Buggy submission (verdict: Runtime Error):
import math
import queue

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

ques = [queue.Queue() for i in range(N)]

for i, Ai in enumerate(As):
    qi = ques[i]
    for j in range(1,int(Ai/2)+1) :
        if (Ai % j == 0) :
            qi.put(j)
    
flag = True
while flag:
    for i, qi in enumerate(ques):
        if(qi.empty()) :
            As[i] = As[-1]
            flag = False
            break
        else :
            qi.get()

min_num = As[0]
gcd_num = math.gcd(As[0], As[1])
for i in range(2,N) :
    #改善の余地あり
    gcd_num = math.gcd(gcd_num, As[i])
    if (gcd_num==1) :
        break

print(gcd_num)