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 math
import sys
import heapq



X,Y,Z,K = map(int, input().split())
A = list(map(int, sys.stdin.readline().rstrip().split()))
B = list(map(int, sys.stdin.readline().rstrip().split()))
C = list(map(int, sys.stdin.readline().rstrip().split()))
A = sorted(A, key=lambda x:-x)
B = sorted(B, key=lambda x:-x)
C = sorted(C, key=lambda x:-x)


heap_q = []
heapq.heappush(heap_q, [-1*(A[0]+B[0]+C[0]), 0, 0, 0])
smt,at,bt,ct = [0, 0, 0, 0]

for _ in range(K):
  sm,a,b,c = heapq.heappop(heap_q)
  print(-sm)

  if len(heap_q) > 0:
    smt,at,bt,ct = heapq.heappop(heap_q)
  while len(heap_q) > 0 and sm==smt and a==at and b==bt and c==ct:
    smt,at,bt,ct = heapq.heappop(heap_q)
  heapq.heappush(heap_q, [smt,at,bt,ct])

  if a+1 < X:
    heapq.heappush(heap_q, [-A[a+1]-B[b]-C[c], a+1, b, c])
  if b+1 < Y:
    heapq.heappush(heap_q, [-A[a]-B[b+1]-C[c], a, b+1, c])
  if c+1 < Z:
    heapq.heappush(heap_q, [-A[a]-B[b]-C[c+1], a, b, c+1])

Solution (Python):
from heapq import heappush, heappop

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

A = sorted([int(e) for e in input().split()], reverse=True)
B = sorted([int(e) for e in input().split()], reverse=True)
C = sorted([int(e) for e in input().split()], reverse=True)


def get_value(ai, bi, ci):
    return -(A[ai] + B[bi] + C[ci])


heap = []
heappush(heap, (get_value(0, 0, 0), 0, 0, 0))
used = {(0, 0, 0)}

for k in range(K):
    v, ia, ib, ic = heappop(heap)
    print(-v)

    if ia + 1 < X and (ia + 1, ib, ic) not in used:
        heappush(heap, (get_value(ia + 1, ib, ic), ia + 1, ib, ic))
        used.add((ia + 1, ib, ic))
    if ib + 1 < Y and (ia, ib + 1, ic) not in used:
        heappush(heap, (get_value(ia, ib + 1, ic), ia, ib + 1, ic))
        used.add((ia, ib + 1, ic))
    if ic + 1 < Z and (ia, ib, ic + 1) not in used:
        heappush(heap, (get_value(ia, ib, ic + 1), ia, ib, ic + 1))
        used.add((ia, ib, ic + 1))

Solution (Python):
import heapq


def heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap) - 1)


def heappop_max(heap):
    lastelt = heap.pop()
    if heap:
        return_item = heap[0]
        heap[0] = lastelt
        heapq._siftup_max(heap, 0)
        return return_item
    return lastelt


X, Y, Z, K = map(int, input().split())
A = sorted([int(i) for i in input().split()], reverse=True)
B = sorted([int(i) for i in input().split()], reverse=True)
C = sorted([int(i) for i in input().split()], reverse=True)
ans = []
heap_queue = [(A[0]+B[0]+C[0], 0, 0, 0)]
arg_hash = {}

for i in range(K):
    maxim, a, b, c = heappop_max(heap_queue)
    print(maxim)
    arg_a = (a+1, b, c)
    arg_b = (a, b + 1, c)
    arg_c = (a, b, c + 1)

    if a + 1 < X and arg_a not in arg_hash:
        heappush_max(heap_queue, (A[a + 1] + B[b] + C[c], a + 1, b, c))
        arg_hash[arg_a] = 1
    if b + 1 < Y and arg_b not in arg_hash:
        heappush_max(heap_queue, (A[a] + B[b+1] + C[c], a, b+1, c))
        arg_hash[arg_b] = 1
    if c + 1 < Z and arg_c not in arg_hash:
        heappush_max(heap_queue, (A[a] + B[b] + C[c+1], a, b, c+1))
        arg_hash[arg_c] = 1

Solution (Python):
import heapq,sys


