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
import itertools

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

A = list(int(a) for a in input().split())
B = list(int(b) for b in input().split())
C = list(int(c) for c in input().split())

hq = []
for i, j in itertools.product(A, B):
    heapq.heappush(hq, (i+j)*-1)

AB = []
while len(hq)>0 and len(AB)<=k:
    AB.append(heapq.heappop(hq)*-1)

hq = []
for i, j in itertools.product(AB, C):
    heapq.heappush(hq, (i+j)*-1)

for _ in range(k):
    print(heapq.heappop(hq)*-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()))

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

kouho=[]
for sm in ab:
    for tmp in c:
        kouho.append(sm+tmp)

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

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

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

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

h = A[0] + B[0] + C[0] +1
l = 0
while h > l+1:
    m = (h+l) // 2

    num = 0
    for a in A:
        j = len(C) - 1
        for i in B:
            while i + C[j] < m - a:
                j -= 1
                if j < 0: break
            else:
                num += j + 1
                continue
            break
    if num >= K:
        l = m
    else:
        h = m

ans = []
for a in A:
    j = len(C) - 1
    for i in B:

        while i + C[j] <= l - a:
            j -= 1
            if j < 0: break
        else:
            for k in range(j+1):
                ans.append(a+i+C[k])
            continue
        break

ans.sort(reverse=True)
for _ in range(K-len(ans)):
    ans.append(l)
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)
d = []
e = []
for m in range(x):
    for n in range(y):
        d.append(a[m]+b[n])
d.sort(reverse = True)
for m in range(z):
    for n in range(min(3000, len(d))):
        e.append(c[m]+d[n])
e.sort(reverse = True)
for n in range(k):
    print(e[n])

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

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

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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

    num = 0
    for a in A:
        j = len(C) - 1
        for i in B:
            while i + C[j] < m - a:
                j -= 1
                if j < 0: break
            else:
                num += j + 1
                continue
            break
    if num >= K:
        l = m
    else:
        h = m

ans = []
for a in A:
    j = len(C) - 1
    for i in B:

        while i + C[j] <= l - a:
            j -= 1
            if j < 0: break
        else:
            for k in range(j+1):
                ans.append(a+i+C[k])
            continue
        break

ans.sort(reverse=True)
for _ in range(K-len(ans)):
    ans.append(l)
print(*ans[:K], sep='\n')

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

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

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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

    num = 0
    for a in A:
        j = len(C) - 1
        for i in B:
            while i + C[j] < m - a:
                j -= 1
                if j < 0: break
            else:
                num += j + 1
                continue
            break
    if num > K:
        l = m
    else:
        h = m

ans = []
for a in A:
    j = len(C) - 1
    for i in B:

        while i + C[j] <= l - a:
            j -= 1
            if j < 0: break
        else:
            for k in range(j+1):
                ans.append(a+i+C[k])
            continue
        break

ans.sort(reverse=True)
for _ in range(K-len(ans)):
    ans.append(l)
print(*ans[:K], sep='\n')

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

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

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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

    num = 0
    for a in A:
        j = len(C) - 1
        for i in B:
            while i + C[j] < m - a:
                j -= 1
                if j < 0: break
            else:
                num += j + 1
                continue
            break
    if num > K:
        l = m
    else:
        h = m

ans = []
cnt = 0
for a in A:
    j = len(C) - 1
    last_cnt = cnt
    for i in B:

        while i + C[j] <= l - a:
            j -= 1
            if j < 0: break
        else:
            for k in range(j+1):
                ans.append(a+i+C[k])
                cnt += 1
            continue
        break
ans.sort(reverse=True)
for _ in range(K-len(ans)):
    ans.append(l)
print (*ans[:K],sep='\n')

Solution (Python):
import sys
input=sys.stdin.readline
from heapq import *
from collections import defaultdict
x, y, z, k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))

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

ans = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
heapify(ans)
log = defaultdict(lambda :0)

d = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
for i in range(k):
    temp, s, t, u = heappop(ans)
    temp = temp * (-1)
    print(temp)
    for ds, dt, du in d:
        ns = s + ds
        nt = t + dt
        nu = u + du
        if not(0 <= ns < x and 0 <= nt < y and 0 <= nu < z):continue
        if log[(ns, nt, nu)]:continue
        heappush(ans, [-(a[ns] + b[nt] + c[nu]), ns, nt, nu])
        log[(ns, nt, nu)] = 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()))

