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())
x_list = list(map(int, input().split()))
y_list = list(map(int, input().split()))
z_list = list(map(int, input().split()))

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


next_dict = {}

def sumxyz(tup):
  return x_list[tup[0]] + y_list[tup[1]] + z_list[tup[2]]

def argmax(l):
  mxi = -1
  mx = -1
  for i, v in enumerate(l):
    if v >= mx:
      mx = v
      mxi = i
  return mxi

def add_next(tup):
  def add_not_exist(ntup):
    sm = sumxyz(ntup)
    if ntup not in next_dict:
	    next_dict[ntup] = sm
    
  if tup[0] < len(x_list)-1:
    ntup = (tup[0]+1, tup[1], tup[2])
    add_not_exist(ntup)
  if tup[1] < len(y_list)-1:
    ntup = (tup[0], tup[1]+1, tup[2])
    add_not_exist(ntup)
  if tup[2] < len(z_list)-1:
    ntup = (tup[0], tup[1], tup[2]+1)
    add_not_exist(ntup)

for i in range(K):
  if i == 0:
    print(sumxyz((0,0,0)))
    add_next((0,0,0))
  else:
    v = next_dict.values()
    k = list(next_dict.keys())
    max_idx = argmax(v)
    print(next_dict[k[max_idx]])
    next_dict[k[max_idx]] = -1
    
    add_next(k[max_idx])

Solution (Python):
from heapq import *


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)
a.append(-float('inf'))
b.append(-float('inf'))
c.append(-float('inf'))

done = {}
h = []
heappush(h, (-(a[0] + b[0] + c[0]), 0, 0, 0))
done[0,0,0] = 1
ind = 0
while ind < k:
    t = heappop(h)
    print(-t[0])
    ind += 1
    if (t[1]+1, t[2], t[3]) not in done:
        done[t[1]+1, t[2], t[3]] = 1
        heappush(h, (-(a[t[1]+1] + b[t[2]] + c[t[3]]), t[1] + 1, t[2], t[3]))
    if (t[1], t[2]+1, t[3]) not in done:
        done[t[1], t[2]+1, t[3]] = 1
        heappush(h, (-(a[t[1]] + b[t[2]+1] + c[t[3]]), t[1], t[2]+1, t[3]))
    if (t[1], t[2], t[3]+1) not in done:
        done[t[1], t[2], t[3]+1] = 1
        heappush(h, (-(a[t[1]] + b[t[2]] + c[t[3]+1]), t[1], t[2], t[3]+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()))

xy = []
for aa in a:
    for bb in b:
        xy.append(aa + bb)
xy.sort(reverse=True)
t =[]
for d in xy[:k]:
    for cc in c:
        t.append(d+cc)
t.sort(reverse=True)
for tt in t[:k]:
    print(tt)

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

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

ls1 = sorted(ls1,reverse=True)

ls1 = ls1[:k]
ls2 = []
for i in range(min(z,k)):
    for j in range(len(ls1)):
        ls2.append(c[i]+ls1[j])

ls2 = sorted(ls2,reverse=True)

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

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

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

ab = [ai + bi for bi in b[:k] for ai in a[:k]]
ab.sort(reverse=True)

s = [ci + abi for abi in ab[:k] for ci in c[:k]]
s.sort(reverse=True)

for si in s[:k]:
    print(si)

Solution (Python):
import sys

input = sys.stdin.readline

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

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

d = []
for ai, aa in enumerate(a, 1):
    for bi, bb in enumerate(b, 1):
        for ci, cc in enumerate(c, 1):
            if ai * bi * ci > k:
                break
                
                
            d.append(aa + bb + cc)

d.sort(reverse=True)

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

Solution (Python):
import sys

input = sys.stdin.readline

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

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

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

e = [dd + cc for dd in d[:k] for cc in c[:k]]
e.sort(reverse=True)

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

tmp=[]
for i in range(x):
    for j in range(y):
        tmp.append(a[i]+b[j])
tmp.sort()
tmp2=[]
for i in range(min(k,x*y)):
    for j in range(z):
        tmp2.append(tmp[-1-i]+c[j])
tmp2.sort()

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

Solution (Python):


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

