Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
import heapq

N,M = (int(x) for x in input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
for i in range(M):
    B,C = (int(x) for x in input().split())
    for j in range(B):
        temp = heapq.heappop(A)
        if temp < C:
            heapq.heappush(A,C)
        else:
            heapq.heappush(A,temp)
            break
print(sum(A))

Fixed submission:
import sys
input = sys.stdin.readline
import heapq

N,M = (int(x) for x in input().split())
A = list(map(int, input().split()))
BC = []
changeable = N
broke = False
for i in range(M):
    B,C = (int(x) for x in input().split())
    BC.append((C*(-1),B))
heapq.heapify(A)
heapq.heapify(BC)
for i in range(M):
    t1 = heapq.heappop(BC)
    if t1[1] < changeable:
        for j in range(t1[1]):
            t2 = heapq.heappop(A)
            if t2 < t1[0] * (-1):
                heapq.heappush(A,t1[0]*(-1))
            else:
                heapq.heappush(A,t2)
                broke = True
                break
        else:
            changeable -= t1[1]
    else:
        for j in range(changeable):
            t2 = heapq.heappop(A)
            if t2 < t1[0] * (-1):
                heapq.heappush(A,t1[0]*(-1))
            else:
                heapq.heappush(A,t2)
                break
        break
    if broke:
        break
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
import heapq

N,M = (int(x) for x in input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
for i in range(M):
    B,C = (int(x) for x in input().split())
    for j in range(B):
        temp = heapq.heappop(A)
        if temp < C:
            heapq.heappush(A,C)
        else:
            heapq.heappush(A,temp)
            break
print(sum(A))

Fixed submission:
import sys
input = sys.stdin.readline
import heapq

N,M = (int(x) for x in input().split())
A = list(map(int, input().split()))
BC = []
changeable = N
broke = False
for i in range(M):
    B,C = (int(x) for x in input().split())
    BC.append((C*(-1),B))
heapq.heapify(A)
heapq.heapify(BC)
for i in range(M):
    t1 = heapq.heappop(BC)
    if t1[1] < changeable:
        for j in range(t1[1]):
            t2 = heapq.heappop(A)
            if t2 < t1[0] * (-1):
                heapq.heappush(A,t1[0]*(-1))
            else:
                heapq.heappush(A,t2)
                broke = True
                break
        else:
            changeable -= t1[1]
    else:
        for j in range(changeable):
            t2 = heapq.heappop(A)
            if t2 < t1[0] * (-1):
                heapq.heappush(A,t1[0]*(-1))
            else:
                heapq.heappush(A,t2)
                break
        break
    if broke:
        break
print(sum(A))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
ans = 0
for i in range(1,N):
    if N % i == 0:
        temp = N // i - 1
        d,m = divmod(N,temp)
        if d != m:
            break
        else:
            ans += temp
print(ans)

Fixed submission:
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())
divisors = make_divisors(N)
ans = 0
divisors.sort(reverse=True)
for x in divisors:
    if x > 1:
        temp = x - 1
        d,m = divmod(N,temp)
        if d != m:
            break
        else:
            ans += temp
print(ans)

Buggy python submission (verdict: Runtime Error):
N = int(input())
ans = 0
for i in range(1,N+1):
    if N % i == 0:
        temp = N // i - 1
        d,m = divmod(N,temp)
        if d != m:
            break
        else:
            ans += temp
print(ans)

Fixed submission:
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())
divisors = make_divisors(N)
ans = 0
divisors.sort(reverse=True)
for x in divisors:
    if x > 1:
        temp = x - 1
        d,m = divmod(N,temp)
        if d != m:
            break
        else:
            ans += temp
print(ans)
Buggy python submission (verdict: Runtime Error):
from itertools import product
from heapq import *

X,Y,Z,K,*f = map(int, open(0).read().split())
A = f[:X]
B = f[X:X+Y]
C = f[X+Y:]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
AB = [sum(x) * (-1) for x in product(A,B)]
heapify(AB)
AB_picked = []
for i in range(K):
    AB_picked.append(heappop(AB)*(-1))