c = sorted(c, reverse=True)
ab = []
for i in range(x):
    for j in range(y):
        temp = a[i] + b[j]
        ab.append(temp)

ab = sorted(ab, reverse=True)
ans = []
for i in range(z):
    for j in range(min(x * y, k)):
        temp = c[i] + ab[j]
        ans += [temp]
ans = sorted(ans, reverse=True)

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

Solution (Python):
x, y, z, K = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse = True)
b = sorted(list(map(int, input().split())), reverse = True)
c = sorted(list(map(int, input().split())), reverse = True)
lst = []

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:
                break
            lst.append(a[i] + b[j] + c[k])

new_lst = sorted(lst, reverse = True)
for i in range(K):
    print(new_lst[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)

K=min(K,3000)

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

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

for k in range(K):
	print(ABC[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()))
a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
ans = []
for s in range(x):
    for t in range(y):
        if (s + 1) * (t + 1) > k:
            break
        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 sys
import itertools
readline = sys.stdin.buffer.readline

X, Y, Z, K = map(int, readline().split())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))
C = list(map(int, readline().split()))
AB = [a+b for (a,b) in list(itertools.product(A,B))]
AB.sort(reverse=True)
ans = [ab + c for (ab,c) in list(itertools.product(AB[:K],C))]
ans.sort(reverse=True)
for i in range(K):
  print(ans[i])

Solution (Python):

def inputlist(): return [int(k) for k in input().split()]

X,Y,Z,K = inputlist()
A = inputlist()
B = inputlist()
C = inputlist()
li = []
for i in range(X):
    for j in range(Y):
        li.append(A[i]+B[j])
li.sort(reverse=True)
lis = []
lit=[]
for i in range(min(X*Y,K)):
    lis.append(li[i])
for i in range(min(X*Y,K)):
    for j in range(Z):
        lit.append(lis[i]+C[j])
lit.sort(reverse=True)
for i in range(K):
    print(lit[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)
l=[]
for i in range(x):
  if i+1>k:
    break
  for j in range(y):
    if (i+1)*(j+1)>k:
      break
    for h in range(z):
      if (i+1)*(j+1)*(h+1)>k:
        break
      l.append(a[i]+b[j]+c[h])
l=sorted(l,reverse=True)
for i in l[:k]:
  print(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 p in range(min(k,len(a))):
  for q in range(min(k,len(b))):
    for r in range(min(k,len(c))):
      if((p+1)*(q+1)*(r+1) > k):
        break
      ans.append(a[p] + b[q] + c[r])
      
ans = sorted(ans, reverse = True)
for i in range(k):
  print(ans[i])

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

a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(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[:k]:
    for j in c:
        abc.append(i + j)
abc.sort(reverse=True)

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

Solution (Python):
def main():
    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)
    
    anslist = []
    for ai, a in enumerate(A):
        for bi, b in enumerate(B):
            for ci, c in enumerate(C):
                if (ai+1)*(bi+1)*(ci+1)<=k:
                    anslist.append(a+b+c)
                else:
                    break    
    anslist.sort(reverse=True)
    for a in anslist[:k]:
        print(a)
main()

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)
Q=[[-A[0]-B[0]-C[0],0,0,0]]
heapq.heapify(Q)
ans=[]
ijk={(0,0,0)}
while len(ans)<K:
  abc,i,j,k=heapq.heappop(Q)
  if i+1<X and  not (i+1,j,k) in ijk:
    ijk.add((i+1,j,k))
    heapq.heappush(Q,[-A[i+1]-B[j]-C[k],i+1,j,k])
  if j+1<Y and  not (i,j+1,k) in ijk:
    ijk.add((i,j+1,k))
    heapq.heappush(Q,[-A[i]-B[j+1]-C[k],i,j+1,k])
  if k+1<Z and  not (i,j,k+1) in ijk:
    ijk.add((i,j,k+1))
    heapq.heappush(Q,[-A[i]-B[j]-C[k+1],i,j,k+1])
  ans.append(-abc)
for i in ans:
  print(i)

Solution (Python):
from bisect import bisect_left
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()))
bc=[]
for i in b:
  for j in c:
    bc.append(i+j)
