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

Solution (Python):
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ab = []
for a in A:
	for b in B:
		ab += [a + b]

ab3000 = sorted(ab)[::-1][:3000]

abc = []
for ab in ab3000:
	for c in C:
		abc += [ab + c]

abc.sort(reverse=True)

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

Solution (Python):
x,y,z,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))

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


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

ABC=[ab+c for ab in AB[:min(k,x*y)] for c in C]
ABC.sort(reverse=True)

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

Solution (Python):
import sys

def input(): return sys.stdin.readline()[:-1]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def MATINT(h): return [list(map(int, input().split())) for _ in range(h)]
def MATSTR(h): return [input() for _ in range(h)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
inf = float('inf')
mod = 10 ** 9 + 7

def main():
    X, Y, Z, K = MAP()
    A = sorted(LIST(), reverse=True)
    B = sorted(LIST(), reverse=True)
    C = sorted(LIST(), reverse=True)
    tmp = []
    for aa in range(X):
        for bb in range(Y):
            for cc in range(Z):
                if (aa + 1) * (bb + 1) * (cc + 1) <= K:
                    tmp.append(A[aa] + B[bb] + C[cc])
                else:
                    break
    tmp.sort(reverse=True)
    for kk in range(K):
        print(tmp[kk])

if __name__ == '__main__':
    main()

Solution (Python):


x,y,z,k =  list(map(int, input().split() ))

aa = list(map(int, input().split() ))
bb = list(map(int, input().split() ))
cc = list(map(int, input().split() ))

aa.sort(reverse = True)
bb.sort(reverse = True)
cc.sort(reverse = True)


ab = [i + j for i in aa for j in bb]


ab.sort(reverse = True)

max_len = min(k, len(ab))
abc = [i + j for i in ab[:max_len] for j in cc]
abc.sort(reverse = True)

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

Solution (Python):


x,y,z,k =  list(map(int, input().split() ))

aa = list(map(int, input().split() ))
bb = list(map(int, input().split() ))
cc = list(map(int, input().split() ))

aa.sort(reverse = True)
bb.sort(reverse = True)
cc.sort(reverse = True)


ab = []
for i in aa:
	for j in bb:
		ab.append(i + j)




ab.sort(reverse = True)

max_len = min(k, len(ab))
abc = []
for i in ab[:max_len]:
	for j in cc:
		abc.append(i + j)


abc.sort(reverse = True)

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

Solution (Python):


x,y,z,k =  list(map(int, input().split() ))

aa = list(map(int, input().split() ))
bb = list(map(int, input().split() ))
cc = list(map(int, input().split() ))

aa.sort(reverse = True)
bb.sort(reverse = True)
cc.sort(reverse = True)


ab = [i + j for i in aa for j in bb]


ab.sort(reverse = True)

max_len = min(k, len(ab))
abc = [i + j for i in ab[:max_len] for j in cc]
abc.sort(reverse = True)

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

kouho = []

for i in range(x):
    for j in range(y):
        for k in range(z):
            if (i+1)*(j+1)*(k+1) > K:
                break
            kouho.append(a[i]+b[j]+c[k])


kouho.sort(reverse=True)

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

x_pls_y = []

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

xy_pls_z = []

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

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

Solution (Python):
inf = input().split()
X = int(inf[0])
Y = int(inf[1])
Z = int(inf[2])
K = int(inf[3])

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 a in A:
  for b in B:
    ab +=[a+b]
ab.sort(reverse=True)
C.sort(reverse=True)
state = [[ab[0]+C[i],0,i] for i in range(len(C))]
count  = 0
while count<K:
  s = max(state)
  print(s[0])
  count +=1
  x = s[1]
  i = s[2]
  if x<X*Y-1:
    state[i][0] = C[i]+ab[x+1]
    state[i][1] = x+1
  else:
    state[i][0] = -1

Solution (Python):
inf = input().split()
X = int(inf[0])
Y = int(inf[1])
Z = int(inf[2])
K = int(inf[3])

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 a in A:
  for b in B:
    ab +=[a+b]
ab.sort(reverse=True)
C.sort(reverse=True)
state = [[ab[0]+C[i],0,i] for i in range(len(C))]
count  = 0
while count<K:
  s = max(state)
  print(s[0])
  count +=1
  x = s[1]
  i = s[2]
  if x<X*Y-1:
    state[i][0] = C[i]+ab[x+1]
    state[i][1] = x+1
  else:
    state[i][0] = -1

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 = []
hs = {}
val = A[0]+B[0]+C[0]
heappush(q, (-val, 0, 0, 0))
hs[(0, 0, 0)] = 1
for _ in range(K):
    ans, a, b, c = heappop(q)
    t_a = (a+1, b, c)
    t_b = (a, b+1, c)
    t_c = (a, b, c+1)
    print(-ans)
    if a<X-1 and t_a not in hs:
        hs[t_a] = 1
        heappush(q, ((-(A[a+1]+B[b]+C[c]),)+t_a))
    if b<Y-1 and t_b not in hs:
        hs[t_b] = 1
        heappush(q, ((-(A[a]+B[b+1]+C[c]),)+t_b))
    if c<X-1 and t_c not in hs:
        hs[t_c] = 1
        heappush(q, ((-(A[a]+B[b]+C[c+1]),)+t_c))

Solution (Python):
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

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

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

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

Solution (Python):
def solve(string):
    import heapq
    x, y, z, k, *abc = map(int, string.split())
    a, b, c = map(lambda x: sorted(x, reverse=True), [abc[:x], abc[x:x + y], abc[x + y:]])
    hq = []
    heapq.heappush(hq, (-(a[0] + b[0] + c[0]), 0, 0, 0))
    ans = []
    check = set([(0, 0, 0)])
    for _ in range(k):
        m, i, j, k = heapq.heappop(hq)
        ans.append(str(-m))
        if i + 1 < x and (i + 1, j, k) not in check:
            heapq.heappush(hq, (-(a[i + 1] + b[j] + c[k]), i + 1, j, k))
            check.add((i + 1, j, k))
        if j + 1 < y and (i, j + 1, k) not in check:
            heapq.heappush(hq, (-(a[i] + b[j + 1] + c[k]), i, j + 1, k))
            check.add((i, j + 1, k))
        if k + 1 < z and (i, j, k + 1) not in check:
            heapq.heappush(hq, (-(a[i] + b[j] + c[k + 1]), i, j, k + 1))
            check.add((i, j, k + 1))
    return "\n".join(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input() for _ in range(4)])))

