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 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)
l1=sorted([i+j for i in a for j in b],reverse=True)[:k]
l2=sorted([i+j for i in l1 for j in c],reverse=True)[:k]
for i in l2:
  print(i)

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

a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ans = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
heapq.heapify(ans)
added = {"0 0 0"}
for _ in range(k):
    w = heapq.heappop(ans)
    print(-w[0])
    i, j, m = w[1:]
    dx = [1, 0, 0]
    dy = [0, 1, 0]
    dz = [0, 0, 1]
    for r in range(3):
        ni, nj, nm = i + dx[r], j + dy[r], m + dz[r]
        if not (ni < x and nj < y and nm < z):
            continue
        g = " ".join(map(str, (ni, nj, nm)))
        if g not in added:
            added.add(g)
            heapq.heappush(ans, (-(a[ni] + b[nj] + c[nm]), ni, nj, nm))

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)
ans = []
for i in range(x):
    for j in range(y):
        for m in range(z):
            if (i+1) * (j+1) * (m+1) > k:
                break
            ans.append(a[i] + b[j] + c[m])

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

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

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 abc[:k]:
    print(i)

Solution (Python):
import heapq
from collections import defaultdict


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



que = []
heapq.heapify(que)

heapq.heappush(que, ((A[0] + B[0] + C[0])*-1, (0, 0, 0)))


used = defaultdict(int)
used[(0, 0, 0)] = 1

ans = []
while len(ans) < k:
    total, comb = heapq.heappop(que)
    ans.append(total*-1)
    a, b, c = comb
    
    for p, q, r in [[a+1, b, c], [a, b+1, c], [a, b, c+1]]:
        if used[(p, q, r)] == 0 and p < x and q < y and r < z:
            heapq.heappush(que, ((A[p] + B[q] + C[r])*-1, (p, q, r)))
            used[(p, q, r)] = 1

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


ABC = []


from collections import defaultdict
d = defaultdict(int)

que = []

def HeapPush(a, b, c):
    if d[(a, b, c)] == 0:
        heapq.heappush(que, ((A[a] + B[b] + C[c])*-1, a, b, c))  
        d[(a, b, c)] += 1


HeapPush(0, 0, 0)

while len(ABC) < k:
    
    abc, a, b, c = heapq.heappop(que)
    ABC.append(abc*-1)  
    
    if a + 1 < x:
        HeapPush(a+1, b, c)
    if b + 1 < y:
        HeapPush(a, b+1, c)
    if c + 1 < z:
        HeapPush(a, b, c+1)
   

for abc in ABC:
    print(abc)

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

limit = [X,Y,Z]

nums = []
heapq.heapify(nums)
num = 0
abc = [[] for i in range(3)]

for i in range(3):
  abc[i] = sorted(list(map(int,input().split())),reverse = True)
  num += abc[i][0] * -1
  
heapq.heappush(nums,[num,0,0,0])
yet = []

for i in range(K):
  M = heapq.heappop(nums)
  for j in range(3):
    ID = M[1:]
    ID[j] += 1
    if ID[j] < limit[j]:
      a = ID[0]
      b = ID[1]
      c = ID[2]
      m = (abc[0][a] + abc[1][b] + abc[2][c]) * - 1
      if ID not in yet:
        heapq.heappush(nums,[m,a,b,c])
        yet.append(ID)
  print(M[0] * -1)

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

a = list(map(int,input().split()))

b = list(map(int,input().split()))

c = list(map(int,input().split()))

ab = []
ans = []
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort(reverse=True)

if len(ab) < k:
    for i in range(len(ab)):
        for j in range(z):
            ans.append(ab[i]+c[j])
else:
    for i in range(k):
        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):

import sys
import heapq

def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    def next_state(s):
        def _next_state(i, j, k):
            return -sum([A[i], B[j], C[k]]), (i, j, k)
        _, (i, j, k) = s
        if i+1 < len(A):
            yield _next_state(i+1, j, k)
        if j+1 < len(B):
            yield _next_state(i, j+1, k)
        if k+1 < len(C):
            yield _next_state(i, j, k+1)

    ans = set()
    A = tuple(sorted(A, reverse=True))
    B = tuple(sorted(B, reverse=True))
    C = tuple(sorted(C, reverse=True))
    q = [(-sum([A[0], B[0], C[0]]), (0, 0, 0))]
    ans.add(q[0])
    for _ in range(K):
        s = heapq.heappop(q)
        for ns in next_state(s):
            if ns in ans:
                continue
            ans.add(ns)
            heapq.heappush(q, ns)
    return sorted((-v for v, _ in ans), reverse=True)[:K]



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

