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())
cakes = [list(map(int, input().split())) for _ in range(3)]
 
cakes = list(map(lambda x : sorted(x, reverse=True), cakes))
ans = list()
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1)*(j+1)*(k+1)>K:
                break
            ans.append(cakes[0][i] + cakes[1][j] + cakes[2][k])
ans.sort(reverse=True)
print("\n".join(map(str, ans[:K])))

Solution (Python):
X,Y,Z,K = map(int, input().split())
cakes = [list(map(int, input().split())) for _ in range(3)]
 
cakes = list(map(lambda x : sorted(x, reverse=True), cakes))
ans = list()
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1)*(j+1)*(k+1)>K:
                break
            ans.append(cakes[0][i] + cakes[1][j] + cakes[2][k])
ans.sort(reverse=True)
print("\n".join(map(str, ans[:K])))

Solution (Python):
import itertools
import sys

input = sys.stdin.readline

maxn = 10000000000

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)

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

cand.sort(reverse=True)
for k in range(K):
    print(cand[k])

Solution (Python):
X,Y,Z,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
asort,bsort,csort = sorted(A,reverse=True),sorted(B,reverse=True),sorted(C,reverse=True)
print(asort[0] + bsort[0] + csort[0])
already = [[0,0,0]]
x,y,z = 0,0,0
place_sum = []
check = []
for k in range(K - 1):
    if [x + 1,y,z] not in already and x + 1 < X:
        already.append([x + 1,y,z])
        place_sum.append([x + 1,y,z])
        check.append(asort[x + 1] + bsort[y] + csort[z])
    if [x,y + 1,z] not in already and y + 1 < Y:
        already.append([x,y + 1,z])
        place_sum.append([x,y + 1,z])
        check.append(asort[x] + bsort[y + 1] + csort[z])
    if [x,y,z + 1] not in already and z + 1 < Z:
        already.append([x,y,z + 1])
        place_sum.append([x,y,z + 1])
        check.append(asort[x] + bsort[y] + csort[z + 1])
    print(max(check))
    x,y,z = place_sum[check.index(max(check))][0],place_sum[check.index(max(check))][1],place_sum[check.index(max(check))][2]
    place_sum.pop(check.index(max(check)))
    check.remove(max(check))

Solution (Python):
from heapq import *

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 = sorted([int(x) for x in A], reverse=True)
B = sorted([int(x) for x in B], reverse=True)
C = sorted([int(x) for x in C], reverse=True)

res = []
memo = []
ans = []
heappush(res, (-A[0]-B[0]-C[0], 0, 0, 0))
while len(ans) < K:
  e, i, j, k = heappop(res)
  ans.append(-e)
  if i+1 < X and [i+1, j, k] not in memo:
    heappush(res, (-A[i+1]-B[j]-C[k], i+1, j, k))
    memo.append([i+1, j, k])
  if j+1 < Y and [i, j+1, k] not in memo:
    heappush(res, (-A[i]-B[j+1]-C[k], i, j+1, k))
    memo.append([i, j+1, k])
  if k+1 < Z and [i, j, k+1] not in memo:
    heappush(res, (-A[i]-B[j]-C[k+1], i, j, k+1))
    memo.append([i, j, k+1])

for i in ans:
  print(i)

Solution (Python):
import itertools
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)
A,B,C =A[:k],B[:k],C[:k]
AB = sorted(list(i+j for i in A for j in B), reverse=True)[:k]

ABC = sorted(list(i+j for i in AB for j in C),reverse = True)[:k]

for i in ABC:
    print(i)

Solution (Python):
from sys import exit

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 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):
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([-aa for aa in a])
b = sorted([-bb for bb in b])
c = sorted([-cc for cc in c])
a_diff = [a[i+1]-a[i] for i in range(x-1)]
b_diff = [b[i+1]-b[i] for i in range(y-1)]
c_diff = [c[i+1]-c[i] for i in range(z-1)]

