Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
_list = []

for _ in range(n):
    _list.append(int(input()))

for i in range(n):
    if i==0:
        tmp_list = _list[1:]
        tmp_max = max(tmp_list)
        print(tmp_max)
        
        if _list[0] < tmp_max:
            maxi = tmp_max
        else:
            maxi = _list[0]

    elif 0<i<n-1:
        if _list[i] < maxi:
            print(maxi)
        else:
            tmp_list = _list[:i] + _list[i+1:]
            print(max(tmp_list))

    else:
        if _list[i] < maxi:
            print(maxi)
        else:
            tmp_list = _list[:n-1]
            print(max(tmp_list))

Fixed submission:
n = int(input())
a = []

for _ in range(n):
    a.append(int(input()))

b = sorted(a, reverse=True)

for i in range(n):
    if a[i] == b[0]:
        print(b[1])
    else:
        print(b[0])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
_list = []

for _ in range(n):
    _list.append(int(input()))

maxi = 0
for i in range(n):
    if i==0:
        tmp_list = _list[1:]
    elif 0<i<n-1:
        tmp_list = _list[:i] + _list[i+1:]
    else:
        tmp_list = _list[:n-1]

    tmp_max = max(tmp_list)
    print(tmp_max)

Fixed submission:
n = int(input())
a = []

for _ in range(n):
    a.append(int(input()))

b = sorted(a, reverse=True)

for i in range(n):
    if a[i] == b[0]:
        print(b[1])
    else:
        print(b[0])
Buggy python submission (verdict: Runtime Error):
p, q, r = list(map(int, input().split()))
list_ = sorted(list_)

print(list_[0]+list_[1])

Fixed submission:
list_ = sorted(list(map(int, input().split())))
print(list_[0]+list_[1])
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = [int(input()) for _ in range(M)]
mod = 10**9+7

if N==1:
    print(1)
    exit()

step = [0] * (N+1)
step[0] = 1
step[1] = 1

for i in range(N+1):
    if i==0 or i==1:
        if i in A:
            step[i] = 0
    else:
        step[i] = step[i-1]+step[i-2]
        if i in A:
            step[i] = 0
        step[i] %= mod

print(step[-1])

Fixed submission:
N, M = map(int, input().split())
A = set([int(input()) for _ in range(M)])
mod = 10**9+7

step = [0] * (N+1)
step[0] = 1
step[1] = 1

for i in range(N+1):
    if i==0 or i==1:
        if i in A:
            step[i] = 0
    else:
        step[i] = step[i-1]+step[i-2]
        if i in A:
            step[i] = 0
        step[i] %= mod

print(step[-1])
Buggy python submission (verdict: Time Limit Exceeded):
from heapq import (
    heapify,  # 優先度付きキューの生成
    heappop,  # キューから値を取り出す
    heappush,   # キューに値を格納
    heappushpop,   # push -> popの順
    heapreplace,   # pop -> pushの順
    )

N, M = map(int, input().split())
A = list(map(int, input().split()))
A = [-i for i in A]

bc = []
for _ in range(M):
    B, C = map(int, input().split())
    bc.extend([-C]*B)

A = A + bc
heapify(A)
total = 0

for _ in range(N):
    total += -heappop(A)

print(total)

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

dic = {}
for i in range(N):
    tmp = A[i]
    if tmp in dic:
        dic[tmp] += 1
    else:
        dic[tmp] = 1

for _ in range(M):
    B, C = map(int, input().split())
    if C in dic:
        dic[C] += B
    else:
        dic[C] = B

dic = sorted(dic.items(), reverse=True)

ans, cnt = 0, 0
i = 0
while True:
    k, v = dic[i]
    cnt += v
    if cnt < N:
        ans += k*v
    elif cnt == N:
        ans += k*v
        break
    else:
        ans += k*(v - (cnt-N))
        break
    i += 1

print(ans)

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

bc = {}
for _ in range(M):
    B, C = map(int, input().split())
    if C in bc:
        bc[C] += B
    else:
        bc[C] = B

bc = sorted(bc.items(), reverse=True)
BC = []

for k, v in bc:
    BC.extend([k]*v)

for i in range(min(N, len(BC))):
    a = A[i]
    b = BC[i]
    A[i] = max(a, b)

print(sum(A))

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