A = sorted(A, reverse=True)[:K+1]
B = sorted(B, reverse=True)[:K+1]
C = sorted(C, reverse=True)[:K+1]

res = []

for i in A:
    for j in B:
        res.append(i + j)
res2 = []
res = sorted(res, reverse=True)[:K+1]

for i in C:
    for re in res:
        res2.append(i + re)

res2 = sorted(res2, reverse=True)[:K+1]

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

Solution (Python):
from heapq import heappop,heappush;import sys;input=sys.stdin.readline
x,y,z,k=map(int,input().split());a,b,c=list(map(int,input().split())),list(map(int,input().split())),list(map(int,input().split()))
q,h,l=[],[],0
for i in a:
  for j in b:heappush(q,-(i+j));l+=1
for _ in range(min(l,k)):
  t=heappop(q)
  for i in c:heappush(h,t-i)
for i in range(k):print(-heappop(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()))
AandB = []
for i in range(0, X):
	for j in range(0, Y):
		AandB.append(A[i]+B[j])
AandB.sort(reverse = True)
if len(AandB) > K:
	AandB = AandB[:K]
anslist = []
for i in range(0, len(AandB)):
	tmplist = []
	for j in range(0, Z):
		tmplist.append(AandB[i]+C[j])
	anslist = anslist + tmplist
	anslist.sort(reverse = True)
	if len(anslist) > K:
		anslist = anslist[:K]
for i in range(0, K):
	print(anslist[i])

Solution (Python):
x,y,z,k=map(int,input().split());a,b,c=list(map(int,input().split())),list(map(int,input().split())),list(map(int,input().split()))
aa=[]
for i in a:
  for j in b:aa.append(i+j)
aa.sort(reverse=1)
aa=aa[:k+1]
s=[]
for i in aa:
  for j in c:s.append(i+j)
s.sort(reverse=1)
for i in range(k):print(s[i])

Solution (Python):
from heapq import heappop,heappush;import sys;input=sys.stdin.readline
x,y,z,k=map(int,input().split());a,b,c=list(map(int,input().split())),list(map(int,input().split())),list(map(int,input().split()))
q,h,l=[],[],0
for i in a:
  for j in b:heappush(q,-(i+j));l+=1
for _ in range(min(l,k)):
  t=heappop(q)
  for i in c:heappush(h,t-i)
for i in range(k):print(-heappop(h))

Solution (Python):
import numpy as np

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()
a.reverse()
b.sort()
b.reverse()
c.sort()
c.reverse()

arr = []

