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):
from bisect import bisect_left, bisect_right
from math import ceil
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()

bc = [x+y for x in b for y in c]
bc.sort()


def count_bigger_than(X):
    res = 0
    for v in a:
        res += len(bc)-bisect_right(bc,X-v-1)
    return res

  

right_bound = 3*10**10 + 1
left_bound = 0 

while right_bound - left_bound > 1:
    m = (right_bound + left_bound)//2
    if count_bigger_than(m)>k:
        left_bound = m
    else:
        right_bound = m


V = left_bound

ans_que = []

for v in a:
    for i in range(len(bc)-1,-1,-1):
        if v+bc[i]>=V:
            ans_que.append(v+bc[i])
        else:
            break

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

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

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    X, Y, Z, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))
    C = list(map(int, readline().split()))

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

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

    ans = [0] * K
    added = {(0, 0, 0)}

    for idx in range(K):
        taste, i, j, k = heappop(hq)
        ans[idx] = -taste
        if i < X - 1 and (i + 1, j, k) not in added:
            heappush(hq, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
            added.add((i + 1, j, k))
        if j < Y - 1 and (i, j + 1, k) not in added:
            heappush(hq, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
            added.add((i, j + 1, k))
        if k < Z - 1 and (i, j, k + 1) not in added:
            heappush(hq, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
            added.add((i, j, k + 1))

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


if __name__ == '__main__':
    main()

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

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    X, Y, Z, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))
    C = list(map(int, readline().split()))

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

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

    ans = [0] * K
    added = {(0, 0, 0)}

    for idx in range(K):
        taste, i, j, k = heappop(hq)
        ans[idx] = -taste
        if i < X - 1 and (i + 1, j, k) not in added:
            heappush(hq, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
            added.add((i + 1, j, k))
        if j < Y - 1 and (i, j + 1, k) not in added:
            heappush(hq, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
            added.add((i, j + 1, k))
        if k < Z - 1 and (i, j, k + 1) not in added:
            heappush(hq, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
            added.add((i, j, k + 1))

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


if __name__ == '__main__':
    main()

Solution (Python):


import sys 
from heapq import *
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
X, Y, Z, K = map(int, readline().split())
A = list(map(int,readline().split()))
B = list(map(int,readline().split()))
C = list(map(int,readline().split()))

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

q = [(-A[0]-B[0]-C[0],0,0,0)]
heapify(q)
result = []
used = set()

while True:
    S,i,j,k = heappop(q)
    result.append(-S)
    if len(result) == K:
        break
    if i < X-1 and (i+1,j,k) not in used:
        heappush(q,(-A[i+1]-B[j]-C[k],i+1,j,k))
        used.add((i+1,j,k))
    if j < Y-1 and (i,j+1,k) not in used:
        heappush(q,(-A[i]-B[j+1]-C[k],i,j+1,k))
        used.add((i,j+1,k))
    if k < Z-1 and (i,j,k+1) not in used:
        heappush(q,(-A[i]-B[j]-C[k+1],i,j,k+1))
        used.add((i,j,k+1))

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

Solution (Python):


import sys 
from itertools import product
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
X, Y, Z, K = map(int, readline().split())
A = list(map(int,readline().split()))
B = list(map(int,readline().split()))
C = list(map(int,readline().split()))

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

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

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

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

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    X, Y, Z, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))
    C = list(map(int, readline().split()))

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

    hq = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
    ans = [0] * K
    seen = {(0, 0, 0)}

    for idx in range(K):
        s, i, j, k = heappop(hq)
        ans[idx] = -s

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

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    X, Y, Z, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))
    C = list(map(int, readline().split()))

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

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

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

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

    return


if __name__ == '__main__':
    main()

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

ans = []
for i in range(X):
    for j in range(Y):
        ij = (i+1)*(j+1)
        if ij > K:
            break
        for k in range(Z):
            if ij*(k+1) > K:
                break
            ans.append(A[i]+B[j]+C[k])
