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):
import heapq


def solve():
    X, Y, Z, K = map(int, input().split())
    a_ls = sorted(map(lambda x: -int(x), input().split()))
    b_ls = sorted(map(lambda x: -int(x), input().split()))
    c_ls = sorted(map(lambda x: -int(x), input().split()))
    hq = []
    heapq.heappush(hq, (a_ls[0]+b_ls[0]+c_ls[0], 0, 0, 0))
    hq_hash = {(0, 0, 0)}
    
    for _ in range(K):
        ans, i, j, k = heapq.heappop(hq)
        
        if i+1 < X and not((i+1, j, k) in hq_hash):
            hq_hash.add((i+1, j, k))
            heapq.heappush(hq, (a_ls[i+1]+b_ls[j]+c_ls[k], i+1, j, k))
        if j+1 < Y and not((i, j+1, k) in hq_hash):
            hq_hash.add((i, j+1, k))
            heapq.heappush(hq, (a_ls[i]+b_ls[j+1]+c_ls[k], i, j+1, k))
        if k+1 < Z and not((i, j, k+1) in hq_hash):
            hq_hash.add((i, j, k+1))
            heapq.heappush(hq, (a_ls[i]+b_ls[j]+c_ls[k+1], i, j, k+1))
        
        print(-ans)


if __name__ == '__main__':
    solve()

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

for l in [A, B, C]:
    l.sort()

exists = set()
q = [(-(A[X - 1] + B[Y - 1] + C[Z - 1]), (X - 1, Y - 1, Z - 1))]
for _ in range(K):
    v, n = heappop(q)
    print(-v)
    for i in range(3):
        if n[i] > 0:
            t = [n[j] for j in range(3)]
            t[i] -= 1
            nn = tuple(t)
            if nn not in exists:
                exists.add(nn)
                nv = A[nn[0]] + B[nn[1]] + C[nn[2]]
                heappush(q, (-nv, nn))

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)

exists = set()
q = [(-(A[0] + B[0] + C[0]), (0, 0, 0))]
for _ in range(K):
    v, i = heappop(q)
    print(-v)
    if i[0] < X - 1:
        ni = (i[0] + 1, i[1], i[2])
        if ni not in exists:
            exists.add(ni)
            nv = A[i[0] + 1] + B[i[1]] + C[i[2]]
            heappush(q, (-nv, ni))
    if i[1] < Y - 1:
        ni = (i[0], i[1] + 1, i[2])
        if ni not in exists:
            exists.add(ni)
            nv = A[i[0]] + B[i[1] + 1] + C[i[2]]
            heappush(q, (-nv, ni))
    if i[2] < Z - 1:
        ni = (i[0], i[1], i[2] + 1)
        if ni not in exists:
            exists.add(ni)
            nv = A[i[0]] + B[i[1]] + C[i[2] + 1]
            heappush(q, (-nv, ni))

Solution (Python):

from heapq import heappush,heappop,heapify
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)
d=[]
for i in range(x):
    for j in range(y):
        if i*j>k:
            break
        for l in range(z):
            if i*j*l>k:
                break
            heappush(d,-a[i]-b[j]-c[l])
for i in range(k):
    print(-heappop(d))

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()))
d=[]
for i in range(x):
    for j in range(y):
        heappush(d,-a[i]-b[j])
e=[]
for i in range(min(3000,x*y)):
    e.append(-heappop(d))
f=[]
for i in range(len(e)):
    for j in range(z):
        heappush(f,-e[i]-c[j])
for i in range(k):
    print(-heappop(f))

Solution (Python):
import heapq

