Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N, K = map(int,input().split())
A = list(map(int,input().split()))

A.sort()
max_a = -1
min_diff = K
for i in range(N):
    a = A[i]
    if a == K:
        print("POSSIBLE")
        exit()
    if a > max_a:
        max_a = a
    if i < N-1:
        diff = A[i+1]-A[i]
        if diff < min_diff:
            min_diff = diff

if max_a < K:
    print("IMPOSSIBLE")
    exit()
else:
    for a in A:
        if (K-a) % min_diff == 0:
            print("POSSIBLE")
            exit()

print("IMPOSSIBLE")

Fixed submission:
N, K = map(int,input().split())
A = list(map(int,input().split()))

def get_gcd(a,b):
    if b > a:
        a, b = b, a
    while a % b != 0:
        a, b = b, a % b
    return b

gcd  = A[0]
max_a = A[0]
for i in range(1, N):
    a = A[i]
    if a == K:
        print("POSSIBLE")
        exit()
    if a > max_a:
        max_a = a
    gcd = get_gcd(gcd, a)

if max_a < K:
    print("IMPOSSIBLE")
    exit()
else:
    if K % gcd == 0:
        print("POSSIBLE")
    else:
        print("IMPOSSIBLE")
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(input().split())

l = []
r = []
if N % 2 == 0:
    for i, a in enumerate(A):
        if i % 2 == 1:
            l.append(a)
        else:
            r.append(a)
        l.reverse()
else:
    for i, a in enumerate(A):
        if i % 2 == 0:
            l.append(a)
        else:
            r.append(a)
        l.reverse()

ans = l + r
print(" ".join(ans))


Fixed submission:
N = int(input())
A = list(input().split())

l = []
r = []
flag = False
if N % 2 == 0:
    for i in range(N-1, 0, -2):
        if flag:
            print(" ", end="")
        print(A[i], end="")
        flag = True
    for i in range(0, N, 2):
        if flag:
            print(" ", end="")
        print(A[i], end="")
else:
    for i in range(N-1, -1, -2):
        if flag:
            print(" ", end="")
        print(A[i], end="")
        flag = True
    for i in range(1, N, 2):
        if flag:
            print(" ", end="")
        print(A[i], end="")


print()
Buggy python submission (verdict: Time Limit Exceeded):
K, S = map(int, input().split())
ans = 0
for i in range(K+1):
    for j in range(K+1):
        for k in range(K+1):
            if i + j + k == S:
                ans += 1
print(ans)

Fixed submission:
K, S = map(int, input().split())
ans = 0
for i in range(K+1):
    for j in range(K+1):
        if 0 <= S-i-j <= K:
            ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, x = map(int,input().split())
A = list(map(int,input().split()))

ans = 0
for i in range(0, N-1):
    a = A[i]
    b = A[i+1]
    while a + b > x:
        if b > 0:
            b -= 1
            ans += 1
        elif a > 0:
            a -= 1
            ans += 1
    A[i] = a
    A[i+1] = b

print(ans)

Fixed submission:
N, x = map(int,input().split())
A = list(map(int,input().split()))

ans = 0
for i in range(0, N-1):
    while A[i] + A[i+1] > x:
        tmp = A[i + 1] + A[i] - x
        if A[i+1] > 0:
            if tmp > A[i+1]:
                tmp = A[i+1]
            A[i+1] -= tmp
            ans += tmp
        elif A[i] > 0:
            if tmp > A[i]:
                tmp = A[i]
            A[i] -= tmp
            ans += tmp

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
T = []
A = []
for _ in range(N):
    t, a = map(int,input().split())
    T.append(t)
    A.append(a)

ct = T[0]
ca = A[0]
for i in range(1, N):
    t = T[i]
    a = A[i]
    m = 1
    while (t + a) * m < (ct + ca) or m * t < ct or m * a < ca:
        m += 1
    ct = m * t
    ca = m * a

print(ct+ca)




Fixed submission:
N = int(input())
T = []
A = []
for _ in range(N):
    t, a = map(int,input().split())
    T.append(t)
    A.append(a)

