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)

bi_comb = []
for i in range(min(X, K)):
    for j in range(min(Y, K)):
        if (i+1)*(j+1) <= K:
            bi_comb += [A[i]+B[j]]
bi_comb.sort(reverse = True)

tri_comb = []
for k in range(min(len(bi_comb), K)):
    for l in range(min(Z, K)):
        if (k+1)*(l+1) <= K:
            tri_comb += [bi_comb[k]+C[l]]
tri_comb.sort(reverse = True)

print (*tri_comb[: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()))

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

bi_comb = []
for i in range(min(X, K)):
    for j in range(min(Y, K)):
        if (i+1)*(j+1) <= K:
            bi_comb += [A[i]+B[j]]
bi_comb.sort(reverse = True)

tri_comb = []
for k in range(min(len(bi_comb), K)):
    for l in range(min(Z, K)):
        if (k+1)*(l+1) <= K:
            tri_comb += [bi_comb[k]+C[l]]
tri_comb.sort(reverse = True)

print (*tri_comb[: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()))

lst = []
for i in A:
    for j in B:
        lst += [i + j]

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

ans = []
for i in range(min(K, len(lst))):
    for j in range(min(K, len(C))):
        ans += [lst[i]+C[j]]

ans.sort(reverse = True)
for i in range(K):
    print (ans[i])

Solution (Python):

import sys


def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    import heapq
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    h = []
    heapq.heappush(h, [-(A[0]+B[0]+C[0]), 0, 0, 0])
    ans = []
    used = set()
    used.add((0, 0, 0))
    for _ in range(K):
        e, i, j, k = heapq.heappop(h)
        ans.append(-e)
        if i+1 < X and not (i+1, j, k) in used:
            used.add((i+1, j, k))
            heapq.heappush(h, [-(A[i+1]+B[j]+C[k]), i+1, j, k])
        if j+1 < Y and not (i, j+1, k) in used:
            used.add((i, j+1, k))
            heapq.heappush(h, [-(A[i]+B[j+1]+C[k]), i, j+1, k])
        if k+1 < Z and not (i, j, k+1) in used:
            used.add((i, j, k+1))
            heapq.heappush(h, [-(A[i]+B[j]+C[k+1]), i, j, k+1])
    for elem in ans:
        print(elem)

























def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  
    Y = int(next(tokens))  
    Z = int(next(tokens))  
    K = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(X)]  
    B = [int(next(tokens)) for _ in range(Y)]  
    C = [int(next(tokens)) for _ in range(Z)]  
    solve(X, Y, Z, K, A, B, C)

if __name__ == '__main__':
    main()

Solution (Python):

import sys


def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    import heapq
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    h = []
    heapq.heappush(h, [-(A[0]+B[0]+C[0]), 0, 0, 0])
    ans = []
    used = set()
    used.add((0, 0, 0))
    for _ in range(K):
        e, i, j, k = heapq.heappop(h)
        ans.append(-e)
        if i+1 < X and not (i+1, j, k) in used:
            used.add((i+1, j, k))
            heapq.heappush(h, [-(A[i+1]+B[j]+C[k]), i+1, j, k])
        if j+1 < Y and not (i, j+1, k) in used:
            used.add((i, j+1, k))
            heapq.heappush(h, [-(A[i]+B[j+1]+C[k]), i, j+1, k])
        if k+1 < Z and not (i, j, k+1) in used:
            used.add((i, j, k+1))
            heapq.heappush(h, [-(A[i]+B[j]+C[k+1]), i, j, k+1])
    for elem in ans:
        print(elem)

























def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  
    Y = int(next(tokens))  
    Z = int(next(tokens))  
    K = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(X)]  
    B = [int(next(tokens)) for _ in range(Y)]  
    C = [int(next(tokens)) for _ in range(Z)]  
    solve(X, Y, Z, K, A, B, C)

if __name__ == '__main__':
    main()

Solution (Python):

import sys


