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

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

x,y,z,k = inpl()
a = inpl()
b = inpl()
c = inpl()
a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
q = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
heapify(q)
old = set((0,0,0))
for i in range(k):
    k,l,m,n = heappop(q)
    print(-k)
    if l + 1 < x and not (l+1,m,n) in old:
        heappush(q,[-(a[l+1] + b[m] + c[n]), l+1, m, n])
        old.add((l+1,m,n))
    if m + 1 < y and not(l,m+1,n) in old:
        heappush(q,[-(a[l] + b[m+1] + c[n]), l, m+1, n])
        old.add((l,m+1,n))
    if n + 1 < z and not (l,m,n+1)  in old:
        heappush(q,[-(a[l] + b[m] + c[n+1]), l, m, n+1])
        old.add((l,m,n+1))

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)
queue = []
heapq.heappush(queue, (-(a[0] + b[0] + c[0]), 0, 0, 0))
already = set([0, 0, 0])
ans = []
for _ in range(k):
    val, p, q, r = heapq.heappop(queue)
    ans.append(-val)
    if ((p + 1, q, r) not in already) and p + 1 < x:
        heapq.heappush(queue, (-(a[p+1] + b[q] + c[r]), p+1, q, r))
        already.add((p+1, q, r))
    if ((p, q+1, r) not in already) and q + 1 < y:
        heapq.heappush(queue, (-(a[p] + b[q+1] + c[r]), p, q+1, r))
        already.add((p, q+1, r))
    if ((p, q, r+1) not in already) and r + 1 < z:
        heapq.heappush(queue, (-(a[p] + b[q] + c[r+1]), p, q, r+1))
        already.add((p, q, r+1))
for i in ans:
    print(i)

Solution (Python):
x,y,z,k = map(int,input().split())
list1 = [list(map(int,input().split())) for i in range(3) ]

list_ab = []
list_ab_c = []

for i in list1[0]:
	for j in list1[1]:
		list_ab.append(i+j)
list_ab.sort(reverse=True)
for i in list_ab[:3000]:
	for j in list1[2]:
		list_ab_c.append(i+j)

list_ab_c.sort(reverse=True)

for i in range(k):
	print(list_ab_c[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)
a = []; b = []; c = []

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


answer = []
for i, j, k in candidate:
    answer.append(A[i - 1] + B[j - 1] + C[k - 1])

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

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

for _ in range(K):
    tmp, i, j, k = heapq.heappop(d)
    print(-tmp)
    if (i+1,j,k) not in ijk_set and i+1 <= X-1:
        heapq.heappush(d, (-(A[i+1]+B[j]+C[k]), i+1, j, k))
        ijk_set.add((i+1,j,k))
    if (i,j+1,k) not in ijk_set and j+1 <= Y-1:
        heapq.heappush(d, (-(A[i]+B[j+1]+C[k]), i, j+1, k))
        ijk_set.add((i,j+1,k))
    if (i,j,k+1) not in ijk_set and k+1 <= Z-1:
        heapq.heappush(d, (-(A[i]+B[j]+C[k+1]), i, j, k+1))
        ijk_set.add((i,j,k+1))

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

def main():
    X, Y, Z, K = [int(x) for x in 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()]

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

    ans_s = sorted(ans, reverse=True)

    ans2 = []
    for a in ans_s[:3000]:
        for c in C:
            ans2.append(a + c)

    ans2_s = sorted(ans2, reverse=True)

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import collections
import heapq


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    x, y, z, k = list(map(int, input().rstrip('\n').split()))
    a = list(map(int, input().rstrip('\n').split()))
    b = list(map(int, input().rstrip('\n').split()))
    c = list(map(int, input().rstrip('\n').split()))
    a.sort(reverse=True)
    b.sort(reverse=True)
    c.sort(reverse=True)
    ql = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
    heapq.heapify(ql)
    fq = collections.defaultdict(int)
    fq[0, 0, 0]
    for i in range(k):
        s, ap, bp, cp = heapq.heappop(ql)
        for app, bpp, cpp in ([[ap+1, bp, cp], [ap, bp+1, cp], [ap, bp, cp+1]]):
            if app < x and bpp < y and cpp < z:
                if (app, bpp, cpp) not in fq:
                    heapq.heappush(ql, [-(a[app] + b[bpp] + c[cpp]), app, bpp, cpp])
                    fq[app, bpp, cpp]
        print(-s)


if __name__ == '__main__':
    solve()

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)

