Problem:
 Score: 400 points    Problem Statement  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.      Constraints   1 \leq X \leq 1 \ 000  1 \leq Y \leq 1 \ 000  1 \leq Z \leq 1 \ 000  1 \leq K \leq \min(3 \ 000, X \times Y \times Z)  1 \leq A_i \leq 10 \ 000 \ 000 \ 000  1 \leq B_i \leq 10 \ 000 \ 000 \ 000  1 \leq C_i \leq 10 \ 000 \ 000 \ 000  All values in input are integers.         Input  Input is given from Standard Input in the following format:  X  Y  Z  K  A_1 \ A_2 \ A_3 \ ... \ A_X  B_1 \ B_2 \ B_3 \ ... \ B_Y  C_1 \ C_2 \ C_3 \ ... \ C_Z       Output  Print K lines. The i -th line should contain the i -th value stated in the problem statement.        Sample Input 1 2 2 2 8
4 6
1 5
3 8      Sample Output 1 19
17
15
14
13
12
10
8  There are 2 \times 2 \times 2 = 8 ways to choose three cakes, as shown below in descending order of the sum of the deliciousness of the cakes:   (A_2, B_2, C_2) : 6 + 5 + 8 = 19  (A_1, B_2, C_2) : 4 + 5 + 8 = 17  (A_2, B_1, C_2) : 6 + 1 + 8 = 15  (A_2, B_2, C_1) : 6 + 5 + 3 = 14  (A_1, B_1, C_2) : 4 + 1 + 8 = 13  (A_1, B_2, C_1) : 4 + 5 + 3 = 12  (A_2, B_1, C_1) : 6 + 1 + 3 = 10  (A_1, B_1, C_1) : 4 + 1 + 3 = 8        Sample Input 2 3 3 3 5
1 10 100
2 20 200
1 10 100      Sample Output 2 400
310
310
301
301  There may be multiple combinations of cakes with the same sum of the deliciousness. For example, in this test case, the sum of A_1, B_3, C_3 and the sum of A_3, B_3, C_1 are both 301 .
However, they are different ways of choosing cakes, so 301 occurs twice in the output.       Sample Input 3 10 10 10 20
7467038376 5724769290 292794712 2843504496 3381970101 8402252870 249131806 6310293640 6690322794 6082257488
1873977926 2576529623 1144842195 1379118507 6003234687 4925540914 3902539811 3326692703 484657758 2877436338
4975681328 8974383988 2882263257 7690203955 514305523 6679823484 4263279310 585966808 3752282379 620585736      Sample Output 3 23379871545
22444657051
22302177772
22095691512
21667941469
21366963278
21287912315
21279176669
21160477018
21085311041
21059876163
21017997739
20703329561
20702387965
20590247696
20383761436
20343962175
20254073196
20210218542
20150096547  Note that the input or output may not fit into a 32 -bit integer type.

Solution (Python):


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)

Comb = []
for i in range(X):
    for j in range(Y):
        if (i + 1) * (j + 1) > K:
            break
        for k in range(Z):
            if (i + 1) * (j + 1) * (k + 1) > K:
                break
            Comb.append(A[i] + B[j] + C[k])

Comb.sort(reverse=True)
for i in range(K):
    print(Comb[i])

Solution (Python):
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()))
 
A.sort(reverse = True)
B.sort(reverse = True)
C.sort(reverse = True)
 
now = [0, 0, 0]
que = [[A[0] + B[0] + C[0], 0, 0, 0]]
memory = [[0, 0, 0]]
ans = []
while len(ans) < k:
  maxli = max(que, key = lambda x:x[0])
  ans.append(maxli[0])
  now = maxli[1:]
  que.remove(maxli)
  n0 = min(now[0] + 1, x - 1)
  n1 = min(now[1] + 1, y - 1)
  n2 = min(now[2] + 1, z - 1)
  a = A[n0]
  b = B[n1]
  c = C[n2]
  if [n0, now[1], now[2]] not in memory:
    que.append([a + B[now[1]] + C[now[2]], n0, now[1], now[2]])
    memory.append([n0, now[1], now[2]])
  if [now[0], n1, now[2]] not in memory:
    que.append([A[now[0]] + b + C[now[2]], now[0], n1, now[2]])
    memory.append([now[0], n1, now[2]])
  if [now[0], now[1], n2] not in memory:
    que.append([A[now[0]] + B[now[1]] + c, now[0], now[1], n2])
    memory.append([now[0], now[1], n2])
  if que == []:
    break

