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

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

ABC = []
for a in range(X):
    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])
            
for ai in sorted(ABC, reverse=True)[:K]:
    print(ai)

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


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


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

from itertools import product

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

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

ABC = ABC[:K]

print(*ABC, sep='\n')

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)

ab = []
for ai in a:
    for bj in b:
        ab.append(ai+bj)
ab.sort(reverse=True)
ab = ab[:K]
cond = []
for abi in ab:
    for cj in c:
        cond.append(abi+cj)
cond = sorted(cond, reverse=True)[:K]
for condi in cond:
    print(condi)

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


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


X, Y, Z, K = read_ints()
A = list(map(int, read().split()))
B = list(map(int, read().split()))
C = list(map(int, read().split()))

from itertools import product

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

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

ABC = ABC[:K]

print(*ABC, sep='\n')

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

from itertools import product

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

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

ABC = ABC[:K]

print(*ABC, sep='\n')

Solution (Python):
import numpy as np

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

AA, BB = np.meshgrid(A,B)
AB = AA + BB
AB = np.sort(AB.flatten())[::-1][:k]

ABAB, CC = np.meshgrid(AB,C)
ABC = ABAB + CC
ABC = np.sort(ABC.flatten())[::-1][:k]

for abc in ABC:
    print(abc)

Solution (Python):
import itertools
import sys

input = sys.stdin.readline

maxn = 10000000000

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.append(-maxn)
B.append(-maxn)
C.append(-maxn)

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

apos, bpos, cpos = 0, 0, 0

while (apos + 1) * (bpos + 1) * (cpos + 1) < min(2 * 10 ** 6, X * Y * Z):
    if (
        apos < len(A) - 2
        and abs(A[apos] - A[apos + 1]) < abs(B[bpos] - B[bpos + 1])
        and abs(A[apos] - A[apos + 1]) < abs(C[cpos] - C[cpos + 1])
    ):
        apos += 1
    elif bpos < len(B) - 2 and B[bpos] - B[bpos + 1] < C[cpos] - C[cpos + 1]:
        bpos += 1
    else:
        cpos += 1

res = []
for a in list(itertools.product(A[: apos + 1], B[: bpos + 1], C[: cpos + 1])):
    res.append(a[0] + a[1] + a[2])

res.sort(reverse=True)
for k in range(K):
    print(res[k])

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

a = []
for x in range(X):
    for y in range(Y):
        for z in range(Z):
            if ((x + 1) * (y + 1) * (z + 1) <= K):
                a.append(A[x] + B[y] + C[z])
            else:
                break

for b in sorted(a, reverse=True)[:K]:
    print(b)

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 = sorted([a + b for a in A for b in B], reverse=True)[:K]
ABC = sorted([ab + c for ab in AB for c in C], reverse=True)[:K]
for a in ABC:
    print(a)

Solution (Python):
import heapq

first = input()
c1 = input()
c2 = input()
c3 = input()
times = int(first.split(' ')[3])
ca = sorted([int(x) for x in c1.split(' ')], reverse=True)
cb = sorted([int(x) for x in c2.split(' ')], reverse=True)
cc = sorted([int(x) for x in c3.split(' ')], reverse=True)
h = []
ex = set()
i, j, k = 0, 0, 0
s = ca[i] + cb[j] + cc [k]
heapq.heappush(h, (-s, (s, i, j, k)))
ex.add((i, j, k))
for t in range(times):
    m = heapq.heappop(h)
    print(m[1][0])
    i, j, k = m[1][1], m[1][2], m[1][3]
    ex.discard((i, j, k))
    if i+1 < len(ca):
        if (i+1, j, k) not in ex:
            s1 = ca[i+1] + cb[j] + cc[k]
            heapq.heappush(h, (-s1, (s1, i+1, j, k)))
            ex.add((i+1, j, k))
    if j+1 < len(cb):
        if (i, j+1, k) not in ex:
            s2 = ca[i] + cb[j+1] + cc[k]
            heapq.heappush(h, (-s2, (s2, i, j+1, k)))
            ex.add((i, j+1, k))
    if k+1 < len(cc):
        if (i, j, k+1) not in ex:
            s3 = ca[i] + cb[j] + cc[k+1]
            heapq.heappush(h, (-s3, (s3, i, j, k+1)))
            ex.add((i, j, k+1))

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

	solve(a, b, c, x, y, z, k)