Solution (Python):
def solve(string):
    import heapq
    x, y, z, k, *abc = map(int, string.split())
    a, b, c = map(lambda x: sorted(x, reverse=True), [abc[:x], abc[x:x + y], abc[x + y:]])
    hq = []
    heapq.heappush(hq, (-(a[0] + b[0] + c[0]), 0, 0, 0))
    ans = []
    check = set([(0, 0, 0)])
    for _ in range(k):
        m, i, j, k = heapq.heappop(hq)
        ans.append(str(-m))
        if i + 1 < x and (i + 1, j, k) not in check:
            heapq.heappush(hq, (-(a[i + 1] + b[j] + c[k]), i + 1, j, k))
            check.add((i + 1, j, k))
        if j + 1 < y and (i, j + 1, k) not in check:
            heapq.heappush(hq, (-(a[i] + b[j + 1] + c[k]), i, j + 1, k))
            check.add((i, j + 1, k))
        if k + 1 < z and (i, j, k + 1) not in check:
            heapq.heappush(hq, (-(a[i] + b[j] + c[k + 1]), i, j, k + 1))
            check.add((i, j, k + 1))
    return "\n".join(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input() for _ in range(4)])))

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

x, y, z, k = map(int, readline().split())
a = list(map(int, readline().split()))
b = list(map(int, readline().split()))
c = list(map(int, readline().split()))

a.sort(); a.reverse()
b.sort(); b.reverse()
c.sort(); c.reverse()

added = [[0,0,0]]
heap = [[a[0]+b[0]+c[0],0,0,0]]
n = 1

def print_heap(heap):
	m=0
	while m<len(heap):
		m2=m*2+1
		print(heap[m:min(m2,len(heap))])
		m=m2

def upheap(heap,n):
	i = n-1
	while i > 0:
		parent = (i-1)//2
		if heap[i][0] > heap[parent][0]:
			tmp = heap[parent]
			heap[parent] = heap[i]
			heap[i] = tmp
			i = parent
		else:
			break
	
	