def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    AB = []
    for a in A:
        for b in B:
            AB.append(a+b)
    AB.sort(reverse=True)
    C.sort(reverse=True)
    AB = AB[:K]
    C = C[:K]
    ABC = []
    for ab in AB:
        for c in C:
            ABC.append(ab+c)
    ABC.sort(reverse=True)
    for i in range(K):
        print(ABC[i])
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  
    Y = int(next(tokens))  
    Z = int(next(tokens))  
    K = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(X)]  
    B = [int(next(tokens)) for _ in range(Y)]  
    C = [int(next(tokens)) for _ in range(Z)]  
    solve(X, Y, Z, K, A, B, C)

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import*
from math import*
from collections import*
from heapq import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = 10**18
mod = 10**9+7
from functools import reduce
import sys
sys.setrecursionlimit(10**7)




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)
ABK = AB[:min(K,X*Y)]
D = []
for i in range(min(K,X*Y)):
    for j in range(Z):
        D.append(ABK[i]+C[j])
D.sort(reverse=True)
for i in range(K):
    print(D[i])

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

print(*tri_deli[: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()))

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

xyz = []

for ia in range(len(a)):
    for ib in range(len(b)):
        for ic in range(len(c)):
            if (ia+1)*(ib+1)*(ic+1) <= k:
                xyz.append(a[ia]+b[ib]+c[ic])
            else:
                break

xyz.sort(reverse=True)
for l in xyz[:k]:
    print(l)

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()
s = ab[x*y-k:]
for i in s:
    for j in c:
        abc.append(i+j)

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

Solution (Python):
import sys

x, y, z, k = map(int, sys.stdin.readline().split())

xlist = sorted(list([int(i)
                     for i in sys.stdin.readline().split()]), reverse=True)[:min(x, k)]
ylist = sorted(list([int(i)
                     for i in sys.stdin.readline().split()]), reverse=True)[:min(y, k)]
zlist = sorted(list([int(i)
                     for i in sys.stdin.readline().split()]), reverse=True)[:min(z, k)]


anslist = [0]
for i in range(min(x, k)):
    for j in range(min(y, k)):
        for l in range(min(z, k)):
            if (i+1)*(j+1)*(l+1) <= k:
                anslist.append(xlist[i] + ylist[j] + zlist[l])
            else:
                break

anslist = sorted(anslist, reverse=True)
for i in range(k):
    print(anslist[i])

Solution (Python):
X,Y,Z,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A.sort(reverse= True)
B.sort(reverse= True)
C.sort(reverse= True)
LIS = []
for i in range(X):
    INTB = 3000//(i+1)
    for j in range(min(INTB+1,Y)):
        INT = 3000//((i+1)*(j+1))
        for k in range(min(INT+1,Z)):
            SUM = A[i] + B[j] + C[k]
            LIS.append(SUM)
LIS.sort(reverse = True)
for i in range(K):
    print(LIS[i])

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

temp = []
t = 0
for a in A:
    for b in B:
        temp.append(a+b)
temp = (sorted(temp)[::-1])[:K]

ans = []
for a in temp:
    for c in C:
        ans.append(a+c)
ans = (sorted(ans)[::-1])[:K]
print(*ans, sep="\n")

Solution (Python):

import sys
import itertools


def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)

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

    ABC.sort(reverse=True)

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




def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  
    Y = int(next(tokens))  
    Z = int(next(tokens))  
    K = int(next(tokens))  
    A = [ int(next(tokens)) for _ in range(X) ]  
    B = [ int(next(tokens)) for _ in range(Y) ]  
    C = [ int(next(tokens)) for _ in range(Z) ]  
    solve(X, Y, Z, K, A, B, C)

if __name__ == '__main__':
    main()

Solution (Python):
X, Y, Z, K = map(int, input().split())
A = sorted([int(x) for x in input().split()], reverse=True)
B = sorted([int(x) for x in input().split()], reverse=True)
C = sorted([int(x) for x in input().split()], reverse=True)

bi_comb = []
for i in range(min(X, K)):
    for j in range(min(Y, K)):
        if (i+1)*(j+1) > K:
            break
        bi_comb.append(A[i] + B[j])
bi_comb.sort(reverse=True)

tri_comb = []
for k in range(min(len(bi_comb), K)):
    for l in range(min(Z, K)):
        if (k+1)*(l+1) > K:
            break
        tri_comb.append(bi_comb[k] + C[l])