def solve(a, b, c, x, y, z, k):
	a.sort(reverse=True)
	b.sort(reverse=True)
	c.sort(reverse=True)
	stack = list()
	for p in range(x):
		for q in range(y):
			if (p+1)*(q+1) > k:
				break
			for r in range(z):
				if (p+1)*(q+1)*(r+1) > k:
					break
				stack.append(a[p]+b[q]+c[r])
	stack.sort(reverse=True)
	for i in range(k):
		print(stack[i])

if __name__ == '__main__':
    main()

Solution (Python):
import heapq
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
B = list(map(int, input().split()))
B.sort(reverse=True)
C = list(map(int, input().split()))
C.sort(reverse=True)
q = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
s = set()
for n in range(K):
    p, a, b, c = heapq.heappop(q)
    print(-p)
    if a+1 < X and ((a+1, b, c) not in s):
        s.add((a+1, b, c))
        heapq.heappush(q, (-(A[a+1] + B[b] + C[c]), a+1, b, c))
    if b+1 < Y and ((a, b+1, c) not in s):
        s.add((a, b+1, c))
        heapq.heappush(q, (-(A[a] + B[b+1] + C[c]), a, b+1, c))
    if c+1 < Z and ((a, b, c+1) not in s):
        s.add((a, b, c+1))
        heapq.heappush(q, (-(A[a] + B[b] + C[c+1]), a, b, c+1))

Solution (Python):
import sys,bisect
input = sys.stdin.readline
def main():
    x,y,z,k = map(int,input().split())
    a = tuple(map(int,input().split()))
    b = tuple(map(int,input().split()))
    c = tuple(map(int,input().split()))
    
    ab = []
    for e in a:
        for w in b:
            ab.append(e+w)
    ab.sort()
    
    ok = -1
    ng = 10**11
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        cnt = 0
        for e in c:
            cnt += len(ab)-bisect.bisect_left(ab,mid-e)
        if cnt >= k:
            ok = mid
        else:
            ng = mid
    
    res = []
    for e in c:
        ind = bisect.bisect_left(ab,ok-e)
        for i in range(ind,len(ab)):
            res.append(e+ab[i])
    res.sort()
    for i in range(1,k+1):
        print(res[-i])
if __name__ == '__main__':
    main()

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)

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

list1 = []
for ab in list:
    for cc in c:
        list1.append(ab+cc)
list1.sort(reverse = True)
list1 = list1[:k]

for i in range(k):
    print(list1[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=[i+j for i in a for j in b]
ab.sort(key=lambda x: -x)
c.sort(key=lambda x: -x)
ans=[]
ct=[0]
ansc=[ab[0]+c[0]]
while (len(ans)<k):
    if(ct[-1]!=0 and len(ct)<z):
        ansc+=[ab[0]+c[len(ct)]]
        ct+=[0]
    ans+=[max(ansc)]
    tmp=ansc.index(max(ansc))
    if(ct[tmp]==len(ab)-1):
        ansc[tmp]=0
    else:
        ct[tmp]+=1
        ansc[tmp]=ab[ct[tmp]]+c[tmp]
for i in ans:
    print(i)

Solution (Python):
I=lambda:map(int,input().split());*_,k=I();s=lambda*a:sorted(i+j for i in I()for j in a)[:~k:-1];print(*s(*s(*I())))

Solution (Python):
I=lambda:map(int,input().split());*_,k=I();s=lambda*a:sorted(i+j for i in I()for j in a)[:~k:-1];print(*s(*s(*I())))

Solution (Python):
x,y,z,k=map(int, input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
c=list(map(int, input().split()))
ab=[]
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort(key=lambda x: -x)
c.sort(key=lambda x: -x)
ans=[]
ct=[0]
ansc=[ab[0]+c[0]]
while (len(ans)<k):
    if(ct[-1]!=0 and len(ct)<z):
        ansc+=[ab[0]+c[len(ct)]]
        ct+=[0]
    ans+=[max(ansc)]
    tmp=ansc.index(max(ansc))
    if(ct[tmp]==len(ab)-1):
        ansc[tmp]=0
    else:
        ct[tmp]+=1
        ansc[tmp]=ab[ct[tmp]]+c[tmp]
for i in ans:
    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()))


C.sort()
C.reverse()
AB =[0]*(X*Y)

for i in range(X):
    for j in range(Y):
        AB[i*Y+j] =  A[i] + B[j]
        
AB.sort()
AB.reverse()
K2 = min(K,X*Y)
AB = AB[0:K2]
ABC = [0] * (K2*Z)
for i in range(K2):
    for j in range(Z):
        ABC[i*Z+j] = AB[i] + C[j]
        
ABC.sort()
ABC.reverse()
ABC = ABC[0:K]
for i in ABC:
    print(i)

Solution (Python):
from heapq import *

an, bn, cn, 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 = []
hp = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
heapify(hp)
sumi = {(0, 0, 0)}
for _ in range(k):
    hpk = heappop(hp)
    mx = hpk[0]
    ai = hpk[1]
    bi = hpk[2]
    ci = hpk[3]
    if ai + 1 < an and (ai + 1, bi, ci) not in sumi:
        heappush(hp, [mx + a[ai] - a[ai + 1], ai + 1, bi, ci])
        sumi.add((ai + 1, bi, ci))
    if bi + 1 < bn and (ai, bi + 1, ci) not in sumi:
        heappush(hp, [mx + b[bi] - b[bi + 1], ai, bi + 1, ci])
        sumi.add((ai, bi + 1, ci))
    if ci + 1 < cn and (ai, bi, ci + 1) not in sumi:
        heappush(hp, [mx + c[ci] - c[ci + 1], ai, bi, ci + 1])
        sumi.add((ai, bi, ci + 1))
    ans += [-mx]
print(*ans, sep="\n")

Solution (Python):
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()))
	d = [0]*x*y
	for i in range(x):
		for j in range(y):
			d[i*y+j] = a[i]+b[j]
	d.sort(reverse=True)
	c.sort(reverse=True)

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


