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

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)

heap = []
heapq.heappush(heap, (-(a_list[0] + b_list[0] + c_list[0]), 0, 0, 0))
searched = []
result = [0] * K

for k in range(K):
    elem = heapq.heappop(heap)
    result[k] = - elem[0]

    if elem[1] + 1 <= X - 1:
        a_next = (-(a_list[elem[1] + 1] + b_list[elem[2]] + c_list[elem[3]]), elem[1] + 1, elem[2], elem[3])
        if not a_next in searched:
            heapq.heappush(heap, a_next)
            searched.append(a_next)

    if elem[2] + 1 <= Y - 1:
        b_next = (-(a_list[elem[1]] + b_list[elem[2] + 1] + c_list[elem[3]]), elem[1], elem[2] + 1, elem[3])
        if not b_next in searched:
            heapq.heappush(heap, b_next)
            searched.append(b_next)

    if elem[3] + 1 <= Z - 1:
        c_next = (-(a_list[elem[1]] + b_list[elem[2]] + c_list[elem[3] + 1]), elem[1], elem[2], elem[3] + 1)
        if not c_next in searched:
            heapq.heappush(heap, c_next)
            searched.append(c_next)

[print(k) for k in result]

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

A.sort()
B.sort()

E = nlargest(K, [a+b for a in A for b in B])
F = nlargest(K, [e+c for e in E for c in C])

print("\n".join(map(str,F)))

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)
L = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1)*(j+1)*(k+1) <= K:
                L.append(A[i]+B[j]+C[k])
            else:
                break
L.sort(reverse=True)
for l in range(K):
    print(L[l])

Solution (Python):
import heapq
x, y, z, k = map(int, input().split())
X = [int(i) for i in input().split()]
Y = [int(i) for i in input().split()]
Z = [int(i) for i in input().split()]
X.sort(reverse=1)
Y.sort(reverse=1)
Z.sort(reverse=1)

Q = []
s = set()
heapq.heappush(Q, (-(X[0]+Y[0]+Z[0]), 0, 0, 0))

def push(i, j, l):
    if (i,j,l) not in s and i<x and j<y and l<y:
        heapq.heappush(Q, (-(X[i]+Y[j]+Z[l]), i, j, l))
        s.add((i, j, l))

for _ in range(k):
    answer, a, b, c = heapq.heappop(Q)
    print(-answer)
    push(a+1, b, c)
    push(a, b+1, c)
    push(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)
abc = []

for i in range(x):
  
  for j in range(y):
   
    for l in range(z):
      if (i+1)*(j+1)*(l+1)>k:
        break
      abc.append(a[i]+b[j]+c[l])
abc.sort(reverse=True)
abc = abc[0:k]
for i in abc:
  print(i)

Solution (Python):
import heapq
import sys
from itertools import product
X, Y, Z, K = [int(i) for i in sys.stdin.readline().split()]
A_ls = [int(i) for i in sys.stdin.readline().split()]
B_ls = [int(i) for i in sys.stdin.readline().split()]
C_ls = [int(i) for i in sys.stdin.readline().split()]
A_ls.sort(reverse=True)
B_ls.sort(reverse=True)
C_ls.sort(reverse=True)
_sum_ls = [(-(A_ls[0] + B_ls[0] + C_ls[0]), 0, 0, 0)]
heapq.heapify(_sum_ls)
for i in range(K):
    score, a, b, c = heapq.heappop(_sum_ls)
    print(- score)
    if len(A_ls) > a+1:
        cand = (- (A_ls[a+1] + B_ls[b] + C_ls[c]), a+1, b, c)
        if not cand in _sum_ls:
            heapq.heappush(_sum_ls, cand)
    if len(B_ls) > b+1:
        cand = (- (A_ls[a] + B_ls[b+ 1] + C_ls[c]), a, b+1, c)
        if not cand in _sum_ls:
            heapq.heappush(_sum_ls, cand)
    if len(C_ls) > c+1:
        cand = (- (A_ls[a] + B_ls[b] + C_ls[c+1]), a, b, c+1)
        if not cand in _sum_ls:
            heapq.heappush(_sum_ls, cand)

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 = []
heapq.heappush(Q, (-A[0]-B[0]-C[0], 0, 0, 0))
for n in range(K):
    ans = heapq.heappop(Q)
    print(-ans[0])
    i, j, k = ans[1], ans[2], ans[3]
    if i < X-1:
        if (- A[i+1] - B[j] - C[k], i+1, j, k) not in Q:
            heapq.heappush(Q, (- A[i+1] - B[j] - C[k], i+1, j, k))
    if j < Y-1:
        if (- A[i] - B[j+1] - C[k], i, j+1, k) not in Q:
            heapq.heappush(Q, (- A[i] - B[j+1] - C[k], i, j+1, k))
    if k < Z-1:
        if (- A[i] - B[j] - C[k+1], i, j, k+1) not in Q:
            heapq.heappush(Q, (- A[i] - B[j] - C[k+1], i, j, k+1))

