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

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

que = []
heapq.heappush(que, [(A[0]+B[0]+C[0])*-1,0,0,0])
data = [[0,0,0]]
for i in range(K):
    tmp = heapq.heappop(que)
    print(tmp[0]*-1)
    if tmp[1]+1 < len(A) and [(A[tmp[1]+1]+B[tmp[2]]+C[tmp[3]])*-1,tmp[1]+1,tmp[2],tmp[3]] not in data:
        heapq.heappush(que, [(A[tmp[1]+1]+B[tmp[2]]+C[tmp[3]])*-1,tmp[1]+1,tmp[2],tmp[3]])
        data.append([(A[tmp[1]+1]+B[tmp[2]]+C[tmp[3]])*-1,tmp[1]+1,tmp[2],tmp[3]])
    if tmp[2]+1 < len(B) and [(A[tmp[1]]+B[tmp[2]+1]+C[tmp[3]])*-1,tmp[1],tmp[2]+1,tmp[3]] not in data:
        heapq.heappush(que, [(A[tmp[1]]+B[tmp[2]+1]+C[tmp[3]])*-1,tmp[1],tmp[2]+1,tmp[3]])
        data.append([(A[tmp[1]]+B[tmp[2]+1]+C[tmp[3]])*-1,tmp[1],tmp[2]+1,tmp[3]])
    if tmp[3]+1 < len(C) and [(A[tmp[1]]+B[tmp[2]]+C[tmp[3]+1])*-1,tmp[1],tmp[2],tmp[3]+1] not in data:
        heapq.heappush(que, [(A[tmp[1]]+B[tmp[2]]+C[tmp[3]+1])*-1,tmp[1],tmp[2],tmp[3]+1])
        data.append([(A[tmp[1]]+B[tmp[2]]+C[tmp[3]+1])*-1,tmp[1],tmp[2],tmp[3]+1])

Solution (Python):
from itertools import product

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 = sorted([s+t for s,t in product(a,b)], reverse=True)[:3000]
e = sorted([s+t for s,t in product(c,d)], reverse=True)[:k]
[print(i) for i in e]

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

X, Y, Z, K = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]

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

ablst = [m + n for n in b for m in a]

ablst.sort(reverse=True)

c.sort(reverse=True)

abclst = [m + n for n in c[:K] for m in ablst[:K]]

abclst.sort(reverse=True)

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

Solution (Python):
from heapq import heapify, heappush, heappop, heapreplace
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 = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
heapify(D)
F = set()
for _ in range(K):
    p = heappop(D)
    d, x, y, z = p
    print(-d)
    if x < X - 1:
        p = (-(A[x + 1] + B[y] + C[z]), x + 1, y, z)
        if p not in F:
            heappush(D, p)
            F.add(p)
    if y < Y - 1:
        p = (-(A[x] + B[y + 1] + C[z]), x, y + 1, z)
        if p not in F:
            heappush(D, p)
            F.add(p)
    if z < Z - 1:
        p = (-(A[x] + B[y] + C[z + 1]), x, y, z + 1)
        if p not in F:
            heappush(D, p)
            F.add(p)

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()))
AB = sorted((a + b for a in A for b in B), reverse=True)[:min(X*Y, K)]
C.sort()

heap = []
min_sum = float('inf')
for ab in AB:
    if len(heap) < K:
        for c in C:
            heappush(heap, -(ab + c))
            min_sum = min(min_sum, ab + c)
    else:
        left = 0
        right = Z
        while left < right:
            mid = (left+right) // 2
            n = ab + C[mid]
            if n >= min_sum:
                left = mid + 1
            else:
                right = mid
            min_sum = min(min_sum, n)
        for c in C[:right]:
            heappush(heap, -(ab + c))