que = [(a[0]+b[0]+c[0], (0, 0, 0))]
heapq.heapify(que)
s = set([(0, 0, 0)])
while k > 0:
    ans, (ia, ib, ic) = heapq.heappop(que)
    print(-ans)
    
    k -= 1

    if ia < x-1 and (ia+1, ib, ic) not in s:
        heapq.heappush(que, (ans+a_diff[ia], (ia+1, ib, ic)))
        s.add((ia+1, ib, ic))
    if ib < y-1 and (ia, ib+1, ic) not in s:
        heapq.heappush(que, (ans+b_diff[ib], (ia, ib+1, ic)))
        s.add((ia, ib+1, ic))
    if ic < z-1 and (ia, ib, ic+1) not in s:
        heapq.heappush(que, (ans+c_diff[ic], (ia, ib, ic+1)))
        s.add((ia, ib, ic+1))

Solution (Python):
import heapq
 
_x, _y, _z, k = map(int, input().split())
aa = list(reversed(sorted(map(int, input().split()))))
bb = list(reversed(sorted(map(int, input().split()))))
cc = list(reversed(sorted(map(int, input().split()))))
 
ar = []
history = set()
 
 
def push(x, y, z):
    if (x, y, z) in history:
        pass
    else:
        v = aa[x] + bb[y] + cc[z]
        heapq.heappush(ar, (-v, x, y, z))
        history.add((x, y, z))
 
 
def pop():
    (v, x, y, z) = heapq.heappop(ar)
    return (-v, x, y, z)
 
 
push(0, 0, 0)
for _ in range(k):
    (total, x, y, z) = pop()
    print(total)
    if x + 1 < len(aa):
        push(x + 1, y, z)
    if y + 1 < len(bb):
        push(x, y + 1, z)
    if z + 1 < len(cc):
        push(x, y, z + 1)

Solution (Python):
import heapq

_x, _y, _z, k = map(int, input().split())
aa = list(reversed(sorted(map(int, input().split()))))
bb = list(reversed(sorted(map(int, input().split()))))
cc = list(reversed(sorted(map(int, input().split()))))

ar = []
history = set()


def push(x, y, z):
    if (x, y, z) in history:
        pass
    else:
        v = aa[x] + bb[y] + cc[z]
        heapq.heappush(ar, (-v, x, y, z))
        history.add((x, y, z))


def pop():
    (v, x, y, z) = heapq.heappop(ar)
    return (-v, x, y, z)


push(0, 0, 0)
for _ in range(k):
    (total, x, y, z) = pop()
    print(total)
    if x + 1 < len(aa):
        push(x + 1, y, z)
    if y + 1 < len(bb):
        push(x, y + 1, z)
    if z + 1 < len(cc):
        push(x, y, z + 1)

Solution (Python):
x,y,z,w = map(int, input().split())
a = [list(map(int, input().split())) for i in range(3)]
for i in range(3) : a[i].sort(reverse=True)
li = []
for i in range(1, x+1):
  for j in range(1, y+1):
    if i*j > w : break
    for k in range(1, z+1):
      if i*j*k > w : break
      li.append(a[0][i-1]+a[1][j-1]+a[2][k-1])
li.sort(reverse=True)
print("\n".join(map(str,li[:w])))

Solution (Python):
import heapq
from collections import defaultdict as dd

X, Y, Z, K = [int(i) for i in input().split()]
ABCList = [sorted([int(i) for i in input().split()], reverse=True) for _ in range(3)]

pQ = []
heapq.heappush(pQ, [sum(ABC[0] for ABC in ABCList) * (-1), [0, 0, 0]])
QDict = dd(int)
QDict[(0, 0, 0)] = 1
for _ in range(K):
    item = heapq.heappop(pQ)
    print(item[0] * (-1))
    abc = item.copy()[1]
    for i, element in enumerate(abc):
        abcTemp = abc.copy()
        abcTemp[i] += 1
        if abcTemp[i] < len(ABCList[i]) and QDict[tuple(abcTemp)] == 0:
            newItem = [sum([ABCList[i][abcTemp[i]] for i in range(3)]) * (-1), abcTemp]
            heapq.heappush(pQ, newItem)
            QDict[(tuple(abcTemp))] = 1

