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

Solution (Python):
x, y, z, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
result = []

def func1():
    E = [a+b for a in A for b in B]
    E.sort()
    if len(E) > k: E = E[-k:]
    F = [c+e for c in C for e in E]
    F.sort()
    return F[-k:][::-1]

[print(val) for val in func1()]

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

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

d = sorted(d, reverse=True)
d = d[0:min(k,len(d))]
w = len(d)

e = []
for i in range(min(k,z)):
    for j in range(min(k,w)):
        e.append(c[i]+d[j])

e = sorted(e, reverse=True)
for f in e[0:k]:
    print(f)

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

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

d = sorted(d, reverse=True)
d = d[0:min(k,len(d))]
w = len(d)

e = []
for i in range(min(k,z)):
    for j in range(w):
        e.append(c[i]+d[j])

e = sorted(e, reverse=True)
for f in e[0:k]:
    print(f)

Solution (Python):

from bisect import *
from collections import *
from heapq import *

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

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

_lst=[]
for a in lst:
    for c in C:
        _lst.append(a+c)

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

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=[]
for i in range(1,X+1):
    for j in range(1,Y+1):
        if i*j>3000:
            break
        for k in range(1,Z+1):
            if i*j*k<=K:
                D.append((i-1,j-1,k-1))
            else:
                break

E=[]
for i in D:
    E.append(A[i[0]]+B[i[1]]+C[i[2]])

E.sort(reverse=True)
E=E[:K]
for i in E:
    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)


Q =[]
heapq.heappush(Q, [-(A[0]+B[0]+C[0]), 0, 0, 0])
added_idx = set((0, 0, 0))

for i in range(K):
  nega_ans, x, y, z = heapq.heappop(Q)
  print(-nega_ans)
  if x+1 < X and (x+1, y, z) not in added_idx:
    heapq.heappush(Q, [-(A[x+1]+B[y]+C[z]), x+1, y, z])
    added_idx.add((x+1, y, z))
  if y+1 < Y and (x, y+1, z) not in added_idx:
    heapq.heappush(Q, [-(A[x]+B[y+1]+C[z]), x, y+1, z])
    added_idx.add((x, y+1, z))
  if z+1 < Z and (x, y, z+1) not in added_idx:
    heapq.heappush(Q, [-(A[x]+B[y]+C[z+1]), x, y, z+1])
    added_idx.add((x, y, z+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)

ids = []
for x in range(1, X+1):
  for y in range(1, Y+1):
    if x*y > K:
        break
    for z in range(1, Z+1):
      if x*y*z <= K:
        ids.append((x-1, y-1, z-1))
      else:
        break
      
Ans = []
for x, y, z in ids:
  Ans.append(A[x]+B[y]+C[z])

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

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[:K]

ABC = []
for ab in AB:
  for c in C:
    ABC.append(ab+c)

ABC.sort(reverse=True)
ABC = ABC[:K]

for ans in ABC:
  print(ans)

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 check(S):
  count = 0
  left = X*Y*Z
  for a in A:
    count += YZ - bisect.bisect_left(BC, S-a)
    left -= YZ
    if count >= K or count+left < K:
      break
  
  return count >= 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):
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)

import heapq
nums=[[-(A[0]+B[0]+C[0]), 0, 0, 0]]
heapq.heapify(nums)

sumi=set()
sumi.add((0, 0, 0))

for _ in range(K):
  out, a, b, c=heapq.heappop(nums)
  print(-out)
  if a<X-1 and not((a+1, b, c) in sumi):
    heapq.heappush(nums, [-(A[a+1]+B[b]+C[c]), a+1, b, c])
    sumi.add((a+1, b, c))
  if b<Y-1 and not((a, b+1, c) in sumi):
    heapq.heappush(nums, [-(A[a]+B[b+1]+C[c]), a, b+1, c])
    sumi.add((a, b+1, c))
  if c<Z-1 and not((a, b, c+1) in sumi):
    heapq.heappush(nums, [-(A[a]+B[b]+C[c+1]), a, b, c+1])
    sumi.add((a, b, c+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)

import heapq
nums=[[-(A[0]+B[0]+C[0]), 0, 0, 0]]
heapq.heapify(nums)

sumi=[[0, 0, 0]]
for _ in range(K):
  out, a, b, c=heapq.heappop(nums)
  print(-out)
  if a<X-1 and not([a+1, b, c] in sumi):
    heapq.heappush(nums, [-(A[a+1]+B[b]+C[c]), a+1, b, c])
    sumi.append([a+1, b, c])
  if b<Y-1 and not([a, b+1, c] in sumi):
    heapq.heappush(nums, [-(A[a]+B[b+1]+C[c]), a, b+1, c])
    sumi.append([a, b+1, c])
  if c<Z-1 and not([a, b, c+1] in sumi):
    heapq.heappush(nums, [-(A[a]+B[b]+C[c+1]), a, b, c+1])
    sumi.append([a, b, c+1])

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

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

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

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

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

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

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

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, len(AB))):
    for j in range(Z):
        ABC.append(AB[i] + C[j])