ABC = [sum(x) * (-1) for x in product(AB_picked,C)]
heapify(ABC)
for i in range(K):
    print(heappop(ABC)*(-1))

Fixed submission:
from itertools import product
from heapq import *

X,Y,Z,K,*f = map(int, open(0).read().split())
A = f[:X]
B = f[X:X+Y]
C = f[X+Y:]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
AB = [sum(x) * (-1) for x in product(A,B)]
heapify(AB)
AB_picked = []
for i in range(K):
    if AB:
        AB_picked.append(heappop(AB)*(-1))
    else:
        break
ABC = [sum(x) * (-1) for x in product(AB_picked,C)]
heapify(ABC)
for i in range(K):
    print(heappop(ABC)*(-1))
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
l = list(S)
count = 0
end = False
while end != True:
    temp_count = 0
    for i in range(len(l)-1):
        if l[i] == "B" and l[i+1] == "W":
            l[i] = "W"
            l[i+1] = "B"
            count += 1
            temp_count += 1
    if temp_count == 0:
        end = True
print(count)

Fixed submission:
S = input()
l1 = list(S)
l2 = sorted(l1,reverse=True)
m1 = [i for i, x in enumerate(l1) if x == 'W']
m2 = [i for i, x in enumerate(l2) if x == 'W']
count = 0
for i in range(len(m1)):
    count += m1[i] - m2[i]
