Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

def main():
    N = int(input())
    P = np.array([int(x) for x in input().split()])
    ans = 0

    for i in range(N):
        if P[i] <= min(P[0:i+1]):
            ans += 1
    
    print(ans)
        
if __name__ == '__main__':
    main()

Fixed submission:
def main():
    N = int(input())
    P = [int(x) for x in input().split()]
    ans = 0
    minP = P[0]

    for i in range(N):
        minP = min(minP, P[i])
        if P[i] <= minP:
            ans += 1
    
    print(ans)
        
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    P = [int(x) for x in input().split()]
    ans = 0

    for i in range(N):
        if P[i] <= min(P[j] for j in range(i+1)):
            ans += 1

    print(ans)
        
if __name__ == '__main__':
    main()

Fixed submission:
def main():
    N = int(input())
    P = [int(x) for x in input().split()]
    ans = 0
    minP = P[0]

    for i in range(N):
        minP = min(minP, P[i])
        if P[i] <= minP:
            ans += 1
    
    print(ans)
        
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    P = [int(x) for x in input().split()]
    ans = 0

    for i in range(N):
        flg = True
        for j in range(i+1):
            if P[i] > P[j]:
                flg = False
        if flg == True:
            ans += 1
    
    print(ans)
        
if __name__ == '__main__':
    main()

Fixed submission:
def main():
    N = int(input())
    P = [int(x) for x in input().split()]
    ans = 0
    minP = P[0]

    for i in range(N):
        minP = min(minP, P[i])
        if P[i] <= minP:
            ans += 1
    
    print(ans)
        
if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
N = np.array([str(i) for i in range(1,int(input())+1)])
C = np.zeros((10,10), dtype=np.int)
ans = 0

# C[i][j](0≦i,j≦9)のテーブルを構築する: O(N)
for n in N:
    C[int(n[0])][int(n[-1])] += 1

for i in range(1,9+1):
    for j in range(1,9+1):
        ans += C[i][j] * C[j][i]

print(ans)

Fixed submission:
import numpy as np

def main():
    N = np.array([str(i) for i in range(1,int(input())+1)])
    C = np.zeros((10,10), dtype=np.int)
    ans = 0

    # C[i][j](0 <= i,j <= 9)のテーブルを構築する: O(N)
    for n in N:
        C[int(n[0])][int(n[-1])] += 1

    for i in range(1,9+1):
        for j in range(1,9+1):
            ans += C[i][j] * C[j][i]

    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    M = [str(i) for i in range(1,N+1)]
    ans = 0

    for i in M:
        ans += len(list([s for s in M if s.startswith(i[-1]) and s.endswith(i[0])]))

    print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
import numpy as np

def main():
    N = np.array([str(i) for i in range(1,int(input())+1)])
    C = np.zeros((10,10), dtype=np.int)
    ans = 0

    # C[i][j](0 <= i,j <= 9)のテーブルを構築する: O(N)
    for n in N:
        C[int(n[0])][int(n[-1])] += 1

    for i in range(1,9+1):
        for j in range(1,9+1):
            ans += C[i][j] * C[j][i]

    print(ans)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
# E - Double Factorial

N = int(input())

ans = 0
p = 10

if (N%2):
    continue
else:
    while True:
        if p > N:
            break
        ans += N // p
        p *= 5

print(ans)


Fixed submission:
# E - Double Factorial

N = int(input())

ans = 0
p = 10

if (N%2==0):
    while True:
        if p > N:
            break
        ans += N // p
        p *= 5

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(int(x) for x in input().split())
mod = 10**9+7

ans = 0
for i in range(1,N):
    for j in range(i+1,N+1):
        ans += (A[i-1]^A[j-1])%mod

print(ans%mod)

Fixed submission:
import numpy as np

N = int(input())
A = np.array([int(x) for x in input().split()])
mod = 10**9+7
ans = 0

for i in range(60):
    c = np.count_nonzero((A>>i)&1)
    ans += (2 ** i) * c * (N - c)

print(ans%mod)
Buggy python submission (verdict: Time Limit Exceeded):
def main():    
    n = int(input())
    l = list(int(x) for x in input().split())
    ans = 0

    for i in range(n-2):
        for j in range(i+1, n):
            for k in range(j+1, n):
                if (l[i] < l[j]+l[k]) and (l[j] < l[k]+l[i]) and (l[k] < l[i]+l[j]):
                    ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import bisect