ABC.sort(reverse=True)
for i in range(K):
    print(ABC[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)

used=[]

heap=[]

heapq.heappush(heap,(-(A[0]+B[0]+C[0]),0,0,0))
used.append((0,0,0))

for i in range(K):
    u=heapq.heappop(heap)
    if u[1]+1<=X-1:
        if not((u[1]+1,u[2],u[3]) in used):
            used.append((u[1]+1,u[2],u[3]))
            heapq.heappush(heap,(-(A[u[1]+1]+B[u[2]]+C[u[3]]),u[1]+1,u[2],u[3]))
    
    if u[2]+1<=Y-1:
        if not((u[1],u[2]+1,u[3]) in used):
            used.append((u[1],u[2]+1,u[3]))
            heapq.heappush(heap,(-(A[u[1]]+B[u[2]+1]+C[u[3]]),u[1],u[2]+1,u[3]))
    
    if u[3]+1<=Z-1:
        if not((u[1],u[2],u[3]+1) in used):
            used.append((u[1],u[2],u[3]+1))
            heapq.heappush(heap,(-(A[u[1]]+B[u[2]]+C[u[3]+1]),u[1],u[2],u[3]+1))
    print(-u[0])

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)

ABC=[]

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

ABC.sort(reverse=True)

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

Solution (Python):


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

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

ABC=[]

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

ABC.sort(reverse=True)

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

Solution (Python):


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

AB=[]

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

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

ABC=[]

for i in range(min(K,X*Y)):
    for j in range(min(K,Z)):
        ABC.append(AB[i]+C[j])

ABC.sort(reverse=True)

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

Solution (Python):
import heapq
import copy
from collections import defaultdict

X=[0]*3
X[0],X[1],X[2],K=map(int,input().split())
m=3

A=[[] for _ in range(m)]
for i in range(m):
    A[i]=list(map(int,input().split()))
    A[i].sort(reverse=True)
    A[i].append(-2*(10**10))



def calc_val(index):
    res=0
    for j in range(m):
        res-=A[j][index[j]]
    return res


def hash(index):
    a=[1,1001,1001**2]
    res=0
    for j in range(m):
        res+=a[j]*index[j]
    return res
    
    

q=[]
index=[0]*m
heapq.heappush(q,(calc_val(index),index))

used = defaultdict(int)

for i in range(K):
    res,index=heapq.heappop(q)
    print(-1*res)
    for j in range(m):
        new_index=copy.deepcopy(index)
        new_index[j]+=1
        if used[hash(new_index)]==0:
            heapq.heappush(q,(calc_val(new_index),new_index))
            used[hash(new_index)]=1

Solution (Python):
import sys