if __name__ == '__main__':
    main()

Solution (Python):
from heapq import *

an, bn, cn, 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)
la = len(a)
lb = len(b)
lc = len(c)

ans = []
hp = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
heapify(hp)
sumi = {(0, 0, 0)}
for _ in range(k):
    hpk = heappop(hp)
    mx = hpk[0]
    ai = hpk[1]
    bi = hpk[2]
    ci = hpk[3]
    if ai + 1 < la and (ai + 1, bi, ci) not in sumi:
        heappush(hp, [mx + a[ai] - a[ai + 1], ai + 1, bi, ci])
        sumi.add((ai + 1, bi, ci))
    if bi + 1 < lb and (ai, bi + 1, ci) not in sumi:
        heappush(hp, [mx + b[bi] - b[bi + 1], ai, bi + 1, ci])
        sumi.add((ai, bi + 1, ci))
    if ci + 1 < lc and (ai, bi, ci + 1) not in sumi:
        heappush(hp, [mx + c[ci] - c[ci + 1], ai, bi, ci + 1])
        sumi.add((ai, bi, ci + 1))
    ans += [-mx]

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

Solution (Python):
I=lambda:map(int,input().split());*_,k=I();s=lambda*a:sorted(i+j for i in I()for j in a)[:-k-1:-1];print(*s(*s(*I())))

Solution (Python):
I=lambda:map(int,input().split());*_,k=I();s=lambda*a:sorted(i+j for i in I()for j in a)[:-k-1:-1];print(*s(*s(*I())))

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(" ")]
top_bc = []

for bi in b:
    for ci in c:
        top_bc.append(bi + ci)

top_bc = sorted(top_bc, reverse=True)

top_ak = []

for ki in top_bc[0:k]:
    for ai in a:
        top_ak.append(ai + ki)

top_ak = sorted(top_ak, reverse=True)

for top_k in top_ak[0:k]:
    print(top_k)

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)

AB = [ a+b for a in A for b in B]
AB = sorted(AB, reverse=True)
AB = AB[:3000]
ABC = [ab+c for ab in AB for c in C]
ABCs = sorted(ABC, reverse=True)[:K]
for i in range(K):
  print(ABCs[i])

Solution (Python):
X,Y,Z,K = list(map(int,input().split()))
A = [(sorted(list(map(int,input().split())))[::-1]) for i in range(3)]
b = sorted([i+j for i in A[0] for j in A[1]])[-K:]
for i in sorted([i+j for i in b for j in A[2]])[-K:][::-1] :
  print(i)