def downheap(heap, n):
	if n <= 1:
		heap.pop()
		n -= 1
	else:
		heap[0] = heap.pop()
		n -= 1
		i = 0
		while True:
			child = 2 * i + 1
			if child > n-1:
				break
			if child < n-1 and heap[child][0] < heap[child+1][0]:
				child += 1
			if heap[i][0] < heap[child][0]:
				tmp = heap[child]
				heap[child] = heap[i]
				heap[i] = tmp
				i = child
			else:
				break
	
	
	return n

for _ in range(k):
	
	v, pa, pb, pc = map(int, heap[0])
	print(v)
	n = downheap(heap, n)
	if pa+1 < x and not [pa+1, pb, pc] in added:
		added.append([pa+1, pb, pc])
		n += 1
		heap.append([a[pa+1] + b[pb] + c[pc], pa+1, pb, pc])
		upheap(heap, n)
	if pb+1 < y and not [pa, pb+1, pc] in added:
		added.append([pa, pb+1, pc])
		n += 1
		heap.append([a[pa] + b[pb+1] + c[pc], pa, pb+1, pc])
		upheap(heap, n)
	if pc+1 < z and not [pa, pb, pc+1] in added:
		added.append([pa, pb, pc+1])
		n += 1
		heap.append([a[pa] + b[pb] + c[pc+1], pa, pb, pc+1])
		upheap(heap, n)

Solution (Python):
def my_solve(string):
    x, y, z, k, *abc = map(int, string.split())
    a, b, c = abc[:x], abc[x:x + y], abc[x + y:]
    ab = sorted([_a + _b for _b in b for _a in a])[-k:]
    *abc, = map(str, sorted([_ab + _c for _c in c for _ab in ab], reverse=True)[:k])
    return "\n".join(abc)


def solve(string):
    x, y, z, k, *abc = map(int, string.split())
    a, b, c = map(sorted, [abc[:x], abc[x:x + y], abc[x + y:]])
    ans = []
    for ia in range(1, x + 1):
        for ib in range(1, y + 1):
            for ic in range(1, z + 1):
                if ia * ib * ic <= k:
                    ans.append(a[x - ia] + b[y - ib] + c[z - ic])
                else:
                    break
    return "\n".join(list(map(str, sorted(ans, reverse=True)[:k])))


if __name__ == '__main__':
    print(solve('\n'.join([input() for _ in range(4)])))

Solution (Python):
def my_solve(string):
    x, y, z, k, *abc = map(int, string.split())
    a, b, c = abc[:x], abc[x:x + y], abc[x + y:]
    ab = sorted([_a + _b for _b in b for _a in a])[-k:]
    *abc, = map(str, sorted([_ab + _c for _c in c for _ab in ab], reverse=True)[:k])
    return "\n".join(abc)


def solve(string):
    x, y, z, k, *abc = map(int, string.split())
    a, b, c = map(sorted, [abc[:x], abc[x:x + y], abc[x + y:]])
    ans = []
    for ia in range(1, x + 1):
        for ib in range(1, y + 1):
            for ic in range(1, z + 1):
                if ia * ib * ic <= k:
                    ans.append(a[x - ia] + b[y - ib] + c[z - ic])
                else:
                    break
    return "\n".join(list(map(str, sorted(ans, reverse=True)[:k])))


if __name__ == '__main__':
    print(solve('\n'.join([input() for _ in range(4)])))

Solution (Python):
def my_solve(string):
    x, y, z, k, *abc = map(int, string.split())
    a, b, c = abc[:x], abc[x:x + y], abc[x + y:]
    ab = sorted([_a + _b for _b in b for _a in a])[-k:]
    *abc, = map(str, sorted([_ab + _c for _c in c for _ab in ab], reverse=True)[:k])
    return "\n".join(abc)


def solve(string):
    x, y, z, k, *abc = map(int, string.split())
    a, b, c = map(sorted, [abc[:x], abc[x:x + y], abc[x + y:]])
    ans = []
    for ia in range(1, x + 1):
        for ib in range(1, x + 1):
            for ic in range(1, x + 1):
                if ia * ib * ic <= k:
                    ans.append(a[x - ia] + b[y - ib] + c[z - ic])
                else:
                    break
    return "\n".join(list(map(str, sorted(ans, reverse=True)[:k])))