dic = {}
for i in range(N):
    tmp = A[i]
    if tmp in dic:
        dic[tmp] += 1
    else:
        dic[tmp] = 1

for _ in range(M):
    B, C = map(int, input().split())
    if C in dic:
        dic[C] += B
    else:
        dic[C] = B

dic = sorted(dic.items(), reverse=True)

ans, cnt = 0, 0
i = 0
while True:
    k, v = dic[i]
    cnt += v
    if cnt < N:
        ans += k*v
    elif cnt == N:
        ans += k*v
        break
    else:
        ans += k*(v - (cnt-N))
        break
    i += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))

bc = []
for _ in range(M):
    B, C = map(int, input().split())
    bc.extend([C]*B)

bc = list(reversed(sorted(bc)))

for i in range(min(N, len(bc))):
    a = A[i]
    b = bc[i]
    A[i] = max(a, b)

print(sum(A))

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

dic = {}
for i in range(N):
    tmp = A[i]
    if tmp in dic:
        dic[tmp] += 1
    else:
        dic[tmp] = 1

for _ in range(M):
    B, C = map(int, input().split())
    if C in dic:
        dic[C] += B
    else:
        dic[C] = B

dic = sorted(dic.items(), reverse=True)

ans, cnt = 0, 0
i = 0
while True:
    k, v = dic[i]
    cnt += v
    if cnt < N:
        ans += k*v
    elif cnt == N:
        ans += k*v
        break
    else:
        ans += k*(v - (cnt-N))
        break
    i += 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int, input().split())

ans = 0
n_iter_r = N//R + 1
n_iter_g = N//G + 1

for r in range(n_iter_r):
    for g in range(n_iter_g):
        if N - R*r - G*g < 0:
            break
        else:
            if (N - R*r - G*g)%B==0:
                ans += 1

print(ans)

Fixed submission:
R, G, B, N = map(int, input().split())

ans = 0
n_iter_r = N//R + 1
n_iter_g = N//G + 1

for r in range(n_iter_r):
    for g in range(n_iter_g):
        nokori = N - R*r - G*g
        if nokori < 0:
            break
        else:
            if nokori%B==0:
                ans += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int, input().split())

ans = 0
n_iter_r = N//R + 1
n_iter_g = N//G + 1

for r in range(n_iter_r):
    for g in range(n_iter_g):
        if N - R*r - G*g < 0:
            break
        else:
            if (N - R*r - G*g)%B==0:
                if (N - R*r - G*g)//B >= 0:
                    ans += 1

print(ans)

Fixed submission:
R, G, B, N = map(int, input().split())

ans = 0
n_iter_r = N//R + 1
n_iter_g = N//G + 1

for r in range(n_iter_r):
    for g in range(n_iter_g):
        nokori = N - R*r - G*g
        if nokori < 0:
            break
        else:
            if nokori%B==0:
                ans += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int, input().split())

ans = 0
n_iter_r = N//R + 1
n_iter_g = N//G + 1

for r in range(n_iter_r):
    for g in range(n_iter_g):
        if N - R*r - G*g < 0:
            continue
        else:
            if (N - R*r - G*g)%B==0:
                if (N - R*r - G*g)//B >= 0:
                    ans += 1
                    
print(ans)

Fixed submission:
R, G, B, N = map(int, input().split())

ans = 0
n_iter_r = N//R + 1
n_iter_g = N//G + 1

for r in range(n_iter_r):
    for g in range(n_iter_g):
        nokori = N - R*r - G*g
        if nokori < 0:
            break
        else:
            if nokori%B==0:
                ans += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int, input().split())

ans = 0

n_iter_r = int(N/R)+1
n_iter_g = int(N/G)+1

for r in range(n_iter_r):
    for g in range(n_iter_g):
        if (N - R*r - G*g)%B==0:
            if (N - R*r - G*g)//B >= 0:
                ans += 1

print(ans)

Fixed submission:
R, G, B, N = map(int, input().split())

ans = 0
n_iter_r = N//R + 1
n_iter_g = N//G + 1

for r in range(n_iter_r):
    for g in range(n_iter_g):
        nokori = N - R*r - G*g
        if nokori < 0:
            break
        else:
            if nokori%B==0:
                ans += 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = int(input())
b = int(input())
c = int(input())
d = int(input())
e = int(input())

p1, p2, p3, p4, p5, p6 = n, 0, 0, 0, 0, 0
ans = 0