bc.sort()
yz=y*z
ng=10**12
ok=1
while ng-ok>1:
  mid=(ok+ng)//2
  cnt=0
  for j in a:
    cnt+=yz-bisect_left(bc,mid-j)
  
  if cnt>=k:
    ok=mid
  else:
    ng=mid
ans=[]

for i in a:
  idx=bisect_left(bc,ok-i)
  for j in range(idx,yz):
    ans.append(i+bc[j])
ans.sort(reverse=True)

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

Solution (Python):

import sys
from heapq import heapify, heappop, heappush

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))


X, Y, Z, K = lr()
A = lr(); A.sort(reverse=True)
B = lr(); B.sort(reverse=True)
C = lr(); C.sort(reverse=True)

ma = A[0] + B[0] + C[0]
heap = [(-ma, (0, 0, 0))]
used = set()
for _ in range(K):
    result, place = heappop(heap)
    print(-result)
    i, j, k = place
    p = (i+1, j, k)
    if i + 1 < len(A) and p not in used:
        val = A[i+1] + B[j] + C[k]
        used.add(p)
        heappush(heap, (-val, p))
    p = (i, j+1, k)
    if j + 1 < len(B) and p not in used:
        val = A[i] + B[j+1] + C[k]
        used.add(p)
        heappush(heap, (-val, p))
    p = (i, j, k+1)
    if k + 1 < len(C) and p not in used:
        val = A[i] + B[j] + C[k+1]
        used.add(p)
        heappush(heap, (-val, p))

Solution (Python):

import sys
from bisect import bisect_left, bisect_right
import itertools
from heapq import heapify, heappop, heappush

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))


X, Y, Z, K = lr()
A = lr(); A.sort()
B = lr(); B.sort()
C = lr(); C.sort()
D = []
for a, b in itertools.product(A, B):
    D.append(a+b)
D.sort()
if len(D) > K:
    D = D[-K:]

def check(x):
    length = len(D)
    count = 0
    for y in C:
        i = bisect_left(D, x-y)
        count += length - i
    return count >= K

ok = 0; ng = 10 ** 11
while abs(ng-ok) > 1:
    mid = (ok+ng) // 2
    if check(mid):
        ok = mid
    else:
        ng = mid


answer = []
for y in C:
    index = bisect_left(D, ok-y)
    for i in range(index, len(D)):
        heappush(answer, -(D[i]+y))

for i in range(K):
    print(-heappop(answer))

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):
  if i+1>K:
    break
  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
      ans.append(A[i]+B[j]+C[k])
ans=sorted(ans,reverse=True)
for a in ans[: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 av in a:
    for bv in b:
        ab.append(av + bv)
ab = sorted(ab, reverse=True)[:k]
abc = []
for abv in ab:
    for cv in c:
        abc.append(abv + cv)
abc = sorted(abc, reverse=True)[:k]
for ans in abc:
    print(ans)

Solution (Python):
def cake_gen(a,b,c,k):
    d=[]
    for i in a:
        for j in b:
            d.append(i+j)
    d.sort(reverse = True)
    d=d[:k]
    e=[]
    for i in c[:k]:
        for j in d:
            e.append(i+j)
    e.sort(reverse = True) 
    for i in range(k):
        print(e[i])           
    
def ma(): return map(int, input().split())
def main():
    x,y,z,k = ma()
    a = list(ma())
    b = list(ma())
    c = list(ma())
    a.sort(reverse = True)
    b.sort(reverse = True)
    c.sort(reverse = True)
    cake_gen(a,b,c,k)


if __name__ == '__main__':
    main()

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()))
BC=[]
ABC=[]
for b in range(y):
    for c in range(z):
        BC.append(B[b]+C[c])
A.sort(reverse=True)
BC.sort(reverse=True)

for a in range(x):
    for bc in range(min(k,y*z)):
        ABC.append(A[a]+BC[bc])
ABC.sort(reverse=True)
for i in range(k):
    print(ABC[i])