if __name__ == '__main__':
    print(solve('\n'.join([input() for _ in range(4)])))

Solution (Python):
def solve(string):
    x, y, z, k, *abc = map(int, string.split())
    a = abc[:x]
    b = abc[x:x + y]
    c = abc[x + y:]
    ab = sorted([_a + _b for _b in b for _a in a])[-k:]
    *abc, = map(str, sorted([_ab + _c for _c in c for _ab in ab], reverse=True)[:k])
    return "\n".join(abc)


if __name__ == '__main__':
    print(solve('\n'.join([input() for _ in range(4)])))

Solution (Python):



X,Y,Z,K = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()
A.reverse()
B = list(map(int,input().split()))
B.sort()
B.reverse()
C = list(map(int,input().split()))
C.sort()
C.reverse()
S = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1)*(j+1)*(k+1) <= K:
                S.append(A[i]+B[j]+C[k])
            else:
                break
S.sort()
S.reverse()
for i in range(K):
    print(S[i])

Solution (Python):



X,Y,Z,K = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()
A.reverse()
B = list(map(int,input().split()))
B.sort()
B.reverse()
C = list(map(int,input().split()))
C.sort()
C.reverse()
S = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1)*(j+1)*(k+1) <= K:
                S.append(A[i]+B[j]+C[k])
            else:
                break
S.sort()
S.reverse()
for i in range(K):
    print(S[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)

import heapq

heap = []
heapq.heappush(heap, (-(A[0]+B[0]+C[0]), 0, 0, 0))
added = set([(0, 0, 0)])

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

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)
 
heap = []
heapq.heappush(heap, (-(A[0] + B[0] + C[0]), 0, 0, 0))
added = set([(0, 0, 0)])
for _ in range(K):
    p, i, j, k = heapq.heappop(heap)
    if (i + 1, j, k) not in added and i + 1 < X:
        heapq.heappush(heap, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
        added.add((i + 1, j, k))
    if (i, j + 1, k) not in added and j + 1 < Y:
        heapq.heappush(heap, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
        added.add((i, j + 1, k))
    if (i, j, k + 1) not in added and k + 1 < Z:
        heapq.heappush(heap, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
        added.add((i, j, k + 1))
    print(-p)

Solution (Python):
x, y, z, k = list(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)
delist = []
for a in range(x) :
    for b in range(y) :
        if a*b > k :
            break
        for c in range(z) :
            if a*b*c > k :
                break
            else :
                delist.append(alist[a]+blist[b]+clist[c])
delist.sort(reverse=True)
for n in delist[:k] :
    print(n)

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)

heap = []
heapq.heappush(heap, (-(A[0] + B[0] + C[0]), 0, 0, 0))
added = set([(0, 0, 0)])
for _ in range(K):
    p, i, j, k = heapq.heappop(heap)
    if (i + 1, j, k) not in added and i + 1 < X:
        heapq.heappush(heap, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
        added.add((i + 1, j, k))
    if (i, j + 1, k) not in added and j + 1 < Y:
        heapq.heappush(heap, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
        added.add((i, j + 1, k))
    if (i, j, k + 1) not in added and k + 1 < Z:
        heapq.heappush(heap, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
        added.add((i, j, k + 1))
    print(-p)

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)

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

Solution (Python):
from itertools import product

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

AB = sorted([a + b for a, b in product(A, B)], reverse=True)
ABC = sorted([ab + c for ab, c in product(AB[:max(K, Z)],
                                          sorted(C, reverse=True))],
             reverse=True)
for s in ABC[:K]:
    print(s)

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

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

AB.sort(reverse=True)
C.sort(reverse=True)




ABC = []

if len(AB) < k:
	p = len(AB)
else:
	p = k

if len(C) < k:
	q = len(C)
else:
	q = k

for i in range(p):
	for j in range(q):
		ABC.append(AB[i]+C[j])

ABC.sort(reverse=True)


for i in range(k):
	print(ABC[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()))
ans=[]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
for i in range(min(x,k)):
  for j in range(min(y,k)):
    for l in range(min(z,k)):
      if i*j*l>k:
        break
      else:
        ans.append(a[i]+b[j]+c[l])
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()))
ans=[]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
for i in range(min(x,k)):
  for j in range(min(y,k)):
    if i*j>k:
      break
    for l in range(min(z,k)):
      if i*j*l>k:
        break
      else:
        ans.append(a[i]+b[j]+c[l])
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()))
ans=[]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
for i in range(min(x,k)):
  for j in range(min(y,k)):
    if i*j>k:
      break
    for l in range(min(z,k)):
      if i*j*l>k:
        break
      else:
        ans.append(a[i]+b[j]+c[l])
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()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
D = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if i * j * k > K:
                break
            D.append(A[i] + B[j] + C[k])