Solution (Python):
from heapq import heappop,heappush
x,y,z,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse=True)
B = list(map(int,input().split()))
B.sort(reverse=True)
C = list(map(int,input().split()))
C.sort(reverse=True)

Q=[]
heappush(Q,(-(A[0]+B[0]+C[0]),0,0,0))
S=set()

for n in range(K):
	p,i,j,k = heappop(Q)
	print(-p)
	
	if i+1 < x and ((i+1,j,k) not in S):
		S.add((i+1,j,k))
		heappush(Q,(-(A[i+1]+B[j]+C[k]),i+1,j,k))
	if j+1 < y and ((i,j+1,k) not in S):
		S.add((i,j+1,k))
		heappush(Q,(-(A[i]+B[j+1]+C[k]),i,j+1,k))
	if k+1 < z and ((i,j,k+1) not in S):
		S.add((i,j,k+1))
		heappush(Q,(-(A[i]+B[j]+C[k+1]),i,j,k+1))

Solution (Python):
from heapq import *

an, bn, cn, 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)
la = len(a)
lb = len(b)
lc = len(c)

ans = []
hp = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
heapify(hp)
sumi = [[0, 0, 0]]
for _ in range(k):
    hpk = heappop(hp)
    mx = hpk[0]
    ai = hpk[1]
    bi = hpk[2]
    ci = hpk[3]
    if ai + 1 < la and [ai + 1, bi, ci] not in sumi:
        heappush(hp, [mx + a[ai] - a[ai + 1], ai + 1, bi, ci])
        sumi += [[ai + 1, bi, ci]]
    if bi + 1 < lb and [ai, bi + 1, ci] not in sumi:
        heappush(hp, [mx + b[bi] - b[bi + 1], ai, bi + 1, ci])
        sumi += [[ai, bi + 1, ci]]
    if ci + 1 < lc and [ai, bi, ci + 1] not in sumi:
        heappush(hp, [mx + c[ci] - c[ci + 1], ai, bi, ci + 1])
        sumi += [[ai, bi, ci + 1]]
    ans += [-mx]

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

Solution (Python):
import sys
from heapq import heappush, heappop

def main():
    input = sys.stdin.readline
    X, Y, Z, K = map(int, input().split())
    A = sorted(list(map(int, input().split())), key=lambda x: -x)
    B = sorted(list(map(int, input().split())), key=lambda x: -x)
    C = sorted(list(map(int, input().split())), key=lambda x: -x)

    deli = []
    heappush(deli, (-A[0]-B[0]-C[0], 0, 0, 0))
    pushed = set((0,0,0))
    for _ in range(K):
        d = heappop(deli)
        i, j, k = d[1], d[2], d[3]

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

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

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

        print(-d[0])


if __name__ == '__main__':
    main()

Solution (Python):
x,y,z,k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
res = []
for p in range(x):
	for q in range(y):
		if (p+1)*(q+1) > k:
			break
		for r in range(z):
			if (p+1)*(q+1)*(r+1) > k:
				break
			res.append(A[p]+B[q]+C[r])
res.sort(reverse=True)
for i in res[:k]:
	print(i)

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



ab = sorted(a+b for a in aa for b in bb)[:-k-1:-1]
abc = sorted(s+c for s in ab[:k] for c in cc)[:-k-1:-1]
print(*abc, sep='\n')

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

ab = sorted(a+b for a in aa for b in bb)[:-k-1:-1]
abc = sorted(s+c for s in ab[:k] for c in cc)[:-k-1:-1]
print(*abc, sep='\n')

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

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

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

for abc in ABC:
    print(abc)

Solution (Python):
from heapq import nlargest
x,y,z,k=map(int,input().split())
A,B,C=[list(map(int,input().split()))for _ in[0]*3]
D=nlargest(k,(a+b for a in A for b in B))
E=nlargest(k,(d+c for d in D for c in C))
print('\n'.join(map(str,E)))

Solution (Python):



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)
    ac = list()

    for ai in a:
        for ci in c:
            ac.append(ai + ci)

    count = min(x * z, k)
    acs = sorted(ac, reverse=True)[:count]
    ans = list()

    for bi in b:
        for ac_i in acs:
            ans.append(bi + ac_i)

    for j in sorted(ans, reverse=True)[:k]:
        print(j)


if __name__ == '__main__':
    main()

Solution (Python):
num_x, num_y, num_z, k = map(int, input().split())

