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 sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True

def main():
    X,Y,Z,K = LMIIS()
    A = LMIIS()
    B = LMIIS()
    C = LMIIS()
    
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    from heapq import heappush,heappop
    from collections import defaultdict
    d = defaultdict(lambda:False)
    h = []
    ai = bi = ci = 0
    heappush(h,(-(A[ai]+B[bi]+C[ci]),ai,bi,ci))
    d[(0,0,0)] = True
    for _ in range(K):
        v,ai,bi,ci = heappop(h)
        print(-v)
        if ai+1 < X and d[(ai+1,bi,ci)] == False:
            heappush(h,(-(A[ai+1]+B[bi]+C[ci]),ai+1,bi,ci))
            d[(ai+1,bi,ci)] = True
        if bi+1 < Y and d[(ai,bi+1,ci)] == False:
            heappush(h,(-(A[ai]+B[bi+1]+C[ci]),ai,bi+1,ci))
            d[(ai,bi+1,ci)] = True
        if ci+1 < Z and d[(ai,bi,ci+1)] == False:
            heappush(h,(-(A[ai]+B[bi]+C[ci+1]),ai,bi,ci+1))
            d[(ai,bi,ci+1)] = True


    
    


if __name__ == '__main__':
    main()

Solution (Python):

import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True

def main():
    X,Y,Z,K = LMIIS()
    A = LMIIS()
    B = LMIIS()
    C = LMIIS()
    
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    ans = []
    for ai in range(X):
        for bi in range(Y):
            if ai * bi > K:
                break
            for ci in range(Z):
                if ai * bi * ci > K:
                    break
                
                ans.append(A[ai]+B[bi]+C[ci])
    ans.sort(reverse=True)
    for i in range(K):
        print(ans[i])


    
    


if __name__ == '__main__':
    main()

Solution (Python):

import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True

def main():
    X,Y,Z,K = LMIIS()
    A = LMIIS()
    B = LMIIS()
    C = LMIIS()
    

    AB = []
    for a in A:
        for b in B:
            AB.append(a+b)
    
    AB.sort(reverse=True)

    ABC = []
    for ab in AB[:K]:
        for c in C:
            ABC.append(ab+c)
    ABC.sort(reverse=True)
    for i in range(K):
        print(ABC[i])

    pass
if __name__ == '__main__':
    main()

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

F_list = []
for i in range(1, X+1):
  if i > K:
    break
  for j in range(1, Y+1):
    if i*j > K:
      break
    for k in range(1, Z+1):
      if i*j*k > K:
        break
      F_list.append(A_list[i-1]+B_list[j-1]+C_list[k-1])

F_list.sort(reverse=True)

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