used = set()
h = []
heappush(h, (-A[0]-B[0]-C[0], 0, 0, 0))
result = []
used.add((0, 0, 0))

for _ in range(K):
    sum, i, j, k = heappop(h)
    result.append(-sum)
    if i < X-1 and (i+1, j, k) not in used:
        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:
        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:
        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):
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)

s=list()

for l in range(x):
  if l>k:
    break 
  for m in range(y):
    if l*m>k:
      break
    for n in range(z):
      if l*m*n>k:
        break
      s.append(a[l]+b[m]+c[n])
 
s.sort(reverse=True)
for i in range(k):
  print(s[i])

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

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

que = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
V.add((0, 0, 0))
ans = []
for _ in range(K):
    val, a, b, c = heappop(que)
    ans.append(-val)

    for x, y, z in [(0, 0, 1), (0, 1, 0), (1, 0, 0)]:
        x = min(a + x, X - 1)
        y = min(b + y, Y - 1)
        z = min(c + z, Z - 1)
        if not (x, y, z) in V:
            heappush(que, (-(A[x] + B[y] + C[z]), x, y, z))
            V.add((x, y, z))

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

Solution (Python):
X,Y,Z,K = map(int,input().split())
l_a = list(map(int,input().split()))
l_b = list(map(int,input().split()))
l_c = list(map(int,input().split()))
ll = []
li = []
for i in l_a:
    for m in l_b:
        ll.append(i+m)
ll.sort(reverse = True)

del ll[K+1:]
for c in l_c:
    for i in ll:
        li.append(c+i)
li.sort(reverse = True)
for i in range(K):
    print(li[i])

Solution (Python):
X, Y, Z, K = [int(x) for x in input().split()]
A = sorted([int(x) for x in input().split()], reverse=True)
B = sorted([int(x) for x in input().split()], reverse=True)
C = sorted([int(x) for x in input().split()], reverse=True)
lst = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if ((i+1)*(j+1)*(k+1)<=K):
                lst.append(A[i]+B[j]+C[k])
            else:
                break
for l in sorted(lst, reverse=True)[:K]:
    print(l)

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)
K = []
flag = False

for i in range(x):
    for j in range(y):
        for l in range(z):
            heapq.heappush(K,a[i]+b[j]+c[l])
            if len(K)>k:
                mn = heapq.heappop(K)
                if a[i]+b[j]+c[l] == mn:
                    break
        if l == 0:
            break
    if j ==0:
        break
K.sort(reverse= True)
for i in range(k):
    print(K[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)

h = []
heappush(h, (-A[0]-B[0]-C[0], 0, 0, 0))
used = set()
result = []

for _ in range(K):
    sum, i, j, k = heappop(h)
    result.append(-sum)
    if i < X-1 and (i+1, j, k) not in used:
        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:
        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:
        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):
from heapq import heappop, heappush

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 = []
heappush(h, (-A[0]-B[0]-C[0], 0, 0, 0))
used = set()
result = []

for _ in range(K):
    sum, i, j, k = heappop(h)
    result.append(-sum)
    if i < X-1 and (i+1, j, k) not in used:
        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:
        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:
        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 numpy as np
from heapq import *

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 = []
heappush(h, (-A[0]-B[0]-C[0], 0, 0, 0))
used = set()
result = []

for _ in range(K):
    sum, i, j, k = heappop(h)
    result.append(-sum)
    if i < X-1 and (i+1, j, k) not in used:
        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:
        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:
        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 numpy as np
from heapq 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)

h = []
heappush(h, (-A[0]-B[0]-C[0], 0, 0, 0))
used = set()
result = []

for _ in range(K):
    sum, i, j, k = heappop(h)
    result.append(-sum)
    if i < X-1 and (i+1, j, k) not in used:
        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:
        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:
        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):
X,Y,Z,K = map(int, input().split())
A = [int(a) for a in input().split()]
B = [int(a) for a in input().split()]
C = [int(a) for a in input().split()]

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

import heapq