def main():
    X, Y, Z, K = map(int,input().split())
    A = [int(o) for o in input().split()]
    B = [int(o) for o in input().split()]
    C = [int(o) for o in input().split()]
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    A_size = len(A)
    B_size = len(B)
    C_size = len(C)
    heap = []
    heapq.heappush(heap,(-(A[0]+B[0]+C[0]),0,0,0))
    for i in range(K):
        val,a,b,c = heapq.heappop(heap)
        if a < (A_size-1) and (-(A[a+1]+B[b]+C[c]),a+1,b,c) not in heap:
            heapq.heappush(heap,(-(A[a+1]+B[b]+C[c]),a+1,b,c))
        if b < (B_size-1) and (-(A[a]+B[b+1]+C[c]),a,b+1,c) not in heap:
            heapq.heappush(heap,(-(A[a]+B[b+1]+C[c]),a,b+1,c))
        if c < (C_size-1) and (-(A[a]+B[b]+C[c+1]),a,b,c+1) not in heap:
            heapq.heappush(heap,(-(A[a]+B[b]+C[c+1]),a,b,c+1))
        print(-val)
if __name__ == "__main__" :
    main()

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)
ans = []
BC_ans = []
for eb in B:
    for ec in C:
        BC_ans.append(eb+ec)
BC_ans.sort(reverse=True)
BC_ans = BC_ans[:K]
for ea in A[:K]:
    for ebc in BC_ans:
        ans.append(ea+ebc)
ans.sort(reverse=True)
for i in range(K):
    print(ans[i])

Solution (Python):
from heapq import heappop, heappush

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

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

hq = []
heappush(hq, (-(A[0] + B[0] + C[0]), 0, 0, 0))
for _ in range(K):
    ans, i, j, k = heappop(hq)
    print(-ans)
 
    if i+1 < X:
        t = (-(A[i+1] + B[j] + C[k]), i+1, j, k)
        if t not in hq:
            heappush(hq, t)
 
    if j+1 < Y:
        t = (-(A[i] + B[j+1] + C[k]), i, j+1, k)
        if t not in hq:
            heappush(hq, t)
    
    if k+1 < Z:
        t = (-(A[i] + B[j] + C[k+1]), i, j, k+1)
        if t not in hq:
            heappush(hq, t)

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

a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

li=[]
heapq.heappush(li,(-a[0]-b[0]-c[0],0,0,0))

ans=[]
used=set()

for _ in range(k):
    value,i,j,l=heapq.heappop(li)
    ans.append(-value)
    
    if i<x-1 and (i+1,j,l) not in used:
        heapq.heappush(li,(-a[i+1]-b[j]-c[l],i+1,j,l))
        used.add((i+1,j,l))
    if j<y-1 and (i,j+1,l) not in used:
        heapq.heappush(li,(-a[i]-b[j+1]-c[l],i,j+1,l))
        used.add((i,j+1,l))
    if l<z-1 and (i,j,l+1) not in used:
        heapq.heappush(li,(-a[i]-b[j]-c[l+1],i,j,l+1))
        used.add((i,j,l+1))
        
for i in ans:
    print(i)

Solution (Python):
import heapq
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()]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

li=[]
push=heapq.heappush
pop=heapq.heappop

push(li,(-a[0]-b[0]-c[0],0,0,0))
ans=[]
used=set()

for _ in range(k):
    value,i,j,l=pop(li)
    ans.append(-value)
    if i<x-1 and (i+1,j,l) not in used:
        push(li,(-a[i+1]-b[j]-c[l],i+1,j,l))
        used.add((i+1,j,l))
    if j<y-1 and (i,j+1,l) not in used:
        push(li,(-a[i]-b[j+1]-c[l],i,j+1,l))
        used.add((i,j+1,l))
    if l<z-1 and (i,j,l+1) not in used:
        push(li,(-a[i]-b[j]-c[l+1],i,j,l+1))
        used.add((i,j,l+1))
for i in ans:
    print(i)

Solution (Python):
from heapq import heappush, heappop


X,Y,Z,K = map(int, input().split())
a_list = [int(i) for i in input().split()]
b_list = [int(i) for i in input().split()]
c_list = [int(i) for i in input().split()]
a_list.sort(reverse = True)
b_list.sort(reverse = True)
c_list.sort(reverse = True)


hq = []
arg_hash = {}
val = a_list[0] + b_list[0] + c_list[0]
heappush(hq, (-val,0,0,0))