def main():    
    n = int(input())
    L = list(int(x) for x in input().split())
    ans = 0
    c = 0

    L.sort()

    for i in range(n-2):
        for j in range(i+1, n):
            c = L[i]+L[j]
            l = j
            r = bisect.bisect_left(L, c, lo=l) -1
            ans += (r-l)
    print(ans)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import math
a, b = map(int, input().split())
ans = []

for i in range(1,min(a,b)+1):
    if a%i == 0 and b%i ==0:
        while True:
            for j in range(2,int(math.sqrt(i))+1):
                if i%j==0:
                    break
            else:
                ans.append(i)
            break
            
print(len(ans))

Fixed submission:
import collections

def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x%y)

def trial_division(n):
    a = []  
    while n % 2 == 0:
        a.append(2)
        n /= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n /= f
        else:
            f += 2   
    if n != 1: a.append(n)
    return a

def main():
    a, b = map(int, input().split())
    c = collections.Counter(trial_division(gcd(a, b)))
    print(len(c.keys())+1)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
a, b = map(int, input().split())
ans = []

for i in range(1,min(a,b)+1):
    if a%i == 0 and b%i ==0:
        while True:
            for j in range(2,int(math.sqrt(i))+1):
                if i%j==0:
                    break
            else:
                ans.append(i)
            break
            
print(len(ans))
        

Fixed submission:
import collections

def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x%y)

def trial_division(n):
    a = []  
    while n % 2 == 0:
        a.append(2)
        n /= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n /= f
        else:
            f += 2   
    if n != 1: a.append(n)
    return a

def main():
    a, b = map(int, input().split())
    c = collections.Counter(trial_division(gcd(a, b)))
    print(len(c.keys())+1)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
a, b = map(int, input().split())
ans = []

for i in range(1,min(a,b)+1):
    if a%i == 0 and b%i ==0:
        while True:
            for j in range(2,i):
                if i%j==0:
                    break
            else:
                ans.append(i)
            break
            
print(len(ans))
        

Fixed submission:
import collections

def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x%y)

def trial_division(n):
    a = []  
    while n % 2 == 0:
        a.append(2)
        n /= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n /= f
        else:
            f += 2   
    if n != 1: a.append(n)
    return a

def main():
    a, b = map(int, input().split())
    c = collections.Counter(trial_division(gcd(a, b)))
    print(len(c.keys())+1)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
h, w, a, b = map(int, input().split())
m = np.zeros((h,w))
m[0:b,a:] = 1
m[b:,0:a] = 1
for j in range(h):
    print(''.join(m.astype(int).astype(str)[j].tolist()))

Fixed submission:
import numpy as np
h, w, a, b = map(int, input().split())
m = np.zeros((h,w),np.int8)
m[:b,a:] = 1
m[b:,:a] = 1
print('\n'.join(''.join(row) for row in m.astype(str)))
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
a = list([int(x) for x in input().split()])
 
for i in range(m):
    a[a.index(max(a))] = max(a)//2
print(sum(a))

Fixed submission:
s = input()

if s == 'Sunny':
    print('Cloudy')
elif s == 'Cloudy':
    print('Rainy')
else:
    print('Sunny')
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list([int(x) for x in input().split()])
 
for i in range(m):
    a[a.index(max(a))] = max(a)//2
print(sum(a))

Fixed submission:
import heapq
n, m = map(int, input().split())
a = [-int(x) for x in input().split()]
heapq.heapify(a)