for i in range(K):
    print(-heappop(heap))

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)
A.append(-10**20)
B.append(-10**20)
C.append(-10**20)
Q = []
heapq.heapify(Q)
heapq.heappush(Q, (-(A[0]+B[0]+C[0]), 0, 0, 0))  
count = 0
fin = {}
while count < K:
    delicious, a, b, c = heapq.heappop(Q)
    if (a, b, c) not in fin:
        fin[(a, b, c)] = True
        print(-delicious)
        heapq.heappush(Q, (-(A[a+1]+B[b]+C[c]), a+1, b, c))
        heapq.heappush(Q, (-(A[a]+B[b+1]+C[c]), a, b+1, c))
        heapq.heappush(Q, (-(A[a]+B[b]+C[c+1]), a, b, c+1))
        count += 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()))

a_b = []
for i in range(X):
    for j in range(Y):
        a_b.append(A[i]+B[j])
a_b = sorted(a_b, reverse=True)
ans = []
for k in range(min(K, len(a_b))):
    for l in range(min(K, len(C))):
        ans.append(a_b[k]+C[l])
ans = sorted(ans, reverse=True)
for m in range(K):
    print(ans[m])

Solution (Python):
X,Y,Z,K=[int(s) for s in input().split()]
xls=sorted([int(s) for s in input().split()],reverse=True)
yls=sorted([int(s) for s in input().split()],reverse=True)
zls=sorted([int(s) for s in input().split()],reverse=True)
cake=[]
for a in range(X):
  for b in range(Y):
    ab=(a+1)*(b+1)
    for c in range(Z):
      if ab*(c+1)>K:
        break
      cake.append(xls[a]+yls[b]+zls[c])
    if ab>K:
      break
cake=sorted(cake,reverse=True)
for i in range(K):
  print(cake[i])

Solution (Python):
X,Y,Z,K=[int(s) for s in input().split()]
xls=sorted([int(s) for s in input().split()],reverse=True)
yls=sorted([int(s) for s in input().split()],reverse=True)
zls=sorted([int(s) for s in input().split()],reverse=True)
xyls=[]
for x in xls[:K]:
  for y in yls[:K]:
    xyls.append(x+y)
xyls=sorted(xyls,reverse=True)
cake=[]
for xy in xyls[:K]:
  for z in zls[:K]:
    cake.append(xy+z)