def carry(p1, p2, num):
    if p1>=num:
        p1 -= num
        p2 += num
    elif 0<p1<num:
        p2 += p1
        p1 = 0
    return p1, p2

while p6!=n:
    ans += 1
    p5, p6 = carry(p5, p6, e)
    p4, p5 = carry(p4, p5, d)
    p3, p4 = carry(p3, p4, c)
    p2, p3 = carry(p2, p3, b)
    p1, p2 = carry(p1, p2, a)

print(ans)

Fixed submission:
from math import ceil, floor
#ceil()  # 切り上げ。常にup
#floor()  # 切り捨て。常にdown
#int()  # 常に0に近く（正負で挙動違う）
#round(f),  round(f, 1)  # 四捨五入（第二引数で小数点桁数を指定）
n = int(input())
l = [int(input()) for _ in range(5)]
print(ceil(n / min(l)) + 4)
Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
S = input()
S = [c for c in S]
total = [0]*N
cnt = 0

flag = False
for i in range(N):
    if flag:
        if S[i]=='C':
            cnt += 1
        flag = False
    else:
        if S[i]=='A':
            flag = True
    total[i] = cnt

for _ in range(Q):
    l, r = map(int, input().split())
    s = total[l-1:r]
    print(s[-1]-s[0])

Fixed submission:
N, Q = map(int, input().split())
S = input()

total = [0]*(N+1)
for i in range(2, N+1):
    if S[i-2]=='A' and S[i-1]=='C':
        total[i] = total[i-1]+1
    else:
        total[i] = total[i-1]

for i in range(Q):
    l, r = map(int, input().split())
    print(total[r]-total[l])
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
P = [[] for _ in range(N+1)]
ite = []

for _ in range(M):
    p, y = map(int, input().split())
    P[p].append(y)
    ite.append([p, y])

for i in range(N+1):
    P[i] = sorted(P[i])

for i in range(M):
    p, y = ite[i]
    #print(P[p])
    num = P[p].index(y)+1
    idx = '{}{}'.format(str(p).zfill(6), str(num).zfill(6))
    print(idx)

Fixed submission:
N, M = map(int, input().split())
P = [[] for _ in range(N+1)]
ite = []

for _ in range(M):
    p, y = map(int, input().split())
    P[p].append(y)
    ite.append([p, y])

dic = {}
for p in range(N+1):
    P[p] = sorted(P[p])
    for i, y in enumerate(P[p]):
        i += 1
        dic[y] = '{}{}'.format(str(p).zfill(6), str(i).zfill(6))

for i in range(M):
    p, y = ite[i]
    print(dic[y])

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
P = [[] for _ in range(N+1)]
ite = []

for _ in range(M):
    p, y = map(int, input().split())
    P[p].append(y)
    ite.append([p, y])

for i in range(N+1):
    P[i] = sorted(P[i])

for i in range(M):
    p, y = ite[i]
    #print(P[p])
    num = P[p].index(y)+1
    idx = '{}{}'.format(str(p).zfill(6), str(num).zfill(6))
    print(idx)

Fixed submission:
N, M = map(int, input().split())
P = [[] for _ in range(N+1)]
ite = []

for _ in range(M):
    p, y = map(int, input().split())
    P[p].append(y)
    ite.append([p, y])

dic = {}
for p in range(N+1):
    P[p] = sorted(P[p])
    for i, y in enumerate(P[p]):
        i += 1
        dic[y] = '{}{}'.format(str(p).zfill(6), str(i).zfill(6))

for i in range(M):
    p, y = ite[i]
    print(dic[y])
Buggy python submission (verdict: Runtime Error):
from fractions import gcd

def lcm(m, n):
    return (m * n) // gcd(m, n)

def lcm_list(a):
    x = a[0]
    for i in range(1, len(a)):
        x = (x * a[i]) // gcd(x, a[i])
    return x

N, M = map(int, input().split())
S = input()
T = input()

l = lcm(N, M)

if l==N or l==M or N[0]!=M[0]:
    print(-1)
else:
    print(l)

Fixed submission:
from math import gcd

N, M = map(int, input().split())
S = input()
T = input()

def lcm(m, n):
    return (m * n) // gcd(m, n)

lc = lcm(N, M)

ss = lc//N
tt = lc//M

