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 = 1)
b = sorted(list(map(int,input().split())),reverse = 1)
c = sorted(list(map(int,input().split())),reverse = 1)
q = []
heapq.heapify(q)
for i in range(x):
    if i == 0:
        jlim = y
    else:
        jlim = min(-(-k//i),y)
    for j in range(jlim):
        if i*j == 0:
            llim = z
        else:
            llim = min(-(-k//(i*j)),z)
        for l in range(llim):
            heapq.heappush(q,-(a[i]+b[j]+c[l]))
for i in range(k):
    x = heapq.heappop(q)
    print(-x)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

    x,y,z,n = 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)
    
    def check(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==n:
                        return True
        return False

    left = 0
    right = 10**15
    while right-left>1:
        mid = (right+left)//2
        if check(mid):
            left = mid
        else:
            right = mid
    
    res = []
    for i in range(x):
        for j in range(y):
            for k in range(z):
                if a[i]+b[j]+c[k] < left+1:
                    break
                res.append(a[i]+b[j]+c[k])
    res.sort(reverse=1)
    if len(res)<n:
        for i in res:
            print(i)
        for _ in range(n-len(res)):
            print(left)
    else:
        for i in res:
            print(i)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

    x,y,z,n = 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)
    res = []
    hq = [[-(a[0]+b[0]+c[0]), 0, 0, 0]]
    heapq.heapify(hq)
    check = set([(0, 0, 0)])
    for _ in range(n):
        cost, i, j, k = heapq.heappop(hq)
        res.append(-cost)
        if i+1<x and not (i+1, j, k) in check:
            heapq.heappush(hq, [-(a[i+1]+b[j]+c[k]), i+1, j, k])
            check.add((i+1, j, k))
        if j+1<y and not (i, j+1, k) in check:
            heapq.heappush(hq, [-(a[i]+b[j+1]+c[k]), i, j+1, k])
            check.add((i, j+1, k))
        if k+1<z and not (i, j, k+1) in check:
            heapq.heappush(hq, [-(a[i]+b[j]+c[k+1]), i, j, k+1])
            check.add((i, j, k+1))
    for i in res:
        print(i)

if __name__ == '__main__':
    main()

Solution (Python):







def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

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

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

    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.sort(reverse=1)
    ab = ab[:k]
    abc = []
    for i in ab:
        for j in c:
            abc.append(i+j)
    abc.sort(reverse=1)
    for i in range(k):
        print(abc[i])

if __name__ == '__main__':
    main()

Solution (Python):
import sys

import bisect






import itertools






readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    x, y, z, k = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    b = list(map(int, readline().split()))
    c = list(map(int, readline().split()))

    ab = [0] * x * y

    ab = [i + j for i, j in itertools.product(a, b)]
    ab.sort(reverse=True)
    c.sort(reverse=True)

    ab = ab[:k]

    abc = [i + j for i, j in itertools.product(ab, c)]
    abc.sort(reverse=True)

    print(*abc[:k],sep="\n")

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

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

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()
ans.reverse()

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

Solution (Python):

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

ab=[]
ans=[]
for i in range(len(a)):
  for j in range(len(b)):
    ab.append(a[i]+b[j])
    
ab.sort()
ab.reverse()
del ab[k:]

c.sort()
c.reverse()

for i in range(len(ab)):
  for j in range(len(c)):
    ans.append(ab[i]+c[j])
    
ans.sort()
ans.reverse()

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

Solution (Python):
x, y, z, K = map(int, input().split())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
Z = list(map(int, input().split()))
XY = []
for i in range(x):
    for j in range(y):
        XY.append(X[i]+Y[j])
XY.sort()
Z.sort()
XYZ = []
for x in XY[::-1][:K]:
    for z in Z:
        XYZ.append(x+z)
XYZ.sort()
print(*XYZ[::-1][:K])

Solution (Python):

import heapq
ans = []
heapq.heapify(ans)
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)

for i in range(x):
    p = k // (i+1)
    for j in range(min(p + 1, y)):
        q = p // (j+1)
        for l in range(min(q + 1, z)):
            heapq.heappush(ans, -(a[i] + b[j] + c[l]))
            
for _ in range(k):
    print(-heapq.heappop(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)
ans=[]
for i in range(min(x,k+1)):
	for j in range(y):
		if (i+1)*(j+1)>k:
			break
		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()
for i in range(k):
	print(ans[-i-1])

Solution (Python):
x,y,z,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ans=[]
for i in range(min(x,k+1)):
	for j in range(y):
		if (i+1)*(j+1)>k:
			break
		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()
for i in range(k):
	print(ans[-i-1])

Solution (Python):
def main():
	X,Y,Z,k=map(int,input().split())
	a=list(map(int,input().split()))
	b=list(map(int,input().split()))
	c=list(map(int,input().split()))
	ab=[]
	for x in a:
		for y in b:
			ab.append(x+y)
	ab.sort(reverse=True)
	if len(ab)>k:
		ab=ab[:k+1]
	c.sort()
	l=0
	r=max(ab)+max(c)+1
	import bisect
	while r-l>1:
		cnt=0
		mid=(l+r)//2
		for x in ab:
			cnt+=Z-bisect.bisect_left(c,mid-x)
		if cnt>=k:
			l=mid
		else:
			r=mid
	ans=[]
	for i in range(len(ab)):
		for j in range(bisect.bisect_left(c,l-ab[i]),Z):
			ans.append(ab[i]+c[j])
	ans.sort(reverse=True)
	for i in range(min(k,len(ans))):
		print(ans[i])
if __name__=="__main__":
	main()

Solution (Python):
def main():
	X,Y,Z,k=map(int,input().split())
	a=list(map(int,input().split()))
	b=list(map(int,input().split()))
	c=list(map(int,input().split()))
	ab=[]
	for x in a:
		for y in b:
			ab.append(x+y)
	ab.sort(reverse=True)
	if len(ab)>k:
		ab=ab[:k+1]
	c.sort()
	l=0
	r=max(ab)+max(c)+1
	import bisect
	while r-l>1:
		cnt=0
		mid=(l+r)//2
		for x in ab:
			cnt+=Z-bisect.bisect_left(c,mid-x)
		if cnt>=k:
			l=mid
		else:
			r=mid
	ans=[]
	for i in range(len(ab)):
		for j in range(bisect.bisect_left(c,l-ab[i]),Z):
			ans.append(ab[i]+c[j])
	ans.sort(reverse=True)
	for i in range(min(k,len(ans))):
		print(ans[i])
main()

Solution (Python):

def binsearch(l, r, fn):
    while r - l > 1:
        m = (l + r) // 2
        if fn(m):
            l = m
        else:
            r = m
    return l
def main():
    X, Y, Z, K = map(int, input().split())
    D = sorted(map(int, input().split()), reverse=True)
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    A = sorted((i + j for i in B for j in C), reverse=True)
    def calc(x):
        t = 0
        ii = len(D) - 1
        for i in A:
            while ii >= 0 and D[ii] + i < x:
                ii -= 1
            t += ii + 1
            if t >= K:
                return True
            if ii < 0:
                return False
        return t >= K
    l = binsearch(D[-1] + A[-1], D[0] + A[0]+1, calc)
    ii = len(D) - 1
    rl = []
    for i in A:
        while ii >= 0 and D[ii] + i < l + 1:
            ii -= 1
        if ii < 0:
            break
        rl.extend(i + j for j in D[:ii+1])
    rl.sort(reverse=True)
    rl += [l] * (K - len(rl))
    print('\n'.join(str(i) for i in rl))
main()

Solution (Python):
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
from fractions import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

def main():
    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)
    h = []
    for i in A:
        for j in B:
            if len(h) < K:
                heappush(h, i + j)
            elif h[0] < i + j:
                heappush(h, i + j)
                heappop(h)
    AB = h[:]
    AB.sort(reverse=True)
    h = []
    for i in AB:
        for j in C:
            if len(h) < K:
                heappush(h, i + j)
            elif h[0] < i + j:
                heappush(h,i + j)
                heappop(h)
    ABC = h[:]
    ABC.sort(reverse=True)
    for i in range(K):
        print(ABC[i])

main()

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

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

x_y_list = []

for i in a:
    for j in b:
        x_y_list.append(i + j)

x_y_list.sort()

import bisect
left = 0
right = 10 ** 18

import numpy as np

x_y_list_np = np.array(x_y_list, np.int64)
c_np = np.array(c, np.int64)

while right - left > 1:
    mid = (left + right) // 2
    cnt = np.searchsorted(x_y_list_np, mid - c_np, side="right").sum()
    if cnt >= x*y*z - k + 1:
        right = mid
    else:
        left = mid

up_k = right 

ans = []
for i in c:
    idx = bisect.bisect_right(x_y_list, up_k - i)
    for j in range(idx, len(x_y_list)):
        ans.append(i + x_y_list[j])

for i in range(k - len(ans)):
    ans.append(up_k)

ans.sort()

for i in ans[::-1]:
    print(i)

Solution (Python):
xx, yy, zz, 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=1)
b.sort(reverse=1)
c.sort(reverse=1)
ans = []

for x in range(min(xx, k)):
    for y in range(min(yy, k // (x + 1))):
        for z in range(min(zz, k // ((x + 1) * (y + 1)))):
            ans.append(a[x] + b[y] + c[z])
ans.sort(reverse=1)

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

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


AB = [a + b for a in A for b in B]
AB.sort()
AB = AB[-K:]

ans = [ab + c for ab in AB for c in C]
ans.sort()

for x in ans[-K:][::-1]:
    print(x)

Solution (Python):
from collections import deque
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 m in range(z):
            if (i+1)*(j+1)*(m+1) <= k:
                ans.append(a[i]+b[j]+c[m])
            else:
                break

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 = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)




AB = []

for i in range(x):
    for j in range(y):
        AB.append(A[i]+B[j])

AB = sorted(AB, reverse=True)
AB = AB[:min(k, x*y)]


ABC = []
for l in range(min(k, x*y)):
    for m in range(z):
        ABC.append(AB[l] + C[m])

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

Solution (Python):
import heapq

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

A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)

hq = []
heapq.heapify(hq)
i, j, l = 0, 0, 0

heapq.heappush(hq, [(A[i]+B[j]+C[l])*-1, i, j, l])
Used = []

for i in range(k):
    now = heapq.heappop(hq)
    print(now[0]*-1)
    i = now[1]
    j = now[2]
    l = now[3]

    if i >= x-1:
        pass
    elif [(A[i+1]+B[j]+C[l])*-1, i+1, j, l] not in Used:
        Used.append([(A[i+1]+B[j]+C[l])*-1, i+1, j, l])
        heapq.heappush(hq, [(A[i+1]+B[j]+C[l])*-1, i+1, j, l])
        
    if j >= y-1:
        pass
    elif [(A[i]+B[j+1]+C[l])*-1, i, j+1, l] not in Used:
        Used.append([(A[i]+B[j+1]+C[l])*-1, i, j+1, l])
        heapq.heappush(hq, [(A[i]+B[j+1]+C[l])*-1, i, j+1, l])
        
    if l >= z-1:
        pass
    elif [(A[i]+B[j]+C[l+1])*-1, i, j, l+1] not in Used:
        Used.append([(A[i]+B[j]+C[l+1])*-1, i, j, l+1])
        heapq.heappush(hq, [(A[i]+B[j]+C[l+1])*-1, i, j, l+1])

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

def condition(num):
    count=0
    for i in range(0,Z):
        subcount=0
        a=num-C[i]
        s=0
        t=Y-1
        while X-1>=s and t>=0:
            if A[s]+B[t]>=a:
                t-=1
            else:
                subcount+=t+1
                s+=1
        count+=X*Y-subcount
    return count>=K

start=0
end=10**12
while end-start>1:
    test=(end+start)//2
    if condition(test):
        start=test
    else:
        end=test

Kth=0
if condition(end):
    Kth=end
else:
    Kth=start

ans=[]
for i in range(0,Z):
    a=Kth+1-C[i]
    s=0
    t=Y-1
    while X-1>=s and t>=0:
        if A[s]+B[t]>=a:
            for j in range(s,X):
                ans.append(A[j]+B[t]+C[i])
            t-=1
        else:
            s+=1

for i in range(0,K-len(ans)):
    ans.append(Kth)
ans.sort()
for i in range(0,K):
    print(ans[-i-1])

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

X, Y, Z, K = MAP()
A = LIST()
B = LIST()
C = LIST()
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

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

V.sort(reverse=True)

for i in range(K):
    print(V[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 = []
for x in range(X):
    for y in range(Y):
        for z in range(Z):
            if (x + 1) * (y + 1) * (z + 1) <= K:
                ans.append(A[x] + B[y] + C[z])
            else:
                break

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

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)

q = []
x = y = z = 0
heappush(q, (-(A[x] + B[y] + C[z]), (x, y, z)))
done = set((0, 0, 0))
ans = []
for _ in range(K):
    val, (x, y, z) = heappop(q)
    ans.append(-val)
    for i, j, k in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
        nx = x + i; ny = y + j; nz = z + k
        if nx < X and ny < Y and nz < Z and (nx, ny, nz) not in done:
            heappush(q, (-(A[nx] + B[ny] + C[nz]), (nx, ny, nz)))
            done.add((nx, ny, nz))

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

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=[(-1*(A[0]+B[0]+C[0]),0,0,0)]

heapq.heapify(q)

used=[[0,0,0]]

a=0
b=0
c=0
cnt=0
while cnt<k:
    p,a,b,c=heapq.heappop(q)
    cnt+=1
    print(p*-1)
    a+=1
    if (not [a,b,c] in used) and a<x:
        heapq.heappush(q,(-A[a]-B[b]-C[c],a,b,c))
        used.append([a,b,c])
    a-=1
    b+=1
    if (not [a,b,c] in used) and b<y:
        heapq.heappush(q,(-A[a]-B[b]-C[c],a,b,c))
        used.append([a,b,c])
    b-=1
    c+=1
    if (not [a,b,c] in used) and c<z:
        heapq.heappush(q,(-A[a]-B[b]-C[c],a,b,c))
        used.append([a,b,c])

Solution (Python):
from datetime import datetime as dt
import heapq
X,Y,Z,K = list(map(int,input().split(" ")))
As = list(map(int,input().split(" ")))
As.sort(reverse=True)
Bs = list(map(int,input().split(" ")))
Bs.sort(reverse=True)
Cs = list(map(int,input().split(" ")))
Cs.sort(reverse=True)

a = 0
b = 0
c = 0
se = set()
nums = []
num = As[a] + Bs[b] + Cs[c]
heapq.heappush(nums,(-num,(a ,b,c)))

for _ in range(K):
    while True:
        temp = heapq.heappop(nums)
        num = temp[0]
        a,b,c = temp[1]
        if temp[1] in se:
            pass
        else:
            se.add(temp[1])
            break
    print(-num)
    
    if a + 1 < X:
        num = As[a + 1] + Bs[b] + Cs[c]
        heapq.heappush(nums,(-num,(a + 1,b,c)))
    if b + 1 < Y:
        num = As[a ] + Bs[b + 1] + Cs[c]
        heapq.heappush(nums,(-num,(a ,b + 1,c)))
    if c + 1 < Z:
        num = As[a] + Bs[b ] + Cs[c + 1]
        heapq.heappush(nums,(-num,(a ,b,c + 1)))

Solution (Python):
from heapq import nlargest

x,y,z,k = map(int,input().split())
A,B,C = [list(map(int,input().split())) for _ in [0]*3]

AB = nlargest(k,[a+b for a in A for b in B])
ABC = nlargest(k,[ab+c for ab in AB for c in C])
print("\n".join(map(str,ABC)))

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()))
D = [0] * (x * y)
for i in range(x):
  for j in range(y):
    D[i * y + j] = A[i] + B[j]
D.sort(reverse=True)
E = [0] * (z * k)
size = min(x * y, k)
for i in range(z):
  for j in range(size):
    E[i * size + j] = C[i] + D[j]
E.sort(reverse=True)
print(*E[:k], sep='\n')

Solution (Python):
X,Y,Z,K = map(int,input().split())
A = sorted(list(map(int,input().split())),reverse=1)
B = sorted(list(map(int,input().split())),reverse=1)
C = sorted(list(map(int,input().split())),reverse=1)
def solve(P):
    count = 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
                count += 1
                if count >= K:
                    return True
    return False

maxa = A[0]+B[0]+C[0]+1
mina = 0

while maxa -mina > 1:
    mid = (maxa+mina)//2
    if solve(mid):
        mina = mid
    else:
        maxa = mid
ans = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (A[i]+B[j]+C[k]) < mina:
                break
            ans.append(A[i]+B[j]+C[k])

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

Solution (Python):
import heapq
X,Y,Z,K = map(int,input().split())
A = sorted(list(map(int,input().split())),reverse=1)
B = sorted(list(map(int,input().split())),reverse=1)
C = sorted(list(map(int,input().split())),reverse=1)
Q = [(-A[0]-B[0]-C[0],0,0,0)]
heapq.heapify(Q)
abc = [(0,0,0)]
for i in range(K):
    n,a,b,c = heapq.heappop(Q)
    if a < X-1 and (a+1,b,c) not in abc:
        abc.append((a+1,b,c))
        heapq.heappush(Q,(n+A[a]-A[a+1],a+1,b,c))
    if b < Y-1 and (a,b+1,c) not in abc:
        abc.append((a,b+1,c))
        heapq.heappush(Q,(n+B[b]-B[b+1],a,b+1,c))
    if c < Z-1 and (a,b,c+1) not in abc:
        abc.append((a,b,c+1))
        heapq.heappush(Q,(n+C[c]-C[c+1],a,b,c+1))
    print(-n)

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]


def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

x,y,z,k = get_nums_l()

aaa = get_nums_l()
bbb = get_nums_l()
ccc = get_nums_l()

aaa.sort(reverse=True)
bbb.sort(reverse=True)
ccc.sort(reverse=True)

def solve(p):
    count = 0
    for a in aaa:
        for b in bbb:
            for c in ccc:
                if a+b+c < center:
                    break
                count += 1
                if count >= k:
                    return True
    return False

left = 0
right = 30000000001

while left+1 < right:
    center = (left + right) // 2

    if solve(center):
        left = center
    else:
        right = center

border = left

ans = []

for a in aaa:
    for b in bbb:
        for c in ccc:
            if a+b+c >= border:
                ans.append(a+b+c)
            else:
                break

ans.sort(reverse=True)
print("\n".join(list(map(str,ans[:k]))))

Solution (Python):
import heapq
X,Y,Z,K = map(int,input().split())
A = sorted(list(map(int,input().split())),reverse=1)
B = sorted(list(map(int,input().split())),reverse=1)
C = sorted(list(map(int,input().split())),reverse=1)
Q = [(-A[0]-B[0]-C[0],0,0,0)]
heapq.heapify(Q)
for i in range(K):
    n,a,b,c = heapq.heappop(Q)
    if a < X-1 and (n+A[a]-A[a+1],a+1,b,c) not in Q:
        heapq.heappush(Q,(n+A[a]-A[a+1],a+1,b,c))
    if b < Y-1 and (n+B[b]-B[b+1],a,b+1,c) not in Q:
        heapq.heappush(Q,(n+B[b]-B[b+1],a,b+1,c))
    if c < Z-1 and (n+C[c]-C[c+1],a,b,c+1) not in Q:
        heapq.heappush(Q,(n+C[c]-C[c+1],a,b,c+1))
    print(-n)

Solution (Python):
X,Y,Z,K = map(int,input().split())
A = sorted(list(map(int,input().split())),reverse=1)
B = sorted(list(map(int,input().split())),reverse=1)
C = sorted(list(map(int,input().split())),reverse=1)
num= []
for xi in range(1,X+1):
    for yi in range(1,Y+1):
        for zi in range(1,K//(xi*yi)+1):
            if xi*yi*zi <= K and zi < Z+1:
                num.append((xi-1,yi-1,zi-1))

ans = []
for a,b,c in num:
    ans.append(A[a]+B[b]+C[c])
ans.sort(reverse=1)
for i in range(K):
    print(ans[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)
ng = 10**12
ok = 0
def check(chk):
    cnt=0
    for i in range(x):
        for j in range(y):
            for k in range(z):
                if a[i]+b[j]+c[k]<chk:
                    break
                cnt+=1
                if cnt>K:
                    return True
    return False
while ng-ok>1:
    if check((ok+ng)//2):ok=(ok+ng)//2
    else:ng=(ok+ng)//2
cnt=0
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(K):print(l[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()))

ab = [ea + eb for ea in a for eb in b]
ab.sort()

hp = []
for e in c:
    heappush(hp, (-(e + ab[-1]), x * y - 1))

ans = []
for _ in range(k):
    yum, i = heappop(hp)
    yum = -yum
    ans.append(yum)
    if i != 0:
        yum += ab[i-1] - ab[i]
        yum = -yum
        heappush(hp, (yum, i - 1))

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

Solution (Python):
from heapq import nlargest
 
x, y, z, k = map(int, input().split())
A, B, C = [list(map(int, input().split())) for _ in [0] * 3]
AB = nlargest(k, (a + b for a in A for b in B))
ABC = nlargest(k, (ab + c for ab in AB for c in C))
print('\n'.join(map(str, ABC)))

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 = [a + b for a in A for b in B]
AB.sort(reverse=True)
AB = AB[:k]
 
ABC = [ab + c for ab in AB for c in C]
ABC.sort(reverse=True)
ABC = ABC[:k]
 
print('\n'.join(map(str, ABC)))

Solution (Python):
from itertools import product
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 = sorted([a+b for a,b in product(A,B)],reverse=True)[:K]
ans = sorted([a+b for a,b in product(AB,C)],reverse=True)[:K]
for i in ans:
    print(i)

Solution (Python):



import heapq

def main():
  na, nb, nc, 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)
  
  scores = [(-(As[0]+Bs[0]+Cs[0]), 0, 0, 0)]
  used = [(0, 0, 0)]
  def push(i, j, k):
    heapq.heappush(scores, (-(As[i] + Bs[j] + Cs[k]), i, j, k))
    
  for _ in range(K):
    (score, i, j, k) = heapq.heappop(scores)
    print(-score)
    for di, dj, dk in [[1,0,0], [0,1,0], [0,0,1]]:
      if not (i+di < na and j+dj<nb and k+dk < nc):
        continue        
      if (i+di, j+dj, k+dk) not in used:
        push(i+di, j+dj, k+dk)
        used.append((i+di, j+dj, k+dk))

if __name__=='__main__':
  main()