x_list = sorted(list(map(int, input().split())))
y_list = sorted(list(map(int, input().split())))
z_list = sorted(list(map(int, input().split())))

xy_list = sorted([x + y for x in x_list for y in y_list], reverse=True)[:k]
xyz_list = sorted([xy + z for xy in xy_list for z in z_list], reverse=True)[:k]

for xyz in xyz_list:
  print(xyz)

Solution (Python):



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)
    ac = list()

    for ai in a:
        for ci in c:
            ac.append(ai + ci)

    count = min(x * z, k)
    acs = sorted(ac, reverse=True)[:count]
    ans = list()

    for bi in b:
        for ac_i in acs:
            ans.append(bi + ac_i)

    for j in sorted(ans, reverse=True)[:k]:
        print(j)


if __name__ == '__main__':
    main()

Solution (Python):
import itertools

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 = list(sorted(map(sum, itertools.product(a, b)), reverse=True))
c.sort(reverse=True)
e = list(sorted(map(sum, itertools.product(c[:k], d[:k])), reverse=True))
for v in e[:k]:
    print(v)

Solution (Python):
from heapq import heappush, heappop

X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
h = []

maxv = A[0] + B[0] + C[0]


def inv(v):
    return maxv - v


pushed = [[[] for _ in range(Y)] for _ in range(X)]
heappush(h, (0, 0, 0, 0))
for _ in range(K):
    invv, i, j, k = heappop(h)
    print(inv(invv))
    if i + 1 < X and k not in pushed[i + 1][j]:
        heappush(h, (inv(A[i + 1] + B[j] + C[k]), i + 1, j, k))
        pushed[i + 1][j].append(k)
    if j + 1 < Y and k not in pushed[i][j + 1]:
        heappush(h, (inv(A[i] + B[j + 1] + C[k]), i, j + 1, k))
        pushed[i][j + 1].append(k)
    if k + 1 < Z and k + 1 not in pushed[i][j]:
        heappush(h, (inv(A[i] + B[j] + C[k + 1]), i, j, k + 1))
        pushed[i][j].append(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()))
ab=sorted([s+t for s in a for t in b])[-1:-k-1:-1]
abc=sorted([s+t for s in ab for t in c])[-1:-k-1:-1]
for i in range(k):
    print(abc[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)

i=j=k=0
HQ = []
heapq.heapify(HQ)
heapq.heappush(HQ,(-(A[i]+B[j]+C[k]),(i,j,k)))
alreadyAdded = set()
alreadyAdded.add((i,j,k))
for _ in range(K):
    valueNeg,index = heapq.heappop(HQ)
    print(-valueNeg)
    l,m,n = index
    nextIndex = []
    if l+1 < X:
        nextIndex.append((l+1,m,n))
    if m+1 < Y:
        nextIndex.append((l,m+1,n))
    if n+1 < Z:
        nextIndex.append((l,m,n+1))
    for l,m,n in nextIndex:
        if (l,m,n) not in alreadyAdded:
            heapq.heappush(HQ,(-(A[l]+B[m]+C[n]),(l,m,n)))
            alreadyAdded.add((l,m,n))

Solution (Python):
import atexit
import io
import sys

buff = io.BytesIO()
sys.stdout = buff


@atexit.register
def write():
    sys.__stdout__.write(buff.getvalue())


x,y,z,k = map(int, raw_input().split())
a = map(int, raw_input().split())
b = map(int, raw_input().split())
c = map(int, raw_input().split())
l1 = []
for i in xrange(x):
	for j in xrange(y):
		l1.append(a[i]+b[j])
l1.sort(reverse = True)
l2 = []
for i in xrange(min(k,len(l1))):
	for j in xrange(z):
		l2.append(l1[i]+c[j])
l2.sort(reverse = True)
for i in xrange(k):
	print l2[i]

Solution (Python):
x,y,z,k = map(int, input().split())
a_lst = sorted(list(map(int, input().split())), reverse=True)
b_lst = sorted(list(map(int, input().split())), reverse=True)
c_lst = sorted(list(map(int, input().split())), reverse=True)

candidate = []
for n in range(x):
    for m in range(y):
        if (n+1)*(m+1) > k:
            break
        else:
            for l in range(z):
                if (n+1)*(m+1)*(l+1) > k:
                    break
                else:
                    candidate.append(a_lst[n]+b_lst[m]+c_lst[l])
for i in sorted(candidate, reverse=True)[:k]:
    print(i)

Solution (Python):
import heapq
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)
q=[(-(A[0]+B[0]+C[0]),0,0,0)]
S=set()