print(count)
Buggy python submission (verdict: Runtime Error):
def make_divisors(n):
    divisors = set()
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.add(i)
            if i != n // i:
                divisors.add(n//i)
    return divisors

N,M = (int(x) for x in input().split())
if M > N:
    N,M = M,N
    S,T = T,S
S = list(input())
T = list(input())
dn = make_divisors(N)
dm = make_divisors(M)
dnm = dn & dm
if dnm == {1}:
    if S[0] == T[0]:
        print(N*M)
    else:
        print('-1')
else:
    gcd = max(dnm)
    n = N // gcd
    m = M // gcd
    for i in range(N//n):
        if S[n*i] != T[m*i]:
            print('-1')
            break
    else:
        print(N*M//gcd)

Fixed submission:
def make_divisors(n):
    divisors = set()
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.add(i)
            if i != n // i:
                divisors.add(n//i)
    return divisors

N,M = (int(x) for x in input().split())
S = list(input())
T = list(input())
if M > N:
    N,M = M,N
    S,T = T,S
dn = make_divisors(N)
dm = make_divisors(M)
dnm = dn & dm
if dnm == {1}:
    if S[0] == T[0]:
        print(N*M)
    else:
        print('-1')
else:
    gcd = max(dnm)
    n = N // gcd
    m = M // gcd
    for i in range(N//n):
        if S[n*i] != T[m*i]:
            print('-1')
            break
    else:
        print(N*M//gcd)
Buggy python submission (verdict: Runtime Error):
N = int(input())
ans = set()
for i in range(N):
    x,y,h = (int(x) for x in input().split())
    if h > 0:
        for j in range(101):
            for k in range(101):
                ans.add((j,k,h+abs(j-x)+abs(k-y)))
        break
for i in range(N):
    x,y,h = (int(x) for x in input().split())
    temp = set()
    for j in range(101):
        for k in range(101):
            temp.add((j,k,h+abs(j-x)+abs(k-y)))
    if len(ans&temp) > 0:
        ans = ans & temp
for x in ans:
    print(*x)

Fixed submission:
N = int(input())
xyh = [tuple(map(int, input().split())) for _ in range(N)]
for x in xyh:
    if x[2] > 0:
        base = x
        break
for i in range(101):
    for j in range(101):
        h = base[2] + abs(i-base[0]) + abs(j-base[1])
        for x in xyh:
            if x[2] != max(h-abs(i-x[0])-abs(j-x[1]),0):
                break
        else:
            print(i,j,h)
            exit()
Buggy python submission (verdict: Runtime Error):
import bisect

N,K = (int(x) for x in input().split())
x = list(map(int, input().split()))
bs = bisect.bisect_left(x,0)
ans = float('inf')
if x[bs] == 0:
    x.pop(bs)
    K -= 1
neg = bs
pos = N - bs
if len(x) == 0 or K == 0:
    print('0')
elif neg == 0:
    print(x[K-1])
elif pos == 0:
    print(x[-K]*(-1))
else:
    for i in range(min(neg,K)+1):
        if i + pos < K:
                continue
        else:
            if i == 0:
                ans = min(ans,x[bs+K-1])
            elif i == K:
                ans = min(ans,x[bs-K]*(-1))
            else:
                tempn = x[bs-i] * (-1)
                tempp = x[bs+K-i-1]
                ans = min(ans,min(tempn,tempp)*2+max(tempn,tempp))
    print(ans)

Fixed submission:
import bisect

N,K = (int(x) for x in input().split())
x = list(map(int, input().split()))
bs = bisect.bisect_left(x,0)
ans = float('inf')
if bs == N:
    print(x[-K]*(-1))
    exit()
else:
    if x[bs] == 0:
        x.pop(bs)
        K -= 1
neg = bs
pos = N - bs
if len(x) == 0 or K == 0:
    print('0')
elif neg == 0:
    print(x[K-1])
else:
    for i in range(min(neg,K)+1):
        if i + pos < K:
                continue
        else:
            if i == 0:
                ans = min(ans,x[bs+K-1])
            elif i == K:
                ans = min(ans,x[bs-K]*(-1))
            else:
                tempn = x[bs-i] * (-1)
                tempp = x[bs+K-i-1]
                ans = min(ans,min(tempn,tempp)*2+max(tempn,tempp))
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
s,K = open(0).read().split()
K = int(K)
ss = set()
temp = set([''])
for x in s:
    temp = set([''.join((y,x)) for y in temp])
    ss = ss | temp
    temp.add('')
l = sorted(ss)
print(l[K-1])

Fixed submission:
s,K = open(0).read().split()
K = int(K)
ss = set()
temp = set([''])
for x in s:
    temp = set([''.join((y,x)) for y in temp if len(y) <= 4])
    ss = ss | temp
    temp.add('')
l = list(ss)
l.sort()
print(l[K-1])

Buggy python submission (verdict: Time Limit Exceeded):
s,K = open(0).read().split()
K = int(K)
ss = set()
temp = set([''])
for x in s:
    temp = set([''.join((y,x)) for y in temp])
    ss = ss | temp
    temp.add('')
l = sorted(ss)
print(l[K-1])

Fixed submission:
s,K = open(0).read().split()
K = int(K)
ss = set()
temp = set([''])
for x in s:
    temp = set([''.join((y,x)) for y in temp if len(y) <= 4])
    ss = ss | temp
    temp.add('')
l = list(ss)
l.sort()
print(l[K-1])

Buggy python submission (verdict: Time Limit Exceeded):
s,K = open(0).read().split()
K = int(K)
ss = set()
temp = set([''])
for x in s:
    t2 = set([''.join((y,x)) for y in temp])
    ss = ss | t2
    temp = t2.copy()
    temp.add('')
l = sorted(ss)
print(l[K-1])

Fixed submission:
s,K = open(0).read().split()
K = int(K)
ss = set()
temp = set([''])
for x in s:
    temp = set([''.join((y,x)) for y in temp if len(y) <= 4])
    ss = ss | temp
    temp.add('')
l = list(ss)
l.sort()
print(l[K-1])
Buggy python submission (verdict: Runtime Error):
N,Z,W = (int(x) for x in input().split())
a = list(map(int, input().split()))
print(max(abs(a[-1]-W),abs(a[-1]-a[-2])))

Fixed submission:
N,Z,W = (int(x) for x in input().split())
a = list(map(int, input().split()))
if N == 1:
    print(abs(W-a[0]))
else:
    print(max(abs(a[-1]-W),abs(a[-1]-a[-2])))
Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect
N = int(input())
A = sorted(list(map(int,input().split())))
B = sorted(list(map(int,input().split())))
C = sorted(list(map(int,input().split())))
count = 0
for i in range(N):
    minb = bisect(B, A[i])
    if minb == N:
        break
    for j in range(minb, N):
        if B[j] == A[i]:
            continue
        minc = bisect(C, B[j])
        if minc == N:
            break
        count += N - minc
print(count)

Fixed submission:
import bisect
N = int(input())
A = sorted(list(map(int,input().split())))
B = sorted(list(map(int,input().split())))
C = sorted(list(map(int,input().split())))
print(sum(bisect.bisect_left(A, i)*(N-bisect.bisect_right(C, i)) for i in B))

Buggy python submission (verdict: Runtime Error):
from bisect import bisect
N = int(input())
A = sorted(list(map(int,input().split())))
A = sorted(list(map(int,input().split())))
A = sorted(list(map(int,input().split())))
count = 0
for i in range(N):
    minb = bisect(B, A[i])
    if minb == N:
        break
    for j in range(minb, N):
        if B[j] == A[i]:
            continue
        minc = bisect(C, B[j])
        if minc == N:
            break
        count += N - minc
print(count)

Fixed submission:
import bisect
N = int(input())
A = sorted(list(map(int,input().split())))
B = sorted(list(map(int,input().split())))
C = sorted(list(map(int,input().split())))
print(sum(bisect.bisect_left(A, i)*(N-bisect.bisect_right(C, i)) for i in B))

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
B.sort()
C.sort()
count = 0
for i in range(N):
    minb = bisect(B, A[i])
    if minb == N:
        break
    for j in range(minb, N):
        if B[j] == A[i]:
            continue
        minc = bisect(C, B[j])
        if minc == N:
            break
        count += N - minc
print(count)

Fixed submission:
import bisect
N = int(input())
A = sorted(list(map(int,input().split())))
B = sorted(list(map(int,input().split())))
C = sorted(list(map(int,input().split())))
print(sum(bisect.bisect_left(A, i)*(N-bisect.bisect_right(C, i)) for i in B))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
for h in range(1,3501):
    for n in range(1,3501):
        t1 = 4 * h * n
        t2 = N * (h+n)
        if t1 != t2:
            temp = (N*h*n) / (t1-t2)
            if temp.is_integer():
                if temp > 0:
                    print(h,n,int(temp))
                    break
    else:
        continue
    break

Fixed submission:
N = int(input())
for h in range(1,3501):
    for n in range(1,3501):
        t1 = 4 * h * n
        t2 = N * (h+n)
        if t1 != t2:
            temp = (N*h*n) / (t1-t2)
            if temp.is_integer():
                if temp > 0:
                    print(h,n,int(temp))
                    break
    else:
        continue
    break
Buggy python submission (verdict: Runtime Error):
from collections import Counter
N = int(input())
a = list(map(int, input().split()))
c = Counter(a)
d = [c[i]+c[i+1]+c[i+2] for i in range(max(a)-1)]
print(max(d))

Fixed submission:
from collections import Counter
N = int(input())
a = list(map(int, input().split()))
c = Counter(a)
d = [c[i]+c[i+1]+c[i+2] for i in range(99998)]
print(max(d))
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

import scipy.sparse
N = int(input())
lil = scipy.sparse.lil_matrix((N, N), dtype = int)
for i in range(N-1):
    a, b, c = (int(x) for x in input().split())
    lil[a-1, b-1] = c
lil2 = scipy.sparse.csgraph.dijkstra(lil,directed=False).astype(int)
Q, K = (int(x) for x in input().split())
for i in range(Q):
    x, y = (int(x) for x in input().split())
    print(lil2[K-1][x-1]+lil2[K-1][y-1])

Fixed submission:
import sys
input = sys.stdin.readline
from collections import deque

N = int(input())
connected = [[]for _ in range(N+1)]
for i in range(N-1):
    a, b, c = (int(x) for x in input().split())
    connected[a].append((b,c))
    connected[b].append((a,c))
Q, K = (int(x) for x in input().split())
distk = [-1] * (N+1)
d = deque([K])
distk[K] = 0
while d:
    temp = d.popleft()
    for vert, dist in connected[temp]:
        if distk[vert] == -1:
            d.append(vert)
            distk[vert] = distk[temp] + dist
for i in range(Q):
    x, y = (int(x) for x in input().split())
    print(distk[x]+distk[y])

Buggy python submission (verdict: Runtime Error):
import scipy.sparse
N = int(input())
lil = scipy.sparse.lil_matrix((N, N), dtype = int)
for i in range(N-1):
    a, b, c = (int(x) for x in input().split())
    lil[a-1, b-1] = c
lil2 = scipy.sparse.csgraph.dijkstra(lil, directed = False).astype(int)
Q, K = (int(x) for x in input().split())
for i in range(Q):
    x, y = (int(x) for x in input().split())
    print(lil2[K-1][x-1]+lil2[K-1][y-1])

Fixed submission:
import sys
input = sys.stdin.readline
from collections import deque

N = int(input())
connected = [[]for _ in range(N+1)]
for i in range(N-1):
    a, b, c = (int(x) for x in input().split())
    connected[a].append((b,c))
    connected[b].append((a,c))
Q, K = (int(x) for x in input().split())
distk = [-1] * (N+1)
d = deque([K])
distk[K] = 0
while d:
    temp = d.popleft()
    for vert, dist in connected[temp]:
        if distk[vert] == -1:
            d.append(vert)
            distk[vert] = distk[temp] + dist
for i in range(Q):
    x, y = (int(x) for x in input().split())
    print(distk[x]+distk[y])
Buggy python submission (verdict: Runtime Error):
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a

N,K = (int(x) for x in input().split())
A = list(map(int, input().split()))
m = max(A)
if K > m:
    print('IMPOSSIBLE')
else:
    gcd = xgcd(A[0],A[1])
    for i in range(2,N):
        gcd = xgcd(gcd,A[i])
        if gcd == 1:
            print('POSSIBLE')
            break
    else:
        if K % gcd == 0:
            print('POSSIBLE')
        else:
            print('IMPOSSIBLE')

Fixed submission:
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a

N,K = (int(x) for x in input().split())
A = list(map(int, input().split()))
m = max(A)
if N == 1:
    if A[0] == K:
        print('POSSIBLE')
    else:
        print('IMPOSSIBLE')
else:
    if K > m:
        print('IMPOSSIBLE')
    else:
        gcd = xgcd(A[0],A[1])
        for i in range(2,N):
            gcd = xgcd(gcd,A[i])
            if gcd == 1:
                print('POSSIBLE')
                break
        else:
            if K % gcd == 0:
                print('POSSIBLE')
            else:
                print('IMPOSSIBLE')
Buggy python submission (verdict: Time Limit Exceeded):
def memfact(a,m):
    temp = 1
    yield temp
    for i in range(1,a+1):
        temp = temp * i % m
        yield temp
def comb(n,r,m):
    if r == 0: return 1
    return (memf[n]*pow(memf[r],m-2,m)*pow(memf[n-r],m-2,m)) % m


n,*a = map(int, open(0).read().split())
m = 1000000007
checker = set()
for i in range(n+1):
    if a[i] in checker:
        left = a.index(a[i])
        right = n - i
        break
    else:
        checker.add(a[i])
memf = []
mfappend = memf.append
for x in memfact(n+1,m):
    mfappend(x)
for i in range(1,n+2):
    ans = comb(n+1,i,m)
    for j in range(max(0,i-right-1),min(left,i-1)+1):
        ans = (ans-comb(left,j,m)*comb(right,i-j-1,m)) % m
    print(ans)

Fixed submission:
def memfact(a,m):
    temp = 1
    yield temp
    for i in range(1,a+1):
        temp = temp * i % m
        yield temp
def comb(n,r,m):
    if r == 0: return 1
    return (memf[n]*pow(memf[r],m-2,m)*pow(memf[n-r],m-2,m)) % m


n,*a = map(int, open(0).read().split())
m = 1000000007
checker = set()
for i in range(n+1):
    if a[i] in checker:
        left = a.index(a[i])
        right = n - i
        break
    else:
        checker.add(a[i])
memf = []
mfappend = memf.append
for x in memfact(n+1,m):
    mfappend(x)
lr = left + right
for i in range(1,n+2):
    ans = comb(n+1,i,m)
    if i <= lr + 1:
        ans = (ans-comb(lr,i-1,m)) % m
    print(ans)
Buggy python submission (verdict: Runtime Error):
N,M,*f = open(0).read().split()
N = int(N)
M = int(M)
A = f[:N]
B = f[N:]
for i in range(N):
    for j in range(N-M+1):
        if A[i][j:j+M] == B[0]:
            for k in range(1,M):
                if A[i+k][j:j+M] != B[k]:
                    break
            else:
                print('Yes')
                break
    else:
        continue
    break
else:
    print('No')

Fixed submission:
N,M,*f = open(0).read().split()
N = int(N)
M = int(M)
A = f[:N]
B = f[N:]
for i in range(N-M+1):
    for j in range(N-M+1):
        if A[i][j:j+M] == B[0]:
            for k in range(1,M):
                if A[i+k][j:j+M] != B[k]:
                    break
            else:
                print('Yes')
                break
    else:
        continue
    break
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
import itertools
n = int(input())
a = list(map(int,input().split()))
ans = 0
m = 10**9 + 7
for item in itertools.combinations(a, 2):
    ans += item[0] * item[1]
print(ans % m)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
c = 10 ** 9 + 7
ans = 0
s = 0
for i in range(n-1, -1, -1):
    ans += s * a[i]
    s += a[i]
print(ans % c)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
ans = 0
m = 10**9 + 7
for i in range(len(a)):
    ans += a[i] * sum(a[i+1:])
print(ans % m)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
c = 10 ** 9 + 7
ans = 0
s = 0
for i in range(n-1, -1, -1):
    ans += s * a[i]
    s += a[i]
print(ans % c)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
ans = 0
m = 10**9 + 7
for i in range(len(a)):
    ans += a[i] * sum(a[i+1:])
print(ans % m)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
c = 10 ** 9 + 7
ans = 0
s = 0
for i in range(n-1, -1, -1):
    ans += s * a[i]
    s += a[i]
print(ans % c)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
ans = 0
m = 10**9 + 7
for i in range(len(a)):
    for j in range(i+1, len(a)): 
        ans += a[i] * a[j]
print(ans % m)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
c = 10 ** 9 + 7
ans = 0
s = 0
for i in range(n-1, -1, -1):
    ans += s * a[i]
    s += a[i]
print(ans % c)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
q = int(input())
b = [0] * q
c = [0] * q
for i in range(q):
    b[i],c[i] = map(int,input().split())

for i in range(q):
    ans = 0
    for j in range(n):
        if a[j] == b[i]:
            a[j] = c[i]
        ans += a[j]
    print(ans)

Fixed submission:
import collections
n = int(input())
a = list(map(int,input().split()))
ac = collections.Counter(a)
q = int(input())
b = [0] * q
c = [0] * q
for i in range(q):
    b[i],c[i] = map(int,input().split())
sum = sum(a)
for j in range(q):
    div = 0
    if ac[b[j]] != 0:
        div = c[j] * ac[b[j]] - b[j] * ac[b[j]]
        ac[c[j]] += ac[b[j]]
        ac[b[j]] = 0
    sum += div
    print(sum)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
m = 1
for i in range(len(a)):
    m = m * a[i]
if m > 10 ** 18:
    print(-1)
    exit()
else:
    print(m)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
m = 1
if 0 in a:
    print(0)
    exit()
for i in range(len(a)):
    m = m * a[i]
    if m > 10 ** 18:
        print(-1)
        exit()
print(m)