h = [[-A[0]-B[0]-C[0], 0, 0, 0]]
L = []
for _ in range(K):
    t = heapq.heappop(h)
    a = t[1]
    b = t[2]
    c = t[3]
    print(-t[0])
    if a+1 < X and [a+1, b, c] not in L:
        heapq.heappush(h, [-A[a+1]-B[b]-C[c], a+1, b, c])
        L.append([a+1, b, c])
    if b+1 < Y and [a, b+1, c] not in L:
        heapq.heappush(h, [-A[a]-B[b+1]-C[c], a, b+1, c])
        L.append([a, b+1, c])
    if c+1 < Z and [a, b, c+1] not in L:
        heapq.heappush(h, [-A[a]-B[b]-C[c+1], a, b, c+1])
        L.append([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()))

l = []
for i in range(len(a)):
  for j in range(len(b)):
    l.append(a[i]+b[j])

l.sort(reverse=True)
if k <= len(l):
  l = l[:k]

m = []
for i in range(len(l)):
  for j in range(len(c)):
    m.append(l[i]+c[j])

m.sort(reverse=True)   
for i in range(k):
  print(m[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=(sorted(A,reverse=True))[:K]
B=(sorted(B,reverse=True))[:K]

AB=[]
for Ai in A:
    for Bj in B:
        AB.append(Ai+Bj)
AB=(sorted(AB,reverse=True))[:K]

C=(sorted(C,reverse=True))[:K]

ABC=[]
for ABi in AB:
    for Cj in C:
        ABC.append(ABi+Cj)
ABC=(sorted(ABC,reverse=True))[:K]

for ABCi in ABC:
    print(ABCi)

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

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

R  = [sum([A[0], B[0], C[0]])]
Q  = {(0, 0, 1), (0, 1, 0), (1, 0, 0)}
D  = {(0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0)}
iS = [((a, b, c), sum([A[a], B[b], C[c]])) for a, b, c in Q if (a < X) and (b < Y) and (c < Z)]
iS = sorted(iS, key=lambda x: [x[1], x[0]], reverse=True)

while len(R) < K:
    R.append(iS[0][1])
    abc     = iS[0][0]
    a, b, c = abc[0], abc[1], abc[2]
    iS      = iS[1:]
    if a + 1 < X:
        if not (a + 1, b, c) in D:
            s = sum([A[a + 1], B[b], C[c]])
            is_done = False
            for i in range(len(iS)):
                if iS[i][1] < s:
                    iS = iS[:i] + [((a + 1, b, c), s)] + iS[i:]
                    is_done = True
                    break
            if is_done == False:
                iS = iS + [((a + 1, b, c), s)]
            D.add((a + 1, b, c))
    if b + 1 < Y:
        if not (a, b + 1, c) in D:
            s = sum([A[a], B[b + 1], C[c]])
            is_done = False
            for i in range(len(iS)):
                if iS[i][1] < s:
                    iS = iS[:i] + [((a, b + 1, c), s)] + iS[i:]
                    is_done = True
                    break
            if is_done == False:
                iS = iS + [((a, b + 1, c), s)]
            D.add((a, b + 1, c))
    if c + 1 < Z:
        if not (a, b, c + 1) in D:
            s = sum([A[a], B[b], C[c + 1]])
            is_done = False
            for i in range(len(iS)):
                if iS[i][1] < s:
                    iS = iS[:i] + [((a, b, c + 1), s)] + iS[i:]
                    is_done = True
                    break
            if is_done == False:
                iS = iS + [((a, b, c + 1), s)]
            D.add((a, b, c + 1))

for r in R:
    print(r)

Solution (Python):
import heapq as hq

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()))
a.sort()
b.sort()
c.sort()
possibles = [(a[0]+b[0]+c[0],0,0,0)]

done = set()
done |= {(0,0,0)}
def neibor(ai,bi,ci,possibles,done):
    for aj,bj,cj in [(ai+1,bi,ci),(ai,bi+1,ci),(ai,bi,ci+1)]:
        if (aj<=x-1) and (bj<=y-1) and (cj<=z-1) and (not (aj,bj,cj) in done):
            hq.heappush(possibles,(a[aj]+b[bj]+c[cj],aj,bj,cj))
            done  |= {(aj,bj,cj)}
    

def conf(possibles,done):
    yam,ai,bi,ci = hq.heappop(possibles)
    print(-1*yam)
    neibor(ai,bi,ci,possibles,done)