ans = sorted(ans, reverse=True)
for i in range(K):
    print(ans[i])

Solution (Python):
from heapq import heappush, heappop

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

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

    
    heap = []
    heappush(heap, (-(a[0]+b[0]+c[0]), 0, 0, 0))
    s = set()
    for _ in range(k):
        value, i, j, k = heappop(heap)
        print(-value)
        if (i+1, j, k) not in s and i < x - 1:
            s.add((i+1, j, k))
            heappush(heap, (-(a[i+1]+b[j]+c[k]), i+1, j, k))
        if (i, j+1, k) not in s and j < y - 1:
            s.add((i, j+1, k))
            heappush(heap, (-(a[i]+b[j+1]+c[k]), i, j+1, k))
        if (i, j, k+1) not in s and k < z - 1:
            s.add((i, j, k+1))
            heappush(heap, (-(a[i]+b[j]+c[k+1]), i, j, k+1))

main()

Solution (Python):
import sys
from collections import deque
from functools import lru_cache
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]

def main():
    X, Y, Z, K = read_values()
    A = read_list()
    B = read_list()
    C = read_list()

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

    P = []
    for c in C:
        heapq.heappush(P, (-(c + AB[0]), 0))
    
    for _ in range(K):
        s, i = heapq.heappop(P)
        print(-s)
        if i + 1 < len(AB):
            heapq.heappush(P, (s + (AB[i] - AB[i + 1]), i + 1))


if __name__ == "__main__":
    main()

Solution (Python):
icase=0
if icase==0:
    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)

kk=1

ix=0
iy=0
iz=0
ss=[]
s=a[ix]+b[iy]+c[iz]

xyz=[]
xyz.append((ix+1,iy,iz))
xyz.append((ix,iy+1,iz))
xyz.append((ix,iy,iz+1))



ss.append(s)
while kk<k:
    kk+=1
    s1=0
    for ix,iy,iz in xyz:
        s2=a[ix]+b[iy]+c[iz]
        if s2>s1:
            s1=s2
            ixx,iyy,izz=ix,iy,iz

    xyz.remove((ixx,iyy,izz))
    if ixx<x-1 and ((ixx+1,iyy,izz) not in xyz): 
        xyz.append((ixx+1,iyy,izz))
    if iyy<y-1 and ((ixx,iyy+1,izz) not in xyz): 
        xyz.append((ixx,iyy+1,izz))
    if izz<z-1 and ((ixx,iyy,izz+1) not in xyz): 
        xyz.append((ixx,iyy,izz+1))


    ss.append(s1)

for kk in range(k):
    print(ss[kk])

Solution (Python):
import sys