tri_comb.sort(reverse=True)

print(*tri_comb[: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)

ab = sorted([a_k+b_k for a_k in a[:k] for b_k in b[:k]], reverse = True)
abc = sorted([ab_k+c_k for ab_k in ab[:k] for c_k in c[:k]], reverse = True)

for index,number in enumerate(abc):
    if index >= k:
        break
    print(number)

Solution (Python):



from collections import namedtuple
from heapq import *


class Elem:
    def __init__(self, v, a, b, c):
        self.v = v
        self.a = a
        self.b = b
        self.c = c

    def __lt__(self, other):
        return self.v > other.v

    def __str__(self):
        return "{} {} {} {}".format(self.v, self.a, self.b, self.c)

    def __repr__(self):
        return self.__str__()

    def id(self):
        return self.a*1000*1000+self.b*1000+self.c

X, Y, Z, K = list(map(int, input().split()))
la = list(map(int, input().split()))
lb = list(map(int, input().split()))
lc = list(map(int, input().split()))
la.sort(reverse=True)
lb.sort(reverse=True)
lc.sort(reverse=True)
ids = set()

def next(e):
    elems = []
    if e.a < len(la)-1:
        elems.append(Elem(la[e.a+1]+lb[e.b]+lc[e.c],e.a+1,e.b,e.c))
    if e.b < len(lb)-1:
        elems.append(Elem(la[e.a]+lb[e.b+1]+lc[e.c],e.a,e.b+1,e.c))
    if e.c < len(lc)-1:
        elems.append(Elem(la[e.a]+lb[e.b]+lc[e.c+1],e.a,e.b,e.c+1))
    return elems

def main():
    best = Elem(la[0]+lb[0]+lc[0], 0, 0, 0)
    h = []
    heappush(h, best)
    cnt = 0
    while True:
        if len(h) == 0:
            break
        e = heappop(h)
        if e.id() in ids:
            continue
        ids.add(e.id())
        elems = next(e)
        for ne in elems:
            heappush(h, ne)
        print(e.v)
        cnt += 1
        if cnt == K:
            break


if __name__ == '__main__':
    main()

Solution (Python):
X, Y, Z, K = [int(x) for x in input().split()]
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [int(c) for c in input().split()]
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
ABC = []
for a in range(len(A)):
    for b in range(len(B)):
        for c in range(len(C)):
            if (a+1)*(b+1)*(c+1) > K:
                break
            ABC.append(A[a] + B[b] + C[c])
ABC = sorted(ABC, reverse=True)
for i in range(K):
    print(ABC[i])

Solution (Python):
X, Y, Z, K = [int(x) for x in input().split()]
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [int(c) for c in input().split()]
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
ABC = []
for a in range(len(A)):
    for b in range(len(B)):
        if (a+1)*(b+1) > K:
            break
        for c in range(len(C)):
            if (a+1)*(b+1)*(c+1) > K:
                break
            ABC.append(A[a] + B[b] + C[c])
ABC = sorted(ABC, reverse=True)
for i in range(K):
    print(ABC[i])

Solution (Python):
X, Y, Z, K = [int(x) for x in input().split()]
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [int(c) for c in input().split()]
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
ABC = []
for a in range(len(A)):
    for b in range(len(B)):
        if (a+1)*(b+1) > K:
            break
        for c in range(len(C)):
            if (a+1)*(c+1) > K:
                break
            elif (b+1)*(c+1) > K:
                break
            elif (a+1)*(b+1)*(c+1) > K:
                break
            ABC.append(A[a] + B[b] + C[c])
ABC = sorted(ABC, reverse=True)
for i in range(K):
    print(ABC[i])

Solution (Python):
X, Y, Z, K = [int(x) for x in input().split()]
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [int(c) for c in input().split()]
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
ABC = []
for a in range(len(A)):
    if a+1 > K:
        break
    for b in range(len(B)):
        if (a+1)*(b+1) > K:
            break
        for c in range(len(C)):
            if (a+1)*(c+1) > K:
                break
            elif (b+1)*(c+1) > K:
                break
            elif (a+1)*(b+1)*(c+1) > K:
                break
            ABC.append(A[a] + B[b] + C[c])
ABC = sorted(ABC, reverse=True)
for i in range(K):
    print(ABC[i])

Solution (Python):
X, Y, Z, K = [int(x) for x in input().split()]
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [int(c) for c in input().split()]
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
ABC = []
for a in range(len(A)):
    if a+1 > K:
        break
    for b in range(len(B)):
        if b+1 > K:
            break
        elif (a+1)*(b+1) > K:
            break
        for c in range(len(C)):
            if (a+1)*(c+1) > K:
                break
            elif (b+1)*(c+1) > K:
                break
            elif (a+1)*(b+1)*(c+1) > K:
                break
            ABC.append(A[a] + B[b] + C[c])
ABC = sorted(ABC, reverse=True)
for i in range(K):
    print(ABC[i])

Solution (Python):
X, Y, Z, K = [int(x) for x in input().split()]
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [int(c) for c in input().split()]
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
ABC = []
for a in range(len(A)):
    if a+1 > K:
        break
    for b in range(len(B)):
        if b+1 > K:
            break
        elif (a+1)*(b+1) > K:
            break
        for c in range(len(C)):
            if c+1 > K:
                break
            elif (a+1)*(c+1) > K:
                break
            elif (b+1)*(c+1) > K:
                break
            elif (a+1)*(b+1)*(c+1) > K:
                break
            ABC.append(A[a] + B[b] + C[c])
ABC = sorted(ABC, reverse=True)
for i in range(K):
    print(ABC[i])

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

list_a = list(map(int,input().split()))
list_b = list(map(int,input().split()))
list_c = list(map(int,input().split()))

list_a.sort(reverse=True)
list_b.sort(reverse=True)
list_c.sort(reverse=True)

sum_ = []
for a in range(X):
    for b in range(Y):
        for c in range(Z):
            if (a+1)*(b+1)*(c+1) > K:
                break
            sum_.append(list_a[a]+list_b[b]+list_c[c])

sum_.sort(reverse=True)

for i in range(K):
    print(sum_[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 = [0 for i in range(x*y)]
total = [0 for i in range(k*z)]
idx = 0
for i in range(x):
    for j in range(y):
        ab[idx] = a[i]+b[j]
        idx += 1
ab.sort(reverse=True)
idx = 0
for i in range(min(k,x*y)):
    for j in range(z):
        total[idx] = ab[i]+c[j]
        idx += 1
total.sort(reverse=True)
for i in range(k):
    print(total[i])

Solution (Python):

"""
D - Cake 123
https://atcoder.jp/contests/abc123/tasks/abc123_d
AC
"""
import sys

def solve(X, Y, Z, K, A, B, C):
    
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    res = []
    for i, a in enumerate(A, start=1):
        for j, b in enumerate(B, start=1):
            for k, c in enumerate(C, start=1):
                if i*j*k > K:
                    break
                res.append(a+b+c)
    return sorted(res, reverse=True)[:K]


def main(args):
    X, Y, Z, K = map(int, input().split())
    A = [int(a) for a in input().split()]
    B = [int(b) for b in input().split()]
    C = [int(c) for c in input().split()]
    ans = solve(X, Y, Z, K, A, B, C)
    print(*ans, sep='\n')


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):

"""
D - Cake 123
https://atcoder.jp/contests/abc123/tasks/abc123_d
AC
"""
import sys


from itertools import product

def solve(X, Y, Z, K, A, B, C):
    
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    res = []
    for i, a in enumerate(A, start=1):
        for j, b in enumerate(B, start=1):
            for k, c in enumerate(C, start=1):
                if i*j*k > K:
                    break
                res.append(a+b+c)
    return sorted(res, reverse=True)[:K]


def main(args):
    X, Y, Z, K = map(int, input().split())
    A = [int(a) for a in input().split()]
    B = [int(b) for b in input().split()]
    C = [int(c) for c in input().split()]
    ans = solve(X, Y, Z, K, A, B, C)
    print(*ans, sep='\n')


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):

"""
D - Cake 123
https://atcoder.jp/contests/abc123/tasks/abc123_d

"""
import sys


from itertools import product

def solve(X, Y, Z, K, A, B, C):
    
    res_AB = sorted([sum(p) for p in product(A, B)], reverse=True)[:K]
    return sorted([sum(p) for p in product(res_AB, C)], reverse=True)[:K]


def main(args):
    X, Y, Z, K = map(int, input().split())
    A = [int(a) for a in input().split()]
    B = [int(b) for b in input().split()]
    C = [int(c) for c in input().split()]
    ans = solve(X, Y, Z, K, A, B, C)
    print(*ans, sep='\n')


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
X,Y,Z,K = map(int,input().split())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [int(c) for c in input().split()]

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

Dness_ABC = []
for i in range(min(K,len(Dness_AB))):
    for c in C:
        Dness_ABC.append(Dness_AB[i]+c)
Dness_ABC.sort(reverse=True)

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

Solution (Python):
import heapq

def main():
    buf = input()
    buflist = buf.split()
    X = int(buflist[0])
    Y = int(buflist[1])
    Z = int(buflist[2])
    K = int(buflist[3])
    buf = input()
    buflist = buf.split()
    A = list(map(int, buflist))
    buf = input()
    buflist = buf.split()
    B = list(map(int, buflist))
    buf = input()
    buflist = buf.split()
    C = list(map(int, buflist))
    A = list(sorted(A, reverse=True))
    B = list(sorted(B, reverse=True))
    C = list(sorted(C, reverse=True))
    cakeheap = [(-(A[0]+B[0]+C[0]), 0, 0, 0)]
    hashtable = {(-(A[0]+B[0]+C[0]), 0, 0, 0)}
    tastiness = []
    while len(tastiness) < K:
        current = heapq.heappop(cakeheap)
        tastiness.append(-current[0])
        if current[1] < len(A)-1:
            if (-(A[current[1]+1]+B[current[2]]+C[current[3]]), current[1]+1, current[2], current[3]) not in hashtable:
                heapq.heappush(cakeheap, (-(A[current[1]+1]+B[current[2]]+C[current[3]]), current[1]+1, current[2], current[3]))
                hashtable.add((-(A[current[1]+1]+B[current[2]]+C[current[3]]), current[1]+1, current[2], current[3]))
        if current[2] < len(B)-1:
            if (-(A[current[1]]+B[current[2]+1]+C[current[3]]), current[1], current[2]+1, current[3]) not in hashtable:
                heapq.heappush(cakeheap, (-(A[current[1]]+B[current[2]+1]+C[current[3]]), current[1], current[2]+1, current[3]))
                hashtable.add((-(A[current[1]]+B[current[2]+1]+C[current[3]]), current[1], current[2]+1, current[3]))
        if current[3] < len(C)-1:
            if (-(A[current[1]]+B[current[2]]+C[current[3]+1]), current[1], current[2], current[3]+1) not in hashtable:
                heapq.heappush(cakeheap, (-(A[current[1]]+B[current[2]]+C[current[3]+1]), current[1], current[2], current[3]+1))
                hashtable.add((-(A[current[1]]+B[current[2]]+C[current[3]+1]), current[1], current[2], current[3]+1))
    for i in tastiness:
        print(i)


if __name__ == '__main__':
    main()

Solution (Python):

def main():
    buf = input()
    buflist = buf.split()
    X = int(buflist[0])
    Y = int(buflist[1])
    Z = int(buflist[2])
    K = int(buflist[3])
    buf = input()
    buflist = buf.split()
    A = list(map(int, buflist))
    buf = input()
    buflist = buf.split()
    B = list(map(int, buflist))
    buf = input()
    buflist = buf.split()
    C = list(map(int, buflist))
    A = list(sorted(A, reverse=True))
    B = list(sorted(B, reverse=True))
    C = list(sorted(C, reverse=True))
    cakelist = [(A[0]+B[0]+C[0], 0, 0, 0)]
    hashtable = {(A[0]+B[0]+C[0], 0, 0, 0)}
    tastiness = []
    while len(tastiness) < K:
        current = cakelist.pop()
        tastiness.append(current[0])
        if current[1] < len(A)-1:
            if (A[current[1]+1]+B[current[2]]+C[current[3]], current[1]+1, current[2], current[3]) not in hashtable:
                cakelist.append((A[current[1]+1]+B[current[2]]+C[current[3]], current[1]+1, current[2], current[3]))
                hashtable.add((A[current[1]+1]+B[current[2]]+C[current[3]], current[1]+1, current[2], current[3]))
        if current[2] < len(B)-1:
            if (A[current[1]]+B[current[2]+1]+C[current[3]], current[1], current[2]+1, current[3]) not in hashtable:
                cakelist.append((A[current[1]]+B[current[2]+1]+C[current[3]], current[1], current[2]+1, current[3]))
                hashtable.add((A[current[1]]+B[current[2]+1]+C[current[3]], current[1], current[2]+1, current[3]))
        if current[3] < len(C)-1:
            if (A[current[1]]+B[current[2]]+C[current[3]+1], current[1], current[2], current[3]+1) not in hashtable:
                cakelist.append((A[current[1]]+B[current[2]]+C[current[3]+1], current[1], current[2], current[3]+1))
                hashtable.add((A[current[1]]+B[current[2]]+C[current[3]+1], current[1], current[2], current[3]+1))
        cakelist = list(sorted(cakelist))
    for i in tastiness:
        print(i)


if __name__ == '__main__':
    main()

Solution (Python):
from heapq import heappop,heappush
from collections import defaultdict
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 =[]
ans = []
sc = defaultdict(bool)
heappush(q,(-a[0] - b[0] - c[0], 0, 0, 0))

while len(ans) < K:
    deli, i, j, k = heappop(q)
    print(-deli)
    ans.append(-deli)
    
    if i + 1 < x and not sc[(i + 1, j, k)]:
        heappush(q,(-a[i + 1] - b[j] - c[k], i + 1, j, k))
        sc[(i + 1, j, k)] = True
    
    if j + 1 < y and not sc[(i, j + 1, k)]:
        heappush(q,(-a[i] - b[j + 1] - c[k], i, j + 1, k))
        sc[(i, j + 1, k)] = True
    
    if k + 1 < z and not sc[(i, j, k + 1)]:
        heappush(q,(-a[i] - b[j] - c[k + 1], i, j, k + 1))
        sc[(i, j, k + 1)] = True

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)