for i in range(1, lc+1, ss*tt):
    s_tmp = (i - 1) // ss
    t_tmp = (i - 1) // tt

    if S[s_tmp] != T[t_tmp]:
        print(-1)
        exit()

print(lc)
Buggy python submission (verdict: Runtime Error):
s = input()
k = int(input())
flag = True

if len(s)==1:
    print(int(s))
    exit()

new = ''
for i in len(k):
    if s[i]!='1':
        flag = False
        new += i

if flag:
    print(1)
else:
    print(new[0])

Fixed submission:
s = input()
k = int(input())
flag = False

for i in range(min(len(s), k)):
    if s[i] != '1':
        ans = int(s[i])
        flag = True
        break

if flag:
    print(ans)
else:
    print(1)
Buggy python submission (verdict: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, n_origin, s, keta;
    cin >> n;
    n_origin = n;
    s = 0;
    keta = 10;

    while (n >= 1) {
        s += n%10;
        n /= keta;
    }

    if (n_origin%s == 0) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
}

Fixed submission:
n = int(input())
n_origin = n

s = 0
keta = 10

while n >= 1:
    s += n%10
    n //= keta

if n_origin%s==0:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input())

if (n-1)%(k-1)==0:
    print((n-1)//(k-1))
else:
    print((n-1)//(k-1)+1)

Fixed submission:
n, k = map(int, input().split())

if (n-1)%(k-1)==0:
    print((n-1)//(k-1))
else:
    print((n-1)//(k-1)+1)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int, input().split()))

def factorization(n):
    arr, temp = [], n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr

fac = [0] * N
for i in range(N):
    if a[i]%2==0:
        arr = factorization(a[i])
        fac[i] = arr[0][1]

print(sum(fac))

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

def factorization(n):
    cnt = 0
    while n%2==0:
        n /= 2
        cnt += 1
    return cnt

fac = [0] * N
for i in range(N):
    if a[i]%2==0:
        fac[i] = factorization(a[i])

print(sum(fac))
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
k = int(input())
strings = []
for start in range(len(s)):
    for end in range(start+1, len(s)+1):
        moji = s[start:end]
        strings.append(moji)

ans = sorted(list(set(strings)))[k-1]
print(ans)

Fixed submission:
s = input()
k = int(input())
strings = set()
for start in range(len(s)):
    for end in range(start+1, start+1+k):
        moji = s[start:end]
        strings.add(moji)

ans = sorted(list(strings))[k-1]
print(ans)
Buggy python submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = [['.']*(w+2)]
for _ in range(h):
    s.append(['.'] + list(input()) + ['.'])
s.append(['.']*(w+2))
ans = 'Yes'

for i in range(1, w):
    for j in range(1, h):
        if s[i][j] == '#':
            l = s[i][j-1]
            u = s[i-1][j]
            r = s[i][j+1]
            d = s[i+1][j]
            if l=='.' and u=='.' and r=='.' and d=='.':
                ans = 'No'

print(ans)

Fixed submission:
H, W = map(int, input().split())
S = ['.' + input() + '.' for _ in range(H)]
S = ['.'*(W+2)] + S + ['.'*(W+2)]

ans = 'Yes'

for i in range(1, H+1):
    for j in range(1, W+1):
        if S[i][j]=='#':
            if S[i-1][j]!='#' and S[i][j-1]!='#' and S[i+1][j]!='#' and S[i][j+1]!='#':
                print('No')
                exit()

print(ans)

Buggy python submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = [['.']*(w+2)]
for _ in range(h):
    s.append(['.'] + list(input()) + ['.'])
s.append(['.']*(w+2))
ans = 'Yes'

for i in range(1, w+1):
    for j in range(1, h+1):
        if s[i][j] == '#':
            l = s[i][j-1]
            u = s[i-1][j]
            r = s[i][j+1]
            d = s[i+1][j]
            if l=='.' and u=='.' and r=='.' and d=='.':
                ans = 'No'

print(ans)

Fixed submission:
H, W = map(int, input().split())
S = ['.' + input() + '.' for _ in range(H)]
S = ['.'*(W+2)] + S + ['.'*(W+2)]

ans = 'Yes'

for i in range(1, H+1):
    for j in range(1, W+1):
        if S[i][j]=='#':
            if S[i-1][j]!='#' and S[i][j-1]!='#' and S[i+1][j]!='#' and S[i][j+1]!='#':
                print('No')
                exit()

print(ans)

Buggy python submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = [['.']*(w+2)]
for _ in range(h):
    s.append(['.'] + list(input()) + ['.'])
s.append(['.']*(w+2))
ans = 'Yes'

for i in range(1, w+1):
    for j in range(1, h+1):
        if s[i][j] == '#':
            l = s[i][j-1]
            u = s[i-1][j]
            r = s[i][j+1]
            d = s[i+1][j]
            if l=='.' and u=='.' and r=='.' and d=='.':
                ans = 'No'

print(ans)

Fixed submission:
H, W = map(int, input().split())
S = ['.' + input() + '.' for _ in range(H)]
S = ['.'*(W+2)] + S + ['.'*(W+2)]

ans = 'Yes'

for i in range(1, H+1):
    for j in range(1, W+1):
        if S[i][j]=='#':
            if S[i-1][j]!='#' and S[i][j-1]!='#' and S[i+1][j]!='#' and S[i][j+1]!='#':
                print('No')
                exit()

print(ans)
Buggy python submission (verdict: Runtime Error):
n = input()
n = set(n[0], n[1], n[2], n[3])

if len(n)<=2:
    print('Yes')
else:
    print('No')

Fixed submission:
n = input()

if n[0]==n[1]==n[2] or n[1]==n[2]==n[3]:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
N = int(input())
p = list(map(int, input().split()))

ans = 0
for i in range(N):
    if p[i] == i+1:
        p[i], p[i+1] = p[i+1], p[i]
        ans += 1

print(ans)

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

ans = 0
for i in range(N):
    if i != N-1:
        if p[i] == i+1:
            p[i], p[i+1] = p[i+1], p[i]
            ans += 1
    else:
        if p[i] == i+1:
            p[i], p[i-1] = p[i-1], p[i]
            ans += 1

print(ans)
Buggy python submission (verdict: Runtime Error):
from collections import Counter

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

c = Counter(a)  # リストの要素をカウント
cnt = [k for k, v in c.imtes() if v >= 2]

if len(cnt) < 2:
    print(0)
else:
    cnt = sorted(cnt, reverse=True)
    if c[cnt[0]] >= 4:
        print(cnt[0] * cnt[0])
    else:
        print(cnt[0] * cnt[1])

Fixed submission:
from collections import Counter

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

c = Counter(a)  # リストの要素をカウント
cnt = [k for k, v in c.items() if v >= 2]

if len(cnt) < 2:
    print(0)
else:
    cnt = sorted(cnt, reverse=True)
    if c[cnt[0]] >= 4:
        print(cnt[0] * cnt[0])
    else:
        print(cnt[0] * cnt[1])
Buggy python submission (verdict: Runtime Error):
N = int(input())
mod = 10**9+7
s = [0] * (n+1)

for i in range(2, n+1):
    p = 2
    while i>1:
        while i%p==0:
            i //= p
            s[p] += 1
        p += 1

print(s)
ans = 1
for i in s:
    ans *= i+1
    ans %= mod

print(ans)

Fixed submission:
n = int(input())
mod = 10**9+7
s = [0] * (n+1)

for i in range(2, n+1):
    p = 2
    while i>1:
        while i%p==0:
            i //= p
            s[p] += 1
        p += 1

ans = 1
for i in s:
    ans *= i+1
    ans %= mod

print(ans)
Buggy python submission (verdict: Runtime Error):
S = input()

while S:
    if S[:7] == 'dreamer' and S[7] != 'a':
        S = S[7:]
    elif S[:6] == 'eraser':
        S = S[6:]
    elif S[:5] == 'dream':
        S = S[5:]
    elif S[:5] == 'erase':
        S = S[5:]
    else:
        print('No')
        exit()

print('Yes')

Fixed submission:
S = input()
S = ''.join(list(reversed([c for c in S])))

while S:
    if S[:7] == 'remaerd':
        S = S[7:]
    elif S[:6] == 'resare':
        S = S[6:]
    elif S[:5] == 'maerd':
        S = S[5:]
    elif S[:5] == 'esare':
        S = S[5:]
    else:
        print('NO')
        exit()

print('YES')
Buggy python submission (verdict: Time Limit Exceeded):
mod = 998244353
n,k = map(int,input().split())
K = [list(map(int,input().split())) for i in range(k)]

dp = [0]*(n+1)
dp[0] = 1
dp2 = [0]*(n+1)
for i in range(n):
    dp2[i] = (dp2[i]+dp2[i-1])%mod
    dp[i] = (dp[i]+dp2[i])%mod
    for l,r in K:
        dp2[min(i+l,n)] = (dp2[min(i+l,n)]+dp[i])%mod
        dp2[min(i+r+1,n)] = (dp2[min(i+r+1,n)]-dp[i])%mod
print(dp[n-1]%mod)


Fixed submission:
mod = 998244353
n,k = map(int,input().split())
K = [list(map(int,input().split())) for i in range(k)]

dp = [0]*(n+5)
dp[1] = 1
dp2 = [0]*(n+5)
for i in range(1,n+1):
    dp[i] = (dp[i]+dp2[i]+dp2[i-1])%mod
    dp2[i] = (dp2[i]+dp2[i-1])%mod
    for l,r in K:
        dp2[min(i+l,n+2)] += dp[i]
        dp2[min(i+l,n+2)] %= mod
        dp2[min(i+r+1,n+2)] -= dp[i]
        dp2[min(i+r+1,n+2)] %= mod
print(dp[n]%mod)
Buggy python submission (verdict: Time Limit Exceeded):
n,x,m = map(int,input().split())

l = []
ans = 0
for i in range(n):
    if x in l:
        d = l.index(x)
        ans += sum(l[:d])
        le = len(l)-d
        count = sum(l[d:])
        n -= d
        ans += count*(n//le)
        n %= le
        for k in range(d,d+n):
            ans += l[k]
        print(ans)
        exit()
    l.append(x)
    x = (x**2)%m

print(sum(l))


Fixed submission:
n,x,m = map(int,input().split())

l = []
ans = 0
for i in range(n):
    if x in l:
      
        d = l.index(x)
        ans += sum(l[:d])
        le = len(l)-d
        
        count = sum(l[d:])
        #print(count)
        n -= d
        #print(n)
        ans += count*(n//le)
        n %= le
        #print(n,ans)
        for k in range(d,d+n):
            ans += l[k]

        print(ans)
        exit()
        break
    l.append(x)
    x = (x**2)%m

print(sum(l))
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ca = [0]*(n+1)
cb = [0]*(n+1)
for i in a:
    ca[i] += 1
for i in b:
    cb[i] += 1
ma = 0
ind = -1
for i in range(n+1):

    if ca[i]+cb[i]> ma:
        ma = ca[i]+cb[i]
        ind = i
    if ma > n:
        print("No")
        exit()

print("Yes")
ans = [-1]*n
l = a.index(ind)+ca[ind]
s = b.index(ind)
for i in range(s,s+n):
    ans[(l+i)%n] = b[i%n]
print(*ans)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
b = b[::-1]

l = []
num = -1
for i in range(n):
    if a[i] == b[i]:
        l.append(i)
        num = a[i]

ind = 0
for i in range(n):
    if b[i] != num and a[i] != num and ind < len(l):
        b[i],b[l[ind]] = b[l[ind]],b[i]
        ind += 1
if ind < len(l):
    print("No")
else:
    print("Yes")
    print(*b)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
M = max(a)+1
if M == 2:
    print("pairwise coprime")
    exit()
count = [0]*(M)

for i in range(2,M):
    if count[i] == 0:
        for j in range(i,M,i):
            if count[j] == 0:
                count[j] = i
check = [0]*(M)
for i in a:
    x = i
    while x > 1:
        check[count[x]] += 1
        y = count[x]
        while x%y == 0:
            x //= y
ans = max(check)
if ans == 1:
    print("pairwise coprime")
elif ans == n:
    print("not coprime")
else:
    print("setwise coprime")

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
M = max(a)+1
if M == 2:
    print("pairwise coprime")
    exit()
count = [0]*(M)
prime = [2,3,5,7,11,13]
for i in range(14,int(M**0.5)+2):
    check = True
    for j in prime:
        if i%j == 0:
            check = False
            break
    if check:
        prime.append(i)

for i in a:
    for j in prime:
        if i < j:
            break
        if i%j==0:
            while i%j == 0:
                i //= j
            count[j] += 1
    if i > 1:
        count[i] += 1

ans = max(count)

if ans == 1:
    print("pairwise coprime")
elif ans == n:
    print("not coprime")
else:
    print("setwise coprime")