if __name__ == '__main__':
    main()

Solution (Python):
def binsearch(l, r, fn):
    while r - l > 1:
        m = (l + r) // 2
        if fn(m):
            l = m
        else:
            r = m
    return l

def main():
    X, Y, Z, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = sorted(map(int, input().split()), reverse=True)
    AB = sorted((a + b for a in A for b in B), reverse=True)
    def helper(x):
        r = 0
        t = len(C) - 1
        for ab in AB:
            while t >= 0 and C[t] + ab < x:
                t -= 1
            r += t + 1
            if r >= K:
                return True
            if t < 0:
                return False
        return r >= K
    l = binsearch(AB[-1] + C[-1], AB[0] + C[0] + 1, helper)
    r = []
    t = len(C) - 1
    for ab in AB:
        while t >= 0 and C[t] + ab < l + 1:
            t -= 1
        if t < 0:
            break
        r.extend(ab + c for c in C[:t + 1])
    r.sort(reverse=True)
    r += [l] * (K - len(r))
    print('\n'.join(str(i) for i in r))

main()

Solution (Python):
from heapq import heappush, heappop
X, Y, Z, K, *L = map(int, open(0).read().split())
A = L[:X]
B = L[X:X+Y]
C = L[X+Y:]
ab = []
for i in range(X):
  for j in range(Y):
    ab += [A[i]+B[j]]
ab.sort(reverse=True)
ls = []
for i in range(Z):
  heappush(ls,(-ab[0]-C[i],i,0))
for i in range(K):
  m, j, cnt = heappop(ls)
  print(-m)
  if cnt+1<X*Y:
    heappush(ls,(-ab[cnt+1]-C[j],j,cnt+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.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

ab=[i+j for j in b for i in a]
ab.sort(reverse=True)
abc=[i+j for j in c[:k] for i in ab[:k]]
abc.sort(reverse=True)

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

Solution (Python):
import sys,heapq
def input():
    return sys.stdin.readline()[:-1]
X,Y,Z,K=map(int,input().split())
A=list(map(lambda x:int(x)*-1,input().split()))
B=list(map(lambda x:int(x)*-1,input().split()))
C=list(map(lambda x:int(x)*-1,input().split()))
h=[]
heapq.heapify(h)
for i in A:
    for j in B:
        heapq.heappush(h,i+j)
m=min(K,len(h))
l=[None]*m
for i in range(m):
    l[i]=heapq.heappop(h)
h=[]
heapq.heapify(h)
for i in l:
    for j in C:
        heapq.heappush(h,i+j)
for i in range(K):
    print(heapq.heappop(h)*-1)

Solution (Python):
from heapq import heappop, heappush

X, Y, Z, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
A.sort(reverse=True)


D = [i+j for j in C for i in B]
D.sort(reverse=True)


q = []
for i in A[:K]:
    for j in D[:K]:
        heappush(q, -(i+j))

for _ in range(K):
    print(-heappop(q))

Solution (Python):
from heapq import heappop, heappush

X, Y, Z, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
A.sort(reverse=True)

D = [i+j for j in C for i in B]
D.sort(reverse=True)

q = []
for i in A[:K]:
    for j in D[:K]:
        heappush(q, -(i+j))

for _ in range(K):
    print(-heappop(q))

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)

def Nmb(tp):
  return A[tp[0]-1]+B[tp[1]-1]+C[tp[2]-1]

from heapq import heapify, heappop, heappush, heappushpop
hp = []
heapify(hp)
tp = (1, 1 , 1)
heappush(hp, (-Nmb(tp),)+tp)
hist = {tp}

for k in range(K):
  
  rec = heappop(hp)
  print(rec[0]*-1)


  tp = (rec[1]+1, rec[2], rec[3])
  if tp[0]<=X and not tp in hist:
    heappush(hp, (-Nmb(tp),)+tp)
    hist.add(tp)

  tp = (rec[1], rec[2]+1, rec[3])
  if tp[1]<=Y and not tp in hist:
    heappush(hp, (-Nmb(tp),)+tp)
    hist.add(tp)

  tp = (rec[1], rec[2], rec[3]+1)
  if tp[2]<=Z and not tp in hist:
    heappush(hp, (-Nmb(tp),)+tp)
    hist.add(tp)

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)