Solution (Python):
x,y,z,K = (int(i) for i in input().split(" "))

a_ = sorted([int(i) for i in input().split(" ")])[::-1]
b_ = sorted([int(i) for i in input().split(" ")])[::-1]
c_ = sorted([int(i) for i in input().split(" ")])[::-1]

res = []
for i in range(x):
    for j in range(y):
        for k in range(z):
            if (i+1)*(j+1)*(k+1) <= K:
                res.append(a_[i]+b_[j]+c_[k])
            else:
                break

res = sorted(res)[::-1]

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

Solution (Python):
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True 
def ddprint(x):
  if DBG:
    print(x)

x,y,z,k = inm()
a = inl()
b = inl()
c = inl()
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
if x < 60 or y < 60:
    loa = x
    lob = y
elif a[0]-a[60] > b[0]-b[60]:
    loa = 60
    lob = y
else:
    loa = x
    lob = 60

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

u = len(ab)
if z < 60 or u < 60:
    loc = z
    loab = u
elif c[0]-c[60] > ab[0]-ab[60]:
    loc = 60
    loab = u
else:
    loc = z
    loab = 60

abc = []
for i in range(loc):
    for j in range(loab):
        abc.append(c[i]+ab[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)

heap = []
item = (0,(0,0,0))
max_score = A[0] + B[0] + C[0]
xyzset = set(item[1])
answer = []

heapq.heappush(heap,item)

while len(answer) < K:
    score,(x,y,z) = heapq.heappop(heap)
    answer.append(max_score - score)

    if x != X-1 and (x+1,y,z) not in xyzset:
        heapq.heappush(heap,(max_score - A[x+1] - B[y] - C[z],(x+1,y,z)))
        xyzset.add((x+1,y,z))

    if y != Y-1 and (x,y+1,z) not in xyzset:
        heapq.heappush(heap,(max_score - A[x] - B[y+1] - C[z],(x,y+1,z)))
        xyzset.add((x,y+1,z))

    if z != Z-1 and (x,y,z+1) not in xyzset:
        heapq.heappush(heap,(max_score - A[x] - B[y] - C[z+1],(x,y,z+1)))
        xyzset.add((x,y,z+1))
    
for x in answer:
    print(x)

Solution (Python):
import heapq

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)

anslist = []
flag = {}

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


for _ in range(K):
    p = heapq.heappop(anslist)
    while p[1] in flag:
        p = heapq.heappop(anslist)
    print(p[0]*(-1))
    a, b, c = p[1]
    flag[a, b, c] = True
    
    for i, j, k in [[1, 0, 0], [0, 1, 0], [0, 0, 1]]:
        if a+i >= X or b+j >= Y or c+k >= Z:
            continue
        tmp = (a+i, b+j, c+k)
        heapq.heappush(anslist, (-(A[a+i]+B[b+j]+C[c+k]), tmp))

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)

hq = []
visited = set()
c = 0

def visit(i, j, k):
    if (i < X and j < Y and k < Z) and (visited.isdisjoint({(i, j, k)})):
        heapq.heappush(hq, ( -(A[i] + B[j] + C[k]), (i, j, k)))
        visited.add((i, j, k))

visit(0, 0, 0)

while len(hq) != 0 and c < K:
    taberu = heapq.heappop(hq)
    print(-taberu[0])

    visit(taberu[1][0] + 1, taberu[1][1], taberu[1][2])
    visit(taberu[1][0], taberu[1][1] + 1, taberu[1][2])
    visit(taberu[1][0], taberu[1][1], taberu[1][2] + 1)

    c += 1

Solution (Python):
import heapq


class Delicious (object):
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

    def total(self):
        return(A[self.x] + B[self.y] + C[self.z])

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


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)

