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):
from itertools import product
from heapq import heappop, heappush

x,y,z,k = map(int,input().split())
a = sorted(map(int, input().split()), reverse=True)
b = sorted(map(int, input().split()), reverse=True)
c = sorted(map(int, input().split()), reverse=True)


idx = [0,0,0]
heap = []
xxx = [x,y,z]

aaa = idx[0] if idx[0]+1 >= x else idx[0] + 1
bbb = idx[1] if idx[1]+1 >= y else idx[1] + 1
ccc = idx[2] if idx[2]+1 >= z else idx[2] + 1
heappush(heap, [-(a[aaa] + b[idx[1]] + c[idx[2]]), [1, 0, 0], 0])
heappush(heap, [-(a[idx[0]] + b[bbb] + c[idx[2]]), [0, 1, 0], 1])
heappush(heap, [-(a[idx[0]] + b[idx[1]] + c[ccc]), [0, 0, 1], 2])
print(a[idx[0]] + b[idx[1]] + c[idx[2]])
doc = {}
for _ in range(min(k-1, x*y*z)):
    score, ix, pos = heappop(heap)
    print(-score)
    for i in range(3):
        tmp = ix.copy()
        tmp[i] = ix[i] if ix[i] + 1 >= xxx[i] else ix[i] + 1
        if ','.join(map(str, tmp)) not in doc:
            doc[','.join(map(str, tmp))] = 1
            score = a[tmp[0]] + b[tmp[1]] + c[tmp[2]]
            # print(idx, ix, score, heap)
            heappush(heap, [-score, tmp, i])

# lst.sort(reverse=True)



from itertools import product
from heapq import heappop, heappush

x,y,z,k = map(int,input().split())
a = sorted(map(int, input().split()), reverse=True)
b = sorted(map(int, input().split()), reverse=True)
c = sorted(map(int, input().split()), reverse=True)
if x * y * z <= 3000:
    lst = sorted([sum(i) for i in product(a,b,c)], reverse=True)
    [print(i) for i in lst[:k]]
    exit()

idx = [0,0,0]
heap = []
xxx = [x,y,z]

aaa = idx[0] if idx[0]+1 >= x else idx[0] + 1
bbb = idx[1] if idx[1]+1 >= y else idx[1] + 1
ccc = idx[2] if idx[2]+1 >= z else idx[2] + 1
heappush(heap, [-(a[aaa] + b[idx[1]] + c[idx[2]]), [1, 0, 0], 0])
heappush(heap, [-(a[idx[0]] + b[bbb] + c[idx[2]]), [0, 1, 0], 1])
heappush(heap, [-(a[idx[0]] + b[idx[1]] + c[ccc]), [0, 0, 1], 2])
print(a[idx[0]] + b[idx[1]] + c[idx[2]])
doc = {}
for _ in range(min(k-1, x*y*z)):
    score, ix, pos = heappop(heap)
    print(-score)
    for i in range(3):
        tmp = ix.copy()
        tmp[i] = ix[i] if ix[i] + 1 >= xxx[i] else ix[i] + 1
        if ','.join(map(str, tmp)) not in doc:
            doc[','.join(map(str, tmp))] = 1
            score = a[tmp[0]] + b[tmp[1]] + c[tmp[2]]
            # print(idx, ix, score, heap)
            heappush(heap, [-score, tmp, i])

# lst.sort(reverse=True)



Verdicts: Wrong Answer

Accepted