for n in range(K):
  p,i,j,k=heapq.heappop(q)
  print(-p)
  if i+1<x and ((i+1,j,k) not 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 ((i,j+1,k) not 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 ((i,j,k+1) not in S):
    S.add((i,j,k+1))
    heapq.heappush(q,(-(A[i]+B[j]+C[k+1]),i,j,k+1))

Solution (Python):
import heapq
import sys
input=sys.stdin.readline
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)
q=[(-(A[0]+B[0]+C[0]),0,0,0)]
S=set()

for n in range(K):
  p,i,j,k=heapq.heappop(q)
  print(-p)
  if i+1<x and ((i+1,j,k) not 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 ((i,j+1,k) not 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 ((i,j,k+1) not in S):
    S.add((i,j,k+1))
    heapq.heappush(q,(-(A[i]+B[j]+C[k+1]),i,j,k+1))

Solution (Python):
import heapq
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)
q=[]
heapq.heappush(q,(-(A[0]+B[0]+C[0]),0,0,0))
S=set()

for n in range(K):
  p,i,j,k=heapq.heappop(q)
  print(-p)
  if i+1<x and ((i+1,j,k) not 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 ((i,j+1,k) not 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 ((i,j,k+1) not 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()))
ab=[s+t for s in a for t in b]
ab.sort()
ab=ab[-1:-k-1:-1]
abc=[s+t for s in ab for t in c]
abc.sort()
abc=abc[-1:-k-1:-1]
for i in range(len(abc)):
    print(abc[i])

Solution (Python):
from heapq import heappop, heappush


def main():
    x, y, z, k = map(int, input().split())
    aa = sorted(map(int, input().split()))
    bb = sorted(map(int, input().split()))
    cc = sorted(map(int, input().split()))
    v = aa[-1] + bb[-1] + cc[-1]
    t = (len(aa) - 1, len(bb) - 1, len(cc) - 1)
    q = [(-v, t)]
    vs = []
    ts = set()
    while len(vs) < k:
        nv, t = heappop(q)
        if t in ts:
            continue
        vs.append(-nv)
        ts.add(t)
        if t[0] > 0:
            heappush(q, (-(aa[t[0] - 1] + bb[t[1]] + cc[t[2]]), (t[0] - 1, t[1], t[2])))
        if t[1] > 0:
            heappush(q, (-(aa[t[0]] + bb[t[1] - 1] + cc[t[2]]), (t[0], t[1] - 1, t[2])))
        if t[2] > 0:
            heappush(q, (-(aa[t[0]] + bb[t[1]] + cc[t[2] - 1]), (t[0], t[1], t[2] - 1)))
    for v in vs:
        print(v)


if __name__ == "__main__":
    main()

Solution (Python):
X, Y, Z, K = [int(_) for _ in input().split()]
As = [int(_) for _ in input().split()]
Bs = [int(_) for _ in input().split()]
Cs = [int(_) for _ in input().split()]

As.sort()
Bs.sort()
Cs.sort()

choice_stack = [(As[-1] + Bs[-1] + Cs[-1], len(As) - 1, len(Bs) - 1, len(Cs) - 1)]
choice_log = set()

j = 0
while True:
    choice_stack.sort()
    chosen_sum, Ai, Bi, Ci = choice_stack.pop()

    print(chosen_sum)

    step_A = (As[max(0, Ai - 1)] + Bs[Bi] + Cs[Ci], max(0, Ai - 1), Bi, Ci)
    step_B = (As[Ai] + Bs[max(0, Bi - 1)] + Cs[Ci], Ai, max(0, Bi - 1), Ci)
    step_C = (As[Ai] + Bs[Bi] + Cs[max(0, Ci - 1)], Ai, Bi, max(0, Ci - 1))

    if not (step_A in choice_log):
        choice_stack.append(step_A)
        choice_log.add(step_A)
    if not (step_B in choice_log):
        choice_stack.append(step_B)
        choice_log.add(step_B)
    if not (step_C in choice_log):
        choice_stack.append(step_C)
        choice_log.add(step_C)

    j += 1
    if j >= K:
        break