for i in range(K):
    ans, a, b, c = heappop(hq)
    print(-ans)

    arg_a = (a+1, b, c)
    arg_b = (a, b+1, c)
    arg_c = (a, b, c+1)

    if a < X-1 and arg_a not in arg_hash:
        arg_hash[arg_a] = 1
        heappush(hq, (-(a_list[a+1]+b_list[b]+c_list[c]),)+arg_a)
    if b < Y-1 and arg_b not in arg_hash:
        arg_hash[arg_b] = 1
        heappush(hq, (-(a_list[a]+b_list[b+1]+c_list[c]),)+arg_b)
    if c < Z-1 and arg_c not in arg_hash:
        arg_hash[arg_c] = 1
        heappush(hq, (-(a_list[a]+b_list[b]+c_list[c+1]),)+arg_c)

Solution (Python):
a,b,c,k = map(int,input().split())
x = list(map(int,input().split()))
x.sort(reverse=True)
y = list(map(int,input().split()))
y.sort(reverse=True)
z = list(map(int,input().split()))
z.sort(reverse=True)
xy = []
for xx in x:
  for yy in y:
    xy.append(xx+yy)
xy.sort(reverse=True)
xy = xy[:k]
xyz = []
for yx in xy:
  for zz in z:
    xyz.append(yx+zz)
xyz.sort(reverse=True)
for i in range(k):
  print(xyz[i])

Solution (Python):
from heapq import nlargest

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 = nlargest(k, (a + b for a in A for b in B))
ABC = nlargest(k, (ab + c for ab in AB for c in C))
[print(abc) for abc in ABC]

Solution (Python):
import heapq


X,Y,Z,K = map(int,input().split())

Alist = list( map(int, input().split()))
Blist = list(map(int, input().split()))
Clist = list( map(int, input().split()))
Alist.sort(reverse=True)
Blist.sort(reverse=True)
Clist.sort(reverse=True)

sum =[]
heapq.heapify(sum)

arg_hash = {}
heapq.heappush(sum,((Alist[0]+Blist[0]+Clist[0])*(-1),(0,0,0)))

for i in range(0,K):
    ans,(a,b,c) = heapq.heappop(sum)
    print(-ans)

    arg_a = a+1,b,c
    arg_b = a,b+1,c
    arg_c = a,b,c+1

    if a < X-1 and arg_a not in arg_hash:
        heapq.heappush(sum,((Alist[a+1]+Blist[b]+Clist[c])*(-1),arg_a))
        arg_hash[arg_a]=1
    if b < Y-1 and arg_b not in arg_hash:
        heapq.heappush(sum,((Alist[a]+Blist[b+1]+Clist[c])*(-1),arg_b))
        arg_hash[arg_b]=1
    if c < Z - 1 and arg_c not in arg_hash:
        heapq.heappush(sum, ((Alist[a] + Blist[b] + Clist[c+1]) * (-1), arg_c))
        arg_hash[arg_c] = 1

Solution (Python):
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()]
 
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
 
cakes = []
for x, a in enumerate(A):
    if x+1 > k: break
        
    for y, b in enumerate(B):
        if (x+1)*(y+1) > k: break
            
        for z, c in enumerate(C):
            if (x+1)*(y+1)*(z+1) > k: break
                
            cakes.append(a+b+c)
    
cakes.sort(reverse=True)      
for cake in cakes[:k]:
    print(cake)

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

AB=[]
for i in A:
  for j in B:
    heapq.heappush(AB,-i-j)


ABC=[]
for i in range(min(K,X*Y)):
  temp=heapq.heappop(AB)
  for j in C:
    heapq.heappush(ABC,temp-j)

for i in range(min(K,X*Y*Z)):
  print(-heapq.heappop(ABC))

Solution (Python):

x,y,z,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse = True)
a = a[:k]
b = list(map(int,input().split()))
b.sort(reverse = True)
b = b[:k]
c = list(map(int,input().split()))
c.sort(reverse = True)
c = c[:k]
ab = []
for l in a:
  for m in b:
    ab.append(l+m)
ab.sort(reverse = True)
ab = ab[:k]
abc = []
for n in ab:
  for o in c:
    abc.append(n+o)
abc.sort(reverse = True)
abc = abc[:k]
for p in abc:
  print(p)