print(*ans, sep = "\n")

Solution (Python):
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()))

A.sort(reverse = True)
B.sort(reverse = True)
C.sort(reverse = True)

now = [0, 0, 0]
que = [[A[0] + B[0] + C[0], 0, 0, 0]]
memory = [[0, 0, 0]]
ans = []
while len(ans) < k:
  maxli = max(que, key = lambda x:x[0])
  ans.append(maxli[0])
  now = maxli[1:]
  que.remove(maxli)
  a = A[min(now[0] + 1, x - 1)]
  b = B[min(now[1] + 1, y - 1)]
  c = C[min(now[2] + 1, z - 1)]
  if [min(now[0] + 1, x - 1), now[1], now[2]] not in memory:
    que.append([a + B[now[1]] + C[now[2]], min(now[0] + 1, x - 1), now[1], now[2]])
    memory.append([min(now[0] + 1, x - 1), now[1], now[2]])
  if [now[0], min(now[1] + 1, y - 1), now[2]] not in memory:
    que.append([A[now[0]] + b + C[now[2]], now[0], min(now[1] + 1, y - 1), now[2]])
    memory.append([now[0], min(now[1] + 1, y - 1), now[2]])
  if [now[0], now[1], min(now[2] + 1, z - 1)] not in memory:
    que.append([A[now[0]] + B[now[1]] + c, now[0], now[1], min(now[2] + 1, z - 1)])
    memory.append([now[0], now[1], min(now[2] + 1, z - 1)])

print(*ans, sep = "\n")

Solution (Python):
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()))

A.sort(reverse = True)
B.sort(reverse = True)
C.sort(reverse = True)

now = [0, 0, 0]
que = [[A[0] + B[0] + C[0], 0, 0, 0]]
memory = [[A[0] + B[0] + C[0], 0, 0, 0]]
ans = []
while len(ans) < k:
  maxli = max(que, key = lambda x:x[0])
  ans.append(maxli[0])
  now = maxli[1:]
  que.remove(maxli)
  a = A[min(now[0] + 1, x - 1)]
  b = B[min(now[1] + 1, y - 1)]
  c = C[min(now[2] + 1, z - 1)]
  if [a + B[now[1]] + C[now[2]], min(now[0] + 1, x - 1), now[1], now[2]] not in memory:
    que.append([a + B[now[1]] + C[now[2]], min(now[0] + 1, x - 1), now[1], now[2]])
    memory.append([a + B[now[1]] + C[now[2]], min(now[0] + 1, x - 1), now[1], now[2]])
  if [A[now[0]] + b + C[now[2]], now[0], min(now[1] + 1, y - 1), now[2]] not in memory:
    que.append([A[now[0]] + b + C[now[2]], now[0], min(now[1] + 1, y - 1), now[2]])
    memory.append([A[now[0]] + b + C[now[2]], now[0], min(now[1] + 1, y - 1), now[2]])
  if [A[now[0]] + B[now[1]] + c, now[0], now[1], min(now[2] + 1, z - 1)] not in memory:
    que.append([A[now[0]] + B[now[1]] + c, now[0], now[1], min(now[2] + 1, z - 1)])
    memory.append([A[now[0]] + B[now[1]] + c, now[0], now[1], min(now[2] + 1, z - 1)])

print(*ans, sep = "\n")

Solution (Python):
params = [int(x) for x in input().split(" ")]

X = params[0]
Y = params[1]
Z = params[2]

K = params[3]

A = [int(x) for x in input().split(" ")]

B = [int(x) for x in input().split(" ")]

C = [int(x) for x in input().split(" ")]

A.sort(reverse = True)
B.sort(reverse = True)
C.sort(reverse = True)

history = []
ansList = []

queue = []

queue.append([A[0]+B[0]+C[0], 0,0,0])
history.append(queue[0])



while True:
	queue.sort(reverse=True)
	ansList.append(queue[0])

	if len(ansList) >= K:
		break

	Aindex = queue[0][1]
	Bindex = queue[0][2]
	Cindex = queue[0][3]

	queue = queue[1:]

	try:
		nextA = [A[Aindex+1]+B[Bindex]+C[Cindex], Aindex+1, Bindex, Cindex]
		if not nextA in history:
			queue.append(nextA)
			history.append(nextA)
	except:
		pass

	try:
		nextB = [A[Aindex]+B[Bindex+1]+C[Cindex], Aindex, Bindex+1, Cindex]
		if not nextB in history:
			queue.append(nextB)
			history.append(nextB)
	except:
		pass

	try:
		nextC = [A[Aindex]+B[Bindex]+C[Cindex+1], Aindex, Bindex, Cindex+1]
		if not nextC in history:
			queue.append(nextC)
			history.append(nextC)
	except:
		pass
		