Solution (Python):
x, y, z, k = map(int, input().split())
X = [int(i) for i in input().split()]
Y = [int(i) for i in input().split()]
Z = [int(i) for i in input().split()]
X.sort(reverse=1)
Y.sort(reverse=1)
Z.sort(reverse=1)

answer = [i+j for i in X[:k] for j in Y[:k]]
answer.sort(reverse=1)

answer2 = [i+j for i in Z[:k] for j in answer[:k]]
answer2.sort(reverse=1)

for p in answer2[:k]:
    print(p)

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

C.sort(reverse=True)
tmp = []
ans = []

for i in range(X):
	for j in range(Y):
		tmp.append(A[i]+B[j])

tmp.sort(reverse=True)
del tmp[min(K, X*Y):]

for i in range(len(tmp)):
	for j in range(min(Z, K)):
		ans.append(tmp[i]+C[j])

ans.sort(reverse=True)

for i in range(min(K, X*Y*Z)):
	print(ans[i])

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)
ab = []
for i in range(x):
  for j in range(y):
    ab.append(a[i]+b[j])
ab.sort(reverse=True)
ab = ab[0:k]
abc = []
for i in ab:
  for j in c:
    abc.append(i+j)
abc.sort(reverse=True)
abc = abc[0:k]
for i in abc:
  print(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)

e = [i + j for i in a[:k] for j in b[:k]]
e.sort(reverse=True)
e = e[:k]
ans = [i + j for i in e for j in c[:k]]
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()))

import itertools
AB = [a + b for a, b in itertools.product(A,B)]
AB.sort(reverse=True)
ABC = [ab + c for ab, c in itertools.product(AB[:3000],C)]
ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])

Solution (Python):
from heapq import heappush, heapreplace
def main():
    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()))
    heap = []
    for i in b:
        for j in c:
            if len(heap) < k:
                heappush(heap, i+j)
            elif heap[0] < i+j:
                heapreplace(heap, i+j)
    ans = []            
    for i in heap:
        for j in a:
            if len(ans) < k:
                heappush(ans, i+j)
            elif ans[0] < i+j:
                heapreplace(ans, i+j)
    ans.sort()
    for val in reversed(ans):
        print(val)    
    return
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(key=lambda x:-x)
b.sort(key=lambda x:-x)
c.sort(key=lambda x:-x)
beauty=[]
index=0
for i in range(x):
    for j in range(y):
        for l in range(z):
            if (i+1)*(j+1)*(l+1)<=k:
                beauty.append(a[i]+b[j]+c[l])
            else:
                break
beauty.sort(key=lambda x:-x)
for i in range(k):
    print(beauty[i])

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)