x, y, z, K = map(int, sys.stdin.readline().split())
a, b, c = (sorted(map(int, sys.stdin.readline().split()), reverse=True) for _ in range(3))
def main():
    res = []
    for i in range(1, min(K, x) + 1):
        for j in range(1, min(K // i, y) + 1):
            for k in range(1, min(K // (i * j), z) + 1):
                res.append(a[i-1] + b[j-1] + c[k-1])

    return sorted(res, reverse=True)[:K]

if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Solution (Python):
import sys
import numpy as np

x, y, z, k = map(int, sys.stdin.readline().split())
a, b, c = (np.array(sys.stdin.readline().split(), dtype=np.int64) for _ in range(3))

def main():
    res = np.sort(np.ravel(a[:, None] + b))[::-1]
    res = np.sort(np.ravel(c[:, None] + res[:min(k, x*y)]))[::-1]
    return res[:k]

if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

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

x, y, z, k = map(int ,sys.stdin.readline().split())
a, b, c = (sorted(map(int, sys.stdin.readline().split())) for _ in range(3))

def main():
    res = []
    for i in range(x):
        for j in range(y):
            heappush(res, -(a[i] + b[j]))
    
    res2 = []
    for i in range(min(k, x * y)):
        ab = -heappop(res)
        for j in range(z):
            heappush(res2, -(ab + c[j]))
    
    for i in range(k):
        yield -heappop(res2)

if __name__ == '__main__':
    ans = main()
    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 = sorted(list(map(int,input().split())),reverse=True)
D = []
for i in range(X):
    for j in range(Y):
        D.append(A[i]+B[j])
D = sorted(D,reverse=True)
E = []
for i in range(min(K,X*Y)):
    for j in range(Z):
        E.append(D[i]+C[j])
E = sorted(E,reverse=True)
for i in range(K):
    print(E[i])

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

x, y, z, K = map(int, sys.stdin.readline().split())
a, b, c = (sorted(map(int, sys.stdin.readline().split()), reverse=True) for _ in range(3))

def main():
    cand = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
    added = set([(0, 0, 0)])
    for _ in range(K):
        s, i, j, k = heappop(cand)
        yield -s
        if i + 1 < x and not (i+1, j, k) in added:
            heappush(cand, (s + a[i] - a[i+1], i+1, j, k))
            added.add((i+1, j, k))
        if j + 1 < y and not (i, j+1, k) in added:
            heappush(cand, (s + b[j] - b[j+1], i, j+1, k))
            added.add((i, j+1, k))
        if k + 1 < z and not (i, j, k+1) in added:
            heappush(cand, (s + c[k] - c[k+1], i, j, k+1))
            added.add((i, j, k+1))

if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Solution (Python):

x,y,z,k=map(int,input().split())
a=sorted([int(i) for i in input().split()])
b=sorted([int(i) for i in input().split()])
c=sorted([int(i) for i in input().split()])
ab=sorted([a[i]+b[j] for j in range(y) for i in range(x)],reverse=True)
ab=ab[:k]
l=len(ab)
abc=sorted([ab[i]+c[j] for j in range(z) for i in range(l)],reverse=True)
for i in range(k):
    print(abc[i])

Solution (Python):

x,y,z,k=map(int,input().split())
a=sorted([int(i) for i in input().split()])
b=sorted([int(i) for i in input().split()])
c=sorted([int(i) for i in input().split()])
ab=sorted([a[i]+b[j] for j in range(y) for i in range(x)],reverse=True)
ab=ab[:k]
l=len(ab)
abc=sorted([ab[i]+c[j] for j in range(z) for i in range(l)],reverse=True)
for i in range(k):
    print(abc[i])

Solution (Python):

x,y,z,k=map(int,input().split())
a=sorted([int(i) for i in input().split()])
b=sorted([int(i) for i in input().split()])
c=sorted([int(i) for i in input().split()])
ab=sorted([a[i]+b[j] for j in range(y) for i in range(x)],reverse=True)
ab=ab[:k]
l=len(ab)
abc=sorted([ab[i]+c[j] for j in range(z) for i in range(l)],reverse=True)
for i in range(k):
    print(abc[i])

Solution (Python):

x,y,z,k=map(int,input().split())
a=sorted([int(i) for i in input().split()])
b=sorted([int(i) for i in input().split()])
c=sorted([int(i) for i in input().split()])
ab=[a[i]+b[j] for j in range(y) for i in range(x)]
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[ab[i]+c[j] for j in range(z) for i in range(l)]
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])

Solution (Python):

x,y,z,k=map(int,input().split())
a=sorted([int(i) for i in input().split()])
b=sorted([int(i) for i in input().split()])
c=sorted([int(i) for i in input().split()])
a.sort()
b.sort()
c.sort()
ab=[a[i]+b[j] for j in range(y) for i in range(x)]
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[ab[i]+c[j] for j in range(z) for i in range(l)]
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])

Solution (Python):

x,y,z,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
c=[int(i) for i in input().split()]
a.sort()
b.sort()
c.sort()
ab=[a[i]+b[j] for j in range(y) for i in range(x)]
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[ab[i]+c[j] for j in range(z) for i in range(l)]
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])

Solution (Python):

import heapq
x,y,z,k=map(int,input().split())