ds = []
heapq.heappush(ds, Delicious(0, 0, 0))

check = {}

for _ in range(K):
    d = heapq.heappop(ds)
    while (d.x, d.y, d.z) in check:
        d = heapq.heappop(ds)
    print(d.total())
    check[d.x, d.y, d.z] = True

    if d.x < X-1:
        heapq.heappush(ds, Delicious(d.x+1, d.y, d.z))
    if d.y < Y-1:
        heapq.heappush(ds, Delicious(d.x, d.y+1, d.z))
    if d.z < Z-1:
        heapq.heappush(ds, Delicious(d.x, d.y, d.z+1))

Solution (Python):

import sys
import heapq

def pResult(a,b,c):
    print(a[1]+b[1]+c[1])


def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    A.sort(reverse = True)
    B.sort(reverse = True)
    C.sort(reverse = True)
    Q = []
    D = set([])
    I = A[0] + B[0] + C[0]
    heapq.heappush(Q,(-I,I,0,0,0))
    k = 0
    while(True):
        if k == K:
            break
        prev = heapq.heappop(Q)
        ai = prev[2]
        bi = prev[3]
        ci = prev[4]
        if (ai,bi,ci) in D:
            continue
        print(prev[1])
        D.add((ai,bi,ci))
        k+=1
        if ai+1 < len(A):
            i = A[ai + 1] + B[bi] + C[ci]
            heapq.heappush(Q,(-i,i,ai+1,bi,ci))
        if bi+1 < len(B):
            i = A[ai] + B[bi + 1] + C[ci]
            heapq.heappush(Q,(-i,i,ai,bi + 1,ci))
        if ci+1 < len(C):
            i = A[ai] + B[bi] + C[ci + 1]
            heapq.heappush(Q,(-i,i,ai,bi,ci + 1))




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

if __name__ == '__main__':
    main()

Solution (Python):
x,y,z,k = map(int,input().split())
lia = sorted(list(map(int,input().split())),reverse = True)
lib = sorted(list(map(int,input().split())),reverse = True)
lic = sorted(list(map(int,input().split())),reverse = True)
li = []

for s in range(x):
    for t in range(y):
        if s*t > k: break
        for u in range(z):
            if s*t*u <= k:
                li.append(lia[s]+lib[t]+lic[u])
            else: break
ans = sorted(li,reverse = True)[:k]