scores = []
for a in range(len(A)):
    for b in range(len(B)):
        if (a + 1) * (b + 1) > K:
            break
        for c in range(len(C)):
            if (a + 1) * (b + 1) * (c + 1) > K:
                break
            score = A[a] + B[b] + C[c]
            scores.append(score)

scores.sort(reverse=True)

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

Solution (Python):
from itertools import product


X, Y, Z, K = map(int, input().split())
lst_A = list(map(int, input().split()))
lst_B = list(map(int, input().split()))
lst_C = list(map(int, input().split()))
lst_A.sort(reverse=True)
lst_B.sort(reverse=True)
lst_C.sort(reverse=True)

lst_C_K = lst_C[:K]


lst_AB = sorted([a+b for a, b in product(lst_A, lst_B)], reverse=True)
lst_AB_K = lst_AB[:K]

lst_ABC = sorted([ab+c for ab, c in product(lst_AB_K, lst_C_K)], reverse=True)
lst_ABC_K = lst_ABC[:K]

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

Solution (Python):
import itertools

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

D = [a+b for a,b in itertools.product(A[:K], B[:K])]
D.sort(reverse=True)

E = [c+d for c,d in itertools.product(C[:K], D[:K])]
E.sort(reverse=True)

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

Solution (Python):
def d_cake_123_binary_search(X, Y, Z, K, A, B, C):
    
    
    import bisect
    ab = sorted([e1 + e2 for e2 in B for e1 in A])  

    
    accepted, rejected = -1, 10**11
    while abs(accepted - rejected) > 1:
        mid = (accepted + rejected) // 2
        count = sum([len(ab) - bisect.bisect_left(ab, mid - e) for e in C])
        if count >= K:
            accepted = mid
        else:
            rejected = mid

    ans = []
    for e in C:
        idx = bisect.bisect_left(ab, accepted - e)
        for i in range(idx, len(ab)):
            ans.append(e + ab[i])
    ans.sort(reverse=True)
    return '\n'.join(map(str, ans[:K]))

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()]
print(d_cake_123_binary_search(X, Y, Z, K, A, B, C))

Solution (Python):
from heapq import heappush, heappop
s=set()
q=[]
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)
heappush(q, (-(a[0]+b[0]+c[0]), 0, 0, 0))
def push(d, e, f):
    if (d, e, f) not in s and 0<=d<x and 0<=e<y and 0<=f<z:
        heappush(q, (-(a[d]+b[e]+c[f]), d, e, f))
        s.add((d, e, f))
for i in range(k):
    p, d, e, f =heappop(q)
    print(-p)
    push(d+1, e, f)
    push(d, e+1, f)
    push(d, e, f+1)

Solution (Python):
Anum, Bnum, Cnum, 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)

yum = []
for ia in range(Anum):
    if(ia > K):
        break
    for ib in range(Bnum):
        if(ia * ib > K):
            break
        for ic in range(Cnum):
            if(ia * ib * ic > K):
                break
            yum.append(A[ia] + B[ib] + C[ic])
yum.sort(reverse=True)
for k in range(K):
    print(yum[k])

Solution (Python):
Anum, Bnum, Cnum, 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)

yum = []
for ia in range(Anum):
    for ib in range(Bnum):
        if(ia * ib > K):
            break
        for ic in range(Cnum):
            if(ia * ib * ic > K):
                break
            yum.append(A[ia] + B[ib] + C[ic])
yum.sort(reverse=True)
for k in range(K):
    print(yum[k])

Solution (Python):
x, y, z, k = map(int, input().split())
a_array = sorted([int(x) for x in input().split()], reverse=True)
b_array = sorted([int(x) for x in input().split()], reverse=True)
c_array = sorted([int(x) for x in input().split()], reverse=True)


def solver1():
    ab_array = sorted([x + y for x in a_array for y in b_array], reverse=True)[:3000]
    abc_array = sorted([ab + c for ab in ab_array for c in c_array], reverse=True)
    for abc in abc_array[:k]:
        print(abc)


