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

Solution (Python):
import heapq

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


A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
heap = []
already = [[[] for _ in range(Y)] for _ in range(X)]
maxv = A[0]+B[0]+C[0]
heapq.heapify(heap)
heapq.heappush(heap, (0, 0, 0, 0))


def ans(v):
    return maxv - v


for i in range(K):
    res, x, y, z = heapq.heappop(heap)
    print(ans(res))
    if x + 1 < X and z not in already[x+1][y]:
        heapq.heappush(heap, (ans(A[x+1]+B[y]+C[z]), x+1, y, z))
        already[x+1][y].append(z)
    if y + 1 < Y and z not in already[x][y+1]:
        heapq.heappush(heap, (ans(A[x]+B[y+1]+C[z]), x, y+1, z))
        already[x][y+1].append(z)
    if z + 1 < Z and z+1 not in already[x][y]:
        heapq.heappush(heap, (ans(A[x]+B[y]+C[z+1]), x, y, z+1))
        already[x][y].append(z+1)

Solution (Python):
def main():
  if 'get_ipython' not in globals():  
    
    X, Y, Z, K = (int(i) for i in 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()] 
  else:
    A = [1,10,100]
    B = [2,20,200]
    C = [1,10,100]
    
    
    
    X = len(A)
    Y = len(B)
    Z = len(C)
    K = 5
    

  
  AB = []
  A.sort(reverse=True)
  B.sort(reverse=True)
  for i in A[0:K]:
    for j in B[0:K]:
      AB.append(i+j)
  
  AB.sort(reverse=True)
  
  ABC = []
  C.sort(reverse=True)
  
  for i in AB[0:K]:
    for j in C[0:K]:
      ABC.append(i+j)  
  
  ABC.sort(reverse=True)

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

main()

Solution (Python):
X, Y, Z, K = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]
C = [int(_) for _ in input().split()]

A = sorted(A)
B = sorted(B)
C = sorted(C)

maxn = A[0] * B[0] * C[0]

from heapq import *

h = []


heappush(h, (-A[-1] - B[-1] - C[-1], len(A) - 1, len(B) - 1, len(C) - 1))

used = {}