for i in range(m):
    l = -heapq.heappop(a)
    heapq.heappush(a, -(l//2))
    
print(-sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n, m = map(int, input().split())
a = np.array(list([int(x) for x in input().split()]))

for i in range(m):
    a[np.argmax(a)] = max(a)//2
print(sum(a))

Fixed submission:
import heapq
n, m = map(int, input().split())
a = [-int(x) for x in input().split()]
heapq.heapify(a)

for i in range(m):
    l = -heapq.heappop(a)
    heapq.heappush(a, -(l//2))
    
print(-sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = sorted(list([int(x) for x in input().split()]), reverse=True)

for i in range(m):
    a[0] = a[0] // 2
    if n > 1:
        if a[0] < a[1]:
            a = sorted(a, reverse=True)
print(sum(a))

Fixed submission:
import heapq
n, m = map(int, input().split())
a = [-int(x) for x in input().split()]
heapq.heapify(a)

for i in range(m):
    l = -heapq.heappop(a)
    heapq.heappush(a, -(l//2))
    
print(-sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list([int(x) for x in input().split()])

for i in range(m):
    a[a.index(max(a))] = max(a)//2
print(sum(a))


Fixed submission:
import heapq
n, m = map(int, input().split())
a = [-int(x) for x in input().split()]
heapq.heapify(a)

for i in range(m):
    l = -heapq.heappop(a)
    heapq.heappush(a, -(l//2))
    
print(-sum(a))
Buggy python submission (verdict: Time Limit Exceeded):
def change_lr(s, l, r):
    s2 = list(s)
    for i in range(r-l+1):
        if s[l+i] == 'L':
            s2[r-i] = 'R'
        else:
            s2[r-i] = 'L'
    return s2
  
n, k = map(int, input().split())
s = list(input())
ans = 0

for j in range(k):
    c1 = ''
    c2 = ''
    start = 0
    end = 0
    for i in range(1,n):
        if s[i] != s[i-1]:
            c1 = s[i] 
            c2 = s[i-1] 
            start = i
            for k in range(i+2,n):
                if s[k-1] == c1 and s[k] == c2:
                    end = k-1
                    break
            else:
                continue
            break
    if end == 0:
        end = start
        end = n-1

    s = change_lr(s, start, end)

for i in range(1, len(s)):
    if s[i-1] == s[i]:
        ans += 1
        
print(ans)

Fixed submission:
n, k = map(int, input().split())
s = list(input())
h = 0

for i in range(1, len(s)):
    if s[i-1] == s[i]:
        h += 1
        
print(min(n-1, h+k*2))
Buggy python submission (verdict: Runtime Error):
N = list(range(1,int(input())))
print(sum(N))

Fixed submission:
n = int(input())
print(int((n-1)*n//2))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
m = 0

for n in range(1,N):
    if n == N:
        m += 0
    else:
        m += n % (n+1)
print(m)

Fixed submission:
n = int(input())
print(int((n-1)*n//2))
Buggy python submission (verdict: Time Limit Exceeded):
# C - Remainder Minimization 2019
import numpy as np

l, r = map(int, input().split())

ans = 2019
for i in range(l,r):
    for j in range(i+1, r+1):
        ans = min(ans, (i*j)%2019)
        if ans==0:
            break

print(ans)

Fixed submission:
# C - Remainder Minimization 2019
import numpy as np

l, r = map(int, input().split())

ans = 2019
for i in range(l,r):
    for j in range(i+1, r+1):
        ans = min(ans, (i*j)%2019)
        if ans==0:
            break
    else:
        continue
    break

print(ans)

Buggy python submission (verdict: Runtime Error):
# C - Remainder Minimization 2019
import numpy as np

l, r = map(int, input().split())

a = np.arange(l, r+1)
a = a%2019
#print(a)
b = np.copy(a)
b = 2019 - b
#print(b)
c = np.minimum(a, b)
c = np.sort(c)
#print(c)

print((c[0]*c[1])%2019)

Fixed submission:
# C - Remainder Minimization 2019
import numpy as np

l, r = map(int, input().split())

ans = 2019
for i in range(l,r):
    for j in range(i+1, r+1):
        ans = min(ans, (i*j)%2019)
        if ans==0:
            break
    else:
        continue
    break

print(ans)

Buggy python submission (verdict: Runtime Error):
# C - Remainder Minimization 2019
import numpy as np

l, r = map(int, input().split())

a = np.arange(l, r+1)
a = a%2019
a = np.sort(a)

print((a[0]*a[1])%2019)

Fixed submission:
# C - Remainder Minimization 2019
import numpy as np

l, r = map(int, input().split())

ans = 2019
for i in range(l,r):
    for j in range(i+1, r+1):
        ans = min(ans, (i*j)%2019)
        if ans==0:
            break
    else:
        continue
    break

print(ans)

Buggy python submission (verdict: Runtime Error):
# C - Remainder Minimization 2019
import numpy as np

l, r = map(int, input().split())

a = np.arange(l, r+1)
a = a%2019
a = np.sort(a)

print(a[0]*a[1])

Fixed submission:
# C - Remainder Minimization 2019
import numpy as np

l, r = map(int, input().split())

ans = 2019
for i in range(l,r):
    for j in range(i+1, r+1):
        ans = min(ans, (i*j)%2019)
        if ans==0:
            break
    else:
        continue
    break

print(ans)

Buggy python submission (verdict: Runtime Error):
# C - Remainder Minimization 2019
import numpy as np

l, r = map(int, input().split())

a = np.arange(l, r+1)
a = a%2019
np.sort(a)

print(a[0]*a[1])

Fixed submission:
# C - Remainder Minimization 2019
import numpy as np

l, r = map(int, input().split())

ans = 2019
for i in range(l,r):
    for j in range(i+1, r+1):
        ans = min(ans, (i*j)%2019)
        if ans==0:
            break
    else:
        continue
    break

print(ans)
Buggy python submission (verdict: Runtime Error):
# C - Successive Subtraction

N = int(input())
A = list(int(a) for a in input().split())

M = []
P = []
for a in A:
    if a >= 0:
        P.append(a)
    else:
        M.append(a)

P.sort(reverse=True)
M.sort()
lenP = len(P)
lenM = len(M)    

ans = []
anssum = 0

if lenM ==0:
    ans.append((P[-1], P[-2]))
    tmp = P.pop() - P.pop()
    M.append(tmp)
    lenP -= 2
elif lenP ==0:
    ans.append((M[-1], M[-2]))
    tmp = M.pop() - M.pop()
    P.append(tmp)
    lenP += 1
for i in range(lenP-1):
    ans.append((M[-1], P[-1]))
    tmp = M.pop() - P.pop()
    M.append(tmp)
while M != []:
    ans.append((P[-1], M[-1]))
    tmp = P.pop() - M.pop()
    P.append(tmp)
anssum = P.pop()

print(anssum)
for i in range(len(ans)):
    print(' '.join(map(str, ans[i])))


Fixed submission:
# C - Successive Subtraction

N = int(input())
A = list(int(a) for a in input().split())

M = []
P = []
for a in A:
    if a >= 0:
        P.append(a)
    else:
        M.append(a)

P.sort(reverse=True)
M.sort()
lenP = len(P)
lenM = len(M)    

ans = []
anssum = 0

if lenM ==0:
    if lenP >= 3:
        ans.append((P[-1], P[-2]))
        tmp = P.pop() - P.pop()
        M.append(tmp)
        lenP -= 2
    else:
        ans.append((P[-2], P[-1]))
        tmp = P.pop() - P.pop()
        tmp *= -1
        P.append(tmp)
        lenP -= 2
elif lenP ==0:
    ans.append((M[-1], M[-2]))
    tmp = M.pop() - M.pop()
    P.append(tmp)
    lenP += 1
for i in range(lenP-1):
    ans.append((M[-1], P[-1]))
    tmp = M.pop() - P.pop()
    M.append(tmp)
while M != []:
    ans.append((P[-1], M[-1]))
    tmp = P.pop() - M.pop()
    P.append(tmp)
anssum = P.pop()

print(anssum)
for i in range(len(ans)):
    print(' '.join(map(str, ans[i])))
Buggy python submission (verdict: Time Limit Exceeded):
# D - Lamp
import numpy as np

h, w = map(int, input().split())
s = []
for i in range(h):
    s.append(list(input()))
s = np.array(s)

# 横について考える
tblW = np.zeros((h, w),dtype=np.int64)
for i in range(h):
    for j in range(w):
        if s[i, j]=='#' or tblW[i, j]>0:
            continue
        else:
            cnt = 1
            while j+cnt<w and s[i, j+cnt]!='#':
                cnt += 1
            tblW[i, j:j+cnt] = cnt
#print(tblW)
    
# 縦について考える
tblH = np.zeros((h, w),dtype=np.int64)
for j in range(w):
    for i in range(h):
        if s[i, j]=='#' or tblH[i, j]>0:
            continue
        else:
            cnt = 1
            while i+cnt<h and s[i+cnt, j]!='#':
                cnt += 1
            tblH[i:i+cnt, j] = cnt
#print(tblH)

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, tblH[i, j]+tblW[i, j]-1)

print(ans)


Fixed submission:
# D - Lamp
import numpy as np

h, w = map(int, input().split())

# 0: 障害物または壁 1: 通路
# 上下左右の壁の分を加味して+2したグリッドを用意する。
g = np.zeros((h+2,w+2),dtype=np.int64)
for i in range(h):
    grid = np.array(list(input()))
    g[i+1, 1:w+1] = (grid=='.')

# 横について考える
gW = np.copy(g)
gW = np.ravel(gW)
tWLeft = np.arange((w+2)*(h+2))
tWRight = np.copy(tWLeft)

tWLeft[gW==1] = 0
tWRight[gW[::-1]==1] = 0

np.maximum.accumulate(tWLeft, out=tWLeft)
np.maximum.accumulate(tWRight, out=tWRight)

tWRight = len(tWRight) - 1 - tWRight[::-1]

tW = tWRight-tWLeft-1
tW[gW==0] = 0
tW = tW.reshape(h+2, w+2)

# 縦について考える
gH = np.copy(g.T)
gH = np.ravel(gH)
tHLeft = np.arange((w+2)*(h+2))
tHRight = np.copy(tHLeft)

tHLeft[gH==1] = 0
tHRight[gH[::-1]==1] = 0

np.maximum.accumulate(tHLeft, out=tHLeft)
np.maximum.accumulate(tHRight, out=tHRight)

tHRight = len(tHRight) - 1 - tHRight[::-1]

tH = tHRight-tHLeft-1
tH[gH==0] = 0
tH = tH.reshape(w+2, h+2).T

#print(tW)
#print(tH)
print(np.max(tW+tH-1))


'''
# 横について考える
tblW = np.zeros((h, w),dtype=np.int64)
for i in range(h):
    for j in range(w):
        if grid[i, j]=='#' or tblW[i, j]>0:
            continue
        else:
            cnt = 1
            while j+cnt<w and grid[i, j+cnt]!='#':
                cnt += 1
            tblW[i, j:j+cnt] = cnt
print(tblW)
    
# 縦について考える
tblH = np.zeros((h, w),dtype=np.int64)
for j in range(w):
    for i in range(h):
        if grid[i, j]=='#' or tblH[i, j]>0:
            continue
        else:
            cnt = 1
            while i+cnt<h and grid[i+cnt, j]!='#':
                cnt += 1
            tblH[i:i+cnt, j] = cnt
print(tblH)

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, tblH[i, j]+tblW[i, j]-1)

print(ans)
'''
Buggy python submission (verdict: Time Limit Exceeded):
# C - Prison
import numpy as np

n, m = map(int, input().split())
a = np.ones(n, dtype=np.int64)

for i in range(m):
    l, r = map(int, input().split())
    l -= 1
    r -= 1
    b = np.zeros(n, dtype=np.int64)
    b[l:r+1] = 1
    a = a&b

print(sum(a))

Fixed submission:
# C - Prison

n, m = map(int, input().split())

ansl = 1
ansr = n

for i in range(m):
    l, r = map(int, input().split())
    if l>ansl:
        ansl = l
    if r<ansr:
        ansr = r

print(max(ansr-ansl+1,0))
Buggy python submission (verdict: Time Limit Exceeded):
# D - Integer Cards

import numpy as np

n, m = map(int, input().split())
A = np.array(list(int(x) for x in input().split()))

def binary_search(searchList, target):
    l = -1
    r = len(searchList)
    while l+1<r:
        m = (l+r)//2
        if target>=searchList[m]:
            l = m
        else:
            r = m
    return l

for j in range(m):
    A = np.sort(A)
    b, c = map(int, input().split())
    cnt = binary_search(A, c)
    A[0:min(b, cnt+1)] = c
    #print(A)

print(sum(A))



Fixed submission:
# D - Integer Cards

import heapq
from collections import Counter

n, m = map(int, input().split())
A = list(int(x)*-1 for x in input().split())

C = Counter(A)
L = list(C.items())

for j in range(m):
    b, c = map(int, input().split())
    L.append((-c, b))

heapq.heapify(L)

ans = 0
for i in range(n):
    p, q = heapq.heappop(L)
    ans += p
    if q>1:
        heapq.heappush(L, (p, q-1))

print(ans*-1)

Buggy python submission (verdict: Time Limit Exceeded):
# D - Integer Cards
import heapq

n, m = map(int, input().split())
A = list(int(x) for x in input().split())

heapq.heapify(A)

for j in range(m):
    b, c = map(int, input().split())
    for i in range(b):
        a = heapq.heappop(A)
        if a<c:
            heapq.heappush(A, c)
        else:
            heapq.heappush(A, a)
            break

print(sum(A))

Fixed submission:
# D - Integer Cards

import heapq
from collections import Counter

n, m = map(int, input().split())
A = list(int(x)*-1 for x in input().split())

C = Counter(A)
L = list(C.items())

for j in range(m):
    b, c = map(int, input().split())
    L.append((-c, b))

heapq.heapify(L)

ans = 0
for i in range(n):
    p, q = heapq.heappop(L)
    ans += p
    if q>1:
        heapq.heappush(L, (p, q-1))

print(ans*-1)
Buggy python submission (verdict: Time Limit Exceeded):
# D - Cake 123

import heapq
import itertools

x, y, z, k = map(int,input().split())

a = list(int(x) for x in input().split())
b = list(int(x) for x in input().split())
c = list(int(x) for x in input().split())

anslist = []
ip = list(itertools.product(a, b, c))   
for i in range(len(ip)):
    heapq.heappush(anslist, sum(ip[i])*-1)

for i in range(k):
    print(heapq.heappop(anslist)*-1)



Fixed submission:
# D - Cake 123

import heapq
import itertools

x, y, z, k = map(int, input().split())

A = list(int(a) for a in input().split())
B = list(int(b) for b in input().split())
C = list(int(c) for c in input().split())

hq = []
for i, j in itertools.product(A, B):
    heapq.heappush(hq, (i+j)*-1)

AB = []
while len(hq)>0 and len(AB)<=k:
    AB.append(heapq.heappop(hq)*-1)

hq = []
for i, j in itertools.product(AB, C):
    heapq.heappush(hq, (i+j)*-1)

for _ in range(k):
    print(heapq.heappop(hq)*-1)

Buggy python submission (verdict: Runtime Error):
# D - Cake 123

import heapq
import itertools

x, y, z, k = map(int,input().split())

if x*y*z<3000:
    a = list(int(x) for x in input().split())
    b = list(int(x) for x in input().split())
    c = list(int(x) for x in input().split())

    anslist = []
    ip = list(itertools.product(a, b, c))   
    for i in range(len(ip)):
        heapq.heappush(anslist, sum(ip[i])*-1)

    for i in range(k):
        print(heapq.heappop(anslist)*-1)

else:
    a = list(int(x)*-1 for x in input().split())
    b = list(int(x)*-1 for x in input().split())
    c = list(int(x)*-1 for x in input().split())
    inf = 10**18

    heapq.heapify(a)
    heapq.heapify(b)
    heapq.heapify(c)

    la = [inf]
    lb = [inf]
    lc = [inf]

    while len(la)*len(lb)*len(lc)<3000:
        if a[0]<=b[0]<=c[0]:
            tmp = heapq.heappop(a)
            heapq.heappush(a, inf)
            la.append(tmp*-1)
        elif a[0]>b[0]<=c[0]:
            tmp = heapq.heappop(b)
            heapq.heappush(b, inf)
            lb.append(tmp*-1)
        else:
            tmp = heapq.heappop(b)
            heapq.heappush(c, inf)
            lc.append(tmp*-1)

    anslist = []
    ip = list(itertools.product(la, lb, lc))   
    for i in range(len(ip)):
        heapq.heappush(anslist, sum(ip[i])*-1)

    for i in range(k):
        print(heapq.heappop(anslist)*-1)

Fixed submission:
# D - Cake 123

import heapq
import itertools

x, y, z, k = map(int, input().split())

A = list(int(a) for a in input().split())
B = list(int(b) for b in input().split())
C = list(int(c) for c in input().split())

hq = []
for i, j in itertools.product(A, B):
    heapq.heappush(hq, (i+j)*-1)

AB = []
while len(hq)>0 and len(AB)<=k:
    AB.append(heapq.heappop(hq)*-1)

hq = []
for i, j in itertools.product(AB, C):
    heapq.heappush(hq, (i+j)*-1)

for _ in range(k):
    print(heapq.heappop(hq)*-1)