solver1()

Solution (Python):
import sys
from itertools import product


read = sys.stdin.readline
X, Y, Z, K = map(int, read().split())
lst_A = list(map(int, read().split()))
lst_B = list(map(int, read().split()))
lst_C = list(map(int, read().split()))


lst_A.sort(reverse=True)
lst_B.sort(reverse=True)
lst_C.sort(reverse=True)
lst_C_K = lst_C[:K]


lst_AB = sorted([a+b for a, b in product(lst_A, lst_B)], reverse=True)
lst_AB_K = lst_AB[:K]


lst_ABC = sorted([ab+c for ab, c in product(lst_AB_K, lst_C_K)], reverse=True)
lst_ABC_K = lst_ABC[:K]


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

Solution (Python):
import sys
from itertools import product

read = sys.stdin.readline
X, Y, Z, K = map(int, read().split())
lst_A = list(map(int, read().split()))
lst_B = list(map(int, read().split()))
lst_C = list(map(int, read().split()))


lst_A.sort(reverse=True)
lst_B.sort(reverse=True)
lst_C.sort(reverse=True)
lst_C_K = lst_C[:K]


lst_AB = sorted([a+b for a, b in product(lst_A, lst_B)], reverse=True)
lst_AB_K = lst_AB[:K]

lst_ABC = sorted([ab+c for ab, c in product(lst_AB_K, lst_C_K)], reverse=True)
lst_ABC_K = lst_ABC[:K]

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

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)

ABC = []

for i in range(x):
    for j in range(y):
        if (i + 1) * (j + 1)  > k:
            break
        for h in range(z):
            if (i + 1) * (j + 1) * (h + 1) > k:
                break
            ABC.append(A[i] + B[j] + C[h])


ABC.sort(reverse=True)

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

Solution (Python):
import numpy as np
def solve(a, b, c, k):
    x, y, z = len(a), len(b), len(c)
    A = a.reshape(x, 1) + b.reshape(1, y)
    d = np.sort(A.flatten())[::-1][:k]
    B = d.reshape(len(d), 1) + c.reshape(1, z)
    return np.sort(B.flatten())[::-1][:k]

x, y, z, k = map(int, input().split())
a = np.asarray(list(map(int, input().split())))
b = np.asarray(list(map(int, input().split())))
c = np.asarray(list(map(int, input().split())))
print("\n".join(map(str, solve(a, b, c, k))))

Solution (Python):
import sys
readline = sys.stdin.readline
X,Y,Z,K = map(int, readline().split())
A = sorted(list(map(int, readline().split())), reverse=True)
B = sorted(list(map(int, readline().split())), reverse=True)
C = sorted(list(map(int, readline().split())), reverse=True)

def f(thres:int)->bool:
    """return (おいしさが thres 以上の組み合わせ) >= K
    """
    global K
    cnt = 0
    for a in range(X):
        if A[a]+B[0]+C[0]<thres:
            break
        for b in range(Y):
            if A[a]+B[b]+C[0]<thres:
                break
            for c in range(Z):
                if A[a]+B[b]+C[c]<thres:
                    break
                cnt += 1
                if cnt >= K:
                    return True
    return False

def binary_search(f, s:int, t:int)->int:
    """f: function(x:int)->bool
    f(s)=False, f(t)=True，f は広義単調増加
    return : [s, t) での f(x)=True なる最小のx
    """
    ok = s-1
    ng = t
    while ng - ok > 1:
        mid = (ok + ng)//2
        if f(mid): ok = mid
        else:      ng = mid
        

    if ok==s-1:
        raise(ValueError("For all x, f(x)=False"))
    return ok

M = A[0]+B[0]+C[0]
m = A[-1]+B[-1]+C[-1]

thres = binary_search(f, m, M+1)-1