for l in range(1,k+1):
    for m in range(1,(k+1)//l+1):
        for n in range(1,(k+1)//(l*m)+1):
            
            if l-1 < x and m-1 < y and n-1 < z:
                arr.append(a[l-1]+b[m-1]+c[n-1])

arr.sort()
arr.reverse()

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

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

M = []
for n in N:
    for c in C:
        M.append(n + c)
M.sort(reverse=True)
M = M[:K]

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

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 = [(-(A[0]+B[0]+C[0]), 0, 0, 0)]
hs = [(0, 0, 0)]
for i in range(k):
    val, a, b, c = heappop(q)
    print(-val)
    if 0<=a+1<x and (a+1, b, c) not in hs:
        heappush(q, (-(A[a+1]+B[b]+C[c]), a+1, b, c))
        hs.append((a+1, b, c))
    if 0<=b+1<y and (a, b+1, c) not in hs:
        heappush(q, (-(A[a]+B[b+1]+C[c]), a, b+1, c))
        hs.append((a, b+1, c))
    if 0<=c+1<z and (a, b, c+1) not in hs:
        heappush(q, (-(A[a]+B[b]+C[c+1]), a, b, c+1))
        hs.append((a, b, c+1))

Solution (Python):
import sys
from collections import deque  
from collections import defaultdict  
from heapq import heapify, heappush, heappop, heappushpop  
from bisect import bisect_left, bisect_right  



s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
MOD = int(1e+9) + 7

def main():
    X, Y, Z, K = i2nn()
    A = i2nn()
    B = i2nn()
    C = i2nn()
    A.sort()
    A.reverse()
    B.sort()
    B.reverse()
    C.sort()
    C.reverse()
    s = set()
    hq = []
    n = 0
    heappush(hq, ((A[0] + B[0] + C[0]) * -1, (0, 0, 0)))
    while n < K:
        t, abc = heappop(hq)
        if abc in s:
            continue
        s.add(abc)
        (a, b, c) = abc
        if a+1 < X:
            heappush(hq, ((A[a+1] + B[b] + C[c]) * -1, (a+1, b, c)))
        if b+1 < Y:
            heappush(hq, ((A[a] + B[b+1] + C[c]) * -1, (a, b+1, c)))
        if c+1 < Z:
            heappush(hq, ((A[a] + B[b] + C[c+1]) * -1, (a, b, c+1)))
        print(-t)
        n += 1

main()

Solution (Python):
import heapq


def main():
    x, y, z, k = map(int, input().split())
    *a, = map(int, input().split())
    *b, = map(int, input().split())
    *c, = map(int, input().split())

    a = list(reversed(sorted(a)))
    b = list(reversed(sorted(b)))
    c = list(reversed(sorted(c)))

    h = []
    heapq.heappush(h, (-(a[0] + b[0] + c[0]), 0, 0, 0))
    queued = set([(0, 0, 0)])
    tastes = []
    for _ in range(k):
        t, i, j, l = heapq.heappop(h)
        tastes.append(-t)
        if i + 1 < x and (i + 1, j, l) not in queued:
            heapq.heappush(h, (-(a[i + 1] + b[j] + c[l]), i + 1, j, l))
            queued.add((i + 1, j, l))
        if j + 1 < y and (i, j + 1, l) not in queued:
            heapq.heappush(h, (-(a[i] + b[j + 1] + c[l]), i, j + 1, l))
            queued.add((i, j + 1, l))
        if l + 1 < z and (i, j, l + 1) not in queued:
            heapq.heappush(h, (-(a[i] + b[j] + c[l + 1]), i, j, l + 1))
            queued.add((i, j, l + 1))

    for t in tastes:
        print(t)


main()

Solution (Python):
from itertools import product
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())
AB = []
for (a, b) in product(A, B):
    AB.append(a+b)
AB.sort(reverse=True)
C.sort(reverse=True)
ABC = []
for i in range(min(k+1, len(AB))):
    for j in range(len(C)):
        ABC.append(AB[i]+C[j])
ABC.sort(reverse=True)
for i in range(min(k, len(ABC))):
    print ABC[i]

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)


confilm = []

q = [(-(A[0] + B[0] + C[0]) , 0,0,0)]

for _ in range(K) :
    val , i , j , k = heappop(q)

    print(-val)

    t = (i+1,j,k)
    if i + 1 < X and not t in confilm :
        heappush(q,(-(A[i+1] + B[j] + C[k]),) + t)
        confilm.append(t)

    t = (i,j+1,k)
    if j + 1 < Y and not t in confilm :
        heappush(q,(-(A[i] + B[j+1] + C[k]),) + t)
        confilm.append(t)

    t = (i,j,k+1)
    if k + 1 < Z and not t in confilm :
        heappush(q,(-(A[i] + B[j] + C[k+1]),) + t)
        confilm.append(t)