cake=sorted(cake,reverse=True)
for i in range(K):
  print(cake[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)

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

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

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

x,y,z,k = map(int,input().split())
lisa = sorted(map(int,input().split()))
lisb = sorted(map(int,input().split()))
lisc = sorted(map(int,input().split()))

ab = sorted(a+b for a in lisa for b in lisb)[:-k-1:-1]
abc = sorted(ab+c for ab in ab[:k] for c in lisc)[:-k-1:-1]

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

Solution (Python):
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7



X, Y, Z, K = list(map(int, sys.stdin.buffer.readline().split()))
A = list(map(int, sys.stdin.buffer.readline().split()))
B = list(map(int, sys.stdin.buffer.readline().split()))
C = list(map(int, sys.stdin.buffer.readline().split()))

A = np.array(A, dtype=int)
B = np.array(B, dtype=int)
C = np.array(C, dtype=int)

AB = (A[:, None] + B[None, :]).ravel()
AB.sort()
AB = AB[::-1]


l = 0
h = IINF
while abs(l - h) > 1:
    d = (l + h) // 2
    
    cnt = np.searchsorted(d - AB, C, side='right').sum()
    if cnt >= K:
        l = d
    else:
        h = d


counts = list(np.searchsorted(h - AB, C, side='right'))
ans = []
for i, cnt in enumerate(counts):
    ans += (AB + C[i])[:cnt].tolist()
ans += [l] * (K - len(ans))
ans.sort(reverse=True)
if ans:
    print(*ans, sep='\n')

Solution (Python):
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7



X, Y, Z, K = list(map(int, sys.stdin.buffer.readline().split()))
A = list(map(int, sys.stdin.buffer.readline().split()))
B = list(map(int, sys.stdin.buffer.readline().split()))
C = list(map(int, sys.stdin.buffer.readline().split()))

A = np.array(A, dtype=int)
B = np.array(B, dtype=int)
C = np.array(C, dtype=int)

AB = (A[:, None] + B[None, :]).ravel()
AB = np.msort(AB)[::-1][:K]
ABC = (AB[::-1][:K][:, None] + C[None, :]).ravel()
ans = np.msort(ABC)[::-1][:K]

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

Solution (Python):
def examA():
    X = [I() for _ in range(5)]
    k = I()
    X.sort()
    ans = "Yay!"
    if X[-1]-X[0]>k:
        ans = ":("
    print(ans)
    return

def examB():
    X = [I() for _ in range(5)]
    ans = 0
    for i in range(5):
        ans += (X[i]+9)//10 *10
        X[i] = (X[i]-1)%10
    ans -= (9-min(X))
    print(ans)
    return

def examC():
    N = I()
    X = [I() for _ in range(5)]
    ans = max(0,(N-1)//min(X))+5
    print(ans)
    return

def examD():
    X, Y, Z, K = LI()
    A = LI(); B = LI(); C = LI()
    A.sort(reverse = True); B.sort(reverse = True); C.sort(reverse = True)
    que = [(-(A[0]+B[0]+C[0]),0,0,0)]
    heapify(que)
    used = defaultdict(bool)
    used[(0,0,0)] = True
    ans = []
    for _ in range(K):
        now,x,y,z = heappop(que)
        ans.append(-now)
        for ix,iy,iz in [[1,0,0],[0,1,0],[0,0,1]]:
            if used[(ix+x,iy+y,iz+z)]:
                continue
            used[(ix+x, iy+y, iz+z)] = True
            if ix+x>=X or iy+y>=Y or iz+z>=Z:
                continue
            next = A[ix+x]+B[iy+y]+C[iz+z]
            heappush(que,(-next,ix+x,iy+y,iz+z))
    for v in ans:
        print(v)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,inf
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examD()

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

bc = [0] * (y * z)
q = 0
for i in range(y):
    for j in range(z):
        bc[q] = b[i] + c[j]
        q += 1

bc.sort(reverse=True)
r = len(bc)
bc = bc[:min(k, r)]

abc = [0] * (x * min(k, r))
q = 0
for i in range(min(k, r)):
    for j in range(x):
        abc[q] = bc[i] + a[j]
        q += 1

abc.sort(reverse=True)
for i in range(min(k, x*y*z)):
    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()))

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

abc = []

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

for abci in abc[:k]:
  print(abci)

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)

D = {(0,0,0)}
used = set()
ans = []
for k in range(K):
  
  mxdsum = 0
  for d in D:
    dsum = A[d[0]]+B[d[1]]+C[d[2]]
    if dsum >= mxdsum:
      mxdsum = dsum
      mxd = d
  ans.append(mxdsum)
  if mxd[0]+1 < X and (mxd[0]+1,mxd[1],mxd[2]) not in used:
    D.add((mxd[0]+1,mxd[1],mxd[2]))
  if mxd[1]+1 < Y and (mxd[0],mxd[1]+1,mxd[2]) not in used:
    D.add((mxd[0],mxd[1]+1,mxd[2]))
  if mxd[2]+1 < Z and (mxd[0],mxd[1],mxd[2]+1) not in used:
    D.add((mxd[0],mxd[1],mxd[2]+1))
  used.add(mxd)
  D.remove(mxd)

for a in ans:
  print(a)

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)

D = {(0,0,0)}
used = set()
ans = []
for k in range(K):
  
  mxdsum = 0
  for d in D:
    dsum = A[d[0]]+B[d[1]]+C[d[2]]
    if dsum >= mxdsum:
      mxdsum = dsum
      mxd = d
  ans.append(mxdsum)
  if mxd[0]+1 < X and (mxd[0]+1,mxd[1],mxd[2]) not in used:
    D.add((mxd[0]+1,mxd[1],mxd[2]))
  if mxd[1]+1 < Y and (mxd[0],mxd[1]+1,mxd[2]) not in used:
    D.add((mxd[0],mxd[1]+1,mxd[2]))
  if mxd[2]+1 < Y and (mxd[0],mxd[1],mxd[2]+1) not in used:
    D.add((mxd[0],mxd[1],mxd[2]+1))
  used.add(mxd)
  D.remove(mxd)

for a in ans:
  print(a)