Solution (Python):
from heapq import heappush, heappop

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

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
    
hq = []
val = A[0]+B[0]+C[0]
heappush(hq, (-val,0,0,0))
done = [[0,0,0]]

for i in range(k):
    ans, a, b, c = heappop(hq)
    print(-ans)
    
    if [a+1, b, c] not in done and a+1 < x:
        heappush(hq, (-(A[a+1]+B[b]+C[c]), a+1, b, c))
        done.append([a+1, b, c])
    if [a, b+1, c] not in done and b+1 < y:
        heappush(hq, (-(A[a]+B[b+1]+C[c]), a, b+1, c))
        done.append([a, b+1, c])
    if [a, b, c+1] not in done and c+1 < z:
        heappush(hq, (-(A[a]+B[b]+C[c+1]), a, b, c+1))
        done.append([a, b, c+1])

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

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

x = 0
y = 0
z = 0
cakes = []
for x, a in enumerate(A):
    if x+1 > k: break
        
    for y, b in enumerate(B):
        if (x+1)*(y+1) > k: break
            
        for z, c in enumerate(C):
            if (x+1)*(y+1)*(z+1) > k: break
                
            cakes.append(a+b+c)
    
cakes.sort(reverse=True)      
for cake in cakes[:k]:
    print(cake)

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

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

x = 0
y = 0
z = 0
cakes = []
for x, a in enumerate(A):
    for y, b in enumerate(B):
        for z, c in enumerate(C):
            
            if (x+1)*(y+1)*(z+1) > k:
                break
                
            cakes.append(a+b+c)
    
cakes.sort(reverse=True)      
for cake in cakes[:k]:
    print(cake)

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)

p = []
for i in range(x):
  if i <= k:
    for j in range(y):
      if (i+1) * (j+1) <= k:
        for l in range(z):
          if (i+1) * (j+1) * (l+1) <= k:
            p.append(a[i]+b[j]+c[l])
          else:
            break
      else:
        break
  else:
    break

p.sort(reverse=True)

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

Solution (Python):
from bisect import bisect_left as bl
from collections import deque as dq
X, Y, Z, K = map(int, input().split())
ABC = [sorted(list(map(int, input().split()))) for _ in range(3)]
AB = []
for i in range(X):
  for j in range(Y):
    AB.append(ABC[0][i] + ABC[1][j])
AB.sort(reverse = True)
res = []
for i in range(min(K, X * Y)):
  for j in range(Z):
    res.append(AB[i] + ABC[2][j])
res.sort(reverse = True)
for i in range(K):
  print(res[i])

Solution (Python):
from heapq import heappop, heappush

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

a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

hq = []
heappush(hq, (-(a[0]+b[0]+c[0]), 0, 0, 0))
for _ in range(K):
    ans, i, j, k = heappop(hq)
    print(-ans)

    if i+1 < X:
        t = (-(a[i+1]+b[j]+c[k]), i+1, j, k)
        if t not in hq:
            heappush(hq, t)

    if j+1 < Y:
        t = (-(a[i]+b[j+1]+c[k]), i, j+1, k)
        if t not in hq:
            heappush(hq, t)
    
    if k+1 < Z:
        t = (-(a[i]+b[j]+c[k+1]), i, j, k+1)
        if t not in hq:
            heappush(hq, t)

Solution (Python):
from heapq import heappop, heappush

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

a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