Solution (Python):
from heapq import heappush, heappop

x,y,z,k = map(int,input().split())
a_ls = list(map(int, input().split()))
b_ls = list(map(int, input().split()))
c_ls = list(map(int, input().split()))
a_ls.sort(reverse=True)
b_ls.sort(reverse = True)
c_ls.sort(reverse = True)

hq = []
arg_hash = {}
val = a_ls[0] + b_ls[0] + c_ls[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_ls[a+1] + b_ls[b] + c_ls[c]),) + arg_a)
    if b < y - 1 and arg_b not in arg_hash:
        arg_hash[arg_b] = 1
        heappush(hq, (-(a_ls[a] + b_ls[b+1] + c_ls[c]),) + arg_b)
    if c < z - 1 and arg_c not in arg_hash:
        arg_hash[arg_c] = 1
        heappush(hq, (-(a_ls[a] + b_ls[b] + c_ls[c+1]),) + arg_c)

Solution (Python):
import itertools
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 = [a+b for a,b in itertools.product(A,B)]
AB.sort(reverse=True)
AB = AB[:K]
ABC = [ab + c for ab,c in itertools.product(AB,C)]
ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])

Solution (Python):
from heapq import heappush, heappop

x, y, z, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

used = set()
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
kouho = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]


def calc(a, b, c):
    if not (a, b, c) in used:
        used.add((a, b, c))
        heappush(kouho, ((-A[a] - B[b] - C[c]), a, b, c))


used.add((0, 0, 0))
for i in range(k):
    temp, p, q, r = heappop(kouho)
    print(-temp)
    if p + 1 < x:
        calc(p + 1, q, r)
    if q + 1 < y:
        calc(p, q + 1, r)
    if r + 1 < z:
        calc(p, q, r + 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()))
goukei = []
for a in A:
    for b in B:
        goukei.append(a + b)
goukei.sort()
goukei2 = goukei[-k:]
goukei3 = []
for c in C:
    for ab in goukei2:
        goukei3.append(ab + c)
goukei3.sort(reverse=True)
print(*goukei3[:k], sep="\n")

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

a, b, c = (sorted(map(int, sys.stdin.readline().split()), reverse=True) for _ in range(3))

abc = {(0,0,0)}

stu = [[-a[0]-b[0]-c[0], 0,0,0]]
heapq.heapify(stu)
result = []
while k > 0:
    out_p, n, m, l = heapq.heappop(stu)
    if n+1<x and not((n+1,m,l) in abc):
        abc.add((n+1,m,l))
        heapq.heappush(stu, [-a[n+1]-b[m]-c[l], n+1, m, l])
    if m+1<y and not((n,m+1,l) in abc):
        abc.add((n,m+1,l))
        heapq.heappush(stu, [-a[n]-b[m+1]-c[l], n, m+1, l])
    if l+1<z and not((n,m,l+1) in abc):
        abc.add((n,m,l+1))
        heapq.heappush(stu, [-a[n]-b[m]-c[l+1], n, m, l+1])
    result.append(-out_p)
    k -= 1
result.sort(reverse=True)
for i in result:
    print(i)

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.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)
AB = AB[:min(3001,X*Y*Z+1)]


ABC = [ab+c for ab in AB for c in 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(" ")))
AB_que = []

for i in range(X):
    for j in range(Y):
        heapq.heappush(AB_que, A[i] + B[j])

while len(AB_que) > K:
    heapq.heappop(AB_que)

que = []
for ab in AB_que:
    for i in range(Z):
        heapq.heappush(que, -(ab + C[i]))

while K > 0:
    print(-heapq.heappop(que))
    K -= 1

Solution (Python):
import heapq

def main():
  X,Y,Z,K=map(int,input().split())
  A=list(reversed(sorted(map(int,input().split()))))
  B=list(reversed(sorted(map(int,input().split()))))
  C=list(reversed(sorted(map(int,input().split()))))
  q=[(-A[0]-B[0]-C[0],0,0,0)]
  queued={(0,0,0)}
  for i in range(K):
    v,s,t,u=heapq.heappop(q)
    print(-v)
    for e,f,g in [(s,t,u+1),(s,t+1,u),(s+1,t,u)]:
      if e>=X or f>=Y or g>=Z:
        continue
      if (e,f,g) in queued:
        continue
      heapq.heappush(q,(-(A[e]+B[f]+C[g]),e,f,g))
      queued.add((e,f,g))
      