print('\n'.join(map(str,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()))
ABlist = []
ABClist = []

for x in A:
    for y in B:
        sumple = x + y
        ABlist.append(sumple)
    ABlist.sort(reverse=True)
    ABlist = ABlist[:K]

for x in ABlist:
    for z in C:
        test = x + z
        ABClist.append(test)
    ABClist.sort(reverse=True)
    ABClist = ABClist[:K]

for x in ABClist:
    print(x)

Solution (Python):
X,Y,Z,K = list(map(int,input().split()))
A = [(sorted(list(map(int,input().split())))[::-1]) for i in range(3)]

import heapq

heap = []
item = (0,(0,0,0))
heapq.heappush(heap, item)
considered_dict = set(item[1])

ret = []
max_ = A[0][0]+A[1][0]+A[2][0]
while len(ret) < K:
    task  = heapq.heappop(heap)
    score,(x,y,z) =task
    ret.append(score)
    
    if x+1 != X and (x+1,y,z) not in considered_dict:
        heapq.heappush(heap, (max_-A[0][x+1]-A[1][y]-A[2][z],(x+1,y,z) ))
        considered_dict |= {(x+1,y,z)}
        
                       
    if y+1 != Y and (x,y+1,z) not in considered_dict:
        heapq.heappush(heap, (max_-A[0][x]-A[1][y+1]-A[2][z],(x,y+1,z) ))
        considered_dict |= {(x,y+1,z)}
                       
    if z+1 != Z and (x,y,z+1) not in considered_dict:
        heapq.heappush(heap, (max_-A[0][x]-A[1][y]-A[2][z+1],(x,y,z+1) ))
        considered_dict |= {(x,y,z+1)}


for i in ret:
    print(max_-i)

Solution (Python):
def main():
    x,y,z,k = map(int,input().split())
    lia = sorted(list(map(int,input().split())))
    lib = sorted(list(map(int,input().split())))
    lic = sorted(list(map(int,input().split())))

    li2 = sorted(list(a+b for a in lia for b in lib),reverse = True)[:k]
    li3 = sorted(list(ab + c for ab in li2 for c in lic),reverse = True)[:k]

    print('\n'.join(map(str,li3)))

main()

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

a2=a[:k]
b2=[]
for i in a2:
  for j in b[:k]:
    b2.append(i+j)
  b2.sort()
  b2.reverse()
  b2=b2[:k]

c2=[]
for i in b2:
  for j in c[:k]:
    c2.append(i+j)
  c2.sort()
  c2.reverse()
  c2=c2[:k]
for i in c2:
  print(i)

Solution (Python):
import heapq
x,y,z,t=map(int,input().split())
a,b,c=[sorted(list(map(int,input().split())),reverse=True) for i in range(3)]
heap=[(-a[0]-b[0]-c[0],0,0,0)]
ed=set()
for _ in range(t):
	p,i,j,k=heapq.heappop(heap)
	print(-p)
	if i+1<x and (i+1,j,k) not in ed:
		ed.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 ed:
		ed.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 ed:
		ed.add((i,j,k+1))
		heapq.heappush(heap,(-a[i]-b[j]-c[k+1],i,j,k+1))

Solution (Python):
def solve():
  x,y,z,k = (int(i) for i in input().split())
  a = list(int(i) for i in input().split())
  b = list(int(i) for i in input().split())
  c = list(int(i) for i in input().split())
  a = sorted(a,reverse=True)
  b = sorted(b,reverse=True)
  c = sorted(c,reverse=True)
  ab = []
  abc = []
  
  for i in range(x):
    for j in range(y):
      ab.append(a[i]+b[j])
  ab = sorted(ab,reverse = True)
  for t in range(min(k,len(ab))):
    for l in range(z):
      abc.append(ab[t]+c[l])
      
  abc = sorted(abc,reverse = True)
  for i in range(k):
    print(abc[i])

solve()

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

ls = [0]*(len(A)*len(B))
for i in range(len(A)):
    for j in range(len(B)):
        ls[(i*len(B))+j] = A[i] + B[j]
ls.sort(reverse = True)
ls = ls[:k]
check_ls = []
for i in range(len(C)):
    for j in range(len(ls)):
        check_ls.append(C[i] + ls[j])
check_ls.sort(reverse = True)
for i in range(k):
    print(check_ls[i])

Solution (Python):
import heapq

X, Y, Z, K = [int(i) for i in input().split()]
ABCList = [sorted([int(i) for i in input().split()], reverse=True) for _ in range(3)]

pQ = []
heapq.heappush(pQ, [sum(ABC[0] for ABC in ABCList) * (-1), [0, 0, 0]])
QList = [(0, 0, 0)]
for _ in range(K):
    item = heapq.heappop(pQ)
    print(item[0] * (-1))
    abc = item.copy()[1]
    for i, element in enumerate(abc):
        abcTemp = abc.copy()
        abcTemp[i] += 1
        if abcTemp[i] < len(ABCList[i]) and not tuple(abcTemp) in QList:
            newItem = [sum([ABCList[i][abcTemp[i]] for i in range(3)]) * (-1), abcTemp]
            heapq.heappush(pQ, newItem)
            QList.append(tuple(abcTemp))

Solution (Python):
from heapq import heappop, heappush

X,Y,Z,K = map(int, input().split())
xs = [int(x) for x in input().split()]
ys = [int(y) for y in input().split()]
zs = [int(z) for z in input().split()]
xs.sort()
xs.reverse()
ys.sort()
ys.reverse()
zs.sort()
zs.reverse()



def bfs(max = 3000):
    count = 0
    pq = []
    checked = {}

    heappush(pq, (-(xs[0]+ys[0]+zs[0]), (0,0,0)))
    checked[(0,0,0)] = 1
    while count < max:
        value, (x, y, z) = heappop(pq)
        print(-value)
        count += 1
        if x+1 < X and not (x+1, y, z) in checked:
            checked[(x+1, y, z)] = 1
            heappush(pq, (-(xs[x+1]+ys[y]+zs[z]), (x+1, y, z)))
        if y+1 < Y and not (x, y+1, z) in checked:
            checked[(x, y+1, z)] = 1
            heappush(pq, (-(xs[x]+ys[y+1]+zs[z]), (x, y+1, z)))
        if z+1 < Z and not (x, y, z+1) in checked:
            checked[(x, y, z+1)] = 1
            heappush(pq, (-(xs[x]+ys[y]+zs[z+1]), (x, y, z+1)))

bfs(K)

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

ans = []
for x in range(X):
    for y in range(Y):
        ans.append(A[x]+B[y])

ans.sort(reverse=True)
C.sort(reverse=True)
ans2 = []
for k in range(min(K,X*Y)):
    for z in range(min(Z,K)):
        ans2.append(ans[k]+C[z])
ans2.sort(reverse=True)

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

Solution (Python):


from heapq import heappush, heappop
def io_generator():
	return input()


	
class Foo(object):
	def __init__(self, value, state):
		self.state = state
		self.value = value

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

def get_c(a_state, a_mm):
	ret=[]
	for i, _ in enumerate(a_state):
		if a_state[i] >= a_mm[i]-1:
			continue
		ret.append([v + (i==j) for j,v in enumerate(a_state)])

	return ret
	
def gv(a_l,a_state):
	ret= sum([l[s] for l, s in zip(a_l, a_state)])
	return ret
	
def lss(l):
	return '_'.join([str(i) for i in l])
	
def main(io):
	x,y,z,k=map(int, io().split())
	ml=[x,y,z]
	al =list(map(int, io().split()))
	al.sort(reverse=True)
	bl =list(map(int, io().split()))
	bl.sort(reverse=True)
	cl =list(map(int, io().split()))
	cl.sort(reverse=True)
	ls=[al,bl,cl]
	ggv=lambda l : gv(ls,l)
	
	qq = []
	ol=set()
	
	init=[0,0,0]
	heappush(qq,Foo(ggv(init), init))
	ol.add(lss(init))
	
	for _ in range(k):
		ff=heappop(qq)
		print(ff.value)
		cs=ff.state
		nc=get_c(cs,ml)
		for ns in nc:
			if lss(ns) in ol:
				continue
			else:
				heappush(qq, Foo(ggv(ns), ns))
				ol.add(lss(ns))
	return 1



if __name__ == "__main__":
	io= lambda : io_generator()
	
	main(io)

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

Solution (Python):
from queue import PriorityQueue

[X,Y,Z,K] = list(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)

ai = 0
bi = 0
ci = 0

vec = []

for i in range(len(A)):
    for j in range(len(B)):
        for k in range(len(C)):
            if (i+1)*(j+1)*(k+1) <= K:
                vec.append(A[i] + B[j] + C[k])
            else:
                break
vec = sorted(vec, reverse=True)
for i in range(K):
    print(vec[i])

Solution (Python):
import heapq
X, Y, Z, K = map(int, raw_input().split())
A = map(int, raw_input().split())
B = map(int, raw_input().split())
C = map(int, raw_input().split())

h1 = []
for a in A:
  for b in B:
    heapq.heappush(h1, -a - b)

h2 = []
for _ in range(min(K, X * Y)):
  ab = -heapq.heappop(h1)
  for c in C:
    heapq.heappush(h2, -ab - c)

for _ in range(K):
  print -heapq.heappop(h2)

Solution (Python):
def main():
  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)
  A += [-1e10]
  B += [-1e10]
  C += [-1e10]

  memo = [A[i] + B[0] + C[0] for i in range(X + 1)]
  L = [[0 for i in range(Y + 1)] for j in range(X + 1)]

  for i in range(K):
    ma = 0
    for j in range(X):
      if memo[ma] < memo[j]:
        ma = j
    print(memo[ma])
    mb = 0
    mn = 0
    for j in range(Y):
      x = B[j] + C[L[ma][j]]
      if mb < x:
        mb = x
        mn = j
    L[ma][mn] += 1
    n = 0
    for j in range(Y):
      n = max(n, B[j] + C[L[ma][j]])
    memo[ma] = A[ma] + n
 