for ans in ansList:
	print(ans[0])

Solution (Python):
x,y,z,K = map(int,input().split())
abc = [sorted(list(map(int,input().split())), reverse=True) for i in range(3)]

ans=[]
ab=[]

for i in range(x):
    for j in range(y):
        if (i+1)*(j+1) <= K:
            ab.append(abc[0][i]+abc[1][j])

ab.sort(reverse=True)

for i in range(min([len(ab),K])):
    for j in range(z):
        if (i+1)*(j+1) <= K:
            ans.append(ab[i]+abc[2][j])
    
ans.sort(reverse=True)

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

Solution (Python):
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()))

ab = []
for i in range(x):
    for j in range(y):
        ab.append(a[i] + b[j])
ab.sort(reverse = True)

abc = []
for i in range(min(k,len(ab))):
    for j in range(z):
        abc.append(ab[i] + c[j])

abc.sort(reverse = True)

for i in range(k):
    print(abc[i])

Solution (Python):
from heapq import heappush, heappop
X, Y, Z, K = map(int, input().split())
I = lambda: sorted(list(map(int, input().split())), reverse=True)
A = I()
B = I()
C = I()
hq = []

for a in A:
    for b in B:
        for c in C:
            if len(hq) < K:
                heappush(hq, a+b+c)
            else:
                tmp = heappop(hq)
                if tmp >= a+b+c:
                    heappush(hq, tmp)
                    break
                else:
                    heappush(hq, a+b+c)

ans = []
for _ in range(K):
    ans.append(heappop(hq))

ans.sort(reverse=True)
for x in ans:
    print(x)

Solution (Python):
from datetime import datetime as dt
X,Y,Z,K = list(map(int,input().split(" ")))
As = list(map(int,input().split(" ")))
Bs = list(map(int,input().split(" ")))
Cs = list(map(int,input().split(" ")))

As.sort(reverse = True )
Bs.sort(reverse = True )

ABs = []
for a in As:
    for b in Bs:
        ABs.append(a + b)
ABs.sort(reverse = True)
upper = min(len(ABs),K)

ABs = ABs[:upper]
ans = []

for C in Cs:
    for AB in ABs:
        ans.append(AB+C)
ans.sort(reverse = True)
for i in range(K):
    print(ans[i])