from heapq import heapify, heappop, heappush, heappushpop
hp=[]
hist=set([])
heapify(hp)
hp0=[-(A[0]+B[0]+C[0]), 1, 1, 1]
heappush(hp, hp0)
hist.add(tuple(hp0[1:]))
k=0
while k<K:
  k+=1
  sabc=heappop(hp)
  print(sabc[0]*-1)
  for i in [1,2,3]:
    sabc_next = sabc[:]
    sabc_next[i]=sabc[i]+1
    sp = sabc_next[1]*sabc_next[2]*sabc_next[3]
    if sp<=K and sabc_next[1]<=X and sabc_next[2]<=Y and sabc_next[3]<=Z:
      sabc_next[0] = - (A[sabc_next[1]-1]+B[sabc_next[2]-1]+C[sabc_next[3]-1])
      if not tuple(sabc_next[1:]) in hist: 
        heappush(hp,sabc_next)
        hist.add(tuple(sabc_next[1:]))

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

D = [i+j for j in C for i in B]
D.sort(reverse=True)
E = [i+j for j in D[:K] for i in A[:K]]
E.sort(reverse=True)

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

Solution (Python):
from heapq import heappush, heappop

X, Y, Z, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)


def f(x):
    cnt = 0
    for i in A:
        for j in B:
            for k in C:
                if i+j+k<x:break
                cnt += 1
                if cnt>=K:
                    return True
    return False

def get_boarder():
    l, r = 0, A[0]+B[0]+C[0]
    while r-l>1:
        m = (l+r)//2
        if f(m):
            l = m
        else:
            r = m
    return l

boarder = get_boarder()
q = []
for i in A:
    for j in B:
        for k in C:
            if i+j+k<boarder:break
            heappush(q, -(i+j+k))

for _ in range(K):
    print(-heappop(q))

Solution (Python):
import bisect

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)

YZ = Y*Z
BC = []
for b in B:
  for c in C:
    BC.append(b+c)

BC.sort()

def count_large_2(L1, L2, x):
  L1.sort()
  L2.sort(reverse=True)
  N1, N2 = len(L1), len(L2)
  i = j = 0
  c = 0
  while i<N1 and j<N2:
    if L1[i]+L2[j] >= x:
      c += 1
      j += 1
      if j >= N2:
        c += (N1-i-1) * N2
    else:
      i += 1
      if i>=N1:
        continue
      c += j
  return c


def check(S):
  c = count_large_2(BC, A, S)  
  return c >= K



Sl = 0
Sr = A[0]+B[0]+C[0]+1

while Sr - Sl > 1:
  mid = (Sl + Sr) // 2
  if check(mid):
    Sl = mid
  else:
    Sr = mid


Ans = []
for a in A:
  for b in B:
    if a + b + C[0] < Sr:
      break
    for c in C:
      if a + b + c >= Sr:
        Ans.append(a+b+c)
      else:
        break

Ans.sort(reverse=True)
Ans = Ans + [Sl]*(K-len(Ans))
for ans in Ans:
  print(ans)

Solution (Python):
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *
import copy

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)
dic={A[0]+B[0]+C[0]:["0_0_0"]}
usedlst=set(["0_0_0"])
lst=[]
D=[-(A[0]+B[0]+C[0])]
heapify(D)

def strmode(a,b,c):
    return(str(a)+"_"+str(b)+"_"+str(c),A[a]+B[b]+C[c])
while len(lst)<K:

    value=heappop(D)
    lst.append(-value)
    a,b,c=map(int,dic[-value].pop().split("_"))
    for x,y,z in ([[a+1,b,c],[a,b+1,c],[a,b,c+1]]):

        if x<X and y<Y and z<Z:
            name,value=strmode(x,y,z)
            if not name in usedlst:
                usedlst.add(name)
                heappush(D,-value)
                if not value in dic:
                    dic.update({value:[name]})
                else:
                    dic[value].append(name)
for i in lst:
    print(i)

