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())
alist = sorted(list(map(int,input().split())),reverse=True)
blist = sorted(list(map(int,input().split())),reverse=True)
clist = sorted(list(map(int,input().split())),reverse=True)
l = [(-(alist[0]+blist[0]+clist[0]),0,0,0)]
heapq.heapify(l)
count=0
ai=bi=ci=0
selected = set()
selected.add((0,0,0))
while len(l)!=0:
    temp = heapq.heappop(l)
    (tempa,tempb,tempc)=temp[1:]
    print(-temp[0])
    count += 1
    if count==k:break
    if tempa+1<=x-1:
        if not (tempa+1,tempb,tempc) in selected:
            heapq.heappush(l,(-(alist[tempa+1]+blist[tempb]+clist[tempc]),tempa+1,tempb,tempc))
            selected.add((tempa+1,tempb,tempc))    
    if tempb+1<=y-1:
        if not (tempa,tempb+1,tempc) in selected:
            heapq.heappush(l,(-(alist[tempa]+blist[tempb+1]+clist[tempc]),tempa,tempb+1,tempc))
            selected.add((tempa,tempb+1,tempc))
    if tempc+1<=z-1:
        if not (tempa,tempb,tempc+1) in selected:
            heapq.heappush(l,(-(alist[tempa]+blist[tempb]+clist[tempc+1]),tempa,tempb,tempc+1))
            selected.add((tempa,tempb,tempc+1))

Solution (Python):
import sys
import heapq
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readline_s = sys.stdin.readline
readlines = sys.stdin.buffer.readlines

INF = 10 ** 10 + 7


def main():
    X, Y, Z, K = map(int, readline().split())
    A = sorted(list(map(int, readline().split())), reverse=True)
    B = sorted(list(map(int, readline().split())), reverse=True)
    C = sorted(list(map(int, readline().split())), reverse=True)

    ans = solve3(X, Y, Z, K, A, B, C)
    print('\n'.join(map(str, ans)))


def solve3(X, Y, Z, K, A, B, C):

    init_v = -(A[0] + B[0] + C[0])
    q = [(init_v, 0, 0, 0)]
    heapq.heapify(q)
    search = set()
    ans = []

    for k in range(K):
        v, a, b, c = heapq.heappop(q)
        ans.append(-v)
        if a + 1 < X and not ((a + 1, b, c) in search):
            nv = A[a + 1] + B[b] + C[c]
            heapq.heappush(q, (-nv, a + 1, b, c))
            search |= {(a + 1, b, c)}
        if b + 1 < Y and not ((a, b + 1, c) in search):
            nv = A[a] + B[b + 1] + C[c]
            heapq.heappush(q, (-nv, a, b + 1, c))
            search |= {(a, b + 1, c)}
        if c + 1 < Z and not ((a, b, c + 1) in search):
            nv = A[a] + B[b] + C[c + 1]
            heapq.heappush(q, (-nv, a, b, c + 1))
            search |= {(a, b, c + 1)}

    return ans


def solve2(X, Y, Z, K, A, B, C):

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

    return sorted(ABC, reverse=True)[:K]


def solve1(X, Y, Z, K, A, B, C):

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

    AB.sort(reverse=True)
    AB = AB[:min(X * Y, K)]

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

    ABC.sort(reverse=True)

    return ABC[:K]


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readline_s = sys.stdin.readline
readlines = sys.stdin.buffer.readlines

INF = 10 ** 10 + 7


def main():
    X, Y, Z, K = map(int, readline().split())
    A = sorted(list(map(int, readline().split())), reverse=True)
    B = sorted(list(map(int, readline().split())), reverse=True)
    C = sorted(list(map(int, readline().split())), reverse=True)

    ans = solve2(X, Y, Z, K, A, B, C)
    print('\n'.join(map(str, ans)))


def solve2(X, Y, Z, K, A, B, C):

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

    return sorted(ABC, reverse=True)[:K]