for i in range(k):
    yam,ai,bi,ci = hq.heappop(possibles)
    print(-1*yam)
    for aj,bj,cj in [(ai+1,bi,ci),(ai,bi+1,ci),(ai,bi,ci+1)]:
        if (aj<=x-1) and (bj<=y-1) and (cj<=z-1) and (not (aj,bj,cj) in done):
            hq.heappush(possibles,(a[aj]+b[bj]+c[cj],aj,bj,cj))
            done  |= {(aj,bj,cj)}

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_value = []
value = []
flag = False

for a in A:
    for b in B:
        ab_value.append(a + b)

ab_value.sort(reverse = True)
ab_value = ab_value[:K + 1]
C.sort(reverse = True)
i = 0
for k in ab_value:
    for c in C:
        value.append(c + k)
value.sort(reverse= True)
for i in range(K):
    print(value[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 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)

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

Solution (Python):
from sys import stdin
import heapq

X, Y, Z, K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]
C = [int(x) for x in stdin.readline().rstrip().split()]

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

heap = []
heapq.heappush(heap, (-(A[0] + B[0] + C[0]), 0, 0, 0))
s = {(0, 0, 0)}
for _ in range(K):
    v, i, j, k = heapq.heappop(heap)
    if i + 1 < X and (i + 1, j, k) not in s:
        s.add((i + 1, j, k))
        heapq.heappush(heap, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
    if j + 1 < Y and (i, j + 1, k) not in s:
        s.add((i, j + 1, k))
        heapq.heappush(heap, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
    if k + 1 < Z and (i, j, k + 1) not in s:
        s.add((i, j, k + 1))
        heapq.heappush(heap, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
    print(-v)

Solution (Python):
import heapq
X,Y,Z,K=map(int,input().split())
lst1=sorted(list(map(int,input().split())))
lst2=sorted(list(map(int,input().split())))
lst3=sorted(list(map(int,input().split())))
ans=[]
heapq.heappush(ans,[-(lst1[-1]+lst2[-1]+lst3[-1]),1,1,1])
tmp=0
while tmp!=K:
  H=heapq.heappop(ans)
  V=-1*H[0]
  tmp+=1
  a,b,c=-H[1],-H[2],-H[3]
  print(V)
  if a-1>=-X and [-(lst1[a-1]+lst2[b]+lst3[c]),-a+1,-b,-c] not in ans:
    heapq.heappush(ans,[-(lst1[a-1]+lst2[b]+lst3[c]),-a+1,-b,-c])
  if b-1>=-Y and [-(lst1[a]+lst2[b-1]+lst3[c]),-a,-b+1,-c] not in ans :
    heapq.heappush(ans,[-(lst1[a]+lst2[b-1]+lst3[c]),-a,-b+1,-c])
  if c-1>=-Z and [-(lst1[a]+lst2[b]+lst3[c-1]),-a,-b,-c+1] not in ans:
    heapq.heappush(ans,[-(lst1[a]+lst2[b]+lst3[c-1]),-a,-b,-c+1])

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)
inf = float("INF")
A.append(-inf)
B.append(-inf)
C.append(-inf)
s = []
Q = []
heappush(Q,[-(A[0]+B[0]+C[0]),0,0,0])
for _ in range(K):
    tmp = heappop(Q)
    print(-tmp[0])
    i = tmp[1]
    j = tmp[2]
    k = tmp[3]
    if [i+1,j,k] not in s:
        heappush(Q,[-(A[i+1]+B[j]+C[k]),i+1,j,k])
        s.append([i+1,j,k])
    if [i,j+1,k] not in s:
        heappush(Q,[-(A[i]+B[j+1]+C[k]),i,j+1,k])
        s.append([i,j+1,k])
    if [i,j,k+1] not in s:
        heappush(Q,[-(A[i]+B[j]+C[k+1]),i,j,k+1])
        s.append([i,j,k+1])

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)

seen=[(0,0,0)]
Q=[]
heapq.heappush(Q,[-(A[0]+B[0]+C[0]),0,0,0])
for _ in range(K):
  h=heapq.heappop(Q)
  i,j,k=h[1],h[2],h[3]
  print(-h[0])
  if i+1<X:
    if (i+1,j,k) not in seen:
      heapq.heappush(Q,[-(A[i+1]+B[j]+C[k]),i+1,j,k])
      seen.append((i+1,j,k))
  if j+1<Y:
    if (i,j+1,k) not in seen:
      heapq.heappush(Q,[-(A[i]+B[j+1]+C[k]),i,j+1,k])
      seen.append((i,j+1,k))
  if k+1<Z:
    if (i,j,k+1) not in seen:
      heapq.heappush(Q,[-(A[i]+B[j]+C[k+1]),i,j,k+1])
      seen.append((i,j,k+1))

Solution (Python):
import collections
import heapq
 
x, y, z, k = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(3)]
for i in range(3):
    ls[i].sort(reverse=True)