Solution (Python):
def main():

    """ convenient functions
    
    
    
    
    
    """

    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product 
    
    from math import factorial, ceil, floor
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  
        b, e = 2, 0  
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict 
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
    from copy import deepcopy, copy 
    import operator
    from operator import itemgetter
    
    
    from functools import reduce
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd 
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)

    
    
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    modpow = lambda a, n, p = MOD: pow(a, n, p) 
    def modinv(a, p = MOD):
        
        
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        
        
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    """ initialize variables and set inputs
    
        
        
    
        
        
        
        
        
    
        
        
        
        
    
        
        
    """

    
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    R = lambda: list(map(int, r().split()))
    Rmap = lambda: map(int, r().split())
    Rfloat = lambda: list(map(float, r().split()))
    Rtuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    
    
    
    
    
    
    
    """

    if __name__ != '__main__':
        
        sys.stdin = open('sample.txt')

    
    

    X, Y, Z, K = R()
    A = R()
    B = R()
    C = R()
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    anslist = [0] * (10**6)
    index = 0
    for i, a in enumerate(A, 1):
        if i >= K+1:
            break
        for j, b in enumerate(B, 1):
            if j >= floor(K/i)+1:
                break
            for k, c in enumerate(C, 1):
                if k >= floor(K/(i*j))+1:
                    break
                anslist[index] = a+b+c
                index += 1
    anslist.sort(reverse=True)
    for i in range(K):
        print(anslist[i])
    
    

    """memo: how to use defaultdict of list
    
    Dic = defaultdict(list)
    
    Dic[x].append(y)
    
    for k, v in Dic.items():
    """

if __name__ == '__main__':
    main()

Solution (Python):
def main():

    """ convenient functions
    
    
    
    
    
    """

    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product 
    
    from math import factorial, ceil, floor
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  
        b, e = 2, 0  
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict 
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
    from copy import deepcopy, copy 
    import operator
    from operator import itemgetter
    
    
    from functools import reduce
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd 
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)

    
    
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    modpow = lambda a, n, p = MOD: pow(a, n, p) 
    def modinv(a, p = MOD):
        
        
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        
        
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    """ initialize variables and set inputs
    
        
        
    
        
        
        
        
        
    
        
        
        
        
    
        
        
    """

    
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    R = lambda: list(map(int, r().split()))
    Rmap = lambda: map(int, r().split())
    Rfloat = lambda: list(map(float, r().split()))
    Rtuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    
    
    
    
    
    
    
    """

    if __name__ != '__main__':
        
        sys.stdin = open('sample.txt')

    
    

    X, Y, Z, K = R()
    A = R()
    B = R()
    C = R()
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    anslist = [0] * (10**6)
    index = 0
    for i, a in enumerate(A, 1):
        if i >= K+1:
            break
        for j, b in enumerate(B, 1):
            if j >= floor(K/i)+1:
                break
            for k, c in enumerate(C):
                if k >= floor(K/(i*j))+1:
                    break
                anslist[index] = a+b+c
                index += 1
    anslist.sort(reverse=True)
    for i in range(K):
        print(anslist[i])
    
    

    """memo: how to use defaultdict of list
    
    Dic = defaultdict(list)
    
    Dic[x].append(y)
    
    for k, v in Dic.items():
    """

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

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



e = [] 
for i in range(min(k,len(d))):
    p = d.pop()
    e.append(p)
e.sort()
m = len(e)


ans = []
for i in range(m):
    for j in range(z):
        p = e[i] + c[j]
        ans.append(p)
ans.sort()
for i in range(k):
    print(ans[-1-i])

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

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

Q=[]
I=[]
heappush(Q,(-(A[0]+B[0]+C[0]),0,0,0))
for i in range(k):
    qmin,j,k,l=heappop(Q)    
    print(-qmin)

    xi=(j+1,k,l)
    yi=(j,k+1,l)
    zi=(j,k,l+1)

    if j+1<x and xi not in I:
        heappush(Q,(-(A[j+1]+B[k]+C[l]),)+xi)
        I.append(xi)
    if k+1<y and yi not in I:
        heappush(Q,(-(A[j]+B[k+1]+C[l]),)+yi)
        I.append(yi)
    if l+1<z and zi not in I:
        heappush(Q,(-(A[j]+B[k]+C[l+1]),)+zi)
        I.append(zi)

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 = sorted(A, reverse=True)
b = sorted(B, reverse=True)
c = sorted(C, reverse=True)

added = [(0, 0, 0)]
cakes = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
for k in range(K):
	v = heapq.heappop(cakes)
	print(-v[0])
	va, vb, vc = v[1:]
	if va < X - 1 and (va + 1, vb, vc) not in added:
		heapq.heappush(cakes, (-(a[va + 1] + b[vb] + c[vc]), va + 1, vb, vc))
		added += [(va + 1, vb, vc)]
	if vb < Y - 1 and (va, vb + 1, vc) not in added:
		heapq.heappush(cakes, (-(a[va] + b[vb + 1] + c[vc]), va, vb + 1, vc))
		added += [(va, vb + 1, vc)]
	if vc < Z - 1 and (va, vb, vc + 1) not in added:
		heapq.heappush(cakes, (-(a[va] + b[vb] + c[vc + 1]), va, vb, vc + 1))
		added += [(va, vb, vc + 1)]