def solve1(X, Y, Z, K, A, B, C):

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

    AB.sort(reverse=True)
    AB = AB[:min(X * Y, K)]

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

    ABC.sort(reverse=True)

    return ABC[:K]


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readline_s = sys.stdin.readline
readlines = sys.stdin.buffer.readlines

INF = 10 ** 10 + 7


def main():
    X, Y, Z, K = map(int, readline().split())
    A = sorted(list(map(int, readline().split())), reverse=True)
    B = sorted(list(map(int, readline().split())), reverse=True)
    C = sorted(list(map(int, readline().split())), reverse=True)

    ans = solve1(X, Y, Z, K, A, B, C)
    print('\n'.join(map(str, ans)))


def solve1(X, Y, Z, K, A, B, C):

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

    AB.sort(reverse=True)
    AB = AB[:min(X * Y, K)]

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

    ABC.sort(reverse=True)

    return ABC[:K]


if __name__ == '__main__':
    main()

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

heap = list()
used = set()
res = list()

heappush(heap, (-(A[0] + B[0] + C[0]), 0, 0, 0))
used.add((0, 0, 0))

for _ in range(K):
    v, i, j, k = heappop(heap)
    res.append(-v)
    if i + 1 < X and (i + 1, j, k) not in used:
        used.add((i + 1, j, k))
        heappush(heap, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
    if j + 1 < Y and (i, j + 1, k) not in used:
        used.add((i, j + 1, k))
        heappush(heap, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
    if k + 1 < Z and (i, j, k + 1) not in used:
        used.add((i, j, k + 1))
        heappush(heap, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
        
print('\n'.join(map(str, res)))

Solution (Python):
import sys

def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def S(): return sys.stdin.readline().rstrip()


X,Y,Z,K = map(int,S().split())
A = LI()
B = LI()
C = LI()

A = [0] + sorted(A,reverse = True)
B = [0] + sorted(B,reverse = True)
C = [0] + sorted(C,reverse = True)

ANS = []


for i in range(1,X+1):
    for j in range(1,min(K//i+1,Y)+1):
        for k in range(1,min(K//(i*j)+1,Z)+1):
            ANS.append(A[i]+B[j]+C[k])

ANS = sorted(ANS,reverse=True)

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

Solution (Python):
def resolve():
    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)
    
    
    def isOk(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 >= K:
                        return True
        return False
    
    
    def values_higher_than(p):
        sums = []
        for i in range(X):
            for j in range(Y):
                for k in range(Z):
                    if A[i]+B[j]+C[k] < p: 
                        break
                    sums.append(A[i]+B[j]+C[k])
        return sums
    
    left = -1
    right = 10**11
    while (right - left) > 1:
        mid = left + (right - left) // 2
        if isOk(mid):
            left = mid
        else:
            right = mid
    
    
    ans = sorted(values_higher_than(left), reverse=True)
    for i in range(K):
        if i < len(ans):
            print(ans[i])
        else:
            print(left)


if '__main__' == __name__:
    resolve()

Solution (Python):
x,y,z,k=map(int,input().split())
a,b,c=[list(map(int,input().split())) for i in range(3)]
l=sorted([bb+cc for bb in b for cc in c],reverse=1)[:k]
t=sorted([ll+aa for aa in a for ll in l],reverse=1)
for i in range(k):
    print(t[i])

Solution (Python):
def resolve():
    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)
    pushed = set("0-0-0")

    import heapq
    Q = [[-1*(A[0]+B[0]+C[0]), 0, 0, 0]]
    heapq.heapify(Q)
    for i in range(K):
        v, i, j, k = heapq.heappop(Q)
        print(-v)
        if "{}-{}-{}".format(i+1, j, k) not in pushed and i+1 < X:
            heapq.heappush(Q, [-1*(A[i+1]+B[j]+C[k]), i+1, j, k])
            pushed.add("{}-{}-{}".format(i+1, j, k))
        if "{}-{}-{}".format(i, j+1, k) not in pushed and j+1 < Y:
            heapq.heappush(Q, [-1*(A[i]+B[j+1]+C[k]), i, j+1, k])
            pushed.add("{}-{}-{}".format(i, j+1, k))
        if "{}-{}-{}".format(i, j, k+1) not in pushed and k+1 < Z:
            heapq.heappush(Q, [-1*(A[i]+B[j]+C[k+1]), i, j, k+1])
            pushed.add("{}-{}-{}".format(i, j, k+1))

if '__main__' == __name__:
    resolve()

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

    ABC = []
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                if ((i+1)*(j+1)*(k+1) > K):
                    break
                ABC.append(A[i]+B[j]+C[k])
    ABC = sorted(ABC, reverse=True)
    [print(ABC[i]) for i in range(K)]

if '__main__' == __name__:
    resolve()

Solution (Python):
def resolve():
    X, Y, Z, K = list(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 a in A:
        for b in B:
            AB.append(a+b)
    AB = sorted(AB, reverse=True)[:K]
    ABC = []
    for ab in AB:
        for c in C:
            ABC.append(ab+c)
    ABC = sorted(ABC, reverse=True)
    [print(ABC[i]) for i in range(K)]


if '__main__' == __name__:
    resolve()

Solution (Python):
import sys
import itertools

import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

x, y, z, t = map(int, readline().split())
a = sorted(map(int, readline().split()), reverse=True)
b = sorted(map(int, readline().split()), reverse=True)
c = sorted(map(int, readline().split()), reverse=True)

ans = []

for i in range(len(a)):
    for j in range(len(b)):
        ans.append(a[i] + b[j])

ans = sorted(ans, reverse=True)
fin_ans = []
for j in range(len(c)):
    for i in range(min(len(ans), t)):
        fin_ans.append(ans[i] + c[j])

fin_ans = sorted(fin_ans, reverse=True)

for i in fin_ans[:t]:
    print(i)

Solution (Python):

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

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

sum = []
for x in range(X):
    for y in range(Y):
        for z in range(Z):
            if (x+1)*(y+1)*(z+1) <= K:
                sum.append(A[x] + B[y] + C[z])
            else:
                break

sum.sort(reverse=True)
for i in range(K):
    print(sum[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()
b.sort()
c.sort()

bc = []
for bb in b:
    for cc in c:
        bc.append(bb + cc)
bc.sort()

from bisect import bisect_left, bisect_right

def C(x):
    
    ret = 0
    for aa in a:
        idx = bisect_right(bc, x - aa)
        ret += idx
    return ret

kk = x * y * z - k + 1
lb = 0
ub = (10 ** 10) * 4
while ub - lb > 1:
    mid = (ub + lb) // 2
    if C(mid) >= kk:
        ub = mid
    else:
        lb = mid

kth = ub

l = []
for aa in a:
    idx = bisect_left(bc, kth - aa)
    for i in range(idx, len(bc)):
        l.append(aa + bc[i])

l.sort(reverse=True)
l = l[:k]
for ll in l:
    print(ll)

Solution (Python):


if __name__ == '__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()))

    bc = []
    for bb in b:
        for cc in c:
            bc.append(bb + cc)
    bc.sort()

    from bisect import bisect_left

    def C(x):
        
        ret = 0
        for aa in a:
            idx = bisect_left(bc, x - aa)
            ret += len(bc) - idx
        return ret

    lb = 0
    ub = (10 ** 10) * 3 + 1
    while ub - lb > 1:
        mid = (ub + lb) // 2
        if C(mid) >= k:
            lb = mid
        else:
            ub = mid

    kth = lb

    l = []
    for aa in a:
        idx = bisect_left(bc, kth - aa)
        for i in range(idx, len(bc)):
            l.append(aa + bc[i])

    l.sort(reverse=True)
    l = l[:k]
    for ll in l:
        print(ll)

Solution (Python):
X,Y,Z,K= list(map(int,input().split()))
l_A=list(map(int,input().split()))
l_B=list(map(int,input().split()))
l_C=list(map(int,input().split()))
l_A.sort(reverse=True)
l_B.sort(reverse=True)
l_C.sort(reverse=True)
l=[]
for i in range(len(l_A)):
   for j in range(len(l_B)):
      for k in range(len(l_C)):
        if (i+1)*(j+1)*(k+1) > K:
           break
        l+=[l_A[i]+l_B[j]+l_C[k]]
ans=sorted(l,reverse=True)[:K]
print(*ans,sep="\n")

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

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

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

ans = []
for a in range(len(A)):
  for b in range(len(B)):
    for c in range(len(C)):
      if (a + 1) * (b + 1) * (c + 1) > K:
        break
      ans += [A[a] + B[b] + C[c]]
      
ans = sorted(ans,reverse = True)[:K]
print(*ans,sep = "\n")

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

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

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


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


ans = []
for ab in range(len(AB)):
  for c in range(len(C)):
    ans += [AB[ab] + C[c]]
    
ans = sorted(ans,reverse = True)[:K]
for a in ans:
  print(a)

Solution (Python):
def main():
    import heapq

    x, y, z, k, *abc = map(int, open(0).read().split())
    a = sorted(abc[:x], reverse=True)
    b = sorted(abc[x:x + y], reverse=True)
    c = sorted(abc[x + y:], reverse=True)
    s = set()

    h = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
    heapq.heapify(h)
    num = []
    for _ in range(k):
        p, i, j, k = heapq.heappop(h)
        num.append(-p)
        for p, q, r in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
            p, q, r = min(i + p, x - 1), min(j + q, y - 1), min(k + r, z - 1)
            if (p, q, r) not in s:
                s.add((p, q, r))
                v = - (a[p] + b[q] + c[r])
                heapq.heappush(h, (v, p, q, r))

    ans = '\n'.join(map(str, num))
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import heapq

    x, y, z, k, *abc = map(int, open(0).read().split())
    a = sorted(abc[:x], reverse=True)
    b = sorted(abc[x:x + y], reverse=True)
    c = sorted(abc[x + y:], reverse=True)
    s = set()

    h = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
    heapq.heapify(h)
    num = []
    for _ in range(k):
        p, i, j, k = heapq.heappop(h)
        num.append(-p)
        for p, q, r in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
            if i + p < x and j + q < y and k + r < z:
                if (i + p, j + q, k + r) not in s:
                    s.add((i + p, j + q, k + r))
                    v = - (a[i + p] + b[j + q] + c[k + r])
                    heapq.heappush(h, (v, i + p, j + q, k + r))

    ans = '\n'.join(map(str, num))
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import heapq

    x, y, z, k, *abc = map(int, open(0).read().split())
    a = abc[:x]
    b = abc[x:x + y]
    c = abc[x + y:]
    s = set()

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

    h = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
    heapq.heapify(h)
    ans = []
    for _ in range(k):
        p, i, j, k = heapq.heappop(h)
        ans.append(-p)
        for p, q, r in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
            if i + p < x and j + q < y and k + r < z:
                if (i + p, j + q, k + r) not in s:
                    s.add((i + p, j + q, k + r))
                    v = - (a[i + p] + b[j + q] + c[k + r])
                    heapq.heappush(h, (v, i + p, j + q, k + r))

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


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import heapq

    x, y, z, k, *abc = map(int, open(0).read().split())
    a = abc[:x]
    b = abc[x:x + y]
    c = abc[x + y:]
    d = []

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

    h = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
    heapq.heapify(h)
    ans = []
    for _ in range(k):
        p, i, j, k = heapq.heappop(h)
        ans.append(-p)
        for p, q, r in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
            if i + p < x and j + q < y and k + r < z:
                if (i + p, j + q, k + r) not in d:
                    d.append((i + p, j + q, k + r))
                    v = - (a[i + p] + b[j + q] + c[k + r])
                    heapq.heappush(h, (v, i + p, j + q, k + r))

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


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import heapq
    from collections import defaultdict

    x, y, z, k, *abc = map(int, open(0).read().split())
    a = abc[:x]
    b = abc[x:x + y]
    c = abc[x + y:]
    d = defaultdict(list)

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

    h = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
    heapq.heapify(h)
    ans = []
    for _ in range(k):
        p, i, j, k = heapq.heappop(h)
        ans.append(-p)
        for p, q, r in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
            if i + p < x and j + q < y and k + r < z:
                if (j + q, k + r) not in d[i + p]:
                    d[i + p].append((j + q, k + r))
                    v = - (a[i + p] + b[j + q] + c[k + r])
                    heapq.heappush(h, (v, i + p, j + q, k + r))

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


if __name__ == '__main__':
    main()

Solution (Python):


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


















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

a_max = min(k, len(a))
b_max = min(k, len(b))
c_max = min(k, len(c))

abc = []
for i in range(1, k+1):
    if i > len(a):
        break
    for j in range(1, k//i+1):
        if j > len(b):
            break
        for l in range(1, k//(i*j)+1):
            if l > len(c):
                break
            
            abc.append(a[i-1]+b[j-1]+c[l-1])



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

Solution (Python):
import sys

import math


from collections import deque

from fractions  import gcd

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=[0]*(x*y)
c.sort(reverse=True)
for i in range(x):
    for j in range(y):
        ab[y*i+j]=a[i]+b[j]
ab.sort(reverse=True)
d=ab[:k]
ans=[0]*(k*z)
for i in range(len(d)):
    for j in range(z):
        ans[z*i+j]=d[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())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

AB = []
for xi in range(x):
  for yi in range(y):
    AB.append(A[xi]+B[yi])

AB = sorted(AB,reverse=True)
C = sorted(C,reverse=True)

i=0
j=0

ABC = []
for ab in AB[:k]:
  for c in C[:k]:
    ABC.append(ab+c)

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

Solution (Python):
import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect

X, Y, Z, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
i, j, k = 0, 0, 0
heap = [(-A[i] - B[j] - C[k], i, j, k)]
heapq.heapify(heap)
used_set = set([(i, j, k)])
for _ in range(K):
    oisi, i, j, k = heapq.heappop(heap)
    print(-oisi)
    if (i+1, j, k) in used_set:
        pass
    elif(i+1 < len(A)):
        used_set.add((i+1, j, k))
        heapq.heappush(heap, (-A[i+1]-B[j]-C[k], i+1, j, k))
    if (i, j+1, k) in used_set:
        pass
    elif(j+1 < len(B)):
        used_set.add((i, j+1, k))
        heapq.heappush(heap, (-A[i] - B[j+1] - C[k], i, j+1, k))
    if (i, j, k+1) in used_set:
        pass
    elif(k+1 < len(C)):
        used_set.add((i, j, k+1))
        heapq.heappush(heap, (-A[i]-B[j]-C[k+1], i, j, k+1))

Solution (Python):
from sys import stdin
X,Y,Z,K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]
C = [int(x) for x in stdin.readline().rstrip().split()]

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

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

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

Solution (Python):
from heapq import *
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)

cake = []
heapify(cake)
for x in range(X):
  for y in range(min(Y,K//(x+1)+1)):
    for k in range(min(Z,K//((x+1)*(y+1))+1)):
      heappush(cake,-(A[x]+B[y]+C[k]))
for _ in range(K):
  print(abs(heappop(cake)))

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 range(x):
    for j in range(y):
        ab.append(a[i] + b[j])
ab.sort(reverse=True)
ab = ab[:k]
rec = []
for i in range(min(x*y, k)):
    for j in range(z):
        rec.append(ab[i] + c[j])

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

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(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
al = []

hq = []
heapq.heappush(hq,(-(a[0]+b[0]+c[0]),0,0,0))
for i in range(k):
    hqi,ai,bi,ci = heapq.heappop(hq)
    print(-hqi)
    if ai+1 < x and not (ai+1,bi,ci) in al:
        heapq.heappush(hq,(-(a[ai+1]+b[bi]+c[ci]),ai+1,bi,ci))
        al.append((ai+1,bi,ci))
    if bi+1 < y and not (ai,bi+1,ci) in al:
        heapq.heappush(hq,(-(a[ai]+b[bi+1]+c[ci]),ai,bi+1,ci))
        al.append((ai,bi+1,ci))
    if ci+1 < z and not (ai,bi,ci+1) in al:
        heapq.heappush(hq,(-(a[ai]+b[bi]+c[ci+1]),ai,bi,ci+1))
        al.append((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)

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

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

Solution (Python):
from heapq import heappush, heappop
from collections import defaultdict


def main():
    X, Y, Z, K = list(map(int, input().split(' ')))
    A = list(map(int, input().split(' ')))
    B = list(map(int, input().split(' ')))
    C = list(map(int, input().split(' ')))
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    que = [(- A[0] - B[0] - C[0], 0, 0, 0)]  
    already_inserted = defaultdict(int)
    answer = list()
    for _ in range(K):
        q, i, j, k = heappop(que)
        answer.append(-q)
        if i + 1 < X:
            item_a = (- A[i + 1] - B[j] - C[k], i + 1, j, k)
            if already_inserted[item_a] == 0:
                heappush(que, item_a)
                already_inserted[item_a] = 1
        if j + 1 < Y:
            item_b = (- A[i] - B[j + 1] - C[k], i, j + 1, k)
            if already_inserted[item_b] == 0:
                heappush(que, item_b)
                already_inserted[item_b] = 1
        if k + 1 < Z:
            item_c = (- A[i] - B[j] - C[k + 1], i, j, k + 1)
            if already_inserted[item_c] == 0:
                heappush(que, item_c)
                already_inserted[item_c] = 1
    for ans in answer:
        print(ans)


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

ab=[]

for aa in a:
    for bb in b:
        ab.append(aa+bb)

ab.sort(reverse=True)

abk=ab[:k]

ans=[]

for item in abk:
    for cc in c:
        ans.append(item+cc)

ans.sort(reverse=True)

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

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())))[::-1]

A_B = []
for a in A:
    for b in B:
        A_B.append(a + b)

A_B = sorted(A_B)[::-1][:K]

ans = []
for a_b in A_B:
    for c in C:
        ans.append(a_b + c)

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

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())))[::-1]

A_B = []
for a in A:
    for b in B:
        A_B.append(a + b)

A_B = sorted(A_B)

def bin_search(mid):
    res = 0
    idx = 0
    ans = [] 
    for c in C:
        while idx < len(A_B):
            if c + A_B[idx] >= mid:
                res += len(A_B) - idx
                ans.append((c, idx))
                break
            else:
                idx += 1

    return res, ans

ok = 0
ng = pow(10, 10) * 3 + 1

while ok + 1 < ng:
    mid = (ok + ng) // 2
    res, ans = bin_search(mid)
    if res >= K:
        ok = mid
    else:
        ng = mid

res, ans = bin_search(ok)

tmp_ans = []
for c, idx in ans:
    for i in range(idx, len(A_B)):
        tmp_ans.append(A_B[i] + c)
for ans in sorted(tmp_ans)[::-1][:K]:
    print(ans)

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)


def calc(p):
    cnt = 0
    for a in A:
        for b in B:
            for c in C:
                if a + b + c < p:
                    break
                cnt += 1
                if cnt >= K:
                    return True
    return False


end = A[0] + B[0] + C[0] + 1
start = 0
while end - start > 1:
    mid = (end + start)//2
    if calc(mid):
        start = mid
    else:
        end = mid

D = []

for a in A:
    for b in B:
        for c in C:
            if a + b + c < start:
                break
            D.append(a + b + c)
D.sort(reverse=True)
for d in D[:K]:
    print(d)