delicious_list = []
for i in range(x):
    for j in range(min(y,int(k//(i+1)))):
        for l in range(min(z,int(k//((i+1)*(j+1))))):
             delicious_list.append(a[i]+b[j]+c[l])

delicious_list.sort(reverse = True)
for i in range(k):
    print(delicious_list[i])

Solution (Python):
import heapq

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

added = set()
h = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
added.add((0, 0, 0))
for i in range(K):
    cost, ai, bi, ci = heapq.heappop(h)
    print(-cost)

    if ai + 1 < X:
        na = (ai + 1, bi, ci)
        if not na in added:
            heapq.heappush(h, (-(A[ai + 1] + B[bi] + C[ci]),) + na)
            added.add(na)
    if bi + 1 < Y:
        nb = (ai, bi + 1, ci)
        if not nb in added:
            heapq.heappush(h, (-(A[ai] + B[bi + 1] + C[ci]),) + nb)
            added.add(nb)
    if ci + 1 < Z:
        nc = (ai, bi, ci + 1)
        if not nc in added:
            heapq.heappush(h, (-(A[ai] + B[bi] + C[ci + 1]),) + nc)
            added.add(nc)

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)

s = []
for x in range(X) :
    for y in range(Y) :
        s.append(A[x]+B[y])
s.sort(reverse=True)

t = []
for k in range(min(K, len(s))) :
    for z in range(Z) :
        t.append(s[k]+C[z])
        
t.sort(reverse=True)

print("\n".join(map(str, t[:K])))

Solution (Python):
from heapq import heappop, heappush
X, Y, X, 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)
Q = []
heappush(Q,[-1*(A[0]+B[0]+C[0]),(0,0,0)])
memo = [(0,0,0)]
i,j,k = 0,0,0
for i in range(K):
  ans,(i,j,k) = heappop(Q)
  print(-ans)
  if (i+1,j,k) not in memo and i+1 < len(A):
    heappush(Q,[-1*(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 < len(B):
    heappush(Q,[-1*(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 < len(C):
    heappush(Q,[-1*(A[i]+B[j]+C[k+1]),(i,j,k+1)])
    memo.append((i,j,k+1))

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)

pq = [(-a[0]-b[0]-c[0], 0, 0, 0)]
f = set()
for i in range(k):
    s, p, q, r = heappop(pq)
    print(-s)
    
    if p + 1 < x and (p+1, q, r) not in f:
        heappush(pq, (-a[p+1]-b[q]-c[r], p+1, q, r))
        f.add((p+1, q, r))
    if q + 1 < y and (p, q+1, r) not in f:
        heappush(pq, (-a[p]-b[q+1]-c[r], p, q+1, r))
        f.add((p, q+1, r))
    if r + 1 < z and (p, q, r+1) not in f:
        heappush(pq, (-a[p]-b[q]-c[r+1], p, q, r+1))
        f.add((p, q, r+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()))

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

e = []
for i in range(min(len(d), k)):
    for j in range(z):
        e.append(d[i] + c[j])
e.sort(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()))
a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
check_box = [[0, 0, 0, a[0] + b[0] + c[0]]]
for i in range(k):
    check = check_box.pop(0)
    print(check[3])
    if check[0] < x - 1 and [check[0] + 1, check[1], check[2], a[check[0] + 1] + b[check[1]] + c[check[2]]] not in check_box:
        check_box.append([check[0] + 1, check[1], check[2], a[check[0] + 1] + b[check[1]] + c[check[2]]])
    if check[1] < y - 1 and [check[0], check[1] + 1, check[2], a[check[0]] + b[check[1] + 1] + c[check[2]]] not in check_box:
        check_box.append([check[0], check[1] + 1, check[2], a[check[0]] + b[check[1] + 1] + c[check[2]]])
    if check[2] < z - 1 and [check[0], check[1], check[2] + 1, a[check[0]] + b[check[1]] + c[check[2] + 1]] not in check_box:
        check_box.append([check[0], check[1], check[2] + 1, a[check[0]] + b[check[1]] + c[check[2] + 1]])
    check_box.sort(key = lambda x: x[3], reverse = True)

Solution (Python):
import sys
input = sys.stdin.readline

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)

    ind = []
    for i in range(x):
        for j in range(y):
            if (i+1)*(j+1) <= K:
                for k in range(z):
                    if (i+1)*(j+1)*(k+1) <= K:
                        ind.append(A[i]+B[j]+C[k])
                    else:
                        break
            else:
                break
    ind = sorted(ind, reverse=True)
    for i in range(K):
        print(ind[i])

if __name__ == "__main__":
    main()

Solution (Python):
X, Y, Z, K = 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)
Cs.sort(reverse=True)
Fs = []
for i in range(X):
  if (i+1) > K:
    break
  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
      Fs.append(As[i]+Bs[j]+Cs[k])
Fs.sort(reverse=True)
for i in range(K):
  print(Fs[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)

done = set()

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

for _ in range(K):
    p, i, j, k = heapq.heappop(h)
    
    print(-p)

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

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

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

Solution (Python):
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()]
ab = []
for i in range(x):
  for j in range(y):
    tmp = a[i] + b[j]
    ab += [tmp]
ab.sort()
ab = ab[::-1][:k]


ans = []
for i in range(len(ab)):
  for j in range(z):
    tmp = ab[i] + c[j]
    ans += [tmp]
ans.sort()
ans = ans[::-1][:k]
for i in ans:
  print(i)

Solution (Python):
from bisect import bisect_right
import sys
input = sys.stdin.readline


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)

A1 = [A[0]-a for a in A]
B1 = [B[0]-b for b in B]
C1 = [C[0]-c for c in C]

'''
for ia, a in enumerate(A1):
    ib = bisect_right(B1, a)
    ic = bisect_right(C1, a)
    if (ia)*(ib)*(ic) > K:
        break

if ia == X-1:
    A2 = A1
else:
    A2 = A1[:ia+2]
if ib == Y-1:
    B2 = B1
else:
    B2 = B1[:ib+2]
if ic == Z-1:
    C2 = C1
else:
    C2 = C1[:ic+2]
'''
Q = []
for ia, a in enumerate(A1):
    for ib, b in enumerate(B1):
        for ic, c in enumerate(C1):
            if (ia+1) * (ib+1) * (ic+1) > K:
                break 
            Q.append(a+b+c)
Q.sort()
S = A[0] + B[0] + C[0]
for i in range(K):
    print(S-Q[i])

Solution (Python):
from heapq import heappop, heappush,heapify
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)
H = []
heappush(H,[-1*(A[0]+B[0]+C[0]),0,0,0])
count = 1
memo = []
while H and count <= K:
  tmp,i,j,k = heappop(H)
  print(-1*tmp)
  count += 1
  if i+1 < X and ((i+1,j,k) not in memo):
    heappush(H,[-1*(A[i+1]+B[j]+C[k]),i+1,j,k])
    memo.append((i+1,j,k))
  if j+1 < Y and ((i,j+1,k) not in memo):
    heappush(H,[-1*(A[i]+B[j+1]+C[k]),i,j+1,k])
    memo.append((i,j+1,k))
  if k+1 < Z and ((i,j,k+1) not in memo):
    heappush(H,[-1*(A[i]+B[j]+C[k+1]),i,j,k+1])
    memo.append((i,j,k+1))

Solution (Python):
import itertools


x, y, z, k = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
AB = [a + b for (a, b) in list(itertools.product(A, B))]
AB.sort(reverse=True)
ABC = [ab + c for (ab, c) in list(itertools.product(AB[:k], C))]
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())
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)
cand = []

for i in range(len(A)):
    for j in range(len(B)):
        if(i * j > K):
            break
        for k in range(len(C)):
            if(i *  j * k > K):
                break
            heappush(cand, A[i]+B[j]+C[k])
            if(len(cand) > K):
                heappop(cand)

ret = []
for i in range(K):
    ret.append(heappop(cand))

for i in reversed(range(K)):
    print(ret[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 = []
ABC = []
for i in A:
    for j in B:
        AB.append(i+j)
AB.sort(reverse = True)
for i in AB[:K+1]:
    for j in C:
        ABC.append(i+j)
ABC.sort(reverse = True)
for i in range(K):
    print(ABC[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)
ans = []

for p in range(min(k,len(a))):
  for q in range(min(k,len(b))):
    for r in range(min(k,len(c))):
      if((p+1)*(q+1)*(r+1) > k):
        break
      ans.append(a[p] + b[q] + c[r])
      
ans = sorted(ans, 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())), reverse = True)
B = sorted(list(map(int, input().split())), reverse = True)
C = sorted(list(map(int, input().split())), reverse = True)

cakes = []
for i in range(0, min([K, X])):
  for j in range(0, min([K // (i + 1) + 1, Y])):
    for k in range(0, min([K // ((i + 1) * (j + 1)) + 1, Z])):
      cakes.append([i, j, k])
      
values = [A[cake[0]] + B[cake[1]] + C[cake[2]] for cake in cakes]
list.sort(values, reverse = True)

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

Solution (Python):


import heapq

used = []

def getNext(A, B, C, e):
    ret = []
    val = -e[0]
    idx = e[1]
    if idx[0] + 1 < len(A) and [idx[0]+1, idx[1], idx[2]] not in used:
        v = val - A[idx[0]] + A[idx[0]+1]
        ret.append((-v, [idx[0]+1, idx[1], idx[2]]))
        used.append([idx[0]+1, idx[1], idx[2]])
    if idx[1] + 1 < len(B) and [idx[0], idx[1]+1, idx[2]] not in used:
        v = val - B[idx[1]] + B[idx[1]+1]
        ret.append((-v, [idx[0], idx[1]+1, idx[2]]))
        used.append([idx[0], idx[1]+1, idx[2]])
    if idx[2] + 1 < len(C) and [idx[0], idx[1], idx[2]+1] not in used:
        v = val - C[idx[2]] + C[idx[2]+1]
        ret.append((-v, [idx[0], idx[1], idx[2]+1]))
        used.append([idx[0], idx[1], idx[2]+1])

    return ret

def resolve():
    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)
    heap = []
    heapq.heapify(heap)
    count = 0
    e = (-(A[0] + B[0] + C[0]), [0, 0, 0])
    heapq.heappush(heap, e)
    used = [e[1]]
    while count < K:
        val = heapq.heappop(heap)
        print(-val[0])
        count +=1
        next = getNext(A, B, C, val)
        for v in next:
            heapq.heappush(heap, v)


if __name__ == "__main__":
    resolve()

Solution (Python):
import sys
def main():
    input=sys.stdin.readline
    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)

    AB=[]
    append=AB.append
    for i in range(min(K,X)):
        for j in range(min(K,Y)):
            append(A[i]+B[j])
    AB.sort(reverse=True)

    ABC=[]
    appendd=ABC.append
    for k in range(min(K,len(AB))):
        for l in range(min(K,Z)):
            appendd(AB[k]+C[l])
    ABC.sort(reverse=True)

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

if __name__=="__main__":
    main()

Solution (Python):
from heapq import heappop, heappush
from collections import defaultdict

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)

pq = []
heappush(pq, (-(A[0] + B[0] + C[0]), 0, 0, 0))
visited = defaultdict(bool)
visited[(0, 0, 0)] = True

ans = []
for _ in range(K):
    p, i, j, k = heappop(pq)
    ans.append(-p)

    if (i + 1 < X) and (not visited[(i + 1, j, k)]):
        visited[(i + 1, j, k)] = True
        heappush(pq, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
    if (j + 1 < Y) and (not visited[(i, j + 1, k)]):
        visited[(i, j + 1, k)] = True
        heappush(pq, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
    if (k + 1 < Z) and (not visited[(i, j, k + 1)]):
        visited[(i, j, k + 1)] = True
        heappush(pq, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
        
print(*sorted(ans, reverse=True), 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)

ans = []
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:
                ans.append(A[i] + B[j] + C[k])
            else:
                break

print(*sorted(ans, reverse=True)[:K], 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()))

ans1 = []
for i in range(X):
    for j in range(Y):
        ans1.append(A[i] + B[j])
        
ans1 = sorted(ans1, reverse=True)[:K]

ans = []
for i in range(len(ans1)):
    for j in range(Z):
        ans.append(ans1[i] + C[j])
        
print(*sorted(ans, reverse=True)[:K], sep='\n')

Solution (Python):
X,Y,Z,K = list(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)

ma,mb,mc = A[0],B[0],C[0]
sA = [a-ma for a in A] + [-10**15]
sB = [b-mb for b in B] + [-10**15]
sC = [c-mc for c in C] + [-10**15]

ia,ib,ic = 0,0,0
while min(10**7,(X)*(Y)*(Z)) > (ia+1)*(ib+1)*(ic+1):
    m = max(sA[ia+1],sB[ib+1],sC[ic+1])
    if ia < (X-1) and sA[ia+1] == m:
        ia = min(ia+1,X-1)
    if ib < (Y-1) and sB[ib+1] == m:
        ib = min(ib+1,Y-1)
    if ic < (Z-1) and sC[ic+1] == m:
        ic = min(ic+1,Z-1)
ans = []
for a in A[:ia+1]:
    for b in B[:ib+1]:
        for c in C[:ic+1]:
            ans.append(a+b+c)
ans.sort(reverse=True)
for out in ans[:K]:
    print(out)

Solution (Python):
X,Y,Z,K = map(int,input().split())
*A, = map(int,input().split())
*B, = map(int,input().split())
*C, = map(int,input().split())
AB = sorted(a+b for a in A for b in B)[::-1][:K]
ABC = sorted(ab+c for ab in AB for c in C)[::-1][:K]
print(*ABC,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()))
AB = [a + b for a in A for b in B]
AB = sorted(AB, reverse=True)[:K]
ABC = [ab+c for ab in AB for c in C]
ABC = sorted(ABC, reverse=True)[:K]

for abc in ABC:
    print(abc)

Solution (Python):
import heapq

a, b, c, d = map(int, input().split())
A = list(map(int, input().split()))
A = sorted(A, reverse=True)
B = list(map(int, input().split()))
B = sorted(B, reverse=True)
C = list(map(int, input().split()))
C = sorted(C, reverse=True)
ans = []
q_list = []
n_q = []
heapq.heappush(n_q, [-(A[0] + B[0] + C[0]), 0, 0, 0])
for i in range(d):
    x, y, z, v = map(int, heapq.heappop(n_q))
    ans.append(-x)

    if y + 1 < a and [y + 1, z, v] not in q_list:
        heapq.heappush(n_q, [-(A[y + 1] + B[z] + C[v]), y + 1, z, v])
        q_list.append([y + 1, z, v])
    if z + 1 < b and [y, z + 1, v] not in q_list:
        heapq.heappush(n_q, [-(A[y] + B[z + 1] + C[v]), y, z + 1, v])
        q_list.append([y, z + 1, v])
    if v + 1 < c and [y, z, v + 1] not in q_list:
        heapq.heappush(n_q, [-(A[y] + B[z] + C[v + 1]), y, z, v + 1])
        q_list.append([y, z, v + 1])

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

Solution (Python):
X, Y, Z, K = map(int, input().split())
A, B, C = [sorted(list(map(int, input().split())))[:: -1][: K] for _ in range(3)]
D = []
for ai in A:
    for bi in B:
        D.append(ai + bi)

D = sorted(D)[:: -1][: K]

res = []
for di in D:
    for ci in C:
        res.append(di + ci)

res = sorted(res)[:: -1][: K]

for ans in res:
    print(ans)

Solution (Python):
X, Y, Z, K = 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)
Cs.sort(reverse=True)

sumABCs = []
for i in range(1, X+1):
    for j in range(1, Y+1):
        if i*j > K: break
        for k in range(1, Z+1):
            if i*j*k > K: break
            sumABCs.append(As[i-1]+Bs[j-1]+Cs[k-1])

sumABCs.sort(reverse=True)

print('\n'.join(map(str, sumABCs[:K])))

Solution (Python):
def getlist():
	return list(map(int, input().split()))

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

L = []
for i in A:
	for j in B:
		L.append(i + j)

L = sorted(L)
l = []
n = min(len(L), K)
for i in range(n):
	l.append(L[-1 - i])

ans = []
for i in l:
	for j in C:
		ans.append(i + j)

ans = sorted(ans)

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

Solution (Python):
import heapq
x,y,z,k = map(int,input().split())
ax = [int(_) for _ in input().split()]
ay = [int(_) for _ in input().split()]
az = [int(_) for _ in input().split()]
ax.sort(reverse=True)
ay.sort(reverse=True)
az.sort(reverse=True)
INF = - 3 *10 ** 10
ax.extend([INF])
ay.extend([INF])
az.extend([INF])
l = [(-(ax[0]+ay[0]+az[0]),0,0,0)]
checked = {(-(ax[0]+ay[0]+az[0]),0,0,0)}
for i in range(k):
    t = heapq.heappop(l)
    print(-t[0])
    if (-(ax[t[1]+1]+ay[t[2]]+az[t[3]]),t[1]+1,t[2],t[3]) not in checked:
        heapq.heappush(l,(-(ax[t[1]+1]+ay[t[2]]+az[t[3]]),t[1]+1,t[2],t[3])) 
        checked.add((-(ax[t[1]+1]+ay[t[2]]+az[t[3]]),t[1]+1,t[2],t[3]))
    if (-(ax[t[1]]+ay[t[2]+1]+az[t[3]]),t[1],t[2]+1,t[3]) not in checked:
        heapq.heappush(l,(-(ax[t[1]]+ay[t[2]+1]+az[t[3]]),t[1],t[2]+1,t[3]))
        checked.add((-(ax[t[1]]+ay[t[2]+1]+az[t[3]]),t[1],t[2]+1,t[3]))
    if (-(ax[t[1]]+ay[t[2]]+az[t[3]+1]),t[1],t[2],t[3]+1) not in checked:
        heapq.heappush(l,(-(ax[t[1]]+ay[t[2]]+az[t[3]+1]),t[1],t[2],t[3]+1))
        checked.add((-(ax[t[1]]+ay[t[2]]+az[t[3]+1]),t[1],t[2],t[3]+1))

Solution (Python):
a, b, c, d = map(int, input().split())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
cl = list(map(int, input().split()))
al = sorted(al, reverse=True)
bl = sorted(bl, reverse=True)
cl = sorted(cl, reverse=True)
q = [[al[0] + bl[0] + cl[0], 0, 0, 0]]
ans = [[al[0] + bl[0] + cl[0], 0, 0, 0]]
c = -1

    
for i in range(d):
    
    
    s, aq, bq, cq = map(int, q.pop())
    if len(al) > aq+1:
        if [al[aq+1] + bl[bq] + cl[cq], aq+1, bq, cq] not in ans:
            q.append([al[aq+1] + bl[bq] + cl[cq], aq+1, bq, cq])
            ans.append([al[aq+1] + bl[bq] + cl[cq], aq+1, bq, cq])

    if len(bl) > bq+1:
        if [al[aq] + bl[bq+1] + cl[cq], aq, bq+1, cq] not in ans:
            q.append([al[aq] + bl[bq+1] + cl[cq], aq, bq+1, cq])
            ans.append([al[aq] + bl[bq+1] + cl[cq], aq, bq+1, cq])

    if len(cl) > cq+1:
        if [al[aq] + bl[bq] + cl[cq+1], aq, bq, cq+1] not in ans:
            q.append([al[aq] + bl[bq] + cl[cq+1], aq, bq, cq+1])
            ans.append([al[aq] + bl[bq] + cl[cq+1], aq, bq, cq+1])

    q = sorted(q)

    
    
    
    

ans = sorted(ans, reverse=True)
for i in range(d):
    print(ans[i][0])

Solution (Python):
import heapq
a, b, c, d = map(int, input().split())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
cl = list(map(int, input().split()))
al = sorted(al, reverse=True)
bl = sorted(bl, reverse=True)
cl = sorted(cl, reverse=True)
q = []
heapq.heappush(q, [-(al[0] + bl[0] + cl[0]), 0, 0, 0])
ans = []
heapq.heappush(ans, [-(al[0] + bl[0] + cl[0]), 0, 0, 0])


for i in range(d):
    s, aq, bq, cq = heapq.heappop(q)
    if len(al) > aq+1:
        if [-(al[aq+1] + bl[bq] + cl[cq]), aq+1, bq, cq] not in ans:
            heapq.heappush(q, [-(al[aq+1] + bl[bq] + cl[cq]), aq+1, bq, cq])
            heapq.heappush(ans, [-(al[aq+1] + bl[bq] + cl[cq]), aq+1, bq, cq])
            
            

    if len(bl) > bq+1:
        if [-(al[aq] + bl[bq+1] + cl[cq]), aq, bq+1, cq] not in ans:
            heapq.heappush(q, [-(al[aq] + bl[bq+1] + cl[cq]), aq, bq+1, cq])
            heapq.heappush(ans, [-(al[aq] + bl[bq+1] + cl[cq]), aq, bq+1, cq])
            
            

    if len(cl) > cq+1:
        if [-(al[aq] + bl[bq] + cl[cq+1]), aq, bq, cq+1] not in ans:
            heapq.heappush(q, [-(al[aq] + bl[bq] + cl[cq+1]), aq, bq, cq+1])
            heapq.heappush(ans, [-(al[aq] + bl[bq] + cl[cq+1]), aq, bq, cq+1])
            
            

    

    
    
    
    

for i in range(d):
    print(-heapq.heappop(ans)[0])

Solution (Python):
a, b, c, d = map(int, input().split())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
cl = list(map(int, input().split()))
al = sorted(al, reverse=True)
bl = sorted(bl, reverse=True)
cl = sorted(cl, reverse=True)
q = [[al[0] + bl[0] + cl[0], 0, 0, 0]]
ans = [[al[0] + bl[0] + cl[0], 0, 0, 0]]

for i in range(d):
    s, aq, bq, cq = map(int, q.pop())
    if len(al) > aq+1:
        if [al[aq+1] + bl[bq] + cl[cq], aq+1, bq, cq] not in ans:
            q.append([al[aq+1] + bl[bq] + cl[cq], aq+1, bq, cq])
            ans.append([al[aq+1] + bl[bq] + cl[cq], aq+1, bq, cq])

    if len(bl) > bq+1:
        if [al[aq] + bl[bq+1] + cl[cq], aq, bq+1, cq] not in ans:
            q.append([al[aq] + bl[bq+1] + cl[cq], aq, bq+1, cq])
            ans.append([al[aq] + bl[bq+1] + cl[cq], aq, bq+1, cq])

    if len(cl) > cq+1:
        if [al[aq] + bl[bq] + cl[cq+1], aq, bq, cq+1] not in ans:
            q.append([al[aq] + bl[bq] + cl[cq+1], aq, bq, cq+1])
            ans.append([al[aq] + bl[bq] + cl[cq+1], aq, bq, cq+1])

    q = sorted(q)

    
    
    
    

ans = sorted(ans, reverse=True)
for i in range(d):
    print(ans[i][0])

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

ABlist = [i + j for i in Alist[:k] for j in Blist[:k]]

ABlist.sort(reverse=True)

ABClist = [l + m for l in ABlist[:k] for m in Clist[:k]]

ABClist.sort(reverse=True)

print(*ABClist[:k], sep = '\n')

Solution (Python):
X, Y, Z, K = map(int, input().split())
x_list = list(map(int, input().split()))
y_list = list(map(int, input().split()))
z_list = list(map(int, input().split()))

x_list = sorted(x_list, reverse=True)
y_list = sorted(y_list, reverse=True)
z_list = sorted(z_list, reverse=True)

cand = []

import itertools
def add_cand(list1, list2, list3):
  sum2 = []
  for a, b in itertools.product(list1, list2):
    sum2.append(a+b)
  for apb in sorted(sum2, reverse=True)[:K]:
    for c in list3:
      cand.append(apb + c)
      
add_cand(x_list, y_list, z_list)

for v in sorted(cand, reverse=True)[:K]:
  print(v)