D.sort(reverse=True)
for ans in D[:K]:
    print(ans)

Solution (Python):
X,Y,Z,K = map(int,input().split())
A = sorted(list(map(int,input().split())))
B = sorted(list(map(int,input().split())))
C = sorted(list(map(int,input().split())))
A = A[::-1]
B = B[::-1]
C = C[::-1]

ab = []
for i in range(X):
    for j in range(Y):
        ab.append(A[i]+B[j])
ab.sort()
ab=ab[::-1][:K]

ans = []
for k in range(Z):
    for v in ab:
        ans.append(C[k]+v)

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

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

e = []


for va in a:
    for vb in b:
        heapq.heappush(e, -(va+vb))


d = []
ite = min(k, len(e))


for i in range(ite):
    ve = -heapq.heappop(e)
    for vc in c:
        heapq.heappush(d, -(ve+vc))

for i in range(k):
    print(-heapq.heappop(d))

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

e = []

for va in a:
    for vb in b:
        e.append(va+vb)

e.sort(reverse=True)
e = e[:k]

d = []
for ve in e:
    for vc in c:
        heapq.heappush(d, -(ve+vc))

for i in range(k):
    print(-heapq.heappop(d))

Solution (Python):
import heapq

a,b,c,k=map(int,input().split())
arr1=list(map(int,input().split()))
arr1=sorted(arr1,reverse=True)
arr2=list(map(int,input().split()))
arr2=sorted(arr2,reverse=True)
arr3=list(map(int,input().split()))
arr3=sorted(arr3,reverse=True)
heap=[]
heapq.heappush(heap,(-(arr1[0]+arr2[0]+arr3[0]),0,0,0))
s={(0,0,0)}
for _ in range(k):
  v,i,j,k=heapq.heappop(heap)
  if i+1<a and (i+1,j,k) not in s:
    s.add((i+1,j,k))
    heapq.heappush(heap,(-(arr1[i+1]+arr2[j]+arr3[k]),i+1,j,k))
  if j+1<b and (i,j+1,k) not in s:
    s.add((i,j+1,k))
    heapq.heappush(heap,(-(arr1[i]+arr2[j+1]+arr3[k]),i,j+1,k))
  if k+1<c and (i,j,k+1) not in s:
    s.add((i,j,k+1))
    heapq.heappush(heap,(-(arr1[i]+arr2[j]+arr3[k+1]),i,j,k+1))
  print(-v)

Solution (Python):
x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

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

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

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

Solution (Python):
X,Y,Z,K=map(int,input().split())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
z=list(map(int,input().split()))

L=[xx+yy for xx in x for yy in y ]
L=sorted(L,reverse=True)

if K > X*Y:
  LL=[l+zz for l in L for zz in z]
  [print(i) for i in sorted(LL,reverse=True)]   
else:
  LL=[l+zz for l in L[:K] for zz in z]
  LL=sorted(LL,reverse=True)
  [print(ll) for k,ll in zip(range(K),LL)]

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

A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split())) 


A = sorted(A, key = lambda x:-x)
B = sorted(B, key = lambda x:-x)
C = sorted(C, key = lambda x:-x)

AB = [0] * (X * Y)

ABC = []

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

ABC = sorted(ABC, key = lambda x:-x)
for i in range(0, K):
    print(ABC[i])