ans = []
for a in range(X):
    if A[a]+B[0]+C[0]<thres:
        break
    for b in range(Y):
        if A[a]+B[b]+C[0]<thres:
            break
        for c in range(Z):
            if A[a]+B[b]+C[c]<thres:
                break
            ans.append(A[a]+B[b]+C[c])

ans.sort(reverse=True)
print("\n".join(map(str, ans[:K])))

Solution (Python):
from itertools import product


X, Y, Z, K = map(int, input().split())
lst_A = list(map(int, input().split()))
lst_B = list(map(int, input().split()))
lst_C = list(map(int, input().split()))
lst_A.sort(reverse=True)
lst_B.sort(reverse=True)
lst_C.sort(reverse=True)
lst_C_K = lst_C[:K]


lst_AB = sorted([a+b for a, b in product(lst_A, lst_B)], reverse=True)
lst_AB_K = lst_AB[:K]

lst_ABC = sorted([ab+c for ab, c in product(lst_AB_K, lst_C_K)], reverse=True)
lst_ABC_K = lst_ABC[:K]

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

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)

ABC = []
for i in range(x):
    for j in range(y):
        if (i + 1) * (j + 1)  <= k:
            for h in range(z):
                if (i + 1) * (j + 1) * (h + 1) <= k:
                    ABC.append(A[i] + B[j] + C[h])
                else:
                    break    
        else:
            break


ABC.sort(reverse=True)

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

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)

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

e=[]
for i in range(min(k,x*y)):
    for j in range(min(k,z)):
        e.append(d[i]+c[j])
e.sort(reverse=True)

for i in range(min(k,x*y*z)):
    print(e[i])

Solution (Python):
import sys
from itertools import product


X, Y, Z, K = map(int, input().split())
lst_A = list(map(int, input().split()))
lst_B = list(map(int, input().split()))
lst_C = list(map(int, input().split()))
lst_A.sort(reverse=True)
lst_B.sort(reverse=True)
lst_C.sort(reverse=True)



lst_C_K = lst_C[:K]


lst_AB = sorted([a+b for a, b in product(lst_A, lst_B)], reverse=True)
lst_AB_K = lst_AB[:K]

lst_ABC = sorted([ab+c for ab, c in product(lst_AB_K, lst_C_K)], reverse=True)
lst_ABC_K = lst_ABC[:K]

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

Solution (Python):




import itertools
import math


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)
arr = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1)*(j+1)*(k+1) > K:
                break
            arr.append(A[i]+B[j]+C[k])

arr.sort(reverse=True)

ans = '\n'.join(map(str, arr[:K]))
print(ans)

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)
abc = []
for i in range(x):
    if (i+1) > k:
        break
    for j in range(y):
        if (i+1)*(j+1) > k:
            break
        for h in range(z):
            if (i+1)*(j+1)*(h+1) >k:
                break
            abc.append(a[i] +b[j] +c[h])
abc.sort(reverse = True)
for i in range(k):
    print(abc[i])

Solution (Python):
from heapq import heappop,heappush
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,b,c = sorted(a),sorted(b),sorted(c)
q,v = [(a[0]+b[0]+c[0],0,0,0)],[(0,0,0)]
for i in range(k):
    p = heappop(q)
    print(-p[0])
    if p[1]+1<x and (p[1]+1,p[2],p[3]) not in v:
        heappush(q,(a[p[1]+1]+b[p[2]]+c[p[3]],p[1]+1,p[2],p[3]))
        v.append((p[1]+1,p[2],p[3]))
    if p[2]+1<y and (p[1],p[2]+1,p[3]) not in v:
        heappush(q,(a[p[1]]+b[p[2]+1]+c[p[3]],p[1],p[2]+1,p[3]))
        v.append((p[1],p[2]+1,p[3]))
    if p[3]+1<z and (p[1],p[2],p[3]+1) not in v:
        heappush(q,(a[p[1]]+b[p[2]]+c[p[3]+1],p[1],p[2],p[3]+1))
        v.append((p[1],p[2],p[3]+1))