ans = 1
ct = 1
ca = 1
for t, a in zip(T, A):
    n = max((ct + t - 1) // t, (ca + a - 1) // a)
    ct = n * t
    ca = n * a

print(ct+ca)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

N = int(input())
Xb = input().rstrip()
# N = 2*10**5
# Xb = '1'*N


X = 0
t = 1
for i in range(N):
    if Xb[N-1-i]=='1':
        X += t
    t *= 2

def popcount(x):
    return bin(x).count('1')
def calc(X):
    ans = 0
    while X!=0:
        X %= popcount(X)
        ans += 1
    return ans

ret = []
t = 1
for i in range(N):
    if Xb[N-1-i]=='1':
        Xt = X - t
    else:
        Xt = X + t
    ret.append(calc(Xt))
    t *= 2

for i in range(N):
    print(ret[N-1-i])
    

Fixed submission:
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

N = int(input())
Xb = input().rstrip()
p = Xb.count('1')
def popcount(x):
    return bin(x).count('1')
def calc(X):
    ans = 0
    while X!=0:
        X %= popcount(X)
        ans += 1
    return ans
Xp = 0
Xm = 0
tp = 1
tm = 1
b = 0
if p>1:
    flag = True
else:
    flag = False

for i in reversed(range(N)):
    if Xb[i]=='1':
        Xp += tp
        Xp %= (p+1)
        if flag:
            Xm += tm
            Xm %= (p-1)
    tp *= 2
    tp %= (p+1)
    if flag:
        tm *= 2
        tm %= (p-1)

for i in range(N):
    if Xb[i]=='1' and p==1:
        print(0)
        continue
    elif Xb[i]=='1':
        X = (Xm - pow(2,N-1-i,p-1)+p-1)%(p-1)
    else:
        X = (Xp + pow(2,N-1-i,p+1))%(p+1)
    print(calc(X)+1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

N = int(input())
Xb = input().rstrip()
# N = 2*10**5
# Xb = '1'*N


X = 0
t = 1
for i in range(N):
    if Xb[N-1-i]=='1':
        X += t
    t *= 2

def popcount(x):
    return bin(x).count('1')
def calc(X):
    ans = 0
    while X!=0:
        X %= popcount(X)
        ans += 1
    return ans

ret = []
t = 1
for i in range(N):
    if Xb[N-1-i]=='1':
        Xt = X - t
    else:
        Xt = X + t
    ret.append(calc(Xt))
    t *= 2

for i in range(N):
    print(ret[N-1-i])
    

Fixed submission:
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

N = int(input())
Xb = input().rstrip()
p = Xb.count('1')
def popcount(x):
    return bin(x).count('1')
def calc(X):
    ans = 0
    while X!=0:
        X %= popcount(X)
        ans += 1
    return ans
Xp = 0
Xm = 0
tp = 1
tm = 1
b = 0
if p>1:
    flag = True
else:
    flag = False

for i in reversed(range(N)):
    if Xb[i]=='1':
        Xp += tp
        Xp %= (p+1)
        if flag:
            Xm += tm
            Xm %= (p-1)
    tp *= 2
    tp %= (p+1)
    if flag:
        tm *= 2
        tm %= (p-1)

for i in range(N):
    if Xb[i]=='1' and p==1:
        print(0)
        continue
    elif Xb[i]=='1':
        X = (Xm - pow(2,N-1-i,p-1)+p-1)%(p-1)
    else:
        X = (Xp + pow(2,N-1-i,p+1))%(p+1)
    print(calc(X)+1)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

D = int(input())
C = list(map(int,input().split()))
S = [list(map(int,input().split())) for _ in range (D)]
M = 26
INF = 10**20

def judge(t):
    last = [0]*M
    score = 0
    for i in range(D):
        score += S[i][t[i]-1]
        last[t[i]-1] = i+1
        for m in range(M):
            score -= C[m]*(i+1-last[m])
    return score

from random import randint
import time
start = time.time()
ans = [0]*D
lasts = [0]*(M+1)


score = 0
for i in range(D):
    contest = 0
    max_score = -INF
    for m in range(M):
        new_score = score
        new_score += S[i][m]
        for m2 in range(M):
            if m==m2:continue
            new_score -= C[m2]*(i+1-lasts[m2])
        if new_score>max_score:
            contest = m+1
            max_score = new_score
    
    ans[i] = contest
    lasts[contest-1] = i+1
    score = max_score

while (time.time()-start<1.9):
    # 解を変更
    new_ans = [a for a in ans]
    idx = randint(0,D-1)
    val = randint(1,26)
    new_ans[idx] = val
    # スコアが上がっているか？
    new_score = judge(new_ans)
    if new_score>score:
        score = new_score
        ans[idx] = val

for a in ans:
    print(a)

Fixed submission:
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

D = int(input())
C = list(map(int,input().split()))
S = [list(map(int,input().split())) for _ in range (D)]
M = 26
INF = 10**20

ans = [0]*D
lasts = [0]*(M+1)

for i in range(D):
    score = -INF
    contest = 0
    for m in range(1,M+1):
        cur = S[i][m-1]
        cur -= C[m-1]*(i+1-lasts[m])
        if cur>score:
            score = cur
            contest = m
    ans[i] = contest

for a in ans:
    print(a)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

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

ans = 1
for a in A:
    ans *= a

if ans>10**18:
    print(-1)
else:
    print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

N = int(input())
A = list(map(int,input().split()))
A.sort()
ans = 1
for a in A:
    ans *= a
    if ans>10**18:
        print(-1)
        exit()

if ans>10**18:
    print(-1)
else:
    print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

A,B = map(str,input().split())

C = int(B*100)
print(A*C//100)

Fixed submission:
from decimal import Decimal

A,B = map(str,input().split())
A = Decimal(A)
B = Decimal(B)
ans = int(A*B)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = [input() for _ in range(N)]

T = [0]*N
M = [0]*N
for i in range(N):
    res = 0
    idx = 0
    s = S[i]
    l = len(s)
    m = 0
    while idx<l:
        if s[idx]==')':
            res -= 1
            m = min(res,m)
        else:
            res += 1
        idx += 1
    
    M[i] = m
    T[i] = res

Tp = []
Tn = []
for i in range(N):
    if T[i]>=0:
        Tp.append([M[i], T[i]])
    else:
        Tn.append([M[i]-T[i], -T[i]])

Tp.sort(reverse = True)
Tn.sort(reverse = True)
res = 0
for m,t in Tp:
    if res<-m:
        print('No')
        exit()
    res += t

resn = 0
for m,t in Tn:
    if resn<-m:
        print('No')
        exit()
    resn += t
if res==resn:
    print('Yes')
else:
    print('No')

Fixed submission:
N = int(input())
S = [input() for _ in range(N)]

T = [0]*N
M = [0]*N
for i in range(N):
    res = 0
    idx = 0
    s = S[i]
    l = len(s)
    m = 0
    while idx<l:
        if s[idx]==')':
            res -= 1
            m = min(res,m)
        else:
            res += 1
        idx += 1
    
    M[i] = m
    T[i] = res

Tp = []
Tn = []
for i in range(N):
    if T[i]>=0:
        Tp.append([M[i], T[i]])
    else:
        Tn.append([M[i]-T[i], -T[i]])

Tp.sort(reverse = True)
Tn.sort(reverse = True)
res = 0
for m,t in Tp:
    if res<-m:
        print('No')
        exit()
    res += t

resn = 0
for m,t in Tn:
    if resn<-m:
        print('No')
        exit()
    resn += t
if res==resn:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import sys


S = list(input())
N = len(S)
MOD = 2019
#%%
A = [0]*(N+1)
R = [0]*(2030)

R[0] += 1
base = 1
for i in range(N):
    A[i+1] = A[i] + base*int(S[N-1-i])
    base *= 10
    A[i+1] %= MOD
    R[A[i+1]] += 1

ans = 0
for r in R:
    ans += r*(r-1)//2
print(ans)
    

Fixed submission:
# -*- coding: utf-8 -*-
import sys


S = list(input())
N = len(S)
MOD = 2019
#%%
A = [0]*(N+1)
R = [0]*(2030)

R[0] += 1
base = 1
for i in range(N):
    A[i+1] = A[i] + base*int(S[N-1-i])
    base *= 10
    base %= MOD
    A[i+1] %= MOD
    R[A[i+1]] += 1

ans = 0
for r in R:
    ans += r*(r-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import sys


S = list(input())
N = len(S)
MOD = 2019
#%%
A = [0]*(N+1)
R = [0]*(2030)

R[0] += 1
base = 1
for i in range(N):
    A[i+1] = A[i] + base*int(S[N-1-i])
    base *= 10
    A[i+1] %= MOD
    R[A[i+1]] += 1

ans = 0
for r in R:
    ans += r*(r-1)//2
print(ans)
    

Fixed submission:
# -*- coding: utf-8 -*-
import sys


S = list(input())
N = len(S)
MOD = 2019
#%%
A = [0]*(N+1)
R = [0]*(2030)

R[0] += 1
base = 1
for i in range(N):
    A[i+1] = A[i] + base*int(S[N-1-i])
    base *= 10
    base %= MOD
    A[i+1] %= MOD
    R[A[i+1]] += 1

ans = 0
for r in R:
    ans += r*(r-1)//2
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
#%%
cum = [[0]*(N+1) for _ in range(3)]
for i in range(N):
    cum[0][i+1] = cum[0][i] + (S[i]=='R')
    cum[1][i+1] = cum[1][i] + (S[i]=='G')
    cum[2][i+1] = cum[2][i] + (S[i]=='B')
    
def color(i,j):
    RGB = [0]*3
    if S[i]=='R' or S[j]=='R':
        RGB[0] = 1
    if S[i]=='G' or S[j]=='G':
        RGB[1] = 1
    if S[i]=='B' or S[j]=='B':
        RGB[2] = 1
    
    if RGB[0]==0:
        return 0
    elif RGB[1]==0:
        return 1
    else:
        return 2
def col(s):
    if s=='R':
        return 0
    elif s=='G':
        return 1
    else:
        return 2

ans = 0
for i in range(N):
    for j in range(i+1,N):
        if S[i]!=S[j]:
            idx = color(i,j)
            ans += cum[idx][N]-cum[idx][j+1]
            k = 2*j-i
            if k<N and col(S[k])==idx:
                ans -= 1
print(ans)


Fixed submission:
# -*- coding: utf-8 -*-
import sys


N = int(input())
S = list(input())
#%%
r_ruiseki = [0]*(N+1)
b_ruiseki = [0]*(N+1)
g_ruiseki = [0]*(N+1)
for i in range(N):
    r_ruiseki[i+1] = r_ruiseki[i] + (S[i]=='R')
    g_ruiseki[i+1] = g_ruiseki[i] + (S[i]=='G')
    b_ruiseki[i+1] = b_ruiseki[i] + (S[i]=='B')
pos_r = []
pos_b = []
pos_g = []
for i,s in enumerate(S):
    if s=='R':
        pos_r.append(i)
    elif s=='B':
        pos_b.append(i)
    else:
        pos_g.append(i)

ans = 0
for i in pos_r:
    for j in pos_b:
        if j-i<=1:
            continue
        ans += g_ruiseki[j+1] - g_ruiseki[i]
        if (i&1 == j&1):
            if S[(i+j)//2]=='G':
                ans -= 1
    

for i in pos_b:
    for j in pos_r:
        if j-i<=1:
            continue
        ans += g_ruiseki[j+1] - g_ruiseki[i]
        if (i&1 == j&1):
            if S[(i+j)//2]=='G':
                ans -= 1
    

for i in pos_r:
    for j in pos_g:
        if j-i<=1:
            continue
        ans += b_ruiseki[j+1] - b_ruiseki[i]
        if (i&1 == j&1):
            if S[(i+j)//2]=='B':
                ans -= 1
    
for i in pos_g:
    for j in pos_r:
        if j-i<=1:
            continue
        ans += b_ruiseki[j+1] - b_ruiseki[i]
        if (i&1 == j&1):
            if S[(i+j)//2]=='B':
                ans -= 1
    

for i in pos_g:
    for j in pos_b:
        if j-i<=1:
            continue
        ans += r_ruiseki[j+1] - r_ruiseki[i]
        if (i&1 == j&1):
            if S[(i+j)//2]=='R':
                ans -= 1
    
for i in pos_b:
    for j in pos_g:
        if j-i<=1:
            continue
        ans += r_ruiseki[j+1] - r_ruiseki[i]
        if (i&1 == j&1):
            if S[(i+j)//2]=='R':
                ans -= 1
    

print(ans)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys

N,K,C = map(int,input().split())
S = input()
#%%
assign_t = [False]*N
assign_b = [False]*N

i = 0
num = 0
while i<N and num<K:
    if S[i]=='o':
        assign_t[i]=True
        num += 1
        i += C+1
    else:
        i += 1

i = N-1
num = 0
while i>=0 and num<K:
    if S[i]=='o':
        assign_b[i]=True
        num += 1
        i -= C+1
    else:
        i -=1

tot = 0
for s in S:
    if s=='o':
        tot += 1

if tot//(K-1)>1.4*(C+1):
    print()
    sys.exit()

for i in range(N):
    if assign_t[i] and assign_b[i]:
        print(i+1)

Fixed submission:
# -*- coding: utf-8 -*-
import sys

N,K,C = map(int,input().split())
S = input()
#%%
assign_t = [False]*N
assign_b = [False]*N


i = 0
num = 0
while i<N:
    if S[i]=='o':
        assign_t[i]=True
        num += 1
        i += C+1
    else:
        i += 1

if num>K:
    print()
    sys.exit()

i = N-1
num = 0
while i>=0 and num<K:
    if S[i]=='o':
        assign_b[i]=True
        num += 1
        i -= C+1
    else:
        i -=1


for i in range(N):
    if assign_t[i] and assign_b[i]:
        print(i+1)
Buggy python submission (verdict: Time Limit Exceeded):
from queue import Queue

H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
#%%
q = Queue()

dist = [[10**5]*W for _ in range(H)]

dx = [0,1]
dy = [1,0]
c = 0
f = 0
if S[0][0]=='#':
    c = 1
    f = 1
q.put([c,f,0,0])
while not(q.empty()):
    c,f,x,y = q.get()
    for i in range(2):
        nc = c
        nx = x+dx[i]
        ny = y+dy[i]
        if nx>=H or ny>=W:
            continue
        
        if f==0 and (S[nx][ny]=='#'):
            nc = c+1
        
        if nc < dist[nx][ny]: 
            dist[nx][ny] = nc
            nf = S[nx][ny]=='#'
            q.put([nc,nf,nx,ny])
        
print(dist[H-1][W-1])
        
    

Fixed submission:
from queue import Queue

H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
#%%
q = Queue()

dist = [[10**5]*W for _ in range(H)]

dx = [0,1]
dy = [1,0]
c = 0
f = 0
if S[0][0]=='#':
    c = 1
    f = 1
q.put([c,f,0,0])
while not(q.empty()):
    c,f,x,y = q.get()
    for i in range(2):
        nc = c
        nx = x+dx[i]
        ny = y+dy[i]
        if nx>=H or ny>=W:
            continue
        
        if f==0 and (S[nx][ny]=='#'):
            nc = c+1
        
        if nc < dist[nx][ny]: 
            dist[nx][ny] = nc
            nf = S[nx][ny]=='#'
            q.put([nc,nf,nx,ny])
        
print(dist[H-1][W-1])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import bisect_right

N,T = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(N)]
#%%
b0 = []
ab1 = []
for a,b in ab:
    if a==0:
        b0.append(b+1)
    else:
        ab1.append([a/(b+1), a, b+1])

N0 = len(b0)
N1 = len(ab1)

b0.sort()
if N0>0:
    b_ruiseki = [0]*N0
    b_ruiseki[0] = b0[0]
    for i in range(1,N0):
        b_ruiseki[i] = b_ruiseki[i-1] + b0[i]
else:
    b_ruiseki = []

ab1.sort(reverse = True)

INF = T + 100

dp = [[INF]*31 for _ in range(N1+1)]
dp[0][0] = 0
ans = 0
for i in range(N1):
    # dp[i+1][j] : 0~i番目までの店を見て，j個選ぶときの最小時間
    
    for j in range(31):
        if j>i+1:break
        if j==0:
            dp[i+1][j] = 0
        else:
            dp[i+1][j] = min(dp[i][j], dp[i][j-1] + (dp[i][j-1]+1)*ab1[i][1]+ab1[i][2])

for j in range(31):
    if dp[N1][j]>T:break    
    # a>0をj店舗めぐった後にa=0を何店舗めぐれるか
    num0 = bisect_right(b_ruiseki, T - dp[N1][j])
    ans = max(ans, j + num0)
        
    
print(ans)


Fixed submission:
import sys
from bisect import bisect_right

N,T = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(N)]
#%%
b0 = []
ab1 = []
for a,b in ab:
    if a==0:
        b0.append(b+1)
    else:
        ab1.append([(b+1)/a, a, b])

N0 = len(b0)
N1 = len(ab1)

b0.sort()
if N0>0:
    b_ruiseki = [0]*N0
    b_ruiseki[0] = b0[0]
    for i in range(1,N0):
        b_ruiseki[i] = b_ruiseki[i-1] + b0[i]
else:
    b_ruiseki = []

ab1.sort()

INF = T + 100

dp = [[INF]*31 for _ in range(N1+1)]
dp[0][0] = 0
ans = 0
for i in range(N1):
    # dp[i+1][j] : 0~i番目までの店を見て，j個選ぶときの最小時間
    
    for j in range(31):
        if j>i+1:break
        if j==0:
            dp[i+1][j] = 0
        else:
            dp[i+1][j] = min(dp[i][j], (dp[i][j-1]+1)*(ab1[i][1]+1)+ab1[i][2])

for j in range(31):
    if dp[N1][j]>T:break    
    # a>0をj店舗めぐった後にa=0を何店舗めぐれるか
    num0 = bisect_right(b_ruiseki, T - dp[N1][j])
    ans = max(ans, j + num0)
        
    
print(ans)
Buggy python submission (verdict: Runtime Error):
import math
gcd = math.gcd
# from fractions import gcd


MOD = 10**9+7

N = int(input())
A = list(map(int,input().split()))
#%%
def modinv(x,mod):
    return pow(x,mod-2,mod)

B = []
for a in A:
    b = modinv(a,MOD)
    B.append(b)

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

ans = 0

cur = 1
for a in A:
    cur =  lcm(cur,a)
for b in B:
    ans += cur*b
ans %= MOD
print(ans)



Fixed submission:

from fractions import gcd


MOD = 10**9+7

N = int(input())
A = list(map(int,input().split()))
#%%
def modinv(x,mod):
    return pow(x,mod-2,mod)

B = []
for a in A:
    b = modinv(a,MOD)
    B.append(b)

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

ans = 0

cur = 1
for a in A:
    cur =  lcm(cur,a)
for b in B:
    ans += cur*b
ans %= MOD
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd


MOD = 10**9+7

N = int(input())
A = list(map(int,input().split()))
#%%
def modinv(x,mod):
    return pow(x,mod-2,mod)

B = []
for a in A:
    b = modinv(a,MOD)
    B.append(b)

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

ans = 0

cur = 1
for a in A:
    cur =  lcm(cur,a)
for b in B:
    ans += cur*b
    ans %= MOD
print(ans)



Fixed submission:

from fractions import gcd


MOD = 10**9+7

N = int(input())
A = list(map(int,input().split()))
#%%
def modinv(x,mod):
    return pow(x,mod-2,mod)

B = []
for a in A:
    b = modinv(a,MOD)
    B.append(b)

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

ans = 0

cur = 1
for a in A:
    cur =  lcm(cur,a)
for b in B:
    ans += cur*b
ans %= MOD
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
#import math
#gcd = math.gcd
from fractions import gcd



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

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

ans = 0
MOD = 10**9+7
cur = 1
for a in A:
    cur =  lcm(cur,a)
    
for a in A:
    ans += cur//a
    ans %= MOD
print(ans)



Fixed submission:

from fractions import gcd


MOD = 10**9+7

N = int(input())
A = list(map(int,input().split()))
#%%
def modinv(x,mod):
    return pow(x,mod-2,mod)

B = []
for a in A:
    b = modinv(a,MOD)
    B.append(b)

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

ans = 0

cur = 1
for a in A:
    cur =  lcm(cur,a)
for b in B:
    ans += cur*b
ans %= MOD
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
#import math
#gcd = math.gcd
from fractions import gcd



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

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

ans = 0
MOD = 10**9+7

cur = 1
for a in A:
    cur =  lcm(cur,a)

for a in A:
    ans += cur//a
    ans %= MOD
print(ans)



Fixed submission:

from fractions import gcd


MOD = 10**9+7

N = int(input())
A = list(map(int,input().split()))
#%%
def modinv(x,mod):
    return pow(x,mod-2,mod)

B = []
for a in A:
    b = modinv(a,MOD)
    B.append(b)

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

ans = 0

cur = 1
for a in A:
    cur =  lcm(cur,a)
for b in B:
    ans += cur*b
ans %= MOD
print(ans)