Solution (Python):
def main():
  x,y,z,k=map(int,input().split())
  one=[-int(i) for i in input().split()]
  two=[-int(i) for i in input().split()]
  three=[-int(i) for i in input().split()]
  one.sort()
  two.sort()
  three.sort()
  inf=10**11
  one.append(inf)
  two.append(inf)
  three.append(inf)
  
  import heapq 
  
  q=[[one[0]+two[0]+three[0],0,0,0],[inf]]
  heapq.heapify(q)
  for i in range(k):
    m=heapq.heappop(q)
    while q[0]==m:
      m=heapq.heappop(q)
    print(-m[0])
    i,j,k=m[1],m[2],m[3]
    heapq.heappush(q,[one[i+1]+two[j]+three[k],i+1,j,k])
    heapq.heappush(q,[one[i]+two[j+1]+three[k],i,j+1,k])
    heapq.heappush(q,[one[i]+two[j]+three[k+1],i,j,k+1])

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

ans=max(A)+max(B)+max(C)

temp=[]
for a in A:
    for b in B:
        temp.append(a+b)

temp.sort()

dif=[temp[i]-temp[-1] for i in range(len(temp))[::-1]]
dif_c=[C[i]-C[-1] for i in range(len(C))[::-1]]

dif=dif[:K]

temp=[]
for d in dif:
    for dc in dif_c:
        temp.append(ans+d+dc)
temp.sort()
temp=temp[::-1]

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

Solution (Python):
from heapq import nlargest
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=nlargest(k,(x+y for x in a for y in b))
abc=nlargest(k,(xy+z for xy in ab for z in c))
for i in abc:
  print(i)

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 999999999
R = 10**9 + 7
import heapq

def ddprint(x):
  if DBG:
    print(x)

x,y,z,k = inm()
a = inl()
b = inl()
c = inl()

h = []
for i in a:
    for j in b:
        if len(h)<k:
            heapq.heappush(h,i+j)
        else:
            heapq.heappushpop(h,i+j)
g = []
for i in c:
    for j in h:
        if len(g)<k:
            heapq.heappush(g,i+j)
        else:
            heapq.heappushpop(g,i+j)

d = []
for i in range(k):
    d.append(heapq.heappop(g))
for i in range(k-1,-1,-1):
    print(d[i])

Solution (Python):
from heapq import heappush, heappop


def solve(k, ax, bx, cx, x, y, z):
    ans = []
    heap = [(-(ax[0] + bx[0] + cx[0]), 0, 0, 0)]
    d = {(0, 0, 0)}

    for _ in range(k):
        s, ai, bi, ci = heappop(heap)
        ans += [-s]

        if (ai+1, bi, ci) not in d and ai+1 < x:
            heappush(heap, (-(ax[ai+1] + bx[bi] + cx[ci]), ai+1, bi, ci))
            d.add((ai+1, bi, ci))
        if (ai, bi+1, ci) not in d and bi+1 < y:
            heappush(heap, (-(ax[ai] + bx[bi+1] + cx[ci]), ai, bi+1, ci))
            d.add((ai, bi+1, ci))
        if (ai, bi, ci+1) not in d and ci+1 < z:
            heappush(heap, (-(ax[ai] + bx[bi] + cx[ci+1]), ai, bi, ci+1))
            d.add((ai, bi, ci+1))

    return ans


if __name__ == "__main__":
    x, y, z, k = map(int, input().split())
    ax = sorted(list(map(int, input().split())), reverse=True)
    bx = sorted(list(map(int, input().split())), reverse=True)
    cx = sorted(list(map(int, input().split())), reverse=True)
    ans = solve(k, ax, bx, cx, x, y, z)
    print(*ans, sep="\n")

Solution (Python):
X,Y,Z,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
A.sort()
B.sort()
C.sort()
AB=[]
for a in A:
  for b in B:
    AB.append(-(a+b))
import heapq
heapq.heapify(AB)
ABC=[]
for _ in range(min(K,len(AB))):
  ab=heapq.heappop(AB)
  for c in C:
    heapq.heappush(ABC,ab-c)
for i in range(K):
  print(-heapq.heappop(ABC))

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()
AB=[]
for a in A:
  for b in B:
    AB.append(-(a+b))
import heapq
heapq.heapify(AB)
ABC=[]
for _ in range(min(K,len(AB))):
  ab=heapq.heappop(AB)
  for c in C:
    heapq.heappush(ABC,ab-c)
for i in range(K):
  print(-heapq.heappop(ABC))

Solution (Python):
import sys


def solve(k, ax, bx, cx):
    ab = []
    for a in ax:
        for b in bx:
            ab.append(a+b)

    ab.sort(reverse=True)
    abk = ab[:k]

    ans = []
    for c in cx:
        for abi in abk:
            ans.append(c+abi)

    return sorted(ans, reverse=True)[:k]


