Buggy python submission (verdict: Time Limit Exceeded):
X,Y,Z,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))

E=[]
for i in range(X):
  for j in range(Y):
    E.append(A[i]+B[j])

C.sort(reverse=True)
E.sort(reverse=True)
ans=[]
for i in range(min(K,len(E))):
  for j in range(Z):
    ans.append(E[i]+C[j])
ans.sort(reverse=True)

for i in range(K):
  print(ans[i])

Fixed submission:
import heapq

def main():
  X,Y,Z,K=map(int,input().split())
  A=list(reversed(sorted(map(int,input().split()))))
  B=list(reversed(sorted(map(int,input().split()))))
  C=list(reversed(sorted(map(int,input().split()))))
  q=[(-A[0]-B[0]-C[0],0,0,0)]
  queued={(0,0,0)}
  for i in range(K):
    v,s,t,u=heapq.heappop(q)
    print(-v)
    for e,f,g in [(s,t,u+1),(s,t+1,u),(s+1,t,u)]:
      if e>=X or f>=Y or g>=Z:
        continue
      if (e,f,g) in queued:
        continue
      heapq.heappush(q,(-(A[e]+B[f]+C[g]),e,f,g))
      queued.add((e,f,g))
      
if __name__=="__main__":
  main()

Buggy python submission (verdict: Runtime Error):
X,Y,Z,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
N=X*Y
K1=[0]*(N)
n=0
for i in range(X):
  for j in range(Y):
    K1[n]=A[i]+B[j]
    n+=1
K1.sort(reverse=True)
n=0
K2=[0]*(N*Z)
for i in range(N):
  for j in range(Z):
    K2[n]=K1[i]+C[j]
    n+=1
K2.sort(reverse=True)
for i in range(K):
  print(K2[i])

Fixed submission:
import heapq

def main():
  X,Y,Z,K=map(int,input().split())
  A=list(reversed(sorted(map(int,input().split()))))
  B=list(reversed(sorted(map(int,input().split()))))
  C=list(reversed(sorted(map(int,input().split()))))
  q=[(-A[0]-B[0]-C[0],0,0,0)]
  queued={(0,0,0)}
  for i in range(K):
    v,s,t,u=heapq.heappop(q)
    print(-v)
    for e,f,g in [(s,t,u+1),(s,t+1,u),(s+1,t,u)]:
      if e>=X or f>=Y or g>=Z:
        continue
      if (e,f,g) in queued:
        continue
      heapq.heappush(q,(-(A[e]+B[f]+C[g]),e,f,g))
      queued.add((e,f,g))
      
if __name__=="__main__":
  main()

Buggy python submission (verdict: Runtime Error):
x,y,z,k=map(int,input().split())
a=sorted(list(map(int,input().split())),reverse=True)
b=sorted(list(map(int,input().split())),reverse=True)
c=sorted(list(map(int,input().split())),reverse=True)
d=[0]*(x*y)
e=[0]*(x*y*z)
m,n=0,0

for i in range(x):
  for j in range(y):
    d[m]=a[i]+b[j]
    m += 1

d=sorted(d,reverse=True)
for i in range(x*y):
  for j in range(z):
    e[n]=d[i]+c[j]
    n += 1
    
e=sorted(e,reverse=True)
for i in range(k):
  print(e[i])

Fixed submission:
import heapq

def main():
  X,Y,Z,K=map(int,input().split())
  A=list(reversed(sorted(map(int,input().split()))))
  B=list(reversed(sorted(map(int,input().split()))))
  C=list(reversed(sorted(map(int,input().split()))))
  q=[(-A[0]-B[0]-C[0],0,0,0)]
  queued={(0,0,0)}
  for i in range(K):
    v,s,t,u=heapq.heappop(q)
    print(-v)
    for e,f,g in [(s,t,u+1),(s,t+1,u),(s+1,t,u)]:
      if e>=X or f>=Y or g>=Z:
        continue
      if (e,f,g) in queued:
        continue
      heapq.heappush(q,(-(A[e]+B[f]+C[g]),e,f,g))
      queued.add((e,f,g))
      
if __name__=="__main__":
  main()