q = [(-sum([ls[i][0] for i in range(3)]), 0, 0, 0)]
res = []
used = collections.defaultdict(lambda: False)
while len(res) < k:
    s, i, j, l = heapq.heappop(q)
    res.append(-s)
    if i + 1 < x and not used[(i + 1, j, l)]:
        heapq.heappush(q, (-(ls[0][i + 1] + ls[1][j] + ls[2][l]), i + 1, j, l))
        used[(i + 1, j, l)] = True
    if j + 1 < y and not used[(i, j + 1, l)]:
        heapq.heappush(q, (-(ls[0][i] + ls[1][j + 1] + ls[2][l]), i, j + 1, l))
        used[(i, j + 1, l)] = True
    if l + 1 < z and not used[(i, j, l + 1)]:
        heapq.heappush(q, (-(ls[0][i] + ls[1][j] + ls[2][l + 1]), i, j, l + 1))
        used[(i, j, l + 1)] = True
print(*res, sep='\n')

Solution (Python):
import heapq

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

Q = []
heapq.heappush(Q, [-(A[0]+B[0]+C[0]),0,0,0])
for _ in range(K):
  p, i, j, k = heapq.heappop(Q)
  print(-p)
  if i+1<X and [-(A[i+1]+B[j]+C[k]),i+1,j,k] not in Q: heapq.heappush(Q, [-(A[i+1]+B[j]+C[k]),i+1,j,k])
  if j+1<Y and [-(A[i]+B[j+1]+C[k]),i,j+1,k] not in Q: heapq.heappush(Q, [-(A[i]+B[j+1]+C[k]),i,j+1,k])
  if k+1<Z and [-(A[i]+B[j]+C[k+1]),i,j,k+1] not in Q: heapq.heappush(Q, [-(A[i]+B[j]+C[k+1]),i,j,k+1])

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

ans_list = []

x_list.append(-99999999999)
y_list.append(-99999999999)
z_list.append(-99999999999)

l,m,n = 0,0,0

x_list.sort(reverse=True)
y_list.sort(reverse=True)
z_list.sort(reverse=True)

x1 = x_list[0]
y1 = y_list[0]
z1 = z_list[0]

while (l+1)*(m+1)*(n+1) < 100000 and (l<x and m<y and n<z):
    if x1-x_list[l+1] <= y1-y_list[m+1] and x1-x_list[l+1] <= z1-z_list[n+1]:
        l += 1
    elif y1-y_list[m+1] <= x1-x_list[l+1] and y1-y_list[m+1] <= z1-z_list[n+1]:
        m += 1
    elif z1-z_list[n+1] <= x1-x_list[l+1] and z1-z_list[n+1] <= y1-y_list[m+1]:
        n += 1
    else:
        print(ans_list[9999999])
        break

for p in range(l+1):
    for q in range(m+1):
        for r in range(n+1):
            ans_list.append(x_list[p]+y_list[q]+z_list[r])

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

Solution (Python):
x, y, z, k = map(int, input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))
A_B_list = []
for i in range(x):
    for j in range(y):
        A_B = A_list[i] + B_list[j]
        A_B_list.append(A_B)
A_B_ranking = sorted(A_B_list, reverse=True)

A_B_C_list = []
for i in range(min(k, x*y)):
    for j in range(z):
        A_B_C = A_B_ranking[i] + C_list[j]
        A_B_C_list.append(A_B_C)

A_B_C_ranking = sorted(A_B_C_list, reverse=True)
for i in range(k):
    print(A_B_C_ranking[i])

Solution (Python):
import collections
import heapq

x, y, z, k = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(3)]
for i in range(3):
    ls[i].sort(reverse=True)