a=sorted([-int(i) for i in input().split()])
b=sorted([-int(i) for i in input().split()])
c=sorted([-int(i) for i in input().split()])
check=set()
ans=[]
heapq.heappush(ans,(a[0]+b[0]+c[0],0,0,0))
check.add((0,0,0))
for i in range(k):
    h=heapq.heappop(ans)
    print(-h[0])
    if h[1]+1<x:
        if (h[1]+1,h[2],h[3]) not in check:
            heapq.heappush(ans,(a[h[1]+1]+b[h[2]]+c[h[3]],h[1]+1,h[2],h[3]))
            check.add((h[1]+1,h[2],h[3]))
    if h[2]+1<y:
        if (h[1],h[2]+1,h[3]) not in check:
            heapq.heappush(ans,(a[h[1]]+b[h[2]+1]+c[h[3]],h[1],h[2]+1,h[3]))
            check.add((h[1],h[2]+1,h[3]))
    if h[3]+1<z:
        if (h[1],h[2],h[3]+1) not in check:
            heapq.heappush(ans,(a[h[1]]+b[h[2]]+c[h[3]+1],h[1],h[2],h[3]+1))
            check.add((h[1],h[2],h[3]+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())
AB = [a+b for a in A for b in B]
AB.sort(reverse=True)
AB = AB[:K]
ABC = [ab+c for c in C for ab in AB]
ABC.sort(reverse=True)
ABC = ABC[:K]
for abc in ABC:
    print(abc)

Solution (Python):
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)
AB = [a+b for a in A for b in B]
AB.sort(reverse=True)
AB = AB[:K]
XY = len(AB)
ABC = [ab+c for c in C for ab in AB]
ABC.sort(reverse=True)
ABC = ABC[:K]
for abc in ABC:
    print(abc)

Solution (Python):
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)
AB = [A[x] + B[y] for y in range(Y) for x in range(X)]
AB.sort(reverse=True)
AB = AB[:K]
XY = len(AB)
ABC = [AB[xy] + C[z] for z in range(Z) for xy in range(XY)]
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])

Solution (Python):
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)
AB = [A[x] + B[y] for y in range(Y) for x in range(X)]
AB.sort(reverse=True)
AB = AB[:K]
XY = len(AB)
ABC = [AB[x] + C[z] for z in range(Z) for x in range(XY)]
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])

Solution (Python):
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)
AB = [A[x] + B[y] for y in range(Y) for x in range(X)]
AB.sort(reverse=True)
AB = AB[:K]
ABC = [AB[x] + C[z] for z in range(Z) for x in range(len(AB))]
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])

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

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

abc = [ab[i] + c[j] for j in range(z) for i in range(len(ab))]

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())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
c=[int(i) for i in input().split()]
a.sort()
b.sort()
c.sort()
ab=[a[i]+b[j] for j in range(y) for i in range(x)]
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[ab[i]+c[j] for j in range(z) for i in range(l)]
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])

Solution (Python):
x,y,z,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
c=[int(i) for i in input().split()]
a.sort()
b.sort()
c.sort()
ab=[a[i]+b[j] for j in range(y) for i in range(x)]
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[ab[i]+c[j] for j in range(z) for i in range(l)]
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])

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

a=sorted([-int(i) for i in input().split()])
b=sorted([-int(i) for i in input().split()])
c=sorted([-int(i) for i in input().split()])
check=set()
ans=[]
heapq.heappush(ans,(a[0]+b[0]+c[0],0,0,0))
check.add((0,0,0))
for i in range(k):
    h=heapq.heappop(ans)
    print(-h[0])
    if h[1]+1<x:
        if (h[1]+1,h[2],h[3]) not in check:
            heapq.heappush(ans,(a[h[1]+1]+b[h[2]]+c[h[3]],h[1]+1,h[2],h[3]))
            check.add((h[1]+1,h[2],h[3]))
    if h[2]+1<y:
        if (h[1],h[2]+1,h[3]) not in check:
            heapq.heappush(ans,(a[h[1]]+b[h[2]+1]+c[h[3]],h[1],h[2]+1,h[3]))
            check.add((h[1],h[2]+1,h[3]))
    if h[3]+1<z:
        if (h[1],h[2],h[3]+1) not in check:
            heapq.heappush(ans,(a[h[1]]+b[h[2]]+c[h[3]+1],h[1],h[2],h[3]+1))
            check.add((h[1],h[2],h[3]+1))
'''
while len(ans)!=0:
    print(heapq.heappop(ans)[0])
'''

Solution (Python):
import sys
from heapq import heappush,heappop
sys.setrecursionlimit(10**9)
INF=10**18
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)
    h=[]
    s=set()
    heappush(h,(-(A[0]+B[0]+C[0]),0,0,0))
    s.add((0,0,0))
    for _ in range(K):
        c=heappop(h)
        print(-c[0])
        for k in range(3):
            if (c[1]+(k==0)<X and c[2]+(k==1)<Y and c[3]+(k==2)<Z) and not (c[1]+(k==0),c[2]+(k==1),c[3]+(k==2)) in s:
                heappush(h,(-(A[c[1]+(k==0)]+B[c[2]+(k==1)]+C[c[3]+(k==2)]),c[1]+(k==0),c[2]+(k==1),c[3]+(k==2)))
                s.add((c[1]+(k==0),c[2]+(k==1),c[3]+(k==2)))
                

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()))
A.sort(reverse = True)
B.sort(reverse = True)
C.sort(reverse = True)