if __name__ == '__main__':
    _, _, _, k = map(int, input().split())
    ax = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
    bx = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
    cx = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)

    ans = solve(k, ax, bx, cx)

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

Solution (Python):
from heapq import heappop, heappush
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)

queue = []

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

      val = A[x] + B[y] + C[z]
      heappush(queue, -val)

for _ in range(K):
  print(-heappop(queue))

Solution (Python):
from heapq import nlargest

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 = nlargest(k, (a + b for a in A for b in B))
ABC = nlargest(k, (ab + c for ab in AB for c in C))
[print(abc) for abc in ABC]

Solution (Python):
import sys
def input():
    return sys.stdin.readline()
X, Y, Z, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

abl = sorted([a + b for a in A for b in B], reverse=True)[:K]
abcl = sorted([ab + c for ab in abl for c in C], reverse=True)[:K]
for abc in abcl:
    print(abc)

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)

seen = [(0, 0, 0)]
stack = [((0, 0, 0), a[0]+b[0]+c[0])]
for _ in range(k):
    (na, nb, nc), s = stack.pop()
    print(s)
    if na<x-1 and (na+1, nb, nc) not in seen:
        seen.append((na+1, nb, nc))
        stack.append(((na+1, nb, nc), a[na+1]+b[nb]+c[nc]))
    if nb<y-1 and (na, nb+1, nc) not in seen:
        seen.append((na, nb+1, nc))
        stack.append(((na, nb+1, nc), a[na]+b[nb+1]+c[nc]))
    if nc<z-1 and (na, nb, nc+1) not in seen:
        seen.append((na, nb, nc+1))
        stack.append(((na, nb, nc+1) ,a[na]+b[nb]+c[nc+1]))
    stack.sort(key=lambda x:x[1])

Solution (Python):
import heapq

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

check = set()  

heap = []
heapq.heappush(heap, (A[0] + B[0] + C[0], 0, 0, 0))
check.add((0, 0, 0))

for i in range(K):
    s, a, b, c = heapq.heappop(heap)
    print(-s)
    if a + 1 < X and (a + 1, b, c) not in check:
        heapq.heappush(heap, (A[a + 1] + B[b] + C[c], a + 1, b, c))
        check.add((a + 1, b, c))
    if b + 1 < Y and (a, b + 1, c) not in check:
        heapq.heappush(heap, (A[a] + B[b + 1] + C[c], a, b + 1, c))
        check.add((a, b + 1, c))
    if c + 1 < Z and (a, b, c + 1) not in check:
        heapq.heappush(heap, (A[a] + B[b] + C[c + 1], a, b, c + 1))
        check.add((a, b, c + 1))

Solution (Python):
def d_cake_123_binary_search():
    """editorialの解法4 O(K^2log(max(P))"""
    
    import sys
    import bisect
    input = sys.stdin.readline
    X, Y, Z, K = [int(i) for i in input().split()]
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))
    C = tuple(map(int, input().split()))
    ab = sorted([e1 + e2 for e2 in B for e1 in A])  

    
    accepted, rejected = -1, 10**11
    while abs(accepted - rejected) > 1:
        mid = (accepted + rejected) // 2
        count = sum([len(ab) - bisect.bisect_left(ab, mid - e) for e in C])
        if count >= K:
            accepted = mid
        else:
            rejected = mid

    ans = []
    for e in C:
        idx = bisect.bisect_left(ab, accepted - e)
        ans.extend(e + _ab for _ab in ab[idx:])
    ans.sort(reverse=True)
    return '\n'.join(map(str, ans[:K]))

print(d_cake_123_binary_search())

Solution (Python):
def d_cake_123_sort_modified():
    """editorial の解法 1."""
    import sys
    import numpy as np
    input = sys.stdin.readline
    X, Y, Z, K = [int(i) for i in input().split()]
    A = np.array([int(i) for i in input().split()])
    B = np.array([int(i) for i in input().split()])
    C = np.array([int(i) for i in input().split()])

    _a, _b = np.meshgrid(A, B)
    ab = np.sort((_a + _b).flatten())[::-1][:K]
    _ab, _c = np.meshgrid(ab, C)
    ans = np.sort((_ab + _c).flatten())[::-1][:K]
    return '\n'.join(map(str, ans))

print(d_cake_123_sort_modified())

Solution (Python):
x, y, z, k = map(int, input().split())
alist = list(map(int, input().split()))
blist = list(map(int, input().split()))
clist = list(map(int, input().split()))