Solution (Python):
X,Y,Z,K = map(int,input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
C = [int(x) for x in input().split()]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

AB = [a+b for a in A for b in B]
AB.sort(reverse=True)
AB = AB[:min(3001,X*Y*Z+1)]

ABC = [ab+c for ab in AB for c in C]
ABC.sort(reverse=True)

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

Solution (Python):

x,y,z,k = map(int,input().split())
lisa = list(map(int,input().split()))
lisb = list(map(int,input().split()))
lisc = list(map(int,input().split()))
lisa.sort(reverse=True)
lisb.sort(reverse=True)
lisc.sort(reverse=True)
an = []
ans = []
for num in lisb:
    for nu in lisa:
        an.append(num+nu)
an.sort(reverse=True)
an = an[:min(3001,x*y*z+1)]
for num in lisc:
    for nu in an:
        ans.append(num+nu)
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Solution (Python):
x,y,z,K = map(int, input().split())
a = sorted(list(map(int, input().split())))[::-1]
b = sorted(list(map(int, input().split())))[::-1]
c = sorted(list(map(int, input().split())))[::-1]

def check(p, l=None):
    cnt = 0
    for i in range(x):
        for j in range(y):
            for k in range(z):
                if a[i]+b[j]+c[k] < p:
                    break
                if l is not None:
                    l.append(a[i]+b[j]+c[k])
                cnt += 1
                if cnt >= K:
                    return True
    return False

left = 0
right = a[0]+b[0]+c[0] + 1
while right-left > 1:
    mid = (left+right)//2
    if check(mid):
        left = mid
    else:
        right = mid

ans = []
check(right, ans)
ans += [left]*(K - len(ans))
for i in sorted(ans, reverse=True)[:K]:
    print(i)

Solution (Python):
x,y,z,K = map(int, input().split())
abc = [sorted(list(map(int, input().split())), reverse=True) for i in range(3)]

ans = []
for i in range(x):
    for j in range(y):
        for k in range(z):
            if (i+1)*(j+1)*(k+1) <= K:
                ans.append(abc[0][i]+abc[1][j]+abc[2][k])
            else:
                break
for i in sorted(ans)[::-1][:K]:
    print(i)

Solution (Python):
from heapq import heappop, heappush

x,y,z,k = map(int, input().split())
abc = [sorted(list(map(int, input().split())), reverse=True) for i in range(3)]
heap = [[-sum(abc[i][0] for i in range(3)), (0,0,0)]]


cnt = 0
used = set()
while cnt < k:
    score, (a,b,c) = heappop(heap)
    if (a,b,c) in used:
        continue
    print(-score)
    used.add((a,b,c))
    cnt += 1
    for i in range(3):
        na = a+int(i==0)
        nb = b+int(i==1)
        nc = c+int(i==2)
        if 0<=na<x and 0<=nb<y and 0<=nc<z:
            nscore = 0
            for ind, j in enumerate([na,nb,nc]):
                nscore -= abc[ind][j]
            heappush(heap,[nscore, (na,nb,nc)])

Solution (Python):
import math
xyzk = list(map(int,input().split()))
abc = [list(map(int,input().split())) for a in range(3)]
for a in range(3):
    abc[a].sort()
    abc[a].reverse()
abc2=[]
for a in range(len(abc[0])):
    for b in range(len(abc[1])):
        if (a+1)*(b+1)>xyzk[3]:
            break
        for c in range(len(abc[2])):
            if (a+1)*(b+1)*(c+1)>xyzk[3] :
                break
            abc2.append(abc[0][a]+abc[1][b]+abc[2][c])
abc2.sort()
abc2.reverse()
for a in range(xyzk[3]):
    print(abc2[a])

Solution (Python):
import math
xyzk = list(map(int,input().split()))
abc = [list(map(int,input().split())) for a in range(3)]
for a in range(3):
    abc[a].sort()
    abc[a].reverse()
abc2=[]
for a in range(len(abc[0])):
    for b in range(len(abc[1])):
        if (a+1)*(b+1)>xyzk[3]:
            break
        for c in range(len(abc[2])):
            if (a+1)*(b+1)*(c+1)>xyzk[3] :
                break
            abc2.append(abc[0][a]+abc[1][b]+abc[2][c])
abc2.sort()
abc2.reverse()
for a in range(xyzk[3]):
    print(abc2[a])

Solution (Python):
import math
xyzk = list(map(int,input().split()))
abc = [list(map(int,input().split())) for a in range(3)]
ab=[]
for a in abc[0]:
    for b in abc[1]:
        ab.append(a+b)
ab.sort()
ab.reverse()
abc[2].sort()
abc[2].reverse()
abc2=[]
for c in range(len(abc[2])):
    for a in range(min([math.ceil(xyzk[3]),len(ab)])):
        abc2.append(abc[2][c]+ab[a])
abc2.sort()
abc2.reverse()
for a in range(xyzk[3]):
    print(abc2[a])

Solution (Python):
import math
xyzk = list(map(int,input().split()))
abc = [list(map(int,input().split())) for a in range(3)]
ab=[]
for a in abc[0]:
    for b in abc[1]:
        ab.append(a+b)
ab.sort()
ab.reverse()
abc[2].sort()
abc[2].reverse()
abc2=[]
for c in range(len(abc[2])):
    for a in range(min([math.ceil(xyzk[3]/(c+1)),len(ab)])):
        abc2.append(abc[2][c]+ab[a])
abc2.sort()
abc2.reverse()
for a in range(xyzk[3]):
    print(abc2[a])

Solution (Python):
import math
xyzk = list(map(int,input().split()))
abc = [list(map(int,input().split())) for a in range(3)]
ab=[]
for a in abc[0]:
    for b in abc[1]:
        ab.append(a+b)
ab.sort()
ab.reverse()
abc[2].sort()
abc[2].reverse()
abc2=[]
for c in range(len(abc[2])):
    for a in range(min([math.ceil(xyzk[3]/(c+1)),len(ab)])):
        abc2.append(abc[2][c]+ab[a])
abc2.sort()
abc2.reverse()
for a in range(xyzk[3]):
    print(abc2[a])

Solution (Python):
import math
xyzk = list(map(int,input().split()))
abc = [list(map(int,input().split())) for a in range(3)]
ab=[]
for a in abc[0]:
    for b in abc[1]:
        ab.append(a+b)
ab.sort()
ab.reverse()
abc[2].sort()
abc[2].reverse()
abc2=[]
for c in range(len(abc[2])):
    for a in range(min([math.ceil(xyzk[3]/(c+1)),len(ab)])):
        abc2.append(abc[2][c]+ab[a])
abc2.sort()
abc2.reverse()
for a in range(xyzk[3]):
    print(abc2[a])

Solution (Python):
from heapq import heappush, heappop

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()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
h = []
used = set()
used.add((0, 0, 0))
heappush(h, (-(A[0]+B[0]+C[0]), 0, 0, 0))
for i in range(K):
    val, x, y, z = heappop(h)
    print(-val)
    if x < X-1 and (x+1, y, z) not in used:
        used.add((x+1, y, z))
        heappush(h, (-(A[x+1]+B[y]+C[z]), x+1, y, z))
    if y < Y-1 and (x, y+1, z) not in used:
        used.add((x, y+1, z))
        heappush(h, (-(A[x]+B[y+1]+C[z]), x, y+1, z))
    if z < Z-1 and (x, y, z+1) not in used:
        used.add((x, y, z+1))
        heappush(h, (-(A[x]+B[y]+C[z+1]), x, y, z+1))

Solution (Python):
'''
Implement and make use of priority queue using heapq module.
Using priority queue(heapq) is fast because all of insertion, deletion, and popping is done by order O(log n).

heapq documentation: https://docs.python.org/ja/3/library/heapq.html
'''

import heapq


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()))

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)