if __name__=="__main__":
  main()

Solution (Python):


import heapq

def main():
    x, y, z, k = map(int, input().split())
    a = list(reversed(sorted(map(int, input().split()))))
    b = list(reversed(sorted(map(int, input().split()))))
    c = list(reversed(sorted(map(int, input().split()))))
    q = [(-a[0] - b[0] - c[0], 0, 0, 0)]
    queued = {(0, 0, 0)}
    for i in range(k):
        v, s, t, u = heapq.heappop(q)
        print(-v)
        for e, f, g in [(s, t, u + 1), (s, t + 1, u), (s + 1, t, u)]:
            if e >= x or f >= y or g >= z:
                continue
            if (e, f, g) in queued:
                continue
            heapq.heappush(q, (-(a[e] + b[f] + c[g]), e, f, g))
            queued.add((e, f, g))

if __name__ == "__main__":
    main()

Solution (Python):
import itertools

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

mix_ab= [a_+b_ for a_,b_ in itertools.product(a, b)]
mix_ab.sort(reverse=True)
c.sort(reverse=True)

mix_abc= [ab_+c_ for ab_,c_ in itertools.product(mix_ab[:k], c[:k])]
mix_abc.sort(reverse=True)

print(*mix_abc[:k], sep='\n')

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

AB = []
for i in range(X):
	for j in range(Y):
		AB.append(A[i]+B[j])

AB.sort(reverse = True)
AB = AB[:min(K,len(AB))]

ABC = []
for i in range(min(K,len(AB))):
	for j in range(Z):
		ABC.append(AB[i]+C[j])

ABC.sort(reverse = True)

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

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
from heapq import heappop, heappush

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

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

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

cakes = []

heappush(cakes, (-(A[0]+B[0]+C[0]),0, 0, 0))

visited = set((A[0], B[0], C[0]))

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

    if a+1 <= len(A) - 1 and (a+1, b, c) not in visited:
        v = A[a+1] + B[b] + C[c]
        heappush(cakes, (-v, a+1, b, c))
        visited.add((a+1, b, c))
    if b+1 <= len(B) - 1 and (a, b+1, c) not in visited:
        v = A[a] + B[b+1] + C[c]
        heappush(cakes, (-v, a, b+1, c))
        visited.add((a, b+1, c))
    if c+1 <= len(C) - 1 and (a, b, c+1) not in visited:
        v = A[a] + B[b] + C[c+1]
        heappush(cakes, (-v, a, b, c+1))
        visited.add((a, b, c+1))

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

import heapq as hq

x,y,z,k=map(int,input().split())
A=list(reversed(sorted(map(int,input().split()))))
B=list(reversed(sorted(map(int,input().split()))))
C=list(reversed(sorted(map(int,input().split()))))
P=[]
hq.heapify(P)
for a in A:
    for b in B:
        hq.heappush(P,-(a+b))
Q=[]
hq.heapify(Q)
for i in range(min(x*y,3000)):
    p=-hq.heappop(P)
    for c in C:
        hq.heappush(Q,-(p+c))
for i in range(k):
    print(-hq.heappop(Q))

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

from heapq import heappop, heappush, heapify
X,Y,Z,K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

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

sum_list = []
seen_list = set()
ans = []

heappush(sum_list, (-(A[0]+B[0]+C[0]), 0, 0, 0))

for _ in range(K):
    s, i, j, k = heappop(sum_list)
    
    ans.append(-s)

    if i+1 < len(A) and (i+1, j, k) not in seen_list:
        heappush(sum_list, (-(A[i+1]+B[j]+C[k]), i+1, j, k))
        seen_list.add((i+1, j, k))

    if j+1 < len(B) and (i, j+1, k) not in seen_list:
        heappush(sum_list, (-(A[i]+B[j+1]+C[k]), i, j+1, k))
        seen_list.add((i, j+1, k))

    if k+1 < len(C) and (i, j, k+1) not in seen_list:
        heappush(sum_list, (-(A[i]+B[j]+C[k+1]), i, j, k+1))
        seen_list.add((i, j, k+1))

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

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