Solution (Python):
import sys
from heapq import heapify, heappushpop
from bisect import bisect_left as bisect

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

X, Y, Z, K = MAP()
x = MAP()
y = MAP()
z = MAP()

if X > Z:
	x, z = z, x
	X, Z = Z, X
if Y > Z:
	y, z = z, y
	Y, Z = Z, Y

x.sort()
y.sort()
z.sort()

nums = [0] * K
heapify(nums)

for i in range(X-1, -1, -1):
	for j in range(Y-1, -1, -1):
		xy = x[i]+y[j]
		z_idx = bisect(z, nums[0]-xy)
		
		
		for k in range(z_idx, Z):
			heappushpop(nums, xy+z[k])

nums.sort(reverse=True)
for n in nums:
	print(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)

AB = [0] * (K * K)
for itra, a in enumerate(A):
  if itra >= K:
    break
  for itrb, b in enumerate(B):
    AB[itra * K + itrb] = (a + b)
    if itrb >= K - 1:
      break
    
AB.sort(reverse=True)    
    
ABC = [0] * (K * K)
for itrab, ab in enumerate(AB):
  if itrab >= K:
    break
  for itrc, c in enumerate(C):
    ABC[itrab * K + itrc] = (ab + c)
    if itrc >= K - 1:
      break
    
ABC.sort(reverse=True)
for itr, i in enumerate(ABC):
  print(i)
  if itr >= K - 1:
    break

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

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

ANS = []


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

for ans in sorted(ANS, reverse=True)[:K]:
  print(ans)

Solution (Python):
x,y,z,k=map(int,input().split())
A=sorted(list(map(int,input().split())),reverse=True)
B=sorted(list(map(int,input().split())),reverse=True)
C=sorted(list(map(int,input().split())),reverse=True)
ans=[]
for i in range(x):
  j=0
  while (i+1)*(j+1)<=3000 and j<y:
    l=0
    while (i+1)*(j+1)*(l+1)<=3000 and l<z:
      a=A[i]+B[j]+C[l]
      ans.append(a)
      l=l+1
    j=j+1
ans=sorted(ans,reverse=True)
for i in range(k):
  print(ans[i])

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 c in C for d in D ))
for i in E:
    print(i)

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()))
    AB = []
    for a in A:
        for b in B:
            AB.append(a + b)

    AB.sort(reverse=True)
    C.sort(reverse=True)
    ans = []
    for i in range(min(K,X*Y)):
        for j in range(min(K,Z)):
            ans.append(AB[i] + C[j])

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



if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np

X, Y, Z, K = map(int, input().split())
A = np.array(tuple(map(int, input().split())), dtype=np.int64)
B = np.array(tuple(map(int, input().split())), dtype=np.int64)
C = np.array(tuple(map(int, input().split())), dtype=np.int64)

AB = (A + B.reshape((Y, 1))).flatten()
AB.sort()

ABC = (AB[::-1][:K] + C.reshape((Z, 1))).flatten()
ABC.sort()

print(*ABC[::-1][:K], sep="\n", end="")

Solution (Python):
x,y,z,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
AB=[]
for i in range(x):
    for j in range(y):
        AB.append(A[i]+B[j])
AB.sort(reverse=True)
ABC=[]
for i in range(min(k,x*y)):
    for j in range(z):
        ABC.append(AB[i]+C[j])