Solution (Python):
X, Y, Z, K = map(int, input().split())
*A, = sorted(map(int, input().split()))
*B, = sorted(map(int, input().split()))
*C, = sorted(map(int, input().split()))
ab = [i+j for i in A for j in B]
ab = sorted(ab, reverse=True)[:K]
abc= [i+j for i in ab for j in C]
abc.sort(reverse=True)
for i in abc[:K]:
    print(i)

Solution (Python):
X, Y, Z, K = map(int, input().split())
A = sorted([int(a) for a in input().split()])[::-1]
B = sorted([int(a) for a in input().split()])[::-1]
C = sorted([int(a) for a in input().split()])[::-1]
L = [(0,0,0)]
for k in range(K):
    ma = 0
    for i,j,k in L:
        if A[i]+B[j]+C[k] > ma:
            ma = A[i]+B[j]+C[k]
            mai,maj,mak = i,j,k
    if mai+1<X and (mai+1,maj,mak) not in L: L.append((mai+1,maj,mak))
    if maj+1<Y and (mai,maj+1,mak) not in L: L.append((mai,maj+1,mak))
    if mak+1<Z and (mai,maj,mak+1) not in L: L.append((mai,maj,mak+1))
    print(ma)
    L.remove((mai,maj,mak))

Solution (Python):
X, Y, Z, K = map(int, input().split())
*A, = sorted(map(int, input().split()))
*B, = sorted(map(int, input().split()))
*C, = sorted(map(int, input().split()))
ab = [i+j for i in A for j in B]
ab = sorted(ab, reverse=True)[:K]
abc= [i+j for i in ab for j in C]
abc.sort(reverse=True)
for i in abc[:K]:
    print(i)

Solution (Python):

x,y,z,k = map(int,input().split())
a,b,c = [],[],[]
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()

abc = []

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

abc.sort()

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

Solution (Python):
import heapq as hq

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

pri = []
data = []
hq.heapify(pri)

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

hq.heappush(pri, [-A[0]-B[0]-C[0],0,0,0])
data.append([0,0,0])

for i in range(K):
    s = pri[0][1]
    t = pri[0][2]
    u = pri[0][3]
    if s+1 <= X-1:
        if [s + 1, t, u] not in data:
            data.append([s+1, t, u])
            hq.heappush(pri, [-A[s+1]-B[t]-C[u],s+1,t,u])
    if t+1 <= Y-1:
        if [s, t+1, u] not in data:
            data.append([s, t+1, u])
            hq.heappush(pri, [-A[s]-B[t+1]-C[u],s,t+1,u])
    if u+1 <= Z-1:
        if [s, t, u+1] not in data:
            data.append([s, t, u+1])
            hq.heappush(pri, [-A[s]-B[t]-C[u+1],s,t,u+1])
    print(hq.heappop(pri)[0] * -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()))

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

from heapq import heappop,heappush
from collections import defaultdict
f=defaultdict(int)
pq=[]

def push(a,b,c):
    if f[(a,b,c)]==0 and a<X and b<Y and c<Z:
        heappush(pq,(-(A[a]+B[b]+C[c]),a,b,c))
        f[(a,b,c)]=1

push(0,0,0)

for i in range(K):
    d,a,b,c=heappop(pq)
    print(-d)
    push(a+1,b,c)
    push(a,b+1,c)
    push(a,b,c+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()))

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

from heapq import heappop,heappush
from collections import defaultdict
f=defaultdict(int)
pq=[]

def push(a,b,c):
    if f[(a,b,c)]==0 and a<X and b<Y and c<Z:
        heappush(pq,(-(A[a]+B[b]+C[c]),a,b,c))
        f[(a,b,c)]=1

push(0,0,0)