hq = []
heappush(hq, (-(a[0]+b[0]+c[0]), 0, 0, 0))
for _ in range(K):
    ans, i, j, k = heappop(hq)
    print(-ans)

    if i+1 < X:
        t = (-(a[i+1]+b[j]+c[k]), i+1, j, k)
        if t not in hq:
            heappush(hq, t)

    if j+1 < Y:
        t = (-(a[i]+b[j+1]+c[k]), i, j+1, k)
        if t not in hq:
            heappush(hq, t)
    
    if k+1 < Z:
        t = (-(a[i]+b[j]+c[k+1]), i, j, k+1)
        if t not in hq:
            heappush(hq, t)

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)
q = []
l0 = [[0,0,0]]
heapq.heappush(q,[(-1)*(a[0]+b[0]+c[0]),[0,0,0]])
for i in range(k):
    l = heapq.heappop(q)
    print((l[0])*(-1))
    i,j,s = l[1]
    if i < x-1:
        if [i+1,j,s] not in l0:
            heapq.heappush(q,[(-1)*(a[i+1]+b[j]+c[s]),[i+1,j,s]])
            l0.append([i+1,j,s])
    if j < y-1:
        if [i,j+1,s] not in l0:
            heapq.heappush(q,[(-1)*(a[i]+b[j+1]+c[s]),[i,j+1,s]])
            l0.append([i,j+1,s])
    if s < z-1:
        if [i,j,s+1] not in l0:
            heapq.heappush(q,[(-1)*(a[i]+b[j]+c[s+1]),[i,j,s+1]])
            l0.append([i,j,s+1])

Solution (Python):
def slove():
    import sys
    import heapq
    import collections
    input = sys.stdin.readline
    x, y, z, k = list(map(int, input().rstrip('\n').split()))
    a = list(map(int, input().rstrip('\n').split()))
    a.sort(reverse=True)
    b = list(map(int, input().rstrip('\n').split()))
    b.sort(reverse=True)
    c = list(map(int, input().rstrip('\n').split()))
    c.sort(reverse=True)
    q = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
    heapq.heapify(q)
    fq = collections.defaultdict(list)
    fq[(-(a[0] + b[0] + c[0]), 0, 0, 0), 1] = 1
    t = 0
    for _ in range(k):
        if len(q) != 0:
            p = heapq.heappop(q)
            t = p[0]
            aq = p[1]
            bq = p[2]
            cq = p[3]
            if aq + 1 < len(a):
                if (-(a[aq+1] + b[bq] + c[cq]), aq+1, bq, cq) not in fq:
                    heapq.heappush(q, [-(a[aq+1] + b[bq] + c[cq]), aq+1, bq, cq])
                    fq[(-(a[aq+1] + b[bq] + c[cq]), aq+1, bq, cq)] = 1
            if bq + 1 < len(b):
                if (-(a[aq] + b[bq+1] + c[cq]), aq, bq+1, cq) not in fq:
                    heapq.heappush(q, [-(a[aq] + b[bq+1] + c[cq]), aq, bq+1, cq])
                    fq[(-(a[aq] + b[bq+1] + c[cq]), aq, bq+1, cq)] = 1
            if cq + 1 < len(c):
                if (-(a[aq] + b[bq] + c[cq+1]), aq, bq, cq+1) not in fq:
                    heapq.heappush(q, [-(a[aq] + b[bq] + c[cq+1]), aq, bq, cq+1])
                    fq[(-(a[aq] + b[bq] + c[cq+1]), aq, bq, cq+1)] = 1
        print(-t)


if __name__ == '__main__':
    slove()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

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

ans1=[]
ans2=[]

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

if len(ans1)>=k:
    del ans1[k:]
    
for i in range(len(ans1)):
    for j in range(z):
        ans2.append(ans1[i]+c[j])
ans2.sort()
ans2.reverse()

for i in range(k):
    print(ans2[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()))

ans1=[]
ans2=[]

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

if len(ans1)>=k:
    del ans1[k:]
    
for i in range(len(ans1)):
    for j in range(z):
        ans2.append(ans1[i]+c[j])
ans2.sort()
ans2.reverse()

for i in range(k):
    print(ans2[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()))

tmpList = []
for i in range(X):
    for j in range(Y):
        tmpList.append(a[i] + b[j])

tmpList.sort(reverse=True)
ansLst = []
for i in range(min(K, len(tmpList))):
    for j in c:
        ansLst.append(tmpList[i] + j)

ansLst.sort(reverse=True)

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

Solution (Python):
import heapq
x, y, z, w = 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)