candidates = [] 
heapq.heappush(candidates, (-(A[0]+B[0]+C[0]), 0,0,0))


used = {(0,0,0)}

for _ in range(K):

    s, i, j, k = heapq.heappop(candidates)
    print(-s)

    if i < X-1 and (i+1,j,k) not in used:
        heapq.heappush(candidates, (-(A[i+1]+B[j]+C[k]), i+1,j,k))
        used.add((i+1,j,k))

    if j < Y-1 and (i,j+1,k) not in used:
            heapq.heappush(candidates, (-(A[i]+B[j+1]+C[k]), i,j+1,k))
            used.add((i,j+1,k))

    if k < Z-1 and (i,j,k+1) not in used:
            heapq.heappush(candidates, (-(A[i]+B[j]+C[k+1]), i,j,k+1))
            used.add((i,j,k+1))

Solution (Python):
import heapq

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()))
assert len(A) == X and len(B) == Y and len(C) == Z


A = [-x for x in A]
B = [-x for x in B]
C = [-x for x in C]

A.sort()
B.sort()
C.sort()

def val(pos):
    a, b, c = pos
    return A[a] + B[b] + C[c]

def nextpos(pos):
    a, b, c = pos
    if a + 1 < len(A):
        yield (a + 1, b, c)
    if b + 1 < len(B):
        yield (a, b + 1, c)
    if c + 1 < len(C):
        yield (a, b, c + 1)

def cakelist():
    H = []  
    heapq.heappush(H, (val((0, 0, 0)), (0, 0, 0)))
    visited = {(0, 0, 0)}
    while len(H) > 0:
        v, pos = heapq.heappop(H)
        yield -v
        for npos in nextpos(pos):
            if npos not in visited:
                heapq.heappush(H, (val(npos), npos))
                visited.add(npos)

for x, _ in zip(cakelist(), range(K)):
    print(x)

Solution (Python):
import heapq

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()))
assert len(A) == X and len(B) == Y and len(C) == Z


A = [-x for x in A]
B = [-x for x in B]
C = [-x for x in C]

A.sort()
B.sort()
C.sort()

def val(pos):
    a, b, c = pos
    return A[a] + B[b] + C[c]

def nextpos(pos):
    a, b, c = pos
    if a + 1 < len(A):
        yield (a + 1, b, c)
    if b + 1 < len(B):
        yield (a, b + 1, c)
    if c + 1 < len(C):
        yield (a, b, c + 1)

def cakelist():
    H = []  
    heapq.heappush(H, (val((0, 0, 0)), (0, 0, 0)))
    visited = {(0, 0, 0)}
    while len(H) > 0:
        v, pos = heapq.heappop(H)
        yield -v
        for npos in nextpos(pos):
            if npos not in visited:
                heapq.heappush(H, (val(npos), npos))
                visited.add(npos)