main()

Solution (Python):
import math
import functools
import itertools
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

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

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

ab = []
for i in list(itertools.product(A,B)):
    ab.append(sum(i))

ab.sort(reverse=True)

ans = []
for i in list(itertools.product(ab[:3000],C)):
    ans.append(sum(i))

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

ab = []
for i in A:
  for j in B:
    ab.append(i + j)

ab.sort(reverse = True)

abc = []
for i in ab[:3000]:
  for j in C:
    abc.append(i + j)

abc.sort(reverse = True)

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

Solution (Python):
x,y,z,k = map(int, input().split())
aa = sorted(list(map(int, input().split())))
bb = sorted(list(map(int, input().split())))
cc = sorted(list(map(int, input().split())))
 
ab = sorted(a+b for a in aa for b in bb)[:-k-1:-1]
abc = sorted(s+c for s in ab[:k] for c in cc)[:-k-1:-1]
print(*abc, sep='\n')

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

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 = 0
count = 1
heap = [(0, (0, 0, 0))]
log = [(0, 0, 0)]
heapq.heapify(heap)
while count <= K:
    count += 1
    q = heapq.heappop(heap)
    a = q[1][0]
    b = q[1][1]
    c = q[1][2]
    print(A[a]+B[b]+C[c])
    if(a+1 < len(A) and not (a+1, b, c) in log):
        heapq.heappush(heap, (q[0]+A[a]-A[a+1], (a+1, b, c)))
        log.append((a+1, b, c))
    if(b+1 < len(B)  and not (a, b+1, c) in log):
        heapq.heappush(heap, (q[0]+B[b]-B[b+1], (a, b+1, c)))
        log.append((a, b+1, c))
    if(c+1 < len(C)  and not (a, b, c+1) in log):
        heapq.heappush(heap, (q[0]+C[c]-C[c+1], (a, b, c+1)))
        log.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()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