for i in range(min(K, X * Y * Z)):
    v, ix, iy, iz = heappop(h)
    print(-v)
    def push(x, y, z):
        if x >= 0 and y >= 0 and z >= 0 and (x, y, z) not in used:
            heappush(h, (-A[x] - B[y] - C[z], x, y, z))
            used[x, y, z] = 1
    push(ix - 1, iy, iz)
    push(ix, iy - 1, iz)
    push(ix, iy, iz - 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()
A.reverse()
B.sort()
B.reverse()
C.sort()
C.reverse()

anslist = []

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

anslist.sort()
anslist.reverse()
for hoge in range(K):
	print (anslist[hoge])

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

C.sort(reverse=True)

tmp = []
for x in range(X):
    for y in range(Y):
        tmp.append(A[x] + B[y])
        
tmp.sort(reverse=True)

res = []
for i in range(min(K, X*Y)):
    for z in range(Z):
        res.append(tmp[i] + C[z])
 
res.sort(reverse=True)
for i in range(K):
    print(res[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)

ans = []
for xi in range(x): 
    if xi+1 > k: 
        break
    for yi in range(y):
        if (xi+1)*(yi+1) > k:
            break
        for zi in range(z):
            if (xi+1)*(yi+1)*(zi+1) > k:
                break
            ans.append(a[xi]+b[yi]+c[zi])
ans.sort(reverse=True)
for i in range(k):
    print(ans[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()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

ans = []

for j in range(X):
    if j+1 > K:
        break
    for k in range(Y):
        if j*k+1 > K:
            break
        for l in range(Z):
            if j*k*l+1 <= K:
                ans.append(A[j]+B[k]+C[l])
            else: 
                break
                
ans.sort(reverse=True)
for i in range(K):
    print(ans[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)

lenA = X
lenB = Y
lenC = Z 

AB = []

for i in range(0, min(K,lenA)):
  for j in range(0, min(K,lenB)):
      AB.append(A[i]+B[j])
AB.sort(reverse=True)

ABC = []

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

def judge(p):
    cnt = 0
    for i in range(x):
        for j in range(y):
            for kk in range(z):
                if a[i]+b[j]+c[kk] < p : break
                cnt += 1
                if (cnt >= k):return True
    return False

ll,rr = 0,a[0]+b[0]+c[0]
while(rr- ll > 1):
    md = (ll+rr) // 2
    if(judge(md)) : ll = md
    else : rr = md

ans = []
cnt=0
for i in range(x):
    if(cnt >= k):break
    for j in range(y):
        if(cnt >= k):break
        for kk in range(z):
            if(cnt >= k):break
            if a[i]+b[j]+c[kk] < rr : break
            cnt += 1
            ans.append(a[i]+b[j]+c[kk])

if len(ans) <k:
    for i in range(k-len(ans)):
        ans.append(ll)

ans.sort(reverse=True)

for i in ans:
    print(i)

Solution (Python):
import sys

import numpy as np

sys.setrecursionlimit(10000)
INF = float('inf')

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

AB = np.add.outer(A, B).flatten()
AB.sort()
C.sort()
ABC = np.add.outer(AB[-K:], C).flatten()
ABC.sort()

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

Solution (Python):
import heapq
import sys

sys.setrecursionlimit(10000)
INF = float('inf')

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

heap = []
heapq.heappush(heap, (-(A[0] + B[0] + C[0]), 0, 0, 0))
history = {(0, 0, 0)}
for _ in range(K):
    d, a, b, c = heapq.heappop(heap)
    if a + 1 < X and (a + 1, b, c) not in history:
        history.add((a + 1, b, c))
        heapq.heappush(heap, (-(A[a + 1] + B[b] + C[c]), a + 1, b, c))
    if b + 1 < Y and (a, b + 1, c) not in history:
        history.add((a, b + 1, c))
        heapq.heappush(heap, (-(A[a] + B[b + 1] + C[c]), a, b + 1, c))
    if c + 1 < Z and (a, b, c + 1) not in history:
        history.add((a, b, c + 1))
        heapq.heappush(heap, (-(A[a] + B[b] + C[c + 1]), a, b, c + 1))
    print(-d)

Solution (Python):

import sys

import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

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))
kk = k
ans = []
chk = defaultdict(lambda:defaultdict(lambda:defaultdict(bool)))

while(k):
    k -= 1
    v, ai, bi, ci = heapq.heappop(q)
    ans.append(v)
    if ai + 1 < x:
        if chk[ai + 1][bi][ci] == False:
            heapq.heappush(q, (-(a[ai + 1] + b[bi] + c[ci]), ai + 1, bi, ci))
            chk[ai + 1][bi][ci] = True
    if bi + 1 < y:
        if chk[ai][bi + 1][ci] == False:
            heapq.heappush(q, (-(a[ai] + b[bi + 1] + c[ci]), ai, bi + 1, ci))
            chk[ai][bi + 1][ci] = True
    if ci + 1 < z:
        if chk[ai][bi][ci + 1] == False:
            heapq.heappush(q, (-(a[ai] + b[bi] + c[ci + 1]), ai, bi, ci + 1))
            chk[ai][bi][ci + 1] = True
for i in range(kk):
    print(-ans[i])

Solution (Python):


x,y,z,k = [int(i) for i in 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(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

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

ans.sort(reverse=True)

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

Solution (Python):
x, y, z, o = [int(_) for _ in input().split()]
a = sorted([int(_) for _ in input().split()])[::-1]
b = sorted([int(_) for _ in input().split()])[::-1]
c = sorted([int(_) for _ in input().split()])[::-1]
s = []
for i in range(x):
    for j in range(y):
        for k in range(z):
            if (i+1)*(j+1)*(k+1) <= o:
                s.append(a[i]+b[j]+c[k])
            else:
                break
for ans in sorted(s)[::-1][:o]:
    print(ans)

Solution (Python):


import sys

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

def main():
    tk = lambda i, j, k: '%d %d %d' % (i, j, k)
    X, Y, Z, K = i2nn()
    A = i2nn()
    B = i2nn()
    C = i2nn()
    A.sort()
    A.reverse()
    B.sort()
    B.reverse()
    C.sort()
    C.reverse()
    dic = {}
    fin = set()
    def put(i, j, k):
        if i >= X or j >= Y or k >= Z:
            return
        key = tk(i, j, k)
        if key in fin:
            return
        if key not in dic:
            dic[key] = (A[i] + B[j] + C[k], key, i, j, k)
    put(0, 0, 0)
    for _ in range(K):
        v = max(dic.values(), key=lambda v: v[0])
        dic.pop(v[1])
        fin.add(v[1])
        put(v[2]+1, v[3],   v[4]  )
        put(v[2],   v[3]+1, v[4]  )
        put(v[2],   v[3],   v[4]+1)
        print(v[0])

main()

Solution (Python):
x, y, z, k = [int(_) for _ in input().split()]
a = [int(_) for _ in input().split()]
b = [int(_) for _ in input().split()]
c = [int(_) for _ in input().split()]
ab = []
for i in range(x):
    for j in range(y):
        ab.append(a[i] + b[j])
ab.sort(reverse=True)
abc = []
for i in range(min(k, x*y)):
    for j in range(z):
        abc.append(ab[i] + c[j])
abc.sort(reverse=True)
for i in range(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()))
sam = []
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ba,bb,bc = 0,0,0
print(a[0] + b[0] + c[0])
used = set()
for K in range(k - 1):
    if ba < x - 1 and (ba + 1,bb,bc) not in used:
        sam.append([a[ba + 1] + b[bb] + c[bc],ba + 1,bb,bc])
        used.add((ba + 1,bb,bc))
    if bb < y - 1 and (ba,bb + 1,bc) not in used:
        sam.append([a[ba] + b[bb + 1] + c[bc],ba,bb + 1,bc])
        used.add((ba,bb + 1,bc))
    if bc < z - 1 and (ba,bb,bc + 1) not in used:
        sam.append([a[ba] + b[bb] + c[bc + 1],ba,bb,bc + 1])
        used.add((ba,bb,bc + 1))
    big = max(sam)
    sam.remove(big)
    print(big[0])
    ba,bb,bc = big[1],big[2],big[3]

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)

arr = []

for i, a in enumerate(A):
    for j, b in enumerate(B):
        if (i+1) * (j+1) > K:
            break
        for k, c in enumerate(C):
            if (i+1) * (j+1) * (k+1) <= K:
                arr.append(a + b + c)
            else:
                break

arr.sort(reverse=True)

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

Solution (Python):


import heapq

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

yam = []
for l in range(x):
    for m in range(y):
        for n in range(z):
            if k < len(yam) and a[l]+b[m]+c[n] <= yam[0]:
                break
            elif k < len(yam):
                heapq.heappushpop(yam, a[l]+b[m]+c[n])
            else:
                heapq.heappush(yam, a[l]+b[m]+c[n])
yam.sort(reverse=True)


for l in range(k):
        print(yam[l])

Solution (Python):
from heapq import heappush,heappop,heapify
from collections import defaultdict
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()
heap=[(-(A[X-1]+B[Y-1]+C[Z-1]),X-1,Y-1,Z-1)]
heapify(heap)
D=defaultdict(bool)
for k in range(K):
  s,x,y,z=heappop(heap)
  while D[(x,y,z)]:
    s,x,y,z=heappop(heap)
  s*=-1
  D[(x,y,z)]=True
  print(s)
  if x>=1:
    heappush(heap,(-A[x-1]-B[y]-C[z],x-1,y,z))
  if y>=1:
    heappush(heap,(-A[x]-B[y-1]-C[z],x,y-1,z))
  if z>=1:
    heappush(heap,(-A[x]-B[y]-C[z-1],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)

L = []
for i, a in enumerate(A):
  for j, b in enumerate(B):
    if i * j > K:
      continue
    else:
      L.append(a + b)

L.sort(reverse = True)
ans = []
for i, l in enumerate(L):
  if i == K:
    break
  else:
    for c in C:
      ans.append(l + c)
ans.sort(reverse = True)
for i, a in enumerate(ans):
  if i == K:
    break
  else:
    print(a)

Solution (Python):
import heapq

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

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

h = []

heapq.heappush(h, (-A[0]-B[0]-C[0], 0,0,0))

result = []
used = set()

for _ in range(K):
    value, i,j,k = heapq.heappop(h)
    result.append(-value)
    if i < X-1 and (i+1,j,k) not in used:
        heapq.heappush(h, (-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:
        heapq.heappush(h, (-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:
        heapq.heappush(h, (-A[i]-B[j]-C[k+1],i,j,k+1))
        used.add((i,j,k+1))
        
for v in result:
    print(v)

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)

l = []

for p in range(0,x):
    for q in range(0,y):
        for r in range(0,z):
            if len(l) > k and a[p]+b[q]+c[r] <= l[0]:
                break
            elif len(l) > k:
                heapq.heappushpop(l,a[p]+b[q]+c[r])
            else:
                heapq.heappush(l,a[p]+b[q]+c[r])

l.sort()

for i in range(1,k+1):
    print(l[-i])

Solution (Python):
from heapq import heappush, heappop

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_list = []
s = set()
pq = []
item = (-(A[0]+B[0]+C[0]),0,0,0)
heappush(pq,item)

for _ in range(K):
    value, i, j, k = heappop(pq)
    ans_list.append(-value)
    if len(ans_list) == K:
        break
    if i <= X-2 and (i+1,j,k) not in s:
        item = (-(A[i+1]+B[j]+C[k]),i+1,j,k)
        heappush(pq,item)
        s.add((i+1,j,k))
    if j <= Y-2 and (i,j+1,k) not in s:
        item = (-(A[i]+B[j+1]+C[k]),i,j+1,k)
        heappush(pq,item)
        s.add((i,j+1,k))
    if k <= Z-2 and (i,j,k+1) not in s:
        item = (-(A[i]+B[j]+C[k+1]),i,j,k+1)
        heappush(pq,item)
        s.add((i,j,k+1))

for ans in ans_list:
    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

def make(i, j, k):
    return (-A[i]-B[j]-C[k], i, j, k)


used = set()
l = []

heapq.heappush(l, make(0, 0, 0))
used.add((0, 0, 0))

for cnt in range(K):
    V, i, j, k = heapq.heappop(l)
    
    if i < X - 1 and (i+1, j, k) not in used:
        heapq.heappush(l, make(i+1, j, k))
        used.add((i+1, j, k))
    
    if j < Y - 1 and (i, j+1, k) not in used:
        heapq.heappush(l, make(i, j+1, k))
        used.add((i, j+1, k))
    
    if k < Z - 1 and (i, j, k+1) not in used:
        heapq.heappush(l, make(i, j, k+1))
        used.add((i, j, k+1))
    
    print(-V)

Solution (Python):
import heapq

X, Y, Z, K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
A = [-a for a in A]
B = sorted(list(map(int, input().split())), reverse=True)
B = [-b for b in B]
C = sorted(list(map(int, input().split())), reverse=True)
C = [-c for c in C]

checked = []

h = []
heapq.heappush(h, (A[0]+B[0]+C[0], 0, 0, 0))
for _ in range(K):
    val, i, j, k = heapq.heappop(h)
    print(- val)
    if i+1 < X and (i+1, j, k) not in checked:
        heapq.heappush(h, (A[i+1] + B[j] + C[k], i+1, j, k))
        checked.append((i+1, j, k))
    if j+1 < Y and (i, j+1, k) not in checked:
        heapq.heappush(h, (A[i] + B[j+1] + C[k], i, j+1, k))
        checked.append((i, j+1, k))
    if k+1 < Z and (i, j, k+1) not in checked:
        heapq.heappush(h, (A[i] + B[j] + C[k+1], i, j, k+1))
        checked.append((i, j, k+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)
sum_list = []
for a_i in range(x):
    for b_i in range(y):
        if a_i*b_i > k:
            break
        for c_i in range(z):
            if a_i*b_i*c_i > k:
                break
            sum_list.append(a[a_i]+b[b_i]+c[c_i])
sum_list.sort(reverse=True)
for i in range(k):
    print(sum_list[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 = sorted(AB, reverse=True)[:K]
C = sorted(C, reverse=True)
ans = []
for i in C:
    for j in AB:
        ans.append(i + j)

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

Solution (Python):


from heapq import heappop, heappush


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

class CakeSet:
    def __init__(self, status):
        self.status = status
        self.cost = calc_cost(status)

    def next_list(self):
        ret = []
        if self.status[0] + 1 < x:
            ret.append(CakeSet((self.status[0] + 1, self.status[1], self.status[2])))
        if self.status[1] + 1 < y:
            ret.append(CakeSet((self.status[0], self.status[1] + 1, self.status[2])))
        if self.status[2] + 1 < z:
            ret.append(CakeSet((self.status[0], self.status[1], self.status[2] + 1)))
        return ret

    def __lt__(self, other):
        return self.cost > other.cost

def calc_cost(status):
    return a[status[0]] + b[status[1]] + c[status[2]]

def main():
    memo = set()
    initial = CakeSet((0, 0, 0))
    costs = []
    queue = []
    heappush(queue, initial)
    while len(costs) < k:
        cakeSet = heappop(queue)
        if cakeSet.status in memo:
            continue
        costs.append(cakeSet.cost)
        memo.add(cakeSet.status)
        for next_set in cakeSet.next_list():
            heappush(queue, next_set)
    for cost in costs[:k]:
        print(cost)


if __name__ == '__main__':
    main()

Solution (Python):
import heapq

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

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


def f(i, j, k):
    x = A[i] + B[j] + C[k]
    return (-x, (i, j, k))


hq = []
heapq.heappush(hq, f(0, 0, 0))

ans = []
memo = set()
while len(ans) < K:
    v, (i, j, k) = heapq.heappop(hq)
    ans.append(-v)

    for x in range(3):
        a, b, c = i, j, k
        if x == 0:
            a += 1
        elif x == 1:
            b += 1
        else:
            c += 1
        v = (a << 20) + (b << 10) + c
        if v not in memo:
            heapq.heappush(hq, f(a, b, c))
            memo.add(v)

for x in ans:
    print(x)

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)

from heapq import heapify,heappop,heappush
q=[]
heappush(q,(-a[0]-b[0]-c[0],(0,0,0)))
used={(0,0,0):True}
for i in range(k):
    if q[0][1][0]+1<x and (q[0][1][0]+1,q[0][1][1],q[0][1][2]) not in used:
        heappush(q,(-a[q[0][1][0]+1]-b[q[0][1][1]]-c[q[0][1][2]],(q[0][1][0]+1,q[0][1][1],q[0][1][2])))
        used[(q[0][1][0]+1,q[0][1][1],q[0][1][2])]=True
    if q[0][1][1]+1<y and (q[0][1][0],q[0][1][1]+1,q[0][1][2]) not in used:
        heappush(q,(-a[q[0][1][0]]-b[q[0][1][1]+1]-c[q[0][1][2]],(q[0][1][0],q[0][1][1]+1,q[0][1][2])))
        used[(q[0][1][0],q[0][1][1]+1,q[0][1][2])]=True
    if q[0][1][2]+1<z and (q[0][1][0],q[0][1][1],q[0][1][2]+1) not in used:
        heappush(q,(-a[q[0][1][0]]-b[q[0][1][1]]-c[q[0][1][2]+1],(q[0][1][0],q[0][1][1],q[0][1][2]+1)))
        used[(q[0][1][0],q[0][1][1],q[0][1][2]+1)]=True
    print(-heappop(q)[0])

Solution (Python):
from heapq import heappush, heappop


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

INF = 10**15


a = sorted(A, reverse=True) + [-INF]
b = sorted(B, reverse=True) + [-INF]
c = sorted(C, reverse=True) + [-INF]
q = []
heappush(q, (-(a[0] + b[0] + c[0]), 0, 0, 0))
m = 0
d = []
u = set()
while m < K:
    s, i, j, k = heappop(q)
    if (i, j, k) not in u:
        d.append(-s)
        u.add((i, j, k))
        heappush(q, (-(a[i + 1] + b[j] + c[k]), i + 1, j, k))
        heappush(q, (-(a[i] + b[j + 1] + c[k]), i, j + 1, k))
        heappush(q, (-(a[i] + b[j] + c[k + 1]), i, j, k + 1))
        m += 1
ans = '\n'.join(map(str, d))


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

Solution (Python):
import heapq as hq
x, y, z, k = map(int,input().split())
arr_a = sorted(list(map(int,input().split())) ,reverse = True)
arr_b = sorted(list(map(int,input().split())) ,reverse = True)
arr_c = sorted(list(map(int,input().split())) ,reverse = True)

cnt = set()
P_q = [[-(arr_a[0] + arr_b[0] + arr_c[0]),0,0,0]]

cnt.add((0,0,0))
for _ in range(k):
    tmp = hq.heappop(P_q)
    t, i, j, k = -tmp[0],tmp[1],tmp[2],tmp[3]
    print(t)
    if i + 1 < x and (i+1,j,k) not in cnt:
        hq.heappush(P_q,[-(arr_a[i+1] + arr_b[j] + arr_c[k]) ,i + 1,j,k])
        cnt.add((i+1,j,k))
    if j + 1 < y and (i,j+1,k) not in cnt:
        hq.heappush(P_q,[-(arr_a[i] + arr_b[j+1] + arr_c[k]) ,i ,j + 1,k])
        cnt.add((i,j+1,k))
    if k + 1 < z and (i,j,k+1) not in cnt:
        hq.heappush(P_q,[-(arr_a[i] + arr_b[j] + arr_c[k + 1]) ,i,j,k + 1])
        cnt.add((i,j,k+1))

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

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

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

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

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

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

D = D[:K]
E = []
for c in C:
    for d in D:
        E.append(c + d)
E.sort(reverse=True)

E = E[:K]
for e in E:
    print(e)

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

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

result = []
for i,a in enumerate(A):
    for j,b in enumerate(B):
        if (i + 1) * (j + 1) > K:
            break
        for k,c in enumerate(C):
            if (i + 1) * (j + 1) * (k + 1) > K:
                break
            else:
                result.append(a + b + c)

result.sort(reverse=True)
for i, r in enumerate(result):
    print(r)
    if i == K -1 :
        break

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

dx = list(map(int, input().strip().split()))
dy = list(map(int, input().strip().split()))
dz = list(map(int, input().strip().split()))

dx.sort(reverse=True)
dy.sort(reverse=True)
dz.sort(reverse=True)


xyz = []
for i in range(x):
    if i + 1  > k:
        break
    for j in range(y):
        if (i + 1) * ( j+ 1) > k:
            break
        for t in range(z):
            if (i + 1) * (j + 1) * (t + 1) > k:
                break
            xyz.append(dx[i] + dy[j] + dz[t])

xyz.sort(reverse=True)


for e in xyz[:k]:
    print(e)

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

abc.sort(reverse = True)

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

Solution (Python):
x,y,z,k=map(int,input().split())
list_a=list(map(int,input().split()))
list_b=list(map(int,input().split()))
list_c=list(map(int,input().split()))
list_a.sort(reverse=True)
list_b.sort(reverse=True)
list_c.sort(reverse=True)
dict_p={}
dict_p[(0,0,0)]=list_a[0]+list_b[0]+list_c[0]
pop_list=[]
for i in range(0,k):
    Max=max(dict_p.values())
    print(Max)
    for j in dict_p:
        if dict_p[j]==Max:
            now=j
            dict_p.pop(now)
            pop_list.append(now)
            break
    if now[0]+1<x:
        if not (now[0]+1,now[1],now[2]) in pop_list:
            dict_p[(now[0]+1,now[1],now[2])]=list_a[now[0]+1]+list_b[now[1]]+list_c[now[2]]
    if now[1]+1<y:
        if not (now[0],now[1]+1,now[2]) in pop_list:
            dict_p[(now[0],now[1]+1,now[2])]=list_a[now[0]]+list_b[now[1]+1]+list_c[now[2]]
    if now[2]+1<z:
        if not (now[0],now[1],now[2]+1) in pop_list:
            dict_p[(now[0],now[1],now[2]+1)]=list_a[now[0]]+list_b[now[1]]+list_c[now[2]+1]