ABC.sort(reverse=True)
for i in range(min(k,x*y*z)):
    print(ABC[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 = []
heapq.heappush(Q, (-(A[0]+B[0]+C[0]), 0, 0, 0))
for _ in range(k):
  m, i, j, k = heapq.heappop(Q)
  print(-m)
  if i+1<x:
    a = A[i+1]+B[j]+C[k]
  if j+1<y:
    b = A[i]+B[j+1]+C[k]
  if k+1<z:
    c = A[i]+B[j]+C[k+1]
  if i+1<x and (-a, i+1, j, k) not in Q:
    heapq.heappush(Q, (-a, i+1, j, k))
  if j+1<y and (-b, i, j+1, k) not in Q:
    heapq.heappush(Q, (-b, i, j+1, k))
  if k+1<z and (-c, i, j, k+1) not in Q:
    heapq.heappush(Q, (-c, i, j, k+1))

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)
L = []
for i in range(min(x, k)):
  for j in range(min(y, k//(i+1))):
    for l in range(min(z, k//(i+1)//(j+1))):
      L.append(A[i]+B[j]+C[l])
print(*sorted(L)[::-1][: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()))
L = []
for i in A:
  for j in B:
    L.append(i+j)
S = sorted(L, reverse=True)[:k]
T = []
for i in S:
  for j in sorted(C, reverse=True)[:k]:
    T.append(i+j)
print(*sorted(T, reverse=True)[:k], sep="\n")

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

A = sorted(A)[::-1]
B = sorted(B)[::-1]
C = sorted(C)[::-1]

import bisect, heapq

def myidx(x, y, z, X, Y, Z): return(x * (Y * X) + y * Z + z)

ans = 0
cnt = 0
score = A[0] + B[0] + C[0]
history = {}
q = [(0, 0, 0, 0)]
while len(q) > 0:
    penalty, x, y, z = heapq.heappop(q)
    
    if x < X - 1:
        idx_tmp = myidx(x + 1, y, z, X, Y, Z)
        if history.setdefault(str(idx_tmp), 0) == 0:
            
            history[str(idx_tmp)] = 1
            
            penalty_tmp = penalty + (A[x] - A[x + 1])
            heapq.heappush(q, (penalty_tmp, x + 1, y, z))
    if y < Y - 1:
        idx_tmp = myidx(x, y + 1, z, X, Y, Z)
        if history.setdefault(str(idx_tmp), 0) == 0:
            
            history[str(idx_tmp)] = 1
            
            penalty_tmp = penalty + (B[y] - B[y + 1])
            heapq.heappush(q, (penalty_tmp, x, y + 1, z))
    if z < Z - 1:
        idx_tmp = myidx(x, y, z + 1, X, Y, Z)
        if history.setdefault(str(idx_tmp), 0) == 0:
            
            history[str(idx_tmp)] = 1
            
            penalty_tmp = penalty + (C[z] - C[z + 1])
            heapq.heappush(q, (penalty_tmp, x, y, z + 1))
    
    cnt += 1
    print(score - penalty)
    if cnt == K:
        break

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)[::-1]
B = sorted(B)[::-1]
C = sorted(C)[::-1]

import bisect, heapq

def myidx(x, y, z, X, Y, Z): return(x * (Y * X) + y * Z + z)

ans = 0
cnt = 0
score = A[0] + B[0] + C[0]
history = []
q = [(0, 0, 0, 0)]
while len(q) > 0:
    penalty, x, y, z = heapq.heappop(q)
    
    if x < X - 1:
        idx_tmp = myidx(x + 1, y, z, X, Y, Z)
        if idx_tmp not in history:
            
            heapq.heappush(history, idx_tmp)
            
            penalty_tmp = penalty + (A[x] - A[x + 1])
            heapq.heappush(q, (penalty_tmp, x + 1, y, z))
    if y < Y - 1:
        idx_tmp = myidx(x, y + 1, z, X, Y, Z)
        if idx_tmp not in history:
            
            heapq.heappush(history, idx_tmp)
            
            penalty_tmp = penalty + (B[y] - B[y + 1])
            heapq.heappush(q, (penalty_tmp, x, y + 1, z))
    if z < Z - 1:
        idx_tmp = myidx(x, y, z + 1, X, Y, Z)
        if idx_tmp not in history:
            
            heapq.heappush(history, idx_tmp)
            
            penalty_tmp = penalty + (C[z] - C[z + 1])
            heapq.heappush(q, (penalty_tmp, x, y, z + 1))
    
    cnt += 1
    print(score - penalty)
    if cnt == K:
        break

Solution (Python):
import heapq
 
X,Y,Z,K = map(int,input().split(' '))
An = list(map(int,input().split(' ')))
Bn = list(map(int,input().split(' ')))
Cn = list(map(int,input().split(' ')))
output = []
ABn = [0] * (X*Y)
count = 0
for a in An:
    for b in Bn:
        ABn[count] = a + b
        count += 1
ABn.sort(reverse=True)
ABn = ABn[0:K]

for ab in ABn:
    for c in Cn:
        heapq.heappush(output,(ab+c)*(-1))
 
for _ in range(K):
    print(heapq.heappop(output) * (-1))

Solution (Python):
from heapq import *

def main():
	x, y, z, k, *L = map(int, open(0).read().split())
	s = sorted
	ans = []
	a = s(L[:x])[::-1]
	b = s(L[x:x + y])[::-1]
	c = s(L[x + y:])[::-1]
	h = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
	done = {(0, 0, 0)}
	for _ in range(k):
		v, i, j, k = heappop(h)
		ans += [-v]
		for ni, nj, nk in ((i + 1, j, k), (i, j + 1, k), (i, j, k + 1)):
			if ni == x or nj == y or nk == z:
				continue
			if (ni, nj, nk) not in done:
				heappush(h,(-(a[ni] + b[nj] + c[nk]), ni, nj, nk))
				done.add((ni, nj, nk))
	print(*ans)

if __name__=="__main__":
	main()

Solution (Python):
from heapq import *

def main():
	x, y, z, k, *L = map(int, open(0).read().split())
	s = sorted
	a = s(L[:x])[::-1]
	b = s(L[x:x + y])[::-1]
	c = s(L[x + y:])[::-1]
	h = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
	done = {(0, 0, 0)}
	for _ in range(k):
		v, i, j, k = heappop(h)
		print(-v)
		for ni, nj, nk in ((i + 1, j, k), (i, j + 1, k), (i, j, k + 1)):
			if ni == x or nj == y or nk == z:
				continue
			if (ni, nj, nk) not in done:
				heappush(h,(-(a[ni] + b[nj] + c[nk]), ni, nj, nk))
				done.add((ni, nj, nk))

if __name__=="__main__":
	main()

Solution (Python):
x,y,z,k,*L=map(int,open(0).read().split())
a=L[:x];b=L[x:x+y];c=sorted(L[x+y:])[::-1]
ab=sorted(i+j for i in a for j in b)[::-1][:min(x*y,k)]
print(*sorted(i+j for i in ab for j in c)[::-1][:k],sep="\n")

Solution (Python):
def p_d():
    x, y, z, k = map(int, input().split())
    candles = []
    for i in range(3):
        *tmp, = map(int, input().split())
        tmp.sort(reverse=True)
        candles.append(tmp)
        
    c = []
    for i in range(x):
        for j in range(y):
            c.append(candles[0][i] + candles[1][j])
    c.sort(reverse=True)
    ans = []
    for _c in c[:k]:
        for i in candles[2][:min(z, k)]:
            ans.append(_c + i)
    ans.sort(reverse=True)
    print("\n".join(map(str, ans[:k])))


if __name__ == '__main__':
    p_d()

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=[]
a.sort()
a.reverse()
b.sort()
b.reverse()
for i in range(len(a)):
  for j in range(len(b)):
    ab.append(a[i]+b[j])
    if (i+1)*(j+1)>k:
      break
ab.sort()
ab.reverse()
c.sort()
c.reverse()
l=min(k,len(ab))
for i in range(l):
  for j in range(len(c)):
    abc.append(ab[i]+c[j])
    if (i+1)*(j+1)>k:
      break
abc.sort()
abc.reverse()
for i in range(k):
  print(abc[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()))

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

E = []
for i in range(min(K, len(D))):
    for c in C:
        E.append(D[i]+c)

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

Solution (Python):
from itertools import product
from heapq import heappop, heappush

x,y,z,k = map(int,input().split())
a = sorted(map(int, input().split()), reverse=True)
b = sorted(map(int, input().split()), reverse=True)
c = sorted(map(int, input().split()), reverse=True)
if x * y * z <= 3000:
    lst = sorted([sum(i) for i in product(a,b,c)], reverse=True)
    [print(i) for i in lst[:k]]
    exit()

idx = [0,0,0]
heap = []
xxx = [x,y,z]

aaa = idx[0] if idx[0]+1 >= x else idx[0] + 1
bbb = idx[1] if idx[1]+1 >= y else idx[1] + 1
ccc = idx[2] if idx[2]+1 >= z else idx[2] + 1
heappush(heap, [-(a[aaa] + b[idx[1]] + c[idx[2]]), [1, 0, 0], 0])
heappush(heap, [-(a[idx[0]] + b[bbb] + c[idx[2]]), [0, 1, 0], 1])
heappush(heap, [-(a[idx[0]] + b[idx[1]] + c[ccc]), [0, 0, 1], 2])
print(a[idx[0]] + b[idx[1]] + c[idx[2]])
doc = {}
for _ in range(min(k-1, x*y*z)):
    score, ix, pos = heappop(heap)
    print(-score)
    for i in range(3):
        tmp = ix.copy()
        tmp[i] = ix[i] if ix[i] + 1 >= xxx[i] else ix[i] + 1
        if ','.join(map(str, tmp)) not in doc:
            doc[','.join(map(str, tmp))] = 1
            score = a[tmp[0]] + b[tmp[1]] + c[tmp[2]]
            
            heappush(heap, [-score, tmp, i])

Solution (Python):






X, Y, Z, K = map(int, input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
C = [int(x) for x in input().split()]





AB = []
for x in range(X):
    for y in range(Y):
        AB.append(A[x] + B[y])
AB.sort(reverse=True)
if K < X * Y:
    AB = AB[:K]
W = len(AB)
AB





ABC = []
for w in range(W):
    for z in range(Z):
        ABC.append(AB[w] + C[z])
ABC.sort(reverse=True)
if K < W * Z:
    ABC = ABC[:K]
ABC





print('\n'.join(map(str, ABC)))

Solution (Python):
import heapq

import itertools

class Heapq:
    def __init__(self, arr, desc=False):
        if desc:
            arr = [-a for a in arr]
        self.sign = -1 if desc else 1
        self.hq = arr
        heapq.heapify(self.hq)

    def pop(self):
        return heapq.heappop(self.hq) * self.sign

    def push(self, a):
        heapq.heappush(self.hq, a * self.sign)

    def top(self):
        return self.hq[0] * self.sign

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(reverse=True)

p = itertools.product(A, B)
A_B_list = [sum(v) for v in itertools.product(A, B)]
A_B_list.sort(reverse=True)
A_B_C_list = [sum(v) for v in itertools.product(A_B_list[:3000], C[:3000])]
A_B_C_list.sort(reverse=True)

for ans in A_B_C_list[:K]:
    print(ans)

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

    input = sys.stdin.readline

    x, y, z, k = map(int, input().split())
    A = sorted(map(int, input().split()), reverse=True)
    B = sorted(map(int, input().split()), reverse=True)
    C = sorted(map(int, input().split()), reverse=True)

    def calc(p, q, r):
        if p >= x or q >= y or r >= z:
            return 0
        return A[p] + B[q] + C[r]

    h = [(-calc(0, 0, 0), (0, 0, 0))]

    ret = []
    used = defaultdict(bool)
    for _ in range(k):
        v, t = heappop(h)
        ret.append(-v)
        for j in range(3):
            indices = list(t)
            indices[j] += 1
            indices = tuple(indices)
            if used[indices]:
                continue
            used[indices] = True
            heappush(h, (-calc(*indices), indices))

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


main()

Solution (Python):
def main():
    import sys

    input = sys.stdin.readline

    x, y, z, k = map(int, input().split())
    A = sorted(map(int, input().split()), reverse=True)
    B = sorted(map(int, input().split()), reverse=True)
    C = sorted(map(int, input().split()), reverse=True)

    ret = []
    for p, a in enumerate(A, 1):
        for q, b in enumerate(B, 1):
            if p * q > k:
                break
            for r, c in enumerate(C, 1):
                if p * q * r > k:
                    break
                ret.append(a + b + c)
    ret.sort(reverse=True)
    print(*ret[:k], sep='\n')


main()