Solution (Python):
import heapq
X, Y, Z, K = map(int, input().split())
A = sorted(map(int, input().split()))[::-1]
B = sorted(map(int, input().split()))[::-1]
C = sorted(map(int, input().split()))[::-1]
h = []
s = set([(0, 0, 0)])
heapq.heappush(h, (-A[0]-B[0]-C[0], 0, 0, 0))
for _ in range(K):
    ans, i, j, k = heapq.heappop(h)
    print(-ans)
    if i+1 < X and (i+1, j, k) not in s:
        heapq.heappush(h, (-A[i+1]-B[j]-C[k], i+1, j, k))
        s.add((i+1, j, k))
    if j+1 < Y and (i, j+1, k) not in s:
        heapq.heappush(h, (-A[i]-B[j+1]-C[k], i, j+1, k))
        s.add((i, j+1, k))
    if k+1 < Z and (i, j, k+1) not in s:
        heapq.heappush(h, (-A[i]-B[j]-C[k+1], i, j, k+1))
        s.add((i, j, k+1))

Solution (Python):
import sys

input = sys.stdin.readline

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)
tmp_list = [a+b for a in a_list for b in b_list]
tmp_list.sort(reverse=True)
honest_list = [tmp+c for tmp in tmp_list[:k] for c in c_list]
honest_list.sort(reverse=True)
for h in honest_list[:k]:
    print(h)

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()))
e=[]
for ai in a:
    for bi in b:
        e.append(ai+bi)
e.sort(reverse=True)
e=e[:k]
ans=[]
for ci in c:
    for ei in e:
        ans.append(ci+ei)
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

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)

cnt = 0
q = []
arg = (0, 0, 0)
visited = {}
visited[arg] = True
heapq.heappush(q, (-(a[arg[0]] + b[arg[1]] + c[arg[2]]),) + arg)

while cnt < k:
    score, ia, ib, ic = heapq.heappop(q)
    print(-score)
    cnt += 1
    arg_a = (ia + 1, ib, ic)
    arg_b = (ia, ib + 1, ic)
    arg_c = (ia, ib, ic + 1)
    if arg_a[0] <= x - 1 and arg_a not in visited:
        heapq.heappush(
            q, (-(a[arg_a[0]] + b[arg_a[1]] + c[arg_a[2]]),) + arg_a)
        visited[arg_a] = True
    if arg_b[1] <= y - 1 and arg_b not in visited:
        heapq.heappush(
            q, (-(a[arg_b[0]] + b[arg_b[1]] + c[arg_b[2]]),) + arg_b)
        visited[arg_b] = True
    if arg_c[2] <= z - 1 and arg_c not in visited:
        heapq.heappush(
            q, (-(a[arg_c[0]] + b[arg_c[1]] + c[arg_c[2]]),) + arg_c)
        visited[arg_c] = True

Solution (Python):
def get_input():
    X, Y, Z, K = [int(_) for _ in raw_input().split()]
    A = [int(_) for _ in raw_input().split()]
    B = [int(_) for _ in raw_input().split()]
    C = [int(_) for _ in raw_input().split()]
    return X, Y, Z, K, A, B, C

def solve2():
    X, Y, Z, K, A, B, C = get_input()
    A.sort(reverse = True)
    B.sort(reverse = True)
    C.sort(reverse = True)
    sumABC = []
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                if (i + 1) * (j + 1) * (k + 1) > K:
                    break
                sumABC.append(A[i] + B[j] + C[k])
    sumABC.sort(reverse = True)
    for i in range(K):
        print sumABC[i]

if __name__ == '__main__':
    solve2()

Solution (Python):
def get_input():
    X, Y, Z, K = [int(_) for _ in raw_input().split()]
    A = [int(_) for _ in raw_input().split()]
    B = [int(_) for _ in raw_input().split()]
    C = [int(_) for _ in raw_input().split()]
    return X, Y, Z, K, A, B, C

def solve2():
    X, Y, Z, K, A, B, C = get_input()
    A.sort(reverse = True)
    B.sort(reverse = True)
    C.sort(reverse = True)
    sumABC = []
    for i in range(X):
        for j in range(Y):
            if (i + 1) * (j + 1) > K:
                break
            for k in range(Z):
                if (i + 1) * (j + 1) * (k + 1) > K:
                    break
                sumABC.append(A[i] + B[j] + C[k])
    sumABC.sort(reverse = True)
    for i in range(K):
        print sumABC[i]

if __name__ == '__main__':
    solve2()