bclist=[]

for i in range(y):
    now=blist[i]
    for j in range(z):
        bclist.append(now+clist[j])

bclist=sorted(bclist)
bclist2=bclist[-k:]


ng=len(bclist2)
abclist=[]

for i in range(ng):
    now=bclist2[i]
    for j in range(x):
        abclist.append(now+alist[j])

abclist=sorted(abclist)
for i in range(k):
    print(abclist[-1-i])

Solution (Python):
import sys
import heapq as hq
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)
ans = []
for p in range(1,x+1):
  for q in range(1,y+1):
    for r in range(min(k//(p*q)+1,z)):
      ans.append(-(a[p-1]+b[q-1]+c[r]))
hq.heapify(ans)
for i in range(k):
  s = hq.heappop(ans)
  print(-s)

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)
ans = []
for p in range(1,x+1):
  for q in range(1,y+1):
    for r in range(min(k//(p*q)+1,z)):
      ans.append(-(a[p-1]+b[q-1]+c[r]))
hq.heapify(ans)
for i in range(k):
  s = hq.heappop(ans)
  print(-s)

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)
bc = [x + y for y in c for x in b]
bc.sort(reverse=True)
ans = [x + y for y in a for x in bc[:k]]
ans.sort(reverse=True)
print(*ans[:k], sep="\n")

Solution (Python):
X, Y, Z, K = map(int, input().split())
A = [int(aaa) for aaa in input().split()]
B = [int(bbb) for bbb in input().split()]
C = [int(ccc) for ccc in input().split()]
kari = []
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
for i in range(X):
    for j in range(Y):
        kari.append(A[i] + B[j])
karisize = len(kari)
kari = sorted(kari, reverse=True)
kouho_list = [0] * Z
index_list = [0] * Z
for k in range(Z):
    kouho_list[k] = kari[0] + C[k]
print(kouho_list[0])
if karisize == 1:
    kouho_list[0] = -10**9
else:
    index_list[0] += 1
    kouho_list[0] = kari[1] + C[0]
for p in range(K - 1):
    ans = max(kouho_list)
    max_index = kouho_list.index(ans)
    print(ans)
    if index_list[max_index] == karisize - 1:
        kouho_list[max_index] = -10 ** 9
    else:
        index_list[max_index] += 1
        kouho_list[max_index] = C[max_index] + kari[index_list[max_index]]

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)

result = []

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
      result.append(A[x] + B[y] + C[z])

for k in list(sorted(result, reverse=True))[:K]:
  print(k)

Solution (Python):
import sys
import heapq
def input(): return sys.stdin.readline().rstrip()

def main():
    X, Y, Z, K = map(int, input().split())
    A, B, C = [sorted(map(int, input().split()), reverse=True) for _ in range(3)]

    hp = []
    heapq.heappush(hp, (- A[0] - B[0] - C[0], 0, 0, 0))
    exists = set([(0, 0, 0)])
    for _ in range(K):
        v, x, y, z = heapq.heappop(hp)
        print(-v)
        if x + 1 < X and (x + 1, y, z) not in exists:
            heapq.heappush(hp, (v + A[x] - A[x+1], x + 1, y, z))
            exists.add((x + 1, y, z))
        if y + 1 < Y and (x, y + 1, z) not in exists:
            heapq.heappush(hp, (v + B[y] - B[y+1], x, y + 1, z))
            exists.add((x, y + 1, z))
        if z + 1 < Z and (x, y, z + 1) not in exists:
            heapq.heappush(hp, (v + C[z] - C[z+1], x, y, z + 1))
            exists.add((x, y, z + 1))

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from itertools import product
def input(): return sys.stdin.readline().rstrip()

def main():
    X, Y, Z, K = map(int, input().split())
    A, B, C = [sorted(map(int, input().split())) for _ in range(3)]

    AB = [a + b for a, b in product(A, B)]
    AB = sorted(AB, reverse=True)[:K]

    ABC = [ab + c for ab, c in product(AB, C)]
    ABC = sorted(ABC, reverse=True)[:K]
    for i in ABC:
        print(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, B, C = [sorted(map(int, input().split())) for _ in range(3)]

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

    ABC = [ab + c for ab in AB for c in C]
    ABC = sorted(ABC, reverse=True)[:K]
    for i in ABC:
        print(i)

if __name__ == '__main__':
    main()