from heapq import heappop, heappush, heapify
X,Y,Z,K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

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

sum_list = []
seen_list = set()
ans = []

heappush(sum_list, (-(A[0]+B[0]+C[0]), 0, 0, 0))
seen_list.add((0, 0, 0))

for _ in range(K):
    s, i, j, k = heappop(sum_list)
    
    ans.append(-s)

    if i+1 < len(A) and (i+1, j, k) not in seen_list:
        heappush(sum_list, (-(A[i+1]+B[j]+C[k]), i+1, j, k))
        seen_list.add((i+1, j, k))

    if j+1 < len(B) and (i, j+1, k) not in seen_list:
        heappush(sum_list, (-(A[i]+B[j+1]+C[k]), i, j+1, k))
        seen_list.add((i, j+1, k))

    if k+1 < len(C) and (i, j, k+1) not in seen_list:
        heappush(sum_list, (-(A[i]+B[j]+C[k+1]), i, j, k+1))
        seen_list.add((i, j, k+1))

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

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

def main():

    from heapq import heappop, heappush, heapify
    X,Y,Z,K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))

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

    sum_list = []
    seen_list = set()
    ans = []

    heappush(sum_list, (-(A[0]+B[0]+C[0]), 0, 0, 0))
    seen_list.add((0, 0, 0))

    for _ in range(K):
        s, i, j, k = heappop(sum_list)
        
        ans.append(-s)

        if i+1 < len(A) and (i+1, j, k) not in seen_list:
            heappush(sum_list, (-(A[i+1]+B[j]+C[k]), i+1, j, k))
            seen_list.add((i+1, j, k))

        if j+1 < len(B) and (i, j+1, k) not in seen_list:
            heappush(sum_list, (-(A[i]+B[j+1]+C[k]), i, j+1, k))
            seen_list.add((i, j+1, k))

        if k+1 < len(C) and (i, j, k+1) not in seen_list:
            heappush(sum_list, (-(A[i]+B[j]+C[k+1]), i, j, k+1))
            seen_list.add((i, j, k+1))

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

if __name__ == "__main__":
    main()

Solution (Python):
X, Y, Z, K = map(int, input().split())
x = sorted(list(map(int, input().split())))
y = sorted(list(map(int, input().split())))
z = sorted(list(map(int, input().split())))


import heapq
ans = []
for i in range(X-1, -1, -1):
  for j in range(Y-1, -1, -1):
    for k in range(Z-1, -1, -1):
      if (X-i)*(Y-j)*(Z-k) <= K:
        sub = x[i]+y[j]+z[k]
        ans.append(sub)
      else:
        break
ans = list(map(lambda x: x*(-1), ans)) 
heapq.heapify(ans)
for i in range(K):
  print(heapq.heappop(ans)*(-1))

Solution (Python):
import heapq
import math

NN = 2020202
MOD = 10**9+7
INF = float("inf")

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)

que = [(-(A[0]+B[0]+C[0]), 0, 0, 0)]
seen = set([(0, 0, 0)])

for i in range(k):
    v, ai, bi, ci = heapq.heappop(que)
    print(-v)
    if ai < len(A)-1 and (ai+1, bi, ci) not in seen:
        seen.add((ai+1, bi, ci))
        heapq.heappush(que, (-(A[ai+1]+B[bi]+C[ci]), ai+1, bi, ci))
    if bi < len(B)-1 and (ai, bi+1, ci) not in seen:
        seen.add((ai, bi+1, ci))
        heapq.heappush(que, (-(A[ai]+B[bi+1]+C[ci]), ai, bi+1, ci))
    if ci < len(C)-1 and (ai, bi, ci+1) not in seen:
        seen.add((ai, bi, ci+1))
        heapq.heappush(que, (-(A[ai]+B[bi]+C[ci+1]), ai, bi, ci+1))

Solution (Python):
from heapq import nlargest
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=nlargest(k,(x+y for x in a for y in b))
abc=nlargest(k,(x+y for x in ab for y in c))
for i in abc:
  print(i)