q = []
ans = []
flag = []
first = [A[0] + B[0] + C[0], [0, 0, 0]]
heapq.heappush(q, first)
flag.append(first[1])
for h in range(w):
    max_ans = heapq.heappop(q)
    ans.append(abs(max_ans[0]))
    i, j, k = max_ans[1]

    if i == x-1:
        lst1 =  [-A[i]-B[j]-C[k], [i, j, k]]
    else:
        lst1 =  [-A[i+1]-B[j]-C[k], [i+1, j, k]]
    if lst1[1] not in flag:
        heapq.heappush(q, lst1)
        flag.append(lst1[1])

    if j == y-1:
        lst2 = [-A[i]-B[j]-C[k], [i, j, k]]
    else:
        lst2 = [-A[i]-B[j+1]-C[k], [i, j+1, k]]
    if lst2[1] not in flag: 
        heapq.heappush(q, lst2)
        flag.append(lst2[1])

    if k == z-1:
        lst3 = [-A[i]-B[j]-C[k], [i, j, k]]
    else:
        lst3 = [-A[i]-B[j]-C[k+1], [i, j, k+1]]
    if lst3[1] not in flag:
        heapq.heappush(q, lst3)
        flag.append(lst3[1])
    
    print(ans[h])

Solution (Python):
from heapq import heappop,heappush
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)

q = []
heappush(q,(-(A[0]+B[0]+C[0]),0,0,0))
for i in range(K):
    ans, i,j,k = heappop(q)
    print(-ans)
    if i+1 < X:
        t = (-(A[i+1]+B[j]+C[k]),i+1,j,k)
        if t not in q:
            heappush(q,t)
    if j+1 < Y:
        t = (-(A[i]+B[j+1]+C[k]),i,j+1,k)
        if t not in q:
            heappush(q,t)
    if k+1 < Z:
        t = (-(A[i]+B[j]+C[k+1]),i,j,k+1)
        if t not in q:
            heappush(q,t)

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

D = []

for i in range(min(k, x)):
    for j in range(min(k, y)):
        for l in range(min(k, z)):
            if i*j*l <= k:
                D.append(A[i] + B[j] + C[l])
            else:
                break

D.sort(reverse=1)