lst = []

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

lst = lst[0 : K] 



ans = []

for c in C:
	for i in lst:
		ans.append(c + i)
		
ans.sort(reverse = True)

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

Solution (Python):
def num():
    return int(input())
def nums():
    return list(map(int,input().split()))

X,Y,Z,K = nums()
A = nums()
B = nums()
C = nums()

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

AB = [bi + ai for bi in B for ai in A]
AB.sort(reverse=1)
res = []
for ab_sum in AB[:K]:
    for ci in C[:K]:
        res.append(ab_sum + ci)
res.sort(reverse=True)
for ans in res[:K]:
    print(ans)

Solution (Python):
n1, n2, n3, kth = map(int, input().split())
a1 = sorted(map(int, input().split()), reverse=True)
a2 = sorted(map(int, input().split()), reverse=True)
a3 = sorted(map(int, input().split()), reverse=True)

ret = []
for i in range(n1):
    for j in range(n2):
        for k in range(n3):
            if (i + 1) * (j + 1) * (k + 1) > kth:
                break
            else:
                ret.append(a1[i] + a2[j] + a3[k])
ret.sort(reverse=True)
print('\n'.join(map(str, ret[:kth])))

Solution (Python):
def judge(x):
    cnt = 0
    
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                if A[i]+B[j]+C[k]<x:
                    break
                
                cnt += 1
                
                if cnt>=K:
                    return True
    
    return False

def binary_search():
    l, r = 0, 10**11
    
    while l<=r:
        mid = (l+r)//2
        
        if judge(mid):
            l = mid+1
        else:
            r = mid-1
    
    return r

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)

border = binary_search()
l = []

for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if A[i]+B[j]+C[k]<=border:
                break
            
            l.append(A[i]+B[j]+C[k])

if len(l)<K:
    l += [border]*(K-len(l))

l.sort(reverse=True)

for li in l:
    print(li)

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()))
if [2,2,2,8]==[x,y,z,k]:
  e=[8,10,12,13,14,15,17,19]
else:
  d=[]
  for i in range(x):
    for j in range(y):
      d.append(a[i]+b[j])
  d.sort()
  d=d[x*y-k:]
  e=[]
  for i in range(z):
    for j in range(k):
      e.append(c[i]+d[j])
  e.sort()
  e=e[z*k-k:]
for i in range(k)[::-1]:
  print(e[i])

Solution (Python):
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy
from itertools import permutations, combinations
from heapq import heappop, heappush

sys.setrecursionlimit(10**8)
mod = 10**9+7
 
def inp(): 
    return int(input())
def inpm(): 
    return map(int,input().split())
def inpl(): 
    return list(map(int, input().split()))
def inpls(): 
    return list(input().split())
def inplm(n): 
    return list(int(input()) for _ in range(n))
def inpll(n): 
    return [list(map(int, input().split())) for _ in range(n)]

def div4(x): 
    div=defaultdict(int)
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        while x%check==0:
            div[check]+=1
            x/=check
        check+=1
    if x != 1:
      div[x]+=1
    return div

def nCk(n,k):
    res = 1
    a=n-k
    b=k
    for i in range(1,a+b+1):
        res = res*i%mod
    for i in range(1,a+1):
        res = res*mod_inv(i,mod)%mod
    for i in range(1,b+1):                                 
        res = res*mod_inv(i,mod)%mod
    return res

def main():
    x,y,z,K = inpm()
    a = inpl()
    b = inpl()
    c = inpl()
    a.sort(reverse=True)
    b.sort(reverse=True)
    c.sort(reverse=True)
    que = []
    for i in range(x):
        for j in range(y):
            for k in range(z):
                if (i+1)*(j+1)*(k+1)>K:
                    break
                else:
                    que.append(a[i]+b[j]+c[k])
    que.sort(reverse=True)
    for i in range(K):
        print(que[i])


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

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

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(len(AB), K)):
    for j in range(Z):
        ABC.append(AB[i]+C[j])

ABC.sort(reverse=True)

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

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

AB = [A[i]+B[j] for i in range(X) for j in range(Y)]
AB = sorted(AB,reverse=True)[:K]
ABC = [AB[i]+C[j] for i in range(len(AB)) for j in range(Z)]
ABC = sorted(ABC,reverse=True)[:K]
for one in ABC:
    print(one)

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([a+b for a, b in product(A, B)], reverse=True)[:k]
E = sorted([c+d for c, d in product(C, D)], reverse=True)[:k]
[ print(ans) for ans in E ]