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):
X, Y, Z, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
A = sorted([int(x) for x in A], reverse=True)
B = sorted([int(x) for x in B], reverse=True)
C = sorted([int(x) for x in C], reverse=True)

res = []
memo = []
i, j, k = 0, 0, 0
while len(res) < K:
  if [i, j, k] not in memo:
    res.append(A[i]+B[j]+C[k])
    memo.append([i,j,k])
  if i+1 < X and [i+1, j, k] not in memo:
    res.append(A[i+1]+B[j]+C[k])
    memo.append([i+1, j, k])
  if j+1 < Y and [i, j+1, k] not in memo:
    res.append(A[i]+B[j+1]+C[k])
    memo.append([i, j+1, k])
  if k+1 < Z and [i, j, k+1] not in memo:
    res.append(A[i]+B[j]+C[k+1])
    memo.append([i, j, k+1])
  if i < X:
    i += 1
  if j < Y:
    j += 1
  if k < Z:
    k += 1
res = sorted(res, reverse=True)[:K]
for i in res:
  print(i)

X, Y, Z, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
A = sorted([int(x) for x in A], reverse=True)
B = sorted([int(x) for x in B], reverse=True)
C = sorted([int(x) for x in C], reverse=True)

res = []
memo = []
i, j, k = 0, 0, 0
while len(res) < K:
  if [i, j, k] not in memo:
  	res.append(A[i]+B[j]+C[k])
    memo.append([i,j,k])
  if i+1 < X and [i+1, j, k] not in memo:
  	res.append(A[i+1]+B[j]+C[k])
    memo.append([i+1, j, k])
  if j+1 < Y and [i, j+1, k[ not in memo:
  	res.append(A[i]+B[j+1]+C[k])
    memo.append([i, j+1, k])
  if k+1 < Z and [i, j, k+1] not in memo:
  	res.append(A[i]+B[j]+C[k+1])
    memo.append([i, j, k+1])
  if i < X:
    i += 1
  if j < Y:
    j += 1
  if k < Z:
    k += 1
res = sorted(res, reverse=True)[:K]
for i in res:
  print(i)

a,b,c,K = int(input()),int(input()),int(input()),int(input())
A = input().split(' ')
B = input().split(' ')
C = input().split(' ')
A = sorted([int(x) for x in A], reversed=True)
B = sorted([int(x) for x in B], reversed=True)
C = sorted([int(x) for x in C], reversed=True)

res = []
i, j, k = 0, 0, 0
while len(res) < K:
  res.append(A[i]+B[j]+C[k])
  res.append(A[i+1]+B[j]+C[k])
  res.append(A[i]+B[j+1]+C[k])
  res.append(A[i]+B[j]+C[k+1])
  i,j,k = i+1, j+1, k+1
res = sorted(res, reversed=True)[:K]
for i in res:
  print(i)

from heapq import *

a,b,c,K = int(input()),int(input()),int(input()),int(input())
A = sorted([int(x) for x in input().split(' ')], reversed=True)
B = sorted([int(x) for x in input().split(' ')], reversed=True)
C = sorted([int(x) for x in input().split(' ')], reversed=True)

res = []
i, j, k = 0, 0, 0
while len(res) < K:
  res.append(A[i]+B[j]+C[k])
  res.append(A[i+1]+B[j]+C[k])
  res.append(A[i]+B[j+1]+C[k])
  res.append(A[i]+B[j]+C[k+1])
  i,j,k = i+1, j+1, k+1
res = sorted(res, reversed=True)[:K]
for i in res:
  print(i)

X, Y, Z, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
A = sorted([int(x) for x in A], reversed=True)
B = sorted([int(x) for x in B], reversed=True)
C = sorted([int(x) for x in C], reversed=True)

res = []
i, j, k = 0, 0, 0
while len(res) < K:
  res.append(A[i]+B[j]+C[k])
  res.append(A[i+1]+B[j]+C[k])
  res.append(A[i]+B[j+1]+C[k])
  res.append(A[i]+B[j]+C[k+1])
  i,j,k = i+1, j+1, k+1
res = sorted(res, reversed=True)[:K]
for i in res:
  print(i)

from heapq import *

a,b,c,K = int(input()),int(input()),int(input()),int(input())
A = sorted([int(x) for x in input().split(' ')], reversed=True)
B = sorted([int(x) for x in input().split(' ')], reversed=True)
C = sorted([int(x) for x in input().split(' ')], reversed=True)

res = []
i, j, k = 0, 0, 0
while len(res) < K:
  heappush(res, A[i]+B[j]+C[k])
  heappush(res, A[i+1]+B[j]+C[k])
  heappush(res, A[i]+B[j+1]+C[k])
  heappush(res, A[i]+B[j]+C[k+1])
res = nlargeest(K, res)
for i in res:
  print(i)

X, Y, Z, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
A = sorted([int(x) for x in A], reverse=True)
B = sorted([int(x) for x in B], reverse=True)
C = sorted([int(x) for x in C], reverse=True)

res = []
i, j, k = 0, 0, 0
while len(res) < K:
  res.append(A[i]+B[j]+C[k])
  if i+1 < X:
  	res.append(A[i+1]+B[j]+C[k])
  if j+1 < Y:
  	res.append(A[i]+B[j+1]+C[k])
  if k+1 < Z:
  	res.append(A[i]+B[j]+C[k+1])
  if i < X:
    i += 1
  if j < Y:
    j += 1
  if k < Z:
    k += 1
res = sorted(res, reverse=True)[:K]
for i in res:
  print(i)

X, Y, Z, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
A = sorted([int(x) for x in A], reverse=True)
B = sorted([int(x) for x in B], reverse=True)
C = sorted([int(x) for x in C], reverse=True)

res = []
memo = []
i, j, k = 0, 0, 0
while len(res) < K:
  if [i, j, k] not in memo:
    res.append(A[i]+B[j]+C[k])
    memo.append([i,j,k])
  if i+1 < X and [i+1, j, k] not in memo:
    res.append(A[i+1]+B[j]+C[k])
    memo.append([i+1, j, k])
  if j+1 < Y and [i, j+1, k] not in memo:
    res.append(A[i]+B[j+1]+C[k])
    memo.append([i, j+1, k])
  if k+1 < Z and [i, j, k+1] not in memo:
    res.append(A[i]+B[j]+C[k+1])
    memo.append([i, j, k+1])
  if i+1 < X:
    i += 1
  if j+1 < Y:
    j += 1
  if k+1 < Z:
    k += 1
res = sorted(res, reverse=True)[:K]
for i in res:
  print(i)

from heapq import *

a,b,c,K = int(input()),int(input()),int(input()),int(input())
A = sorted([int(x) for x in input().split(' ')], reversed=True)
B = sorted([int(x) for x in input().split(' ')], reversed=True)
C = sorted([int(x) for x in input().split(' ')], reversed=True)

res = []
i, j, k = 0, 0, 0
while len(res) < K:
  heappush(res, A[i]+B[j]+C[k])
  heappush(res, A[i+1]+B[j]+C[k])
  heappush(res, A[i]+B[j+1]+C[k])
  heappush(res, A[i]+B[j]+C[k+1])
  i,j,k = i+1, j+1, k+1
res = nlargest(K, res)
for i in res:
  print(i)

from heapq import *

X, Y, Z, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
A = sorted([int(x) for x in A], reverse=True)
B = sorted([int(x) for x in B], reverse=True)
C = sorted([int(x) for x in C], reverse=True)

res = []
memo = []
ans = []
i, j, k = 0, 0, 0
while len(ans) < K:
  if [i, j, k] not in memo:
    heappush(res, (-A[i]-B[j]-C[k], i, j, k))
  if i+1 < X and [i+1, j, k] not in memo:
    heappush(res, (-A[i+1]-B[j]-C[k], i+1, j, k))
  if j+1 < Y and [i, j+1, k] not in memo:
    heappush(res, (-A[i]-B[j+1]-C[k], i, j+1, k))
  if k+1 < Z and [i, j, k+1] not in memo:
    heappush(res, (-A[i]-B[j]-C[k+1], i, j, k+1))
  if i+1 < X:
    i += 1
  if j+1 < Y:
    j += 1
  if k+1 < Z:
    k += 1
  e, x, y, z = heappop(res)
  ans.append(-e)

for i in ans:
  print(i)

X, Y, Z, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
A = sorted([int(x) for x in A], reverse=True)
B = sorted([int(x) for x in B], reverse=True)
C = sorted([int(x) for x in C], reverse=True)

res = []
i, j, k = 0, 0, 0
while len(res) < K:
  res.append(A[i]+B[j]+C[k])
  res.append(A[i+1]+B[j]+C[k])
  res.append(A[i]+B[j+1]+C[k])
  res.append(A[i]+B[j]+C[k+1])
  if i < X:
    i += 1
  if j < Y:
    j += 1
  if k < Z:
    k += 1
res = sorted(res, reverse=True)[:K]
for i in res:
  print(i)

X, Y, Z, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
A = sorted([int(x) for x in A], reverse=True)
B = sorted([int(x) for x in B], reverse=True)
C = sorted([int(x) for x in C], reverse=True)

res = []
memo = []
i, j, k = 0, 0, 0
while len(res) < K:
  if (i, j, k) not in memo:
  	res.append(A[i]+B[j]+C[k])
    memo.append((i,j,k))
  if i+1 < X and (i+1, j, k) not in memo:
  	res.append(A[i+1]+B[j]+C[k])
    memo.append((i+1, j, k))
  if j+1 < Y and (i, j+1, k) not in memo:
  	res.append(A[i]+B[j+1]+C[k])
    memo.append((i, j+1, k))
  if k+1 < Z and (i, j, k+1) not in memo:
  	res.append(A[i]+B[j]+C[k+1])
    memo.append((i, j, k+1))
  if i < X:
    i += 1
  if j < Y:
    j += 1
  if k < Z:
    k += 1
res = sorted(res, reverse=True)[:K]
for i in res:
  print(i)

line = input()
K = int(line.split(' ')[-1])
A = input().split(' ')
B = input().split(' ')
C = input().split(' ')
A = sorted([int(x) for x in A], reversed=True)
B = sorted([int(x) for x in B], reversed=True)
C = sorted([int(x) for x in C], reversed=True)

res = []
i, j, k = 0, 0, 0
while len(res) < K:
  res.append(A[i]+B[j]+C[k])
  res.append(A[i+1]+B[j]+C[k])
  res.append(A[i]+B[j+1]+C[k])
  res.append(A[i]+B[j]+C[k+1])
  i,j,k = i+1, j+1, k+1
res = sorted(res, reversed=True)[:K]
for i in res:
  print(i)

from heapq import *

a,b,c,K = int(input()),int(input()),int(input()),int(input())
A = sorted([int(x) for x in input().split(' ')], reversed=True)
B = sorted([int(x) for x in input().split(' ')], reversed=True)
C = sorted([int(x) for x in input().split(' ')], reversed=True)

res = []
i, j, k = 0, 0, 0
while len(res) < K:
  heappush(res, A[i]+B[j]+C[k])
  heappush(res, A[i+1]+B[j]+C[k])
  heappush(res, A[i]+B[j+1]+C[k])
  heappush(res, A[i]+B[j]+C[k+1])
  i,j,k = i+1, j+1, k+1
res = nlargeest(K, res)
for i in res:
  print(i)

X, Y, Z, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
A = sorted([int(x) for x in A], reverse=True)
B = sorted([int(x) for x in B], reverse=True)
C = sorted([int(x) for x in C], reverse=True)

res = []
i, j, k = 0, 0, 0
while len(res) < K:
  res.append(A[i]+B[j]+C[k])
  res.append(A[i+1]+B[j]+C[k])
  res.append(A[i]+B[j+1]+C[k])
  res.append(A[i]+B[j]+C[k+1])
  i,j,k = i+1, j+1, k+1
res = sorted(res, reverse=True)[:K]
for i in res:
  print(i)

a,b,c,K = int(input()),int(input()),int(input()),int(input())
A = sorted([int(x) for x in input().split(' ')], reversed=True)
B = sorted([int(x) for x in input().split(' ')], reversed=True)
C = sorted([int(x) for x in input().split(' ')], reversed=True)

res = []
i, j, k = 0, 0, 0
while len(res) < K:
  res.append(A[i]+B[j]+C[k])
  res.append(A[i+1]+B[j]+C[k])
  res.append(A[i]+B[j+1]+C[k])
  res.append(A[i]+B[j]+C[k+1])
  i,j,k = i+1, j+1, k+1
res = sorted(res, reversed=True)[:K]
for i in res:
  print(i)

from heapq import *

X, Y, Z, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
A = sorted([int(x) for x in A], reverse=True)
B = sorted([int(x) for x in B], reverse=True)
C = sorted([int(x) for x in C], reverse=True)

res = []
memo = []
ans = []
heappush(res, (-A[0]-B[0]-C[0], 0, 0, 0))
while len(ans) < K:
  e, i, j, k = heappop(res)
  ans.append(-e)
  if i+1 < X and [i+1, j, k] not in memo:
    heappush(res, (-A[i+1]-B[j]-C[k], i+1, j, k))
    memo.append([i+1, j, k])
  if j+1 < Y and [i, j+1, k] not in memo:
    heappush(res, (-A[i]-B[j+1]-C[k], i, j+1, k))
    memo.append([i, j+1, k])
  if k+1 < Z and [i, j, k+1] not in memo:
    heappush(res, (-A[i]-B[j]-C[k+1], i, j, k+1))
    memo.append([i, j, k+1])

for i in ans:
  print(i)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted