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

Solution (Python):
import heapq

X, Y, Z, K = map(int, input().split())
A = 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)

Q = []
heapq.heappush(Q, (-1*(A[0]+B[0]+C[0]), 0, 0, 0))
registered = set()
registered.add((0, 0, 0))
for _ in range(K):
    s, ai, bi, ci = heapq.heappop(Q)
    print(-s)

    if ai+1 < X and (ai+1, bi, ci) not in registered:
        heapq.heappush(Q, (-1*(A[ai+1]+B[bi]+C[ci]), ai+1, bi, ci))
        registered.add((ai+1, bi, ci))
    if bi+1 < Y and (ai, bi+1, ci) not in registered:
        heapq.heappush(Q, (-1*(A[ai]+B[bi+1]+C[ci]), ai, bi+1, ci))
        registered.add((ai, bi+1, ci))
    if ci+1 < Z and (ai, bi, ci+1) not in registered:
        heapq.heappush(Q, (-1*(A[ai]+B[bi]+C[ci+1]), ai, bi, ci+1))
        registered.add((ai, bi, ci+1))

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)

Q = []
heapq.heappush(Q, (-1*(A[0]+B[0]+C[0]), 0, 0, 0))
registered = set()
registered.add((0, 0, 0))
for t in range(K):
    s, ai, bi, ci = heapq.heappop(Q)
    print(-s)

    if ai+1 < X and (ai+1, bi, ci) not in registered:
        heapq.heappush(Q, (-1*(A[ai+1]+B[bi]+C[ci]), ai+1, bi, ci))
        registered.add((ai+1, bi, ci))
    if bi+1 < Y and (ai, bi+1, ci) not in registered:
        heapq.heappush(Q, (-1*(A[ai]+B[bi+1]+C[ci]), ai, bi+1, ci))
        registered.add((ai, bi+1, ci))
    if ci+1 < Z and (ai, bi, ci+1) not in registered:
        heapq.heappush(Q, (-1*(A[ai]+B[bi]+C[ci+1]), ai, bi, ci+1))
        registered.add((ai, bi, ci+1))

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)

Q = []
heapq.heappush(Q, (-(A[0]+B[0]+C[0]), 0, 0, 0))
registered = set()
registered.add((0, 0, 0))
for t in range(K):
    s, ai, bi, ci = heapq.heappop(Q)
    print(-s)

    if ai+1 < X and (ai+1, bi, ci) not in registered:
        heapq.heappush(Q, (-(A[ai+1]+B[bi]+C[ci]), ai+1, bi, ci))
        registered.add((ai+1, bi, ci))
    if bi+1 < Y and (ai, bi+1, ci) not in registered:
        heapq.heappush(Q, (-(A[ai]+B[bi+1]+C[ci]), ai, bi+1, ci))
        registered.add((ai, bi+1, ci))
    if ci+1 < Z and (ai, bi, ci+1) not in registered:
        heapq.heappush(Q, (-(A[ai]+B[bi]+C[ci+1]), ai, bi, ci+1))
        registered.add((ai, bi, ci+1))

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)

Q = []
heapq.heappush(Q, (-(A[0]+B[0]+C[0]), 0, 0, 0))
registered = set()
registered.add((0, 0, 0))
for t in range(K):
    s, ai, bi, ci = heapq.heappop(Q)
    print(-s)

    if ai+1 < X and (ai+1, bi, ci) not in registered:
        heapq.heappush(Q, (-(A[ai+1]+B[bi]+C[ci]), ai+1, bi, ci))
        registered.add((ai+1, bi, ci))
    if bi+1 < Y and (ai, bi+1, ci) not in registered:
        heapq.heappush(Q, (-(A[ai]+B[bi+1]+C[ci]), ai, bi+1, ci))
        registered.add((ai, bi+1, ci))
    if ci+1 < Z and (ai, bi, ci+1) not in registered:
        heapq.heappush(Q, (-(A[ai]+B[bi]+C[ci+1]), ai, bi, ci+1))
        registered.add((ai, bi, ci+1))

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)

comb = []
for a in range(X):
    b_max = K // (a+1)
    for b in range(min(Y, b_max)):
        c_max = K // ((a+1)*(b+1))
        for c in range(min(Z, c_max)):
            comb.append(sum([A[a], B[b], C[c]]))

comb.sort(reverse=True)

print(*comb[:K], sep='\n')

Solution (Python):
import itertools

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

comb_ab = []
for a, b in itertools.product(A, B):
    comb_ab.append(sum([a, b]))

comb_ab_top = sorted(comb_ab, reverse=True)[:K]

comb_abc = []
for ab, c in itertools.product(comb_ab_top, C):
    comb_abc.append(sum([ab, c]))

ans = sorted(comb_abc, reverse=True)[:K]
print(*ans, sep='\n')

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)

a_b = []
for i in range(x):
    for j in range(y):
        if (i+1)*(j+1) <= k:
            a_b.append(a[i] + b[j])
        else:
            break
a_b.sort(reverse=True)
ans = []
a_b = a_b[:k]
c = c[:k]
for i in range(min(k, len(a_b))):
    for j in range(min(k, z)):
        ans.append(a_b[i] + c[j])

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

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

alist.sort(reverse=True)
blist.sort(reverse=True)
clist.sort(reverse=True)

ablist = []
for i in alist:
  for j in blist:
      ablist.append(i+j)
      
ablist.sort(reverse=True)
ablist = ablist[0:K]

abclist = []
for ij in ablist:
  for k in clist:
    abclist.append(ij+k)
    
abclist.sort(reverse=True)
for ijk in range(K):
  print(abclist[ijk])

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

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

Solution (Python):
import sys
import heapq

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)

q = [[(A[0]+B[0]+C[0])*(-1),0,0,0]]
heapq.heapify(q)
count = 0
alr = [[0,0,0]]

while count < K:
    poped = heapq.heappop(q)
    ans = poped[0]*(-1)
    print(ans)
    i, j, k = poped[1], poped[2], poped[3]
    if i+1 < X and not [i+1,j,k] in alr:
        heapq.heappush(q, [(A[i+1]+B[j]+C[k])*(-1), i+1, j, k])
        alr.append([i+1,j,k])
    if j+1 < Y and not [i,j+1,k] in alr:
        heapq.heappush(q, [(A[i]+B[j+1]+C[k])*(-1), i, j+1, k])
        alr.append([i,j+1,k])
    if k+1 < Z and not [i,j,k+1] in alr:
        heapq.heappush(q, [(A[i]+B[j]+C[k+1])*(-1), i, j, k+1])
        alr.append([i,j,k+1])
    count += 1

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()))
AB = []
ABC = []

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

AB.sort(reverse=True)
for i in range(min(len(AB), 3000)):
    for c in C:
        ABC.append(AB[i]+c)
        
ABC.sort(reverse=True)

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

Solution (Python):

X, Y, Z, K = map(int, input().split())
A = sorted(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 = []
for i in range(0, X):
    for j in range(0, Y):
        for k in range(0, Z):
            if (i+1) * (j+1) * (k+1) <= K:
                M.append(A[i] + B[j] + C[k])
            else:
                break

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

Solution (Python):

import heapq
x,y,z,k = map(int, input().split())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
cl = list(map(int, input().split()))

al.sort(reverse=True)
bl.sort(reverse=True)
cl.sort(reverse=True)
regist = set()
regist.add((0,0,0))
q = [(-1*(al[0]+bl[0]+cl[0]),0,0,0)]
heapq.heapify(q)
for _ in range(k):
    s,ai,bi,ci = heapq.heappop(q)
    print(-s,flush=True)

    if ai+1<x and (ai+1,bi,ci) not in regist:
        heapq.heappush(q, (-1*(al[ai+1]+bl[bi]+cl[ci]), ai+1,bi,ci))
        regist.add((ai+1,bi,ci))

    if bi+1<y and (ai,bi+1,ci) not in regist:
        heapq.heappush(q, (-1*(al[ai]+bl[bi+1]+cl[ci]), ai,bi+1,ci))
        regist.add((ai,bi+1,ci))

    if ci+1<z and (ai,bi,ci+1) not in regist:
        heapq.heappush(q, (-1*(al[ai]+bl[bi]+cl[ci+1]), ai,bi,ci+1))
        regist.add((ai,bi,ci+1))

Solution (Python):
import numpy as np
import pprint


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


calclated_flag = np.zeros((X,Y,Z), dtype=np.bool) 

ans = [] 


x=0
y=0
z=0
stock=[[x,y,z,A_list[x] + B_list[y] + C_list[z]]]
calclated_flag[x,y,z]=True


for i in range(K):
  
  stock.sort(key=lambda x: x[3]) 
  ans.append(stock[len(stock)-1][3]) 

  temp = stock.pop() 
  x=temp[0]
  y=temp[1]
  z=temp[2]

  
  if x+1 <= X-1:
    if calclated_flag[x+1,y,z]!=True:
      stock.append([x+1,y,z,A_list[x+1] + B_list[y] + C_list[z]])
      calclated_flag[x+1,y,z]=True
      
  if y+1 <= Y-1:
    if calclated_flag[x,y+1,z]!=True: 
      stock.append([x,y+1,z,A_list[x] + B_list[y+1] + C_list[z]])
      calclated_flag[x,y+1,z]=True
      
  if z+1 <= Z-1:
    if calclated_flag[x,y,z+1]!=True:
      stock.append([x,y,z+1,A_list[x] + B_list[y] + C_list[z+1]])
      calclated_flag[x,y,z+1]=True
  

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

Solution (Python):
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 = sorted([e+f for e in a[:k] for f in b[:k]], reverse=True)
abc = sorted([g+h for g in ab[:k] for h in c[:k]], reverse=True)
for i, ele in enumerate(abc):
  if i>=k:
    break
  print(ele)

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

Solution (Python):
x,y,z,k = [ int(it) for it in input().split() ]
al = [ int(it) for it in input().split() ]
bl = [ int(it) for it in input().split() ]
cl = [ int(it) for it in input().split() ]

abl = [ a + b for a in al for b in bl]
abl.sort()
abl.reverse()
abl=abl[:k]

abcl = [ ab + c for ab in abl for c in cl]
abcl.sort()
abcl.reverse()
abcl=abcl[:k]
print ( "\n".join( [ str(it) for it in abcl ]) )

Solution (Python):
X, Y, Z, K = map(int, input().split())
xyz = []
xyz.append(list(map(int, input().split())))
xyz.append(list(map(int, input().split())))
xyz.append(list(map(int, input().split())))
for v in xyz:
    v.sort()
    v.reverse()

ans = []
for x in range(X):
    for y in range(Y):
        if x*y > K:
            break
        for z in range(Z):
            if x*y*z > K:
                break
            ans.append(xyz[0][x]+xyz[1][y]+xyz[2][z])

ans.sort()
ans.reverse()
print("\n".join([str(v) for v in ans[: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()))

for l in [b,c]:
    p = []
    for i in a:
        for j in l:
            p.append(i+j)
    a = sorted(p, reverse=True)[:k]

for i in a:
    print(i)

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

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

li = []

for p in range(len(A)):
    for q in range(len(B)):
        value = min(- int(- k / ((p + 1) * (q + 1))), len(C))
        for r in range(value):
            tasty = A[p] + B[q] + C[r]
            li.append(tasty)

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








import heapq

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


used = set()
l = []

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

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

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

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

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

from heapq import *

h = []


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

used = {}

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

Solution (Python):
x,y,z,k = map(int,input().split())
a = 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)

r = []
for i in range(x):
  for j in range(y):
    for h in range(z):
      if (i+1)*(j+1)*(h+1) > k:
                break
      r.append(a[i]+b[j]+c[h])
list.sort(r,reverse=True)
for i in range(k):
  print(r[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(' ')))

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

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

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

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

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

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

import heapq


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

index_hstr = []
deliciouses = []

pq = []
heapq.heappush(pq, (-a[0] - b[0] - c[0], 0, 0, 0))
index_hstr.append((0, 0, 0))

for t in range(k):
    deli, i, j, k = heapq.heappop(pq)
    if i+1<x and (i+1, j, k) not in index_hstr:
        heapq.heappush(pq, (-a[i+1] - b[j] - c[k], i+1, j, k))
        index_hstr.append((i+1, j, k))
    if j+1<y and (i, j+1, k) not in index_hstr:
        heapq.heappush(pq, (-a[i] - b[j+1] - c[k], i, j+1, k))
        index_hstr.append((i, j+1, k))
    if k+1<z and (i, j, k+1) not in index_hstr:
        heapq.heappush(pq, (-a[i] - b[j] - c[k+1], i, j, k+1))
        index_hstr.append((i, j, k+1))
    deliciouses.append(-deli)


print('\n'.join(str(i) for i in deliciouses))

Solution (Python):

x, y, z, k = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]

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

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

abc = []
abc.extend([i+j for j in ab[:k] for i in c[:k]])
abc.sort(reverse=True)

print('\n'.join(str(i) for i in abc[:k]))

Solution (Python):

x, y, z, k = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]

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

ab = []
ab.extend([i+j for j in b for i in a])
ab.sort(reverse=True)

abc = []
abc.extend([i+j for j in ab[:k] for i in c])
abc.sort(reverse=True)

print('\n'.join(str(i) for i in abc[:k]))

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

A_array.sort()
B_array.sort()
C_array.sort()

use_array = []
sum_array = []
candidate_array = []

max_sum = A_array[-1] + B_array[-1] + C_array[-1]

candidate_array = [[X - 1, Y - 1, Z - 1, max_sum]]

use_array.append((X - 1, Y - 1, Z - 1))

for i in range(K):
    candidate_array = sorted(candidate_array, key=lambda x: -x[3])

    x, y, z, s = candidate_array.pop(0)

    sum_array.append(s)
    if x != 0 and (x - 1, y, z) not in use_array:
        use_array.append((x - 1, y, z))
        diff = A_array[x] - A_array[x - 1]
        candidate_array.append([x - 1, y, z, s - diff])

    if y != 0 and (x, y - 1, z) not in use_array:
        use_array.append((x, y - 1, z))
        diff = B_array[y] - B_array[y - 1]
        candidate_array.append([x, y - 1, z, s - diff])

    if z != 0 and (x, y, z - 1) not in use_array:
        use_array.append((x, y, z - 1))
        diff = C_array[z] - C_array[z - 1]
        candidate_array.append([x, y, z - 1, s - diff])

for ans in sum_array:
    print(ans)

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

A_array.sort()
B_array.sort()
C_array.sort()

use_array = []
sum_array = []

candidate = []
heapq.heapify(candidate)

max_sum = A_array[-1] + B_array[-1] + C_array[-1]

heapq.heappush(candidate, (0, X - 1, Y - 1, Z - 1))
use_array.append((X - 1, Y - 1, Z - 1))

for i in range(K):
    s, x, y, z = heapq.heappop(candidate)

    sum_array.append(s)
    if x != 0 and (x - 1, y, z) not in use_array:
        use_array.append((x - 1, y, z))
        diff = A_array[x] - A_array[x - 1]
        heapq.heappush(candidate, (s + diff, x - 1, y, z))

    if y != 0 and (x, y - 1, z) not in use_array:
        use_array.append((x, y - 1, z))
        diff = B_array[y] - B_array[y - 1]
        heapq.heappush(candidate, (s + diff, x, y - 1, z))

    if z != 0 and (x, y, z - 1) not in use_array:
        use_array.append((x, y, z - 1))
        diff = C_array[z] - C_array[z - 1]
        heapq.heappush(candidate, (s + diff, x, y, z - 1))

for ans in sum_array:
    print(max_sum - ans)

Solution (Python):

import heapq
X,Y,Z,K = map(int,input().split())
As = sorted(list(map(int,input().split())),reverse=True)
Bs = sorted(list(map(int,input().split())),reverse=True)
Cs = sorted(list(map(int,input().split())),reverse=True)
q = []
heapq.heapify(q)
heapq.heappush(q,(-(As[0]+Bs[0]+Cs[0]),0,0,0))
list_ijk = [(0,0,0)]
for i in range(K):
    
    v,i,j,k = heapq.heappop(q)
    if i < len(As)-1 and (i+1,j,k) not in list_ijk:
        heapq.heappush(q,(-(As[i+1]+Bs[j]+Cs[k]),i+1,j,k))
        list_ijk.append((i+1,j,k))
    if j < len(Bs)-1 and (i,j+1,k) not in list_ijk:
        heapq.heappush(q,(-(As[i]+Bs[j+1]+Cs[k]),i,j+1,k))
        list_ijk.append((i,j+1,k))
    if k < len(Cs)-1 and (i,j,k+1) not in list_ijk:
        heapq.heappush(q,(-(As[i]+Bs[j]+Cs[k+1]),i,j,k+1))
        list_ijk.append((i,j,k+1))
    print(-v)

Solution (Python):
import heapq
X,Y,Z,K = map(int,input().split())
As = sorted(list(map(int,input().split())),reverse=True)
Bs = sorted(list(map(int,input().split())),reverse=True)
Cs = sorted(list(map(int,input().split())),reverse=True)
q = []
heapq.heapify(q)
heapq.heappush(q,(-(As[0]+Bs[0]+Cs[0]),0,0,0))
list_ijk = [(0,0,0)]
for i in range(K):
    
    v,i,j,k = heapq.heappop(q)
    if i < len(As)-1 and (i+1,j,k) not in list_ijk:
        heapq.heappush(q,(-(As[i+1]+Bs[j]+Cs[k]),i+1,j,k))
        list_ijk.append((i+1,j,k))
    if j < len(Bs)-1 and (i,j+1,k) not in list_ijk:
        heapq.heappush(q,(-(As[i]+Bs[j+1]+Cs[k]),i,j+1,k))
        list_ijk.append((i,j+1,k))
    if k < len(Cs)-1 and (i,j,k+1) not in list_ijk:
        heapq.heappush(q,(-(As[i]+Bs[j]+Cs[k+1]),i,j,k+1))
        list_ijk.append((i,j,k+1))
    print(-v)

Solution (Python):


X,Y,Z,K = map(int,input().split())
As = sorted(list(map(int,input().split())),reverse=True)
Bs = sorted(list(map(int,input().split())),reverse=True)
Cs = sorted(list(map(int,input().split())),reverse=True)


AB = []
for A in As:
    for B in Bs:
        AB.append(A+B)
ABs = sorted(AB,reverse=True)


ABC = []
for i in range(min(len(ABs),K)):
    for C in Cs:
        ABC.append(ABs[i]+C)

ABCs = sorted(ABC,reverse=True)
for i in range(K):
    print(ABCs[i])

Solution (Python):
K = int(input().split()[3])
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:
            break
        for k in range(len(C)):
            if (i+1)*(j+1)*(k+1) > K:
                break
            ans.append(A[i]+B[j]+C[k])
ans.sort(reverse=True)
for _ in range(K):
    print(ans[_])

Solution (Python):
import heapq
X,Y,Z,K=[int(i) for i in 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)



Q=[(-A[0]-B[0]-C[0],0,0,0)]
heapq.heapify(Q)
ans=[]
S=set([(0,0,0)])
for _ in range(K):
  q=heapq.heappop(Q)
  ans+=[-q[0]]
  a=q[1]
  b=q[2]
  c=q[3]
  if a!=X-1 and not ((a+1,b,c) in S):
    heapq.heappush(Q,(-A[a+1]-B[b]-C[c],a+1,b,c))
    S.add((a+1,b,c))
  if b!=Y-1 and not ((a,b+1,c) in S):
    heapq.heappush(Q,(-A[a]-B[b+1]-C[c],a,b+1,c))
    S.add((a,b+1,c))
  if c!=Z-1 and not ((a,b,c+1) in S):
    heapq.heappush(Q,(-A[a]-B[b]-C[c+1],a,b,c+1))
    S.add((a,b,c+1))
for i in ans:
  print(i)

Solution (Python):


from bisect import bisect_left as binl

def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * sy) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x





def main():
    x, y, z, k = intin()
    alist = intina()
    blist = intina()
    clist = intina()

    ablist = []
    for a in alist:
        for b in blist:
            ablist.append(a + b)

    ablist.sort(reverse=True)

    abclist = []
    for ab in ablist[:(k+1)]:
        for c in clist:
            abclist.append(ab + c)

    abclist.sort(reverse=True)

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


if __name__ == '__main__':
    main()

Solution (Python):
x, y, z, o = [int(_) for _ in input().split()]
a = sorted([int(_) for _ in input().split()])[::-1]
b = sorted([int(_) for _ in input().split()])[::-1]
c = sorted([int(_) for _ in input().split()])[::-1]

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

ok, ng = 0, a[0]+b[0]+c[0]
while abs(ok-ng)>1:
    m = (ok+ng)//2
    if isEnough(m):
        ok = m
    else:
        ng = m

l = []
for i in range(x):
    for j in range(y):
        for k in range(z):
            if a[i]+b[j]+c[k] <= ok : break
            l.append(a[i]+b[j]+c[k])
l.sort(reverse=True)
for i in range(o):
    if len(l) > i:
        print(l[i])
    else:
        print(ok)

Solution (Python):



x,y,z,N =(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()]

sa=sorted(a,reverse=True)
sb=sorted(b,reverse=True)
sc=sorted(c,reverse=True)
answer=[]

for i in range(len(sa)):
  for j in range(len(sb)):
    if (i+1)*(j+1)>N:break
    for k in range(len(sb)):
      if (i+1)*(j+1)*(k+1)>N:break 
      answer.append(sa[i]+sb[j]+sc[k])
answer=sorted(answer,reverse=True)
for i in range(0,N):
  print(answer[i])

Solution (Python):
import heapq

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

A.sort()
B.sort()
C.sort()

queue = [(A[0]+B[0]+C[0], 0, 0, 0)]
flg = {}
for i in range(K):
    cake, a, b, c = heapq.heappop(queue)
    print(-cake)
    for j in range(3):
        aa = a + [1, 0, 0][j]
        bb = b + [0, 1, 0][j]
        cc = c + [0, 0, 1][j]
        if aa < X and bb < Y and cc < Z:
            if flg.get((aa, bb, cc)) is None:
                flg[aa, bb, cc] = 1
                heapq.heappush(queue, (A[aa]+B[bb]+C[cc], aa, bb, cc))

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

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

queue = [(A[0]+B[0]+C[0], 0, 0, 0)]
ans = [(A[0]+B[0]+C[0], 0, 0, 0)]

for i in range(K):
    cake = max(queue, key=lambda x: x[0])
    queue.remove(cake)
    a, b, c = cake[1], cake[2], cake[3]
    print(cake[0])
    for j in range(3):
        aa = a + [1, 0, 0][j]
        bb = b + [0, 1, 0][j]
        cc = c + [0, 0, 1][j]
        if aa < X and bb < Y and cc < Z:
            tmp = A[aa]+B[bb]+C[cc]
            if (tmp, aa, bb, cc) not in ans:
                ans.append((tmp, aa, bb, cc))
                queue.append((tmp, aa, bb, cc))

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

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

queue = [(A[0]+B[0]+C[0], 0, 0, 0)]
ans = [(A[0]+B[0]+C[0], 0, 0, 0)]

for i in range(K):
    cake = max(queue, key=lambda x: x[0])
    queue.remove(cake)
    a, b, c = cake[1], cake[2], cake[3]
    for j in range(3):
        aa = a + [1, 0, 0][j]
        bb = b + [0, 1, 0][j]
        cc = c + [0, 0, 1][j]
        if aa < X and bb < Y and cc < Z:
            tmp = A[aa]+B[bb]+C[cc]
            if (tmp, aa, bb, cc) not in ans:
                ans.append((tmp, aa, bb, cc))
                queue.append((tmp, aa, bb, cc))

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

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)

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

L = sorted(L,reverse=True)[:K]
for l in L:
    print(l)