cl = cakelist()
for i in range(K):
    print(next(cl))

Solution (Python):
import heapq

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

h = []
s = set()
ia = ib = ic = 0

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)


def f(ia, ib, ic):
    return (-(A[ia] + B[ib] + C[ic]), ia, ib, ic)


item = f(ia, ib, ic)
heapq.heappush(h, item)
s.add(item)
ans = []

for i in range(K):
    
    
    if len(h) == 0:
        break
    t = heapq.heappop(h)
    ans.append(t)
    if t[1] + 1 < len(A):
        item = f(t[1] + 1, t[2], t[3])
        if not item in s:
            heapq.heappush(h, item)
        s.add(item)
    if t[2] + 1 < len(B):
        item = f(t[1], t[2] + 1, t[3])
        if not item in s:
            heapq.heappush(h, item)
        s.add(item)

    if t[3] + 1 < len(C):
        item = f(t[1], t[2], t[3] + 1)
        if not item in s:
            heapq.heappush(h, item)
        s.add(item)






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

Solution (Python):
import heapq
X,Y,Z,K= map(int,input().split())
K = min(X*Y*Z,K)

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)
pque =[(-(A[0]+B[0]+C[0]),(0,0,0))] 
memo= {}
cnt = 0

while pque:
    value, position = heapq.heappop(pque)
    if memo.get(position) is None :
        memo[position] = True
        cnt += 1 
        if cnt > K:
            break
    else: 
        continue
    print(-value)
    for patern in [[1,0,0],[0,1,0],[0,0,1]]:
        i,j,k = [v1+v2 for v1,v2 in zip(patern,position)]
        if i<X and j<Y and k<Z:
            heapq.heappush(pque,(-(A[i]+B[j]+C[k]),(i,j,k)))

Solution (Python):
import heapq
x, y, z, K = map(int, input().split())
k = min(x * y * z, K)
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)

results = []
pque = [(-(a[0] + b[0] + c[0]), (0, 0, 0))]
visited = {}
cnt = 0
while pque:
    value, position = heapq.heappop(pque)
    if visited.get(position) is None:
        visited[position] = True
        cnt += 1
        if cnt > K:
            break
    else:
        continue

    print(-value)
    results.append(value)
    for pattern in [[0, 0, 1], [0, 1, 0], [1, 0, 0]]:
        i, j, k = [v1 + v2 for v1, v2 in zip(pattern, position)]
        if i < x and j < y and k < z:
            heapq.heappush(pque, (-(a[i] + b[j] + c[k]), (i, j, k)))

Solution (Python):
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()))
 
A = sorted(A, reverse=True)[:K+1]
B = sorted(B, reverse=True)[:K+1]
C = sorted(C, reverse=True)[:K+1]
 
res = []
 
for i in A:
    for j in B:
        res.append(i + j)
res2 = []
res = sorted(res, reverse=True)[:K+1]
 
for i in C:
    for re in res:
        res2.append(i + re)
        
res2 = sorted(res2, reverse=True)[:K+1]
          
 
for i in range(K):
    print(res2[i])

Solution (Python):
x, y, z, k = map(int, input().split())
k = min(x * y * z, k)
a = sorted(list(map(int, input().split())), reverse=True)[:k]
b = sorted(list(map(int, input().split())), reverse=True)[:k]
c = sorted(list(map(int, input().split())), reverse=True)[:k]
def f(a, b):
    return [i + j for i in a for j in b]
ab_results = sorted(f(a, b), reverse=True)
results = sorted(f(c, ab_results[:k]), reverse=True)

print("\n".join(map(str, results[:k])))

Solution (Python):
import numpy as np
x, y, z, k = map(int, input().split())
k = min(x * y * z, k)
a = np.array(list(map(int, input().split())))
b = np.array(list(map(int, input().split())))
c = np.array(list(map(int, input().split())))

ab = np.ravel(a + b[:, None])
ab.sort()
abc = np.ravel(c + ab[-k:, None])
abc.sort()
print(np.sum(abc[-k:][::-1].astype(str).astype(object) + "\n"), end="")

Solution (Python):
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()))

A = sorted(A, reverse=True)[:K+1]
B = sorted(B, reverse=True)[:K+1]
C = sorted(C, reverse=True)[:K+1]

res = []

for i in A:
    for j in B:
        res.append(i + j)
res2 = []
res = sorted(res, reverse=True)[:K+1]