def input():
    return sys.stdin.readline().rstrip()


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

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

    L = []

    for i in range(X):
        s =K//(i+1)
        for j in range(min(Y, s + 1)):
            t =A[i] +B[j]
            for k in range(min(Z, s // (j + 1) + 1)):
                L.append(t + C[k])

    L.sort(reverse=True)

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


if __name__ == "__main__":
    main()

Solution (Python):
import sys



def input():
    return sys.stdin.readline().rstrip()


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

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

    L = []

    for i in range(X):
        for j in range(min(Y, K // (i + 1) + 1)):
            for k in range(min(Z, K // (i + 1) // (j + 1) + 1)):
                L.append(A[i] + B[j] + C[k])

    L.sort(reverse=True)

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


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import heapq



def input():
    return sys.stdin.readline().rstrip()


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

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

    Q = []
    S = set()
    heapq.heapify(Q)
    heapq.heappush(Q, (-A[0] - B[0] - C[0], 0, 0, 0))
    for i in range(K):
        m, a, b, c = heapq.heappop(Q)
        print(-m)
        if (a + 1, b, c) not in S and a + 1 < X:
            heapq.heappush(Q, (-A[a + 1] - B[b] - C[c], a + 1, b, c))
            S.add((a + 1, b, c))
        if (a, b + 1, c) not in S and b + 1 < Y:
            heapq.heappush(Q, (-A[a] - B[b + 1] - C[c], a, b + 1, c))
            S.add((a, b + 1, c))
        if (a, b, c + 1) not in S and c + 1 < Z:
            heapq.heappush(Q, (-A[a ] - B[b] - C[c+1], a, b, c + 1))
            S.add((a, b, c + 1))


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import heapq



def input():
    return sys.stdin.readline().rstrip()


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

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

    L=[]

    for i in range(X):
        for j in range(Y):
            for k in range(X):
                if (i+1)*(j+1)*(k+1)<=K:
                    L.append(A[i]+B[j]+C[k])
                else:
                    break

    L.sort(reverse=True)

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



if __name__ == "__main__":
    main()

Solution (Python):
import sys
import heapq



def input():
    return sys.stdin.readline().rstrip()


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

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

    a = 0
    b = 0
    c = 0
    k = 1

    L = []

    for i in range(X):
        for j in range(Y):
            L.append(A[i] + B[j])

    L.sort(reverse=True)
    newL = L[:min(3000, X * Y)]
    XY = len(newL)
    L=[]
    for i in range(XY):
        for j in range(Z):
            L.append(newL[i]+C[j])

    L.sort(reverse=True)

    for j in range(K):
        print(L[j])



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

from heapq import heappop,heappush,heapify
def main1(a,b,c,k):
  ans=[]
  for x in a:
    for y in b:
      ans.append(-x-y)
  ans.sort()
  ans=ans[:k] if len(ans)>=k else ans+[0]*(k-len(ans))
  ans1=[]
  for z in c:
    for xy in ans:
      heappush(ans1,xy-z)
  for _ in range(k):
    print(-heappop(ans1))
def main2(a,b,c,k):
  a.sort(reverse=True)
  b.sort(reverse=True)
  c.sort(reverse=True)
  al=[]
  for i,x in enumerate(a):
    for j,y in enumerate(b):
      for l,z in enumerate(c):
        if (i+1)*(j+1)*(l+1)<=k:
          al.append(x+y+z)
        else:
          break
  al.sort()
  for _ in range(k):
    print(al.pop())

main2(a,b,c,k)

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

from heapq import heappop,heappush,heapify
def main1(a,b,c,k):
  ans=[]
  for x in a:
    for y in b:
      ans.append(-x-y)
  ans.sort()
  ans=ans[:k] if len(ans)>=k else ans+[0]*(k-len(ans))
  ans1=[]
  for z in c:
    for xy in ans:
      heappush(ans1,xy-z)
  for _ in range(k):
    print(-heappop(ans1))
main1(a,b,c,k)

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

Solution (Python):
x,y,z,k=map(int,input().split())
a=sorted(list(map(int,input().split())),reverse=True)
b=sorted(list(map(int,input().split())),reverse=True)
c=sorted(list(map(int,input().split())),reverse=True)
ab=[]
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort(reverse=True)
ans=[]
for i in range(min(k,x*y)):
    for j in range(z):
        ans.append(ab[i]+c[j])
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Solution (Python):
'''
Created on 2020/09/08

@author: harurun
'''
def main():
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write

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




  d=[]
  for i in A:
    for j in B:
      d.append(i+j)
  d.sort(reverse=True)
  d=d[:K]
  ans=[]
  for k in d:
    for l in C:
      ans.append(k+l)
  ans.sort(reverse=True)
  for m in range(K):
    print(ans[m])
  return
main()

Solution (Python):
import heapq
from collections import defaultdict
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]

used = defaultdict(bool)
pq = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
used[(0, 0, 0)] = True
heapq.heapify(pq)

for _ in range(K):
    d, i, j, k = heapq.heappop(pq)
    print(-d)
    if i + 1 < x and not used[(i + 1, j, k)]:
        heapq.heappush(pq, (-(a[i + 1] + b[j] + c[k]), i + 1, j, k))
        used[(i + 1, j, k)] = True
    if j + 1 < y and not used[(i, j + 1, k)]:
        heapq.heappush(pq, (-(a[i] + b[j + 1] + c[k]), i, j + 1, k))
        used[(i, j + 1, k)] = True
    if k + 1 < z and not used[(i, j, k + 1)]:
        heapq.heappush(pq, (-(a[i] + b[j] + c[k + 1]), i, j, k + 1))
        used[(i, j, k + 1)] = True

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]

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

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]


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

Solution (Python):
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  


X,Y,Z,K = MI()
A,B,C = LI(),LI(),LI()
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

ANS = []
for i in range(X):
    if i >= K:
        break
    a = A[i]
    for j in range(Y):
        if i*j >= K:
            break
        b = B[j]
        for k in range(Z):
            if i*j*k >= K:
                break
            c = C[k]
            ANS.append(a+b+c)

ANS.sort(reverse=True)
print(*ANS[: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 = sorted(list(map(int, input().split())))[::-1]

a1 = sorted(a[i] + b[j] for i in range(x) for j in range(y))[::-1]
a2 = sorted(a1[i] + c[j] for i in range(min(len(a1), k))
            for j in range(z))[::-1]
for i in range(k):
    print(a2[i])

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

x,y,z,k = inpl()
a = inpl()
b = inpl()
c = inpl()
ab = []
for s in a:
    for t in b:
        ab.append(s+t)
ab.sort(reverse = True)
ab = ab[:3000]
abc = []
for s in ab:
    for j in c:
        abc.append(s+j)
abc.sort(reverse = True)
print(*abc[:k])

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

X, Y, Z, K = mapint()
As = list(mapint())
Bs = list(mapint())
Cs = list(mapint())
As.sort(reverse=True)
Bs.sort(reverse=True)
Cs.sort(reverse=True)

from heapq import heappop, heappush
Q = []
heappush(Q, (-(As[0]+Bs[0]+Cs[0]), 0, 0, 0))
s = set()
for _ in range(K):
    v, a, b, c = heappop(Q)
    print(-v)
    if a+1<X and not (a+1, b, c) in s:
        s.add((a+1, b, c))
        heappush(Q, (-(As[a+1]+Bs[b]+Cs[c]), a+1, b, c))
    if b+1<Y and not (a, b+1, c) in s:
        s.add((a, b+1, c))
        heappush(Q, (-(As[a]+Bs[b+1]+Cs[c]), a, b+1, c))
    if c+1<Z and not (a, b, c+1) in s:
        s.add((a, b, c+1))
        heappush(Q, (-(As[a]+Bs[b]+Cs[c+1]), a, b, c+1))

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 = []
AC = []
BC = []
for a in A:
    for b in B:
        AB.append(a+b)
    for c in C:
        AC.append(a+c)
for b in B:
    for c in C:
        BC.append(b+c)
AB.sort(reverse=True)
AC.sort(reverse=True)
BC.sort(reverse=True)

import heapq
hq = []
for x in AB[:K]:
    for c in C:
        heapq.heappush(hq, -x-c)
for x in BC[:K]:
    for a in A:
        heapq.heappush(hq, -x-a)
for x in AC[:K]:
    for b in B:
        heapq.heappush(hq, -x-b)

ans = []
for _ in range(K):
    ans.append(-heapq.heappop(hq))
    heapq.heappop(hq)
    heapq.heappop(hq)
print(*ans, sep='\n')

Solution (Python):
x, y, z, k = map(int, input().split())
alst = list(map(int, input().split()))
blst = list(map(int, input().split()))
clst = list(map(int, input().split()))
alst.sort(reverse = True)
blst.sort(reverse = True)
clst.sort(reverse = True)
ans = []
for i in range(x):
    for j in range(y):
        if i * j > k:
            break
        for l in range(z):
            if i * j * l > k:
                break
            ans.append(alst[i] + blst[j] + clst[l])
ans.sort(reverse = True)
print(*ans[:k], sep = "\n")

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  


X,Y,Z,K = MI()
A,B,C = LI(),LI(),LI()
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

ANS = []
for i in range(X):
    if i >= K:
        break
    a = A[i]
    for j in range(Y):
        if i*j >= K:
            break
        b = B[j]
        for k in range(Z):
            if i*j*k >= K:
                break
            c = C[k]
            ANS.append(a+b+c)

ANS.sort(reverse=True)
print(*ANS[:K],sep='\n')

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 

from decimal import *



X, Y, Z, K = MAP()
A = sorted(LIST(), reverse = True)
B = sorted(LIST(), reverse = True)
C = sorted(LIST(), reverse = True)

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

for a in sorted(ans)[::-1][:K]:
	print(a)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 

from decimal import *



X, Y, Z, K = MAP()
A = sorted(LIST(), reverse = True)
B = sorted(LIST(), reverse = True)
C = sorted(LIST(), reverse = True)

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

AB.sort(reverse = True)
AB = AB[:3000]
ABC = sorted([C[0]+AB[i] for i in range(min(3000, X*Y))], reverse = True)

for j in range(1, Z):
	ABC_tmp = sorted([C[j]+AB[i] for i in range(min(3000, X*Y))])
	ABC += ABC_tmp
	ABC.sort(reverse = True)
	ABC = ABC[:3000]

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 

from decimal import *



X, Y, Z, K = MAP()
A = sorted(LIST(), reverse = True)
B = sorted(LIST(), reverse = True)
C = sorted(LIST(), reverse = True)

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

AB.sort(reverse = True)
AB = AB[:3000]
ABC = sorted([C[0]+AB[i] for i in range(min(3000, X*Y))], reverse = True)

for j in range(1, Z):
	ABC_tmp = sorted([C[j]+AB[i] for i in range(min(3000, X*Y))])
	ABC += ABC_tmp
	ABC.sort(reverse = True)
	ABC = ABC[:3000]

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

Solution (Python):
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
ceil = math.ceil
x,y,z,k = ma()
A = lma()
B=lma()
C=lma()
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
i,j,m = 0,0,0
h=[(-A[i]-B[j]-C[m],i,j,m)]
cnt=0
d=collections.defaultdict(lambda:True)
while cnt<k:
    ans,i,j,m=hq.heappop(h)
    print(-ans)
    if i+1<x and d[(i+1,j,m)]:
        hq.heappush(h,(-A[i+1]-B[j]-C[m],i+1,j,m))
        d[(i+1,j,m)]=False
    if j+1<y and d[(i,j++1,m)]:
        hq.heappush(h,(-A[i]-B[j+1]-C[m],i,j+1,m))
        d[(i,j+1,m)]=False
    if m+1<z and d[(i,j,m+1)]:
        hq.heappush(h,(-A[i]-B[j]-C[m+1],i,j,m+1))
        d[(i,j,m+1)]=False
    cnt+=1

Solution (Python):
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
ceil = math.ceil
x,y,z,k = ma()
A = lma()
B=lma()
C=lma()
AB = [a+b for a in A for b in B]
AB.sort(reverse=True)
C.sort(reverse=True)
ans=[]
iab=min(k,x*y)
ic=min(k,z)
for i in range(ic):
    for j in range(iab):
        ans.append(AB[j]+C[i])
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Solution (Python):
import heapq as hq
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)
abc = [(-(A[0]+B[0]+C[0]), 0,0,0)]
used = set((0,0,0))
while K > 0:
    K -= 1
    p,i,j,k = hq.heappop(abc)
    print(-p)
    for a,b,c in [(i+1,j,k), (i,j+1,k), (i,j,k+1)]:
        if a < x and b < y and c < z and (a,b,c) not in used:
            used.add((a,b,c))
            hq.heappush(abc, (-(A[a]+B[b]+C[c]), a,b,c))