sums = []

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

for s in sorted(sums,reverse=True)[:K]:
    print(s)

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, (-1*(A[0]+B[0]+C[0]), 0, 0, 0))
registered = set()
registered.add((0, 0, 0))
for _ in range(K):
    s, ai, bi, ci = heapq.heappop(Q)
    print(-s)

    if ai+1 < X and (ai+1, bi, ci) not in registered:
        heapq.heappush(Q, (-1*(A[ai+1]+B[bi]+C[ci]), ai+1, bi, ci))
        registered.add((ai+1, bi, ci))
    if bi+1 < Y and (ai, bi+1, ci) not in registered:
        heapq.heappush(Q, (-1*(A[ai]+B[bi+1]+C[ci]), ai, bi+1, ci))
        registered.add((ai, bi+1, ci))
    if ci+1 < Z and (ai, bi, ci+1) not in registered:
        heapq.heappush(Q, (-1*(A[ai]+B[bi]+C[ci+1]), ai, bi, ci+1))
        registered.add((ai, bi, ci+1))

Solution (Python):
import itertools

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

comb_ab = []
for a, b in itertools.product(A, B):
    comb_ab.append(sum([a, b]))

comb_ab_top = sorted(comb_ab, reverse=True)[:K]

comb_abc = []
for ab, c in itertools.product(comb_ab_top, C):
    comb_abc.append(sum([ab, c]))

ans = sorted(comb_abc, reverse=True)[:K]
print(*ans, sep='\n')

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)

comb = []
for a in range(X):
    b_max = K // (a+1)
    for b in range(min(Y, b_max)):
        c_max = K // ((a+1)*(b+1))
        for c in range(min(Z, c_max)):
            comb.append(sum([A[a], B[b], C[c]]))

comb.sort(reverse=True)

print(*comb[:K], sep='\n')