for i in C:
    for re in res:
        res2.append(i + re)
        
res2 = sorted(res2, reverse=True)[:K+1]
          

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

Solution (Python):
*XYZ,K = map(int,input().split())
ABC = [list(map(int,input().split())) for i in range(3)]
for a in ABC:
    a.sort(reverse=1)
import heapq
ans = [0]*K
for a in ABC[0]:
    for b in ABC[1]:
        for c in ABC[2]:
            if a+b+c>ans[0]:
                heapq.heappush(ans,a+b+c)
                heapq.heappop(ans)
            else:
                break
ans.sort(reverse=1)
for i in range(K):
    print(ans[i])

Solution (Python):
x, y, z, k = map(int, input().split())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
cc = list(map(int, input().split()))

aa.sort(reverse=True)
aa = aa[:k]
bb.sort(reverse=True)
bb = bb[:k]
cc.sort(reverse=True)
cc = cc[:k]

dd = [a+b for a in aa for b in bb]
dd.sort(reverse=True)
dd = dd[:k]

ee = [a+b for a in dd for b in cc]
ee.sort(reverse=True)
ee = ee[:k]
print(*ee, sep='\n')

Solution (Python):
input = raw_input
def lsum(x):
    return A[x[0]] + B[x[1]] + C[x[2]]

def nextlist():
    global lx
    idx = 0
    m = lsum(lx[0])
    for i in range(len(lx)):
        s = lsum(lx[i])
        if m < s:
            m = s
            idx = i
    p = lx.pop(idx)
    wa.append(lsum(p))
    if p[0] > 0:
        if not [p[0]-1, p[1], p[2]] in lx:
            lx.append([p[0]-1, p[1], p[2]])
    if p[1] > 0:
        if not [p[0], p[1]-1, p[2]] in lx:
            lx.append([p[0], p[1]-1, p[2]])
    if p[2] > 0:
        if not [p[0], p[1], p[2]-1] in lx:
            lx.append([p[0], p[1], p[2]-1])

l1 = list(map(int, input().split(" ")))

X,Y,Z,K = l1[0],l1[1],l1[2],l1[3]
A = sorted(list(map(int, input().split(" "))))
B = sorted(list(map(int, input().split(" "))))
C = sorted(list(map(int, input().split(" "))))

wa = []
lx = [[X-1,Y-1,Z-1]]

for i in range(K):
    nextlist()

for l in wa:
    print(l)

Solution (Python):
import heapq

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()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

candidates = []
heapq.heappush(candidates, (-(A[0] + B[0] + C[0]), 0, 0, 0))
used = {(0, 0, 0)}
for _ in range(K):
    s, i, j, k = heapq.heappop(candidates)
    print(-s)
    if i < X - 1 and (i+1, j, k) not in used:
        heapq.heappush(candidates, (-(A[i+1] + B[j] + C[k]), i+1, j, k))
        used.add((i+1, j, k))
    if j < Y - 1 and (i, j+1, k) not in used:
        heapq.heappush(candidates, (-(A[i] + B[j+1] + C[k]), i, j+1, k))
        used.add((i, j+1, k))
    if k < Z - 1 and (i, j, k+1) not in used:
        heapq.heappush(candidates, (-(A[i] + B[j] + C[k+1]), i, j, k+1))
        used.add((i, j, k+1))

Solution (Python):
def lsum(x):
    return A[x[0]] + B[x[1]] + C[x[2]]

def nextlist():
    global lx
    idx = 0
    m = lsum(lx[0])
    for i in range(len(lx)):
        s = lsum(lx[i])
        if m < s:
            m = s
            idx = i
    p = lx.pop(idx)
    wa.append(lsum(p))
    if p[0] > 0:
        if not [p[0]-1, p[1], p[2]] in lx:
            lx.append([p[0]-1, p[1], p[2]])
    if p[1] > 0:
        if not [p[0], p[1]-1, p[2]] in lx:
            lx.append([p[0], p[1]-1, p[2]])
    if p[2] > 0:
        if not [p[0], p[1], p[2]-1] in lx:
            lx.append([p[0], p[1], p[2]-1])

l1 = list(map(int, input().split(" ")))

X,Y,Z,K = l1[0],l1[1],l1[2],l1[3]
A = sorted(list(map(int, input().split(" "))))
B = sorted(list(map(int, input().split(" "))))
C = sorted(list(map(int, input().split(" "))))

wa = []
lx = [[X-1,Y-1,Z-1]]