q = [(-sum([ls[i][0] for i in range(3)]), 0, 0, 0)]
res = []
used = collections.defaultdict(lambda: False)
while len(res) < k:
    s, i, j, l = heapq.heappop(q)
    res.append(-s)
    index_list = [
        (i + 1, j, l),
        (i, j + 1, l),
        (i, j, l + 1),
    ]
    for i, (indices, limit) in enumerate(zip(index_list, [x, y, z])):
        if indices[i] < limit and not used[indices]:
            ss = sum([ls[j][l] for j, l in zip(range(3), indices)])
            heapq.heappush(q, (-ss, indices[0], indices[1], indices[2]))
            used[indices] = True
print(*res, sep='\n')

Solution (Python):
import collections
import heapq

x, y, z, k = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(3)]
for i in range(3):
    ls[i].sort(reverse=True)
q = [(-sum([ls[i][0] for i in range(3)]), 0, 0, 0)]
res = []
used = collections.defaultdict(lambda: False)
while len(res) < k:
    s, i, j, l = heapq.heappop(q)
    res.append(-s)
    if i + 1 < x and not used[(i + 1, j, l)]:
        heapq.heappush(q, (-(ls[0][i + 1] + ls[1][j] + ls[2][l]), i + 1, j, l))
        used[(i + 1, j, l)] = True
    if j + 1 < y and not used[(i, j + 1, l)]:
        heapq.heappush(q, (-(ls[0][i] + ls[1][j + 1] + ls[2][l]), i, j + 1, l))
        used[(i, j + 1, l)] = True
    if l + 1 < z and not used[(i, j, l + 1)]:
        heapq.heappush(q, (-(ls[0][i] + ls[1][j] + ls[2][l + 1]), i, j, l + 1))
        used[(i, j, l + 1)] = True
print(*res, sep='\n')

Solution (Python):
import heapq

x, y, z, k = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(3)]
for i in range(3):
    ls[i].sort(reverse=True)
q = [(-sum([ls[i][0] for i in range(3)]), 0, 0, 0)]
res = []
used = [(0, 0, 0)]
while len(res) < k:
    s, i, j, l = heapq.heappop(q)
    res.append(-s)
    if i + 1 < x and (i + 1, j, l) not in used:
        heapq.heappush(q, (-(ls[0][i + 1] + ls[1][j] + ls[2][l]), i + 1, j, l))
        used.append((i + 1, j, l))
    if j + 1 < y and (i, j + 1, l) not in used:
        heapq.heappush(q, (-(ls[0][i] + ls[1][j + 1] + ls[2][l]), i, j + 1, l))
        used.append((i, j + 1, l))
    if l + 1 < z and (i, j, l + 1) not in used:
        heapq.heappush(q, (-(ls[0][i] + ls[1][j] + ls[2][l + 1]), i, j, l + 1))
        used.append((i, j, l + 1))
print(*res, sep='\n')

Solution (Python):
import heapq


def select(ls0, ls1, k):
    ls = []
    for a in ls0:
        for b in ls1:
            heapq.heappush(ls, -(a + b))
    return [-heapq.heappop(ls) for _ in range(min(k, len(ls0) * len(ls1)))]


x, y, z, k = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(3)]
for i in range(3):
    ls[i].sort(reverse=True)
ps = select(ls[0], ls[1], k)
res = select(ps, ls[2], k)
print(*res, sep='\n')

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

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

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

    E.sort(reverse=True)

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

main()

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

    m = set((0, 0, 0))
    q = [[-1*(a[0]+b[0]+c[0]), 0, 0, 0]]
    heapq.heapify(q)
    for _ in range(k):
        ans, ai, bi, ci = heapq.heappop(q)
        if ai+1<x and (ai+1, bi, ci) not in m:
            m.add((ai+1, bi, ci))
            heapq.heappush(q, [-1*(a[ai+1]+b[bi]+c[ci]), ai+1, bi, ci])
        if bi+1<y and (ai, bi+1, ci) not in m:
            m.add((ai, bi+1, ci))
            heapq.heappush(q, [-1*(a[ai]+b[bi+1]+c[ci]), ai, bi+1, ci])
        if ci+1<z and (ai, bi, ci+1) not in m:
            m.add((ai, bi, ci+1))
            heapq.heappush(q, [-1*(a[ai]+b[bi]+c[ci+1]), ai, bi, ci+1])
        print(-1*ans)

if __name__ == "__main__":
    f_d()