for i in range(k):
    
    print(D[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=1)
B.sort(reverse=1)
C.sort(reverse=1)






def d4(p):
    co = 0
    for i in range(x):
        if A[i] + B[0] + C[0] < p:
            break
        else:
            for j in range(y):
                if A[i] + B[j] + C[0] < p:
                    break
                else:
                    for l in range(z):
                        
                        
                        
                        if A[i] + B[j] + C[l] < p:
                            break
                        else:
                            
                            co += 1
                            if co == k:
                                return True
    return False

ri = A[0]+B[0]+C[0] + 1
le = 0
mid = (le + ri) // 2



while ri - le > 1:
    
    
    d4bool = d4(mid)
    if d4bool:
        le = mid
    else:
        ri = mid
    mid = (le + ri) // 2
    

D = []

p = le
for i in range(x):
    if A[i] + B[0] + C[0] < p:
        break
    else:
        for j in range(y):
            if A[i] + B[j] + C[0] < p:
                break
            else:
                for l in range(z):
                    if A[i] + B[j] + C[l] < p:
                        break
                    else:
                        D.append(A[i] + B[j] + C[l])


D.sort(reverse=1)



while len(D) < k:
    D.append(le)



for i in range(k):
    
    print(D[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=1)
B.sort(reverse=1)
C.sort(reverse=1)

D = []

for i in range(min(k, x)):
    for j in range(min(k, y)):
        for l in range(min(k, z)):
            if i*j*l <= k:
                D.append(A[i] + B[j] + C[l])
            else:
                break

D.sort(reverse=1)

for i in range(k):
    print(D[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=1)
B.sort(reverse=1)
C.sort(reverse=1)

D = [0] * (min(k, x) * min(k, y))

for i in range(min(k, x)):
    for j in range(min(k, y)):
        D.append(A[i] + B[j])

D.sort(reverse=1)
E = [0] * (min(k, len(D)) * min(k, z))

for i in range(min(k, len(D))):
    for j in range(min(k, z)):
        E.append(D[i] + C[j])

E.sort(reverse=1)

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

Solution (Python):
from heapq import heappop, heappush
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()]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
memo = []
Q = []
Q.append((-(A[0]+B[0]+C[0]),0,0,0))
memo.append((0,0,0))
count = 1

while count <= K:
  p,i,j,k = heappop(Q)
  print(-p)
  count += 1
  if (i+1,j,k) not in memo and i+1<X:
    heappush(Q,(-(A[i+1]+B[j]+C[k]),i+1,j,k))
    memo.append((i+1,j,k))
  if (i,j+1,k) not in memo and j+1<Y:
    heappush(Q,(-(A[i]+B[j+1]+C[k]),i,j+1,k))
    memo.append((i,j+1,k))
  if (i,j,k+1) not in memo and k+1<Z:
    heappush(Q,(-(A[i]+B[j]+C[k+1]),i,j,k+1))
    memo.append((i,j,k+1))

Solution (Python):
import itertools
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

X,Y,Z,K = IL()
a = IL()
b = IL()
c = IL()

ab = []
for i in itertools.product(a,b):
  ab.append(sum(i))
ab.sort(reverse=True)

ans = []
for i in itertools.product(ab[:3000],c):
  ans.append(sum(i))
ans.sort(reverse=True)

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

Solution (Python):
from heapq import heappush, heappop

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()))
a_list.sort(reverse=True)
b_list.sort(reverse=True)
c_list.sort(reverse=True)
hp = []
checked_dict = {}
max_abc = a_list[0] + b_list[0] + c_list[0]
heappush(hp, (-max_abc,0,0,0))
for _ in range(K):
  ans, ai, bi, ci = heappop(hp)
  print(-ans)
  if ai < X-1 and not (ai+1, bi, ci) in checked_dict:
    checked_dict[(ai+1,bi,ci)] = 1
    heappush(hp, (-(a_list[ai+1]+b_list[bi]+c_list[ci]),ai+1,bi,ci))
  if bi < Y-1 and not (ai,bi+1,ci) in checked_dict:
    checked_dict[(ai,bi+1,ci)] = 1
    heappush(hp, (-(a_list[ai]+b_list[bi+1]+c_list[ci]),ai,bi+1,ci))
  if ci < Z-1 and not (ai,bi,ci+1) in checked_dict:
    checked_dict[(ai,bi,ci+1)] = 1
    heappush(hp, (-(a_list[ai]+b_list[bi]+c_list[ci+1]),ai,bi,ci+1))

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 = []
ABC = []

for i in A:
    for j in B:
        AB.append(i+j)

AB.sort(reverse=True)
AB_max = AB[:K]

for j in AB_max:
    for h in C:
        ABC.append(j+h)

ABC.sort(reverse=True)

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

Solution (Python):
X, Y, Z, K = map(int, input().split(" "))
A, B, C = [list(map(int, input().split(" "))) for _ in range(3)]



ab_cakes = []
for a in A:
    for b in B:
        ab_cakes.append(a+b)

ab_cakes = sorted(ab_cakes, reverse=True)[:K]

abc_cakes = []
for ab in ab_cakes:
    for c in C:
        abc_cakes.append(ab + c)

abc_cakes = sorted(abc_cakes, reverse=True)[:K]

for abc in abc_cakes:
    print(abc)

Solution (Python):
import heapq


def main():
    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)

    used = set()
    candidate = []
    heapq.heappush(candidate, (-A[0]-B[0]-C[0], 0, 0, 0))

    for _ in range(K):
        value, x, y, z = heapq.heappop(candidate)
        print(-value)

        if x+1 < X and (x+1, y, z) not in used:
            heapq.heappush(candidate, (-A[x+1]-B[y]-C[z], x+1, y, z))
            used.add((x+1, y, z))

        if y+1 < Y and (x, y+1, z) not in used:
            heapq.heappush(candidate, (-A[x]-B[y+1]-C[z], x, y+1, z))
            used.add((x, y+1, z))

        if z+1 < Z and (x, y, z+1) not in used:
            heapq.heappush(candidate, (-A[x]-B[y]-C[z+1], x , y, z+1))
            used.add((x, y, z+1))


if __name__ == '__main__':
    main()