for i in range(K):
    d,a,b,c=heappop(pq)
    print(-d)
    push(a+1,b,c)
    push(a,b+1,c)
    push(a,b,c+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()))
A.sort()
B.sort()
C.sort()

A = A[::-1]
B = B[::-1]
C = C[::-1]

anslist = []

for i in range(len(A)):
    if (i + 1) > k:
        break
    for j in range(len(B)):
        if (i + 1)  * (j + 1) > k:
            break
        for l in range(len(C)):
            if (i + 1) * (j + 1) * (l + 1) > k:
                break
            anslist.append(A[i] + B[j] + C[l])

anslist.sort()

anslist = anslist[::-1]

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

A = A[::-1]
B = B[::-1]
C = C[::-1]

anslist = []

for i in range(len(A)):
    if i > k:
        break
    for j in range(len(B)):
        if i * j > k:
            break
        for l in range(len(C)):
            if (i + 1) * (j + 1) * (l + 1) > k:
                break
            anslist.append(A[i] + B[j] + C[l])

anslist.sort()

anslist = anslist[::-1]

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

Solution (Python):
import heapq
X, Y, Z, K = map(int, 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)

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

ans = []
for _ in range(K):
    p, v = heapq.heappop(h)
    i, j, k = v
    ans.append(p)
    if i + 1 < X and j < Y and k < Z and not (-(A[i+1] + B[j] + C[k]), (i+1, j, k)) in h:
        heapq.heappush(h, (-(A[i + 1] + B[j] + C[k]), (i+1, j, k)))
    if i < X and j + 1 < Y and k < Z and not (-(A[i] + B[j+1] + C[k]), (i, j+1, k)) in h:
        heapq.heappush(h, (-(A[i] + B[j+1] + C[k]), (i, j+1, k)))
    if i < X and j < Y and k + 1 < Z and not (-(A[i] + B[j] + C[k+1]), (i, j, k+1)) in h:
        heapq.heappush(h, (-(A[i] + B[j] + C[k+1]), (i, j, k+1)))

for a in ans:
    print(-a)

Solution (Python):
import heapq

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()]
q = []
S = set()
a.sort()
b.sort()
c.sort()

heapq.heappush(q,(a[0]+b[0]+c[0],0,0,0))

for i in range(K):
  p,i,j,k=heapq.heappop(q)
  print(-p)
  if i+1 < X and not((i+1,j,k) in S):
    S.add((i+1,j,k))
    heapq.heappush(q,(a[i+1]+b[j]+c[k],i+1,j,k))
  if j+1 < Y and not((i,j+1,k) in S):
    S.add((i,j+1,k))
    heapq.heappush(q,(a[i]+b[j+1]+c[k],i,j+1,k))
  if k+1 < Z and not((i,j,k+1) in S):
    S.add((i,j,k+1))
    heapq.heappush(q,(a[i]+b[j]+c[k+1],i,j,k+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()))

from itertools import product
D=sorted([a+b for a,b in product(A,B)],reverse=True)
D=D[:K]
E=sorted([d+c for d,c in product(D,C)],reverse=True)
for i in range(K):
    print(E[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()))

from itertools import product
D=sorted([a+b for a,b in product(A,B)],reverse=True)
D=D[:K]
E=sorted([d+c for d,c in product(D,C)],reverse=True)
E=E[:K]
for i in range(K):
    print(E[i])

Solution (Python):

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


X, Y, Z, K = read_ints()
A = read_ints()
B = read_ints()
C = read_ints()

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

ABC = []
for a in range(X):
    if a + 1 > K:
        break
    for b in range(Y):
        if (a + 1) * (b + 1) > K:
            break
        for c in range(Z):
            
            if (a + 1) * (b + 1) * (c + 1) > K:
                break
            ABC.append(A[a] + B[b] + C[c])


ABC.sort(reverse=True)
ABC = ABC[:K]
print(*ABC, sep='\n')