Problem: The Patisserie AtCoder sells cakes with number-shaped candles. There are X, Y and Z kinds of cakes with 1-shaped, 2-shaped and 3-shaped candles, respectively. Each cake has an integer value called _deliciousness_ , as follows:    * The deliciousness of the cakes with 1-shaped candles are A_1, A_2, ..., A_X.   * The deliciousness of the cakes with 2-shaped candles are B_1, B_2, ..., B_Y.   * The deliciousness of the cakes with 3-shaped candles are C_1, C_2, ..., C_Z.  Takahashi decides to buy three cakes, one for each of the three shapes of the candles, to celebrate ABC 123.   There are X \times Y \times Z such ways to choose three cakes.   We will arrange these X \times Y \times Z ways in descending order of the sum of the deliciousness of the cakes.   Print the sums of the deliciousness of the cakes for the first, second, ..., K-th ways in this list.

Submission attempts (in order):
N=list(map(int,(input().split(" "))))
teast=[]
K = N[3]
total=[]
for i in range(3):
    teast.append(list(map(int,input().split(" "))))
A=sorted(teast[0],reverse=True)
B=sorted(teast[1],reverse=True)
C=sorted(teast[2],reverse=True)
for i in range(len(A)):

    for j in range(len(B)):

        for k in range(len(C)):
            if a*b*c>K:
                break
            else:
                total.append(A[i]+B[j]+C[k])

total_s=sorted(total,reverse=True)

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

from heapq import *

Q=[]
teast=[]
X,Y,Z,K=list(map(int,input().split(" ")))

for i in range(3):
    teast.append(list(map(int,input().split(" "))))

A=sorted(teast[0],reverse=True)
B=sorted(teast[1],reverse=True)
C=sorted(teast[2],reverse=True)

arg_hash={}
heappush(Q,(-A[0]-B[0]-C[0],0,0,0))
arg_hash[(0,0,0)]=1

for k in range(K):
    print(Q)
    ans=heappop(Q)
    a,b,c=ans[1:]
    print(-ans[0])
    argA=(a+1,b,c)
    if a <X-1 and( not argA in arg_hash):
        arg_hash[argA]=1
        heappush(Q,(-A[a+1]-B[b]-C[c],)+argA)
    argB=(a,b+1,c)
    if b <Y-1 and( not argB in arg_hash):
        arg_hash[argB]=1
        heappush(Q,(-A[a]-B[b+1]-C[c],)+argB)
    argC=(a,b,c+1)
    if c <Z-1 and( not argC in arg_hash):
        arg_hash[argC]=1
        heappush(Q,(-A[a]-B[b]-C[c+1],)+argC)

N=list(map(int,(input().split(" "))))
teast=[]
K = N[3]
total=[]
c=0
for i in range(3):
    teast.append(list(map(int,input().split(" "))))
A=sorted(teast[0],reverse=True)
B=sorted(teast[1],reverse=True)
C=sorted(teast[2],reverse=True)

for i in range(len(A)):

    for j in range(len(B)):

        for k in range(len(C)):
            if i*j*k>K:
                break
            else:
                total.append(A[i]+B[j]+C[k])

total_s=sorted(total,reverse=True)

for i in range(K):
    print(total_

from heapq import *

Q=[]
teast=[]
X,Y,Z,K=list(map(int,input().split(" ")))

for i in range(3):
    teast.append(list(map(int,input().split(" "))))

A=sorted(teast[0],reverse=True)
B=sorted(teast[1],reverse=True)
C=sorted(teast[2],reverse=True)

arg_hash={}
heappush(Q,(-A[0]-B[0]-C[0],0,0,0))
arg_hash[(0,0,0)]=1

for k in range(K):
    ans=heappop(Q)
    a,b,c=ans[1:]
    print(-ans[0])
    argA=(a+1,b,c)
    if a <X-1 and( not argA in arg_hash):
        arg_hash[argA]=1
        heappush(Q,(-A[a+1]-B[b]-C[c],)+argA)
    argB=(a,b+1,c)
    if b <Y-1 and( not argB in arg_hash):
        arg_hash[argB]=1
        heappush(Q,(-A[a]-B[b+1]-C[c],)+argB)
    argC=(a,b,c+1)
    if c <Z-1 and( not argC in arg_hash):
        arg_hash[argC]=1
        heappush(Q,(-A[a]-B[b]-C[c+1],)+argC)

Verdicts: Runtime Error

Wrong Answer

Runtime Error

Accepted