def main():
    input = sys.stdin.readline
    X, Y, Z, K = map(int, input().split())
    
    A = tuple(map(int,input().split()))
    B = tuple(map(int,input().split()))
    C = tuple(map(int,input().split()))
    
    
    AandB = []
    for x in A:
        for y in B:
            heapq.heappush(AandB,x+y)
            
            if len(AandB) > K:
                heapq.heappop(AandB)
    
    heap_queue = []
    for z in C:
        for elt in AandB:
            heapq.heappush(heap_queue,z+elt)
            if len(heap_queue) > K:
                heapq.heappop(heap_queue)
    heap_queue.sort(reverse = True)
    for e in heap_queue:
        print(e)
if __name__ == '__main__':
    main()

Solution (Python):
great_list = []
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)

for i in range(min(k,len(a)))[:k]:
	for j in range(min(k // (i+1),len(b)))[:k]:
		for l in range(min(k // ((i+1) * (j+1)),len(c)))[:k]:
			great_list.append(a[i] + b[j] + c[l])

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

Solution (Python):
from heapq import heappush, heappop


X,Y,Z,K = map(int, 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_list.sort(reverse = True)
b_list.sort(reverse = True)
c_list.sort(reverse = True)


hq = []
arg_hash = {}
val = a_list[0] + b_list[0] + c_list[0]
heappush(hq, (-val,0,0,0))

for i in range(K):
    ans, a, b, c = heappop(hq)
    print(-ans)

    arg_a = (a+1, b, c)
    arg_b = (a, b+1, c)
    arg_c = (a, b, c+1)

    if a < X-1 and arg_a not in arg_hash:
        arg_hash[arg_a] = 1
        heappush(hq, (-(a_list[a+1]+b_list[b]+c_list[c]),)+arg_a)
    if b < Y-1 and arg_b not in arg_hash:
        arg_hash[arg_b] = 1
        heappush(hq, (-(a_list[a]+b_list[b+1]+c_list[c]),)+arg_b)
    if c < Z-1 and arg_c not in arg_hash:
        arg_hash[arg_c] = 1
        heappush(hq, (-(a_list[a]+b_list[b]+c_list[c+1]),)+arg_c)

Solution (Python):
great_list = []
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)

for i in range(min(k,len(a)))[:k]:
	for j in range(min(k // (i+1),len(b)))[:k]:
		for l in range(min(k // ((i+1) * (j+1)),len(c)))[:k]:
			great_list.append(a[i] + b[j] + c[l])

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

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

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

ans_list = []
for x,a in enumerate(A):
    for y,b in enumerate(B):
        if (x+1)*(y+1)>K:
                break
        for z,c in enumerate(C):
            if (x+1)*(y+1)*(z+1)>K:
                break
            ans_list.append(a+b+c)

ans_list.sort(reverse=True)

for ans in ans_list[:K]:
    print(ans)

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

ansList = []
for i, A in enumerate(ABCList[0]):
    for j, B in enumerate(ABCList[1]):
        for k, C in enumerate(ABCList[2]):
            if (i + 1) * (j + 1) * (k + 1) <= K:
                ansList.append(A + B + C)
            else:
                break

for ans in sorted(ansList, reverse=True)[:K]:
    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)
abc = []
for i in range(x):
    if i > K:
        break
    for j in range(y):
        if i*j > K:
            break
        for k in range(z):
            if i*j*k > K:
                break
            abc.append(a[i]+b[j]+c[k])
abc.sort(reverse=True)
for i in range(K):
    print(abc[i])

Solution (Python):
import heapq
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(key=lambda x:-x)
B.sort(key=lambda y:-y)
C.sort(key=lambda z:-z)
pri=[]
def dp(x,y,z):
  return A[x]+B[y]+C[z]

def priq(a,b,c):
  d=-dp(a,b,c)
  if tuple([d,a,b,c]) not in pri:
    heapq.heappush(pri,(d,a,b,c))

heapq.heappush(pri,(-dp(0,0,0),0,0,0))

for i in range(K):
  d,x,y,z=heapq.heappop(pri)
  print(-d)
  if x<X-1:
    priq(x+1,y,z)
  if y<Y-1:
    priq(x,y+1,z)
  if z<Z-1:
    priq(x,y,z+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)
 
n = []
heapq.heapify(n)
heapq.heappush(n, (-(a[0]+b[0] + c[0]), 0,0,0))
for i in range(K):
    (m, j, k, l) = heapq.heappop(n)
    print(-m)
    if j + 1 <=x-1:
        s = (-(a[j+1]+b[k]+c[l]), j+1, k, l)
        if s not in n:
            heapq.heappush(n ,s)
 
    if k + 1 <=y-1:
        s = (-(a[j]+b[k+1]+c[l]), j, k+1, l)
        if s not in n:
            heapq.heappush(n ,s)
    
    if l+ 1 <=z-1:
        s = (-(a[j]+b[k]+c[l+1]), j, k, l+1)
        if s not in n:
            heapq.heappush(n ,s)

Solution (Python):
X, Y, Z, K = map(int, input().split())
A =list(map(int, input().split()))
B =list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort(reverse = True)
B.sort(reverse = True)
C.sort(reverse = True)
AB =[]
ABC = []
for i in range(X):
    for j in range(Y):
        ab = A[i] + B[j]
        AB.append(ab)
AB.sort(reverse = True)
for i in range(min(K, X*Y)):
    for j in range(Z):
        abc = AB[i] + C[j]
        ABC.append(abc)
ABC.sort(reverse = True)
for i in range(K):
    print(ABC[i])

Solution (Python):
*xyz,k = map(int, input().split()) 
xyz_list = []
xyz_list.append(list(map(int, input().split())))
xyz_list.append(list(map(int, input().split())))
xyz_list.append(list(map(int, input().split())))
xyz_list[0].sort(reverse=True)
xyz_list[1].sort(reverse=True)
xyz_list[2].sort(reverse=True)

print(xyz_list[0][0]+xyz_list[1][0]+xyz_list[2][0])
p = [1,0,0]
num = lambda x: [
    [x[0]+p[i], x[1]+p[(4-i)%3], x[2]+p[(5-i)%3],
    xyz_list[0][x[0]+p[i]]+xyz_list[1][x[1]+p[(4-i)%3]]+xyz_list[2][x[2]++p[(5-i)%3]]]
    for i in range(3) if x[i]+1 < xyz[i]]
stack = num([0,0,0])
num_dict={}
for i in stack:
    if i[3] not in num_dict:
        num_dict[i[3]] = [[i[0], i[1], i[2]]]
    else:
        num_dict[i[3]].append([i[0], i[1], i[2]])
k -= 1
while k != 0:
    x, y, z, maxi = max(stack, key=lambda x:x[3])
    print(maxi)
    k -= 1
    if k == 0:
      break
    stack.remove([x,y,z,maxi])
    for i in num([x,y,z]):
        if i[3] not in num_dict:
            num_dict[i[3]] = [[i[0], i[1], i[2]]]
            stack.append([i[0], i[1], i[2], i[3]])
        elif [i[0], i[1], i[2]] not in num_dict[i[3]]:
            num_dict[i[3]].append([i[0], i[1], i[2]])
            stack.append([i[0], i[1], i[2], i[3]])

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

ans = []

a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

for s in range(x):
    for t in range(y):
        for u in range(z):
            if (s+1) * (t+1) * (u+1) > k:
                break
            ans.append(a[s] + b[t] + c[u])

ans.sort(reverse=True)

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

n = []
heapq.heapify(n)
heapq.heappush(n, (-(a[0]+b[0] + c[0]), 0,0,0))
for i in range(K):
    (m, j, k, l) = heapq.heappop(n)
    print(-m)
    if j + 1 <=x-1:
        s = (-(a[j+1]+b[k]+c[l]), j+1, k, l)
        if s not in n:
            heapq.heappush(n ,s)

    if k + 1 <=y-1:
        s = (-(a[j]+b[k+1]+c[l]), j, k+1, l)
        if s not in n:
            heapq.heappush(n ,s)
    
    if l+ 1 <=z-1:
        s = (-(a[j]+b[k]+c[l+1]), j, k, l+1)
        if s not in n:
            heapq.heappush(n ,s)

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

ab = []

for s in a:
    for t in b:
       ab.append(s + t)

ab.sort(reverse=True)
c.sort(reverse=True)

ans = []

for i in range(min(k, len(ab))):
    for u in c:
        ans.append(ab[i] + u)

ans.sort(reverse=True)

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

Solution (Python):



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

A = sorted(map(int,input().split()),reverse=True)
B = sorted(map(int,input().split()),reverse=True)
C = sorted(map(int,input().split()),reverse=True)



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

ans = sorted([ab + c for ab in AB[:k] for c in C],reverse=True)

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

Solution (Python):



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

A = list(map(int,input().split()))
A.sort(reverse = True)
B = list(map(int,input().split()))
B.sort(reverse = True)
C = list(map(int,input().split()))
C.sort(reverse = True)



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

ans = [ab + c for ab in AB[:k] for c in C]
ans.sort(reverse = True)

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

Solution (Python):
def main():
  import sys
  from heapq import heappush as hpush, heappop as hpop
  input = sys.stdin.readline
  
  X, Y, Z, K = map(int,input().split())
  A = list(map(int,input().split()))
  B = list(map(int,input().split()))
  C = list(map(int,input().split()))
  A.sort(reverse = True)
  B.sort(reverse = True)
  C.sort(reverse = True)
  
  memo = set([(0, 0, 0)])
  h = [(-(A[0]+B[0]+C[0]), (0, 0, 0))]
  for _ in range(K):
    val, (i,j,k) = hpop(h)
    if i+1 < len(A) and (i+1, j, k) not in memo:
      hpush(h, (-(A[i+1]+B[j]+C[k]), (i+1, j, k)))
      memo.add((i+1, j, k))
    if j+1 < len(B) and (i, j+1, k) not in memo:
      hpush(h, (-(A[i]+B[j+1]+C[k]), (i, j+1, k)))
      memo.add((i, j+1, k))
    if k+1 < len(C) and (i, j, k+1) not in memo:
      hpush(h, (-(A[i]+B[j]+C[k+1]), (i, j, k+1)))
      memo.add((i, j, k+1))
    print(-val)

if __name__ == "__main__":
  main()

Solution (Python):



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

A = sorted(map(int,input().split()))[::-1]
B = sorted(map(int,input().split()))[::-1]
C = sorted(map(int,input().split()))[::-1]

AB = sorted([a+b for a in A for b in B])[::-1]

ans = sorted([ab + c for ab in AB[:k] for c in C])[::-1]

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

Solution (Python):
def main():
  import sys
  input = sys.stdin.readline
  
  X, Y, Z, K = map(int,input().split())
  A = list(map(int,input().split()))
  B = list(map(int,input().split()))
  C = list(map(int,input().split()))
  A.sort(reverse = True)
  B.sort(reverse = True)
  C.sort(reverse = True)
  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.sort(reverse = True)
  for r in res[:K]:
    print(r)

if __name__ == "__main__":
  main()

Solution (Python):
def main():
  import sys
  input = sys.stdin.readline
  
  X, Y, Z, K = map(int,input().split())
  A = list(map(int,input().split()))
  B = list(map(int,input().split()))
  C = list(map(int,input().split()))
  A.sort(reverse = True)
  B.sort(reverse = True)
  C.sort(reverse = True)
  AB = [a+b for a in A for b in B]
  AB.sort(reverse = True)
  res = [c+ab for ab in AB[:min(K, len(AB))] for c in C]
  res.sort(reverse = True)
  for r in res[:K]:
    print(r)

if __name__ == "__main__":
  main()

Solution (Python):
from itertools import product

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

BC = [i+j for i,j in list(product(B,C))]
BC.sort(reverse=True)

ans = [i+j for i,j in list(product(BC[:min(3000, X*Y*Z)], A))]
ans.sort(reverse=True)

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

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

list_cake_1 = list(map(int, input().split()))
list_cake_2 = list(map(int, input().split()))
list_cake_3 = list(map(int, input().split()))

list_cake_1 = sorted(list_cake_1, reverse=True)
list_cake_2 = sorted(list_cake_2, reverse=True)
list_cake_3 = sorted(list_cake_3, reverse=True)

list_total_sum = []

for i, cake_1 in enumerate(list_cake_1, 1):
	for j, cake_2 in enumerate(list_cake_2, 1):
		for k, cake_3 in enumerate(list_cake_3, 1):
			if i*j*k > K:
				break
			list_total_sum.append(cake_1 + cake_2 + cake_3)

for element in sorted(list_total_sum, reverse=True)[:K]:
	print(element)

Solution (Python):
import sys

input = sys.stdin.readline

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

list_cake_1 = list(map(int, input().split()))
list_cake_2 = list(map(int, input().split()))
list_cake_3 = list(map(int, input().split()))

list_cake_1 = sorted(list_cake_1, reverse=True)
list_cake_2 = sorted(list_cake_2, reverse=True)
list_cake_3 = sorted(list_cake_3, reverse=True)

list_total_sum = []

for i, cake_1 in enumerate(list_cake_1, 1):
	for j, cake_2 in enumerate(list_cake_2, 1):
		for k, cake_3 in enumerate(list_cake_3, 1):
			if i*j*k > K:
				break
			list_total_sum.append(cake_1 + cake_2 + cake_3)

for element in sorted(list_total_sum, reverse=True)[:K]:
	print(element)

Solution (Python):
import itertools
import heapq
import copy

def inv(l):
    return list(map(lambda x: x*-1, l))

def solve(A,B,C, K):
    A,B,C = inv(A), inv(B), inv(C)
    A = sorted(A)
    B = sorted(B)
    C = sorted(C)
    ind = set([(0,0,0)])

    h = [(A[0]+ B[0]+ C[0], (0,0,0))]
    res = []

    for _ in range(K):
        h2 = copy.copy(h)
        val, (i,j,k) = heapq.heappop(h)
        res += [-val]
        if i+1 < len(A) and (i+1,j,k) not in ind:
            heapq.heappush(h, (A[i+1]+B[j]+C[k], (i+1, j, k)))
            ind |= set([(i+1,j,k)])
        if j+1 < len(B) and (i,j+1, k) not in ind:
            heapq.heappush(h, (A[i]+B[j+1]+C[k], (i, j+1, k)))
            ind |= set([(i,j+1,k)])
        if k+1 < len(C) and (i,j, k+1) not in ind:
            heapq.heappush(h, (A[i]+B[j]+C[k+1], (i, j, k+1)))
            ind |= set([(i,j,k+1)])
    for r in res:
        print(r)

la,lb,lc,K = list(map(int, input().split(' ')))
A = list(map(int, input().split(' ')))
B = list(map(int, input().split(' ')))
C = list(map(int, input().split(' ')))

solve(A,B,C,K)

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)

candidates = []
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:
                candidates.append(A[i] + B[j] + C[k])
            else:
                break
candidates.sort(reverse=True)

for c in candidates[:K]:
    print(c)

Solution (Python):
from itertools import product

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

AB = [a + b for a, b in product(A, B)]
AB.sort(reverse=True)

ABC = [ab + c for ab, c in product(AB[:K], C)]
ABC.sort(reverse=True)

for abc in ABC[:K]:
    print(abc)

Solution (Python):

import sys

def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    l = [0]*X*Y
    for i in range(X):
        for j in range(Y):
            l[i+j*X] = A[i]+B[j]

    l.sort(reverse=True)
    t = min(3000,X*Y)
    l = l[:t]

    ans = [0] * (t) * Z
    for i in range(t):
        for j in range(Z):
            ans[i+j*t] = l[i] + C[j]

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


    return



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):
import sys

input = sys.stdin.readline

X, Y, Z, K = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

a = list(sorted(a, reverse=True))
b = list(sorted(b, reverse=True))
c = list(sorted(c, reverse=True))


e = []
for i, aa in enumerate(a, 1):
    for j, bb in enumerate(b, 1):
        for k, cc in enumerate(c, 1):
            if i * j * k > K: break
            e.append(aa + bb + cc)

for ee in sorted(e, reverse=True)[:K]:
    print(ee)

Solution (Python):
import sys

input = sys.stdin.readline

X, Y, Z, K = map(int, input().split())
a = map(int, input().split())
b = list(map(int, input().split()))
c = map(int, input().split())

d = (aa + bb for aa in a for bb in b)
d = sorted(d, reverse=True)[:K]

e = (cc + dd for cc in c for dd in d)
e = sorted(e, reverse=True)[:K]

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

Solution (Python):
import heapq


XYZK = 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)

hq = []
pushed = []
heapq.heappush(hq, (-(A[0]+B[0]+C[0]), 0, 0, 0))
for _ in range(XYZK[3]):
    a = heapq.heappop(hq)
    print(-a[0])
    i, j, k = a[1:]
    if i < XYZK[0]-1:
        if not (i+1, j, k) in pushed:
            heapq.heappush(hq, (-(A[i+1]+B[j]+C[k]), i+1, j, k))
            pushed.append((i+1, j, k))
    if j < XYZK[1]-1:
        if not (i, j+1, k) in pushed:
            heapq.heappush(hq, (-(A[i]+B[j+1]+C[k]), i, j+1, k))
            pushed.append((i, j+1, k))
    if k < XYZK[2]-1:
        if not (i, j, k+1) in pushed:
            heapq.heappush(hq, (-(A[i]+B[j]+C[k+1]), i, j, k+1))
            pushed.append((i, j, k+1))

Solution (Python):
import bisect

X,Y,Z,K = list(map(int,input().split()))
AA = list(map(int,input().split()))
BB = list(map(int,input().split()))
CC = list(map(int,input().split()))
MAX = max(max(AA),max(BB),max(CC))

AB = []
for a in AA:
  for b in BB:
    AB.append(a+b)
    
AB.sort()
XY = len(AB)

def check(n):
  cnt = 0
  for c in CC:
    cnt += (XY - bisect.bisect_left(AB,n-c))
  return cnt >= K

OK = 0
NG = MAX*3+100
while NG - OK >1:
  mid = (OK+NG)//2
  if check(mid):
    OK = mid
  else:
    NG = mid
    
    
ans = []
for c in CC:
    n = bisect.bisect_left(AB,OK-c)
    for i in range(n,XY):
      ans.append(c+AB[i])
      
ans.sort(reverse=True)
for YUI in range(K):
  print(ans[YUI])

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)
hp = []
heappush(hp, (-(a[0] + b[0] + c[0]), 0, 0, 0))
used = {(0, 0, 0):1}
for _ in range(k):
    ans, d, e, f = heappop(hp)
    print(-ans)
    if(d + 1 < x and not (d + 1, e, f) in used):
        heappush(hp, (-(a[d + 1] + b[e] + c[f]), d + 1, e, f))
        used[(d + 1, e, f)] = 1
    if(e + 1 < y and not (d, e + 1, f) in used):
        heappush(hp, (-(a[d] + b[e + 1] + c[f]), d, e + 1, f))
        used[(d, e + 1, f)] = 1
    if(f + 1 < z and not (d, e, f + 1) in used):
        heappush(hp, (-(a[d] + b[e] + c[f + 1]), d, e, f + 1))
        used[(d, e, f + 1)] = 1

Solution (Python):



from heapq import heapify, heappush, heappop
import sys

input = sys.stdin.readline




class Data:
    def __init__(self, value, indices):
        self.value = value
        self.indices = indices

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





X, Y, Z, K = map(int, input().split())
a = map(int, input().split())
b = map(int, input().split())
c = map(int, input().split())

a = list(sorted(a, reverse=True))
b = list(sorted(b, reverse=True))
c = list(sorted(c, reverse=True))


hq = []
heapify(hq)

t = a[0] + b[0] + c[0]
t *= -1





d = Data(t, (0, 0, 0))
heappush(hq, d)

dtpls = ((1, 0, 0), (0, 1, 0), (0, 0, 1))







st = set()


cnt = 0
for _ in range(K):
    d = heappop(hq)
    print(-d.value)
    for dtpl in dtpls:
        na = d.indices[0] + dtpl[0]
        if na >= X: continue
        nb = d.indices[1] + dtpl[1]
        if nb >= Y: continue
        nc = d.indices[2] + dtpl[2]
        if nc >= Z: continue
        
        
        ntpl = (na, nb, nc)
        if ntpl in st: continue
        

        nt = a[na] + b[nb] + c[nc]
        nt *= - 1
        
        heappush(hq, Data(nt, ntpl))
        st.add(ntpl)

Solution (Python):



from heapq import heapify, heappush, heappop
import sys

input = sys.stdin.readline


class Data:
    def __init__(self, value, indices):
        self.value = value
        self.indices = indices

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


X, Y, Z, K = map(int, input().split())
a = map(int, input().split())
b = map(int, input().split())
c = map(int, input().split())

a = list(sorted(a, reverse=True))
b = list(sorted(b, reverse=True))
c = list(sorted(c, reverse=True))

hq = []
heapify(hq)

t = a[0] + b[0] + c[0]
t *= -1

d = Data(t, (0, 0, 0))

heappush(hq, d)

dtpls = ((1, 0, 0), (0, 1, 0), (0, 0, 1))

st = set()

cnt = 0
for _ in range(K):
    d = heappop(hq)
    print(-d.value)
    for dtpl in dtpls:
        na = d.indices[0] + dtpl[0]
        if na >= X: continue
        nb = d.indices[1] + dtpl[1]
        if nb >= Y: continue
        nc = d.indices[2] + dtpl[2]
        if nc >= Z: continue

        nt = a[na] + b[nb] + c[nc]
        nt *= - 1

        ntpl = (na, nb, nc)
        if ntpl in st: continue
        heappush(hq, Data(nt, ntpl))
        st.add(ntpl)

Solution (Python):



from heapq import heappush, heappop

def solve(X, Y, Z, K, A, B, C):
    def append(cand_a, cand_b, cand_c):
        cand_key = (cand_a, cand_b, cand_c)
        if not cand_key in pushedQ \
                and cand_a < len(A) \
                and cand_b < len(B) \
                and cand_c < len(C):
            yum = A[cand_a] + B[cand_b] + C[cand_c]
            heappush(Q, (-yum, cand_key))
            pushedQ.append(cand_key)

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

    Q = []
    a = 0
    b = 0
    c = 0
    yum = A[a] + B[b] + C[c]
    heappush(Q, (-yum, (a, b, c)))
    pushedQ = [(a, b, c)]

    for _ in range(K):

        max_q = heappop(Q)
        max_yum, max_key = max_q[0] * (-1), max_q[1]
        a, b, c = max_key
        print(max_yum)
        append(a+1, b, c)
        append(a, b+1, c)
        append(a, b, c+1)

if __name__ == "__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()))
    solve(X, Y, Z, K, A, B, C)

Solution (Python):
def int_raw():
    return int(input())

def ss_raw():
    return input().split()

def ints_raw():
    return list(map(int, input().split()))

X,Y,Z,K = ints_raw()

A = ints_raw()
B= ints_raw()
C= ints_raw()
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
def main():
    D = [a+b for a in A for b in B]
    D.sort(reverse=True)
    D = D[:K+1]
    E =[d+c for d in D for c in C]
    E.sort(reverse=True)
    for e in range(K):
        print (E[e])

main()

Solution (Python):
from itertools import product

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)

ab = [ai + bi for (ai, bi) in product(a[:k], b[:k])]
ab.sort(reverse=True)
abc = [abi + ci for (abi, ci) in product(ab[: min(k, x * y * z)], c[:k])]
abc.sort(reverse=True)

for abci in abc[: min(k, x * y * z)]:
    print(abci)

Solution (Python):
from itertools import product

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 = []
count = 0
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
            a.append(A[i] + B[j] + C[k])

a.sort(reverse=True)

for i in range(K):
    print(a[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 i in range(len(A)):
    for j in range(len(B)):
        if (i+1) * (j+1) > K:
            continue
        for k in range(len(C)):
            if (i + 1) *(j + 1)*(k + 1) > K:
                continue
            ans.append(A[i]+B[j]+C[k])

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