for i in range(K):
    nextlist()

for l in wa:
    print(l)

Solution (Python):
import heapq
X, Y, Z, K = [int(i) for i in input().split()]
A = sorted([int(i) for i in input().split()],reverse=True)
B = sorted([int(i) for i in input().split()],reverse=True)
C = sorted([int(i) for i in input().split()],reverse=True)
def set_tuple(A,B,C,x,y,z):
    return (-(A[x]+B[y]+C[z]),x,y,z)
Q = []
heapq.heappush(Q,set_tuple(A,B,C,0,0,0))
Ans = []
memo = {(0,0,0):True}
for _ in range(K):
    e,x,y,z = heapq.heappop(Q)
    Ans.append(-e)
    if x+1 < X and (x+1,y,z) not in memo:
        heapq.heappush(Q,set_tuple(A,B,C,x+1,y,z))
        memo[(x+1,y,z)] = True
    if y+1 < Y and (x,y+1,z) not in memo:
        heapq.heappush(Q,set_tuple(A,B,C,x,y+1,z))
        memo[(x,y+1,z)] = True
    if z+1 < Z and (x,y,z+1) not in memo:
        heapq.heappush(Q,set_tuple(A,B,C,x,y,z+1))
        memo[(x,y,z+1)] = True
for i in Ans[:K]:print(i)

Solution (Python):
import heapq


def solve(k, a_list, b_list, c_list):
    
    a_list.sort(reverse=True)
    b_list.sort(reverse=True)
    c_list.sort(reverse=True)

    priority_queue = []
    heapq.heappush(priority_queue, [-(a_list[0] + b_list[0] + c_list[0]), 0, 0, 0])
    key_map = {(0, 0, 0)}

    ans = []
    
    for _ in range(k):
        sum_data, a_key, b_key, c_key = heapq.heappop(priority_queue)
        ans.append(str(-sum_data))

        if a_key + 1 < len(a_list) and (a_key+1, b_key, c_key) not in key_map:
            heapq.heappush(priority_queue, [-(a_list[a_key+1] + b_list[b_key] + c_list[c_key]), a_key+1, b_key, c_key])
            key_map.add((a_key+1, b_key, c_key))
        if b_key + 1 < len(b_list) and (a_key, b_key+1, c_key) not in key_map:
            heapq.heappush(priority_queue, [-(a_list[a_key] + b_list[b_key+1] + c_list[c_key]), a_key, b_key+1, c_key])
            key_map.add((a_key, b_key+1, c_key))
        if c_key + 1 < len(c_list) and (a_key, b_key, c_key+1) not in key_map:
            heapq.heappush(priority_queue, [-(a_list[a_key] + b_list[b_key] + c_list[c_key+1]), a_key, b_key, c_key+1])
            key_map.add((a_key, b_key, c_key+1))

    return ans


def main():
    x, y, z, k = map(int, input().split())
    a_list = list(map(int, input().split()))
    b_list = list(map(int, input().split()))
    c_list = list(map(int, input().split()))

    ans = solve(k, a_list, b_list, c_list)
    print("\n".join(ans))


main()

Solution (Python):
import heapq
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()))
a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
ans = [(-(a[0]+b[0]+c[0]), 0, 0, 0)]
for i in range(K):
    m, j, k, h = heapq.heappop(ans)
    print(-m)
    if j+1 <= X-1:
        s =(-(a[j+1]+b[k]+c[h]), j+1, k, h)
        if s not in ans:
            heapq.heappush(ans, s)
    if k+1 <= Y-1:
        s =(-(a[j]+b[k+1]+c[h]), j , k+1, h)
        if s not in ans:
            heapq.heappush(ans, s)
    if h+1 <= Z-1:
        s =(-(a[j]+b[k]+c[h+1]), j , k, h+1)
        if s not in ans:
            heapq.heappush(ans, s)

Solution (Python):
def ca():
    x,y,z,k=map(int,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()]
    c.sort(reverse=True)
    AB=[i+j for i in a for j in b]
    AB.sort(reverse=True)
    ans=[i+j for i in AB[:k]for j in c]
    ans.sort(reverse=True)
    for i in ans[:k]:
        print(i)
    exit()
print(ca())

Solution (Python):
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(" ")))

ab = []
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort(reverse=True)
ab = ab[:min(x*y, k)]
abc = []
for i in range(len(ab)):
    for j in range(z):
        abc.append(ab[i]+c[j])
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])