num = []
for a in A:
    for b in B:
        num.append(a+b)
num.sort(reverse = True)
num2 = []
for i in range(min(len(num),k)):
    for j in C:
        num2.append(num[i]+j)
num2.sort(reverse = True)
for i in range(k):
    print(num2[i])

Solution (Python):


x,y,z,k=(int(i) for i in input().split())

a=list(int(i) for i in input().split())
a.sort(reverse=True)
b=list(int(i) for i in input().split())
b.sort(reverse=True)
c=list(int(i) for i in input().split())
c.sort(reverse=True)






x=[]
for h in range(len(a)):
    for i in range(len(b)):
        if (h+1)*(i+1) <=k:
            for j in range(len(c)):
                if (h+1)*(i+1)*(j+1) <=k:
                    x.append(a[h]+b[i]+c[j])

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

Solution (Python):


x,y,z,k=(int(i) for i in input().split())

a=list(int(i) for i in input().split())
a.sort(reverse=True)
b=list(int(i) for i in input().split())
b.sort(reverse=True)
c=list(int(i) for i in input().split())
c.sort(reverse=True)






x=[]
for h in range(len(a)):
    for i in range(len(b)):
        if (h+1)*(i+1) <=k:
            for j in range(len(c)):
                if (h+1)*(i+1)*(j+1) <=k:
                    x.append(a[h]+b[i]+c[j])

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