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 sys

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


def resolve():
    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 a in A:
        for b in B:
            AB.append(a + b)
    AB = sorted(AB, reverse=True)[:k]

    res = []
    for ab in AB:
        for c in C:
            res.append(ab + c)
    res = sorted(res, reverse=True)[:k]
    print(*res, sep="\n")


if __name__ == '__main__':
    resolve()

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_pair = []
for a in A:
    for b in B:
        AB_pair.append(a + b)
AB_pair.sort(reverse=True)

ans = []
for total in AB_pair[:3001]:
    for c in C:
        ans.append(total + c)
ans.sort(reverse=True)
print(*ans[: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=list(map(int, input().split()))

ab=[]
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])

if len(ab)>k:
    ab.sort(reverse=True)
    ab=ab[:k]
else:
    ab.sort(reverse=True)

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

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

Solution (Python):

import sys
from heapq import heappush,heapify,heappop
input = sys.stdin.readline

def make_kth(A, B, k):
    n, m = len(A), len(B)
    q = [(-A[0] - B[0], 0, 0)]
    r=[]
    for _ in range(min(k, n * m)):
        v, s, t = heappop(q)
        r.append(-v)
        if t + 1 < m:
            heappush(q, (-A[s] - B[t + 1], s, t + 1))
        if t == 0 and s + 1 < n:
            heappush(q, (-A[s + 1] - B[0], s + 1, 0))
    return r

def main():
    _, _, _, 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)

    r = make_kth(a, b, k)
    r = make_kth(c, r, k)
    print(*r, sep="\n")

main()

Solution (Python):

import sys
from heapq import heappush,heapify,heappop
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()))

def make_kth(A, B):
    n, m = len(A), len(B)
    A.sort(reverse=1)
    B.sort(reverse=1)
    q = [(-A[0] - B[0], 0, 0)]
    r=[]
    for _ in range(min(k, n * m)):
        v, s, t = heappop(q)
        r.append(-v)
        if t + 1 < m:
            heappush(q, (-A[s] - B[t + 1], s, t + 1))
        if t == 0 and s + 1 < n:
            heappush(q, (-A[s + 1] - B[0], s + 1, 0))
    return r

r = make_kth(a, b)
r = make_kth(c, r)
print(*r,sep="\n")

Solution (Python):

import sys
from heapq import heappush,heapify,heappop
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()))

def make_kth(A, B):
    n, m = len(A), len(B)
    A.sort(reverse=1)
    B.sort(reverse=1)
    q = [(-A[0] - B[0], 0, 0)]
    r=[]
    for _ in range(min(k, n * m)):
        v, s, t = heappop(q)
        r.append(-v)
        if t + 1 < m:
            heappush(q, (-A[s] - B[t + 1], s, t + 1))
        if t == 0 and s + 1 < n:
            heappush(q, (-A[s + 1] - B[0], s + 1, 0))
    return r

r = make_kth(a, b)
r = make_kth(c, r)
print(*r,sep="\n")

Solution (Python):
import sys
from heapq import heappush,heapify,heappop
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()))
d = [i + j for i in a for j in b]
d.sort(reverse=1)
c.sort(reverse=1)
q = [(-d[0] - c[0], 0, 0)]
for _ in range(k):
    v, s, t = heappop(q)
    print(-v)
    if t + 1 < z:
        heappush(q, (-d[s] - c[t + 1], s, t + 1))
    if t == 0 and s + 1 < x * y:
        heappush(q, (-d[s + 1] - c[0], s + 1, 0))

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

import heapq
V={(0,0,0)}
Q=[(-(A[0]+B[0]+C[0]),0,0,0)]
heapq.heapify(Q)

def isIn(tup):
    return tup[0]<x and tup[1]<y and tup[2]<z

for i in range(k):
    now=heapq.heappop(Q)
    print(-now[0])
    nex=(now[1]+1,now[2],now[3])
    if not nex in V and isIn(nex):
        V.add(nex)
        heapq.heappush(Q,(-(A[nex[0]]+B[nex[1]]+C[nex[2]]),)+nex)
    nex=(now[1],now[2]+1,now[3])
    if not nex in V and isIn(nex):
        V.add(nex)
        heapq.heappush(Q,(-(A[nex[0]]+B[nex[1]]+C[nex[2]]),)+nex)
    nex=(now[1],now[2],now[3]+1)
    if not nex in V and isIn(nex):
        V.add(nex)
        heapq.heappush(Q,(-(A[nex[0]]+B[nex[1]]+C[nex[2]]),)+nex)

Solution (Python):
import sys
import heapq

input = sys.stdin.readline


def main():
    X, Y, Z, K = map(int, input().split())
    A = list(map(lambda x: -int(x), input().split()))
    B = list(map(lambda x: -int(x), input().split()))
    C = list(map(lambda x: -int(x), input().split()))

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

    p_queue = [(A[0] + B[0] + C[0], 0, 0, 0)]
    visited = set()
    visited.add((0, 0, 0))
    ans = []
    while len(ans) < K:
        s, i_A, i_B, i_C = heapq.heappop(p_queue)
        ans.append(-s)
        if i_A < X - 1 and (i_A + 1, i_B, i_C) not in visited:
            visited.add((i_A + 1, i_B, i_C))
            t = s - A[i_A] + A[i_A + 1]
            heapq.heappush(p_queue, (t, i_A + 1, i_B, i_C))
        if i_B < Y - 1 and (i_A, i_B + 1, i_C) not in visited:
            visited.add((i_A, i_B + 1, i_C))
            t = s - B[i_B] + B[i_B + 1]
            heapq.heappush(p_queue, (t, i_A, i_B + 1, i_C))
        if i_C < Z - 1 and (i_A, i_B, i_C + 1) not in visited:
            visited.add((i_A, i_B, i_C + 1))
            t = s - C[i_C] + C[i_C + 1]
            heapq.heappush(p_queue, (t, i_A, i_B, i_C + 1))

    print("\n".join(map(str, ans)))


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)

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

cakeList.sort(reverse = True)

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

Solution (Python):


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    X, Y, Z, K = read_ints()
    A = read_ints()
    B = read_ints()
    C = read_ints()
    BC = [b+c for b in B for c in C]
    BC.sort(reverse=True)
    A.sort(reverse=True)
    low, high = 2, max(A)+max(BC)+1
    while high-low > 1:
        min_deliciousness = low+(high-low)//2
        count_k = 0
        i = len(BC)-1
        for a in A:
            while i > -1 and a+BC[i] < min_deliciousness:
                i -= 1
            count_k += (i+1)
        if count_k > K:
            low = min_deliciousness
        else:
            high = min_deliciousness
    answer = []
    for a in A:
        for bc in BC:
            if a+bc < low:
                break
            else:
                answer.append(a+bc)
    answer.sort(reverse=True)
    for a in answer[:K]:
        print(a)


if __name__ == '__main__':
    solve()

Solution (Python):
from heapq import heappop, heappush
def main():
    def push(i, j, k):
        nonlocal ADD, H
        if i >= x or j >= y or k >= z: return
        if (i, j, k) not in ADD:
            heappush(H, (A[i] + B[j] + C[k], i, j, k))
            ADD.add((i, j, k))
    x, y, z, k = map(int, input().split())
    A = sorted(map(lambda x: -int(x), input().split()))
    B = sorted(map(lambda x: -int(x), input().split()))
    C = sorted(map(lambda x: -int(x), input().split()))
    H = []
    ADD = set()
    push(0, 0, 0)
    for _ in range(k):
        ans, i, j, k = heappop(H)
        push(i + 1, j, k)
        push(i, j + 1, k)
        push(i, j, k + 1)
        print(-ans)

if __name__ == '__main__':
    main()

Solution (Python):
from heapq import heappop, heappush
def main():
    def push(i, j, k):
        nonlocal A, B, C, ADD
        if i >= x or j >= y or k >= z: return
        if (i, j, k) not in ADD:
            heappush(H, (A[i] + B[j] + C[k], i, j, k))
            ADD.add((i, j, k))
    x, y, z, k = map(int, input().split())
    INF = 10 ** 11
    A = sorted(list(map(lambda x: -int(x), input().split())))
    B = sorted(list(map(lambda x: -int(x), input().split())))
    C = sorted(list(map(lambda x: -int(x), input().split())))
    H = []
    ADD = set()
    push(0, 0, 0)
    for _ in range(k):
        ans, i, j, k = heappop(H)
        push(i + 1, j, k)
        push(i, j + 1, k)
        push(i, j, k + 1)
        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 = sorted(map(int, input().split()), reverse=True)

AB = [0]*(X*Y)
i = 0
for a in A:
  for b in B:
    AB[i] = a+b
    i += 1
AB.sort(reverse = True)

ans = [0]*(K*Z)
j = 0
for ab in AB[:K]:
  for c in C:
    ans[j] = ab+c
    j += 1

ans.sort(reverse=True)
print(*ans[: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(map(int, input().split()), reverse=True)

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

ans = []
for ab in AB[:K]:
  for c in C:
    ans.append(ab+c)

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

Solution (Python):
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue



sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7


ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))




def main():
    x, y, z, w = ns()
    a = na()
    b = na()
    c = na()

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

    ans = []
    for ai in a:
        for bi in b:
            ans.append(ai + bi)

    ans.sort(reverse=True)
    ans = ans[:3010]

    res = []
    for ci in c:
        for i in range(len(ans)):
            res.append(ci + ans[i])
    res.sort(reverse=True)

    for i in range(w):
        print(res[i])


if __name__ == '__main__':
    main()

Solution (Python):
from heapq import heappop, heappush

X, Y, Z, K = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
c_list = list(map(int, input().split()))

a_list.sort(reverse=True)
b_list.sort(reverse=True)
c_list.sort(reverse=True)

h = []
a = a_list[0]
b = b_list[0]
c = c_list[0]
heappush(h, (-(a + b + c), 0, 0, 0))

key_dict = dict()
key_dict[(0, 0, 0)] = 1

ans_list = []
for i in range(K):
    ans, a, b, c = heappop(h)
    ans_list.append(-ans)
    a1 = (a + 1, b, c)
    b1 = (a, b + 1, c)
    c1 = (a, b, c + 1)
    if a + 1 < X and not a1 in key_dict:
        heappush(h, (-(a_list[a + 1] + b_list[b] + c_list[c]), a + 1, b, c))
        key_dict[(a + 1, b, c)] = 1
    if b + 1 < Y and not b1 in key_dict:
        heappush(h, (-(a_list[a] + b_list[b + 1] + c_list[c]), a, b + 1, c))
        key_dict[(a, b + 1, c)] = 1
    if c + 1 < Z and not c1 in key_dict:
        heappush(h, (-(a_list[a] + b_list[b] + c_list[c + 1]), a, b, c + 1))
        key_dict[(a, b, c + 1)] = 1
for ans in ans_list:
    print(ans)

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

a = list(map(int,input().split()))

b = list(map(int,input().split()))

c = list(map(int,input().split()))

mid = []

for i in range(x):
    for j in range(y):
        mid.append(a[i]+b[j])

mid.sort(reverse=True)

ans = []

for i in range(min(k,len(mid))):
    for j in range(z):
        ans.append(mid[i]+c[j])

ans.sort(reverse=True)

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

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

for i in A:
    for j in B:
        e.append(i+j)
e = sorted(e, reverse=True)

if K<len(e):
    e = e[0:K]

for i in e:
    for j in C:
        s.append(i+j)

s = sorted(s, reverse=True)

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

Solution (Python):
from heapq import heappop,heappush

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 a in A:
  for b in B:
    l.append(-a-b)

l.sort()
l=l[:K] if len(l)>=K else l+[0]*(K-len(l))
ans=[]
for c in C:
  for i in l:
    heappush(ans,i-c)
    
for i in range(K):
  print(-heappop(ans))

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

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

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


if __name__ == '__main__':
    resolve()

Solution (Python):
import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    X, Y, Z, K = NMI()
    A = NLI()
    B = NLI()
    C = NLI()
    AB = []
    for a in A:
        for b in B:
            AB.append(a+b)
    AB.sort(reverse=True)
    C.sort(reverse=True)
    S = []
    for c_idx in range(min(len(C), 3000)):
        for ab_idx in range(min(len(AB), 3000)):
            S.append(C[c_idx] + AB[ab_idx])
    S.sort(reverse=True)
    for i in range(K):
        print(S[i])



if __name__ == "__main__":
    main()

Solution (Python):
from heapq import *  
def solve():
  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)
  a = []
  heapify(a)
  ans = []
  for i in range(X):
    for j in range(Y):
      if (i+1)*(j+1)>K:
        break
      for k in range(Z):
        if (i+1)*(j+1)*(k+1)>K:
          break
        heappush(a,-A[i]-B[j]-C[k])
  for _ in range(K):
    ans.append(-heappop(a))
  return ans
print(*solve(),sep='\n')

Solution (Python):
from bisect import bisect_left

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

if X*Y*Z <= 50000:
  D = sorted([A[i]+B[j]+C[k] for i in range(X) for j in range(Y) for k in range(Z)],reverse=True)
  for i in range(K):
    print(D[i])
  
else:
  A = sorted(A)
  D = sorted([B[i]+C[j] for i in range(Y) for j in range(Z)])

  thr = K
  s = A[-1]+D[max(0,len(D)-thr)]
  prev_idx=0
  selected = [A[-1] + D[i] for i in range(max(0,len(D)-thr),len(D))]
  
  for i in range(1,X):
    r = s - A[-1-i]
    if r<=0:break
    idx = bisect_left(D,r,prev_idx,len(D)-1)
    if idx == len(D):break
    selected.extend([A[-1-i] + D[j] for j in range(idx,len(D))])
    prec_idx = idx

  selected = sorted(selected,reverse=True)

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

Solution (Python):
import sys
import re
import heapq

def value(t):
    global a, b, c
    return -(a[t[0]] + b[t[1]] + c[t[2]])

def main():
    global a, b, c
    x, y, z, k = map(int, re.split(r'\s+', sys.stdin.readline().strip()))
    
    a = list(map(int, re.split(r'\s+', sys.stdin.readline().strip())))
    b = list(map(int, re.split(r'\s+', sys.stdin.readline().strip())))
    c = list(map(int, re.split(r'\s+', sys.stdin.readline().strip())))

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

    done = set()
    q = list()
    t = (x - 1, y - 1, z - 1)
    q.append((value(t), t))

    for _ in range(k):
        val, t = heapq.heappop(q)
        print(-val)
        t0 = (t[0] - 1, t[1], t[2])
        t1 = (t[0], t[1] - 1, t[2])
        t2 = (t[0], t[1], t[2] - 1)
        if (t0 not in done) and (t[0] - 1 >= 0):
            heapq.heappush(q, (value(t0), t0))
            done.add(t0)
        if (t1 not in done) and (t[1] - 1 >= 0):
            heapq.heappush(q, (value(t1), t1))
            done.add(t1)
        if (t2 not in done) and (t[2] - 1 >= 0):
            heapq.heappush(q, (value(t2), t2))
            done.add(t2)
            
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(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

from collections import defaultdict
done = defaultdict(lambda:False)
import heapq
q = [(-A[0]-B[0]-C[0],0,0,0)]
heapq.heapify(q)

for _ in range(K):
    ma,i,j,k = heapq.heappop(q)
    if not done[(i+1,j,k)] and i<X-1:
        done[(i+1,j,k)]=True
        heapq.heappush(q,(-A[i+1]-B[j]-C[k] , i+1,j,k))
    
    if not done[(i,j+1,k)] and j<Y-1:
        done[(i,j+1,k)]=True
        heapq.heappush(q,(-A[i]-B[j+1]-C[k] , i,j+1,k))
    
    if not done[(i,j,k+1)] and k<Z-1:
        done[(i,j,k+1)]=True
        heapq.heappush(q,(-A[i]-B[j]-C[k+1] , i,j,k+1))
    print(-ma)

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.sort(reverse=True)

ab = sorted([aa+bb for aa in A for bb in B ],reverse=True)[:K]
ans = sorted([aabb+cc for aabb in ab for cc in C ],reverse=True)[:K]

for aa in ans:
    print(aa)

Solution (Python):
import sys
input = sys.stdin.readline
import heapq
from collections import defaultdict


def read():
    X, Y, Z, K = map(int, input().strip().split())
    A = list(map(int, input().strip().split()))
    B = list(map(int, input().strip().split()))
    C = list(map(int, input().strip().split()))
    return X, Y, Z, K, A, B, C


def solve(X, Y, Z, K, A, B, C, INF=10000000001):
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    
    used = defaultdict(bool)
    h = []
    p, q, r = 0, 0, 0
    heapq.heappush(h, (-A[p] - B[q] - C[r], p, q, r))
    used[(p, q, r)] = True
    for k in range(K):
        a, p, q, r = heapq.heappop(h)
        print(-a)
        if p+1 < len(A) and not used[(p+1, q, r)]:
            heapq.heappush(h, (-A[p+1] - B[q] - C[r], p+1, q, r))
            used[(p+1, q, r)] = True
        if q+1 < len(B) and not used[(p, q+1, r)]:
            heapq.heappush(h, (-A[p] - B[q+1] - C[r], p, q+1, r))
            used[(p, q+1, r)] = True
        if r+1 < len(C) and not used[(p, q, r+1)]:
            heapq.heappush(h, (-A[p] - B[q] - C[r+1], p, q, r+1))
            used[(p, q, r+1)] = True


if __name__ == '__main__':
    inputs = read()
    solve(*inputs)

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


def read():
    X, Y, Z, K = map(int, input().strip().split())
    A = list(map(int, input().strip().split()))
    B = list(map(int, input().strip().split()))
    C = list(map(int, input().strip().split()))
    return X, Y, Z, K, A, B, C


def solve(X, Y, Z, K, A, B, C, INF=10000000001):
    
    AB = [a+b for a, b in product(A, B)]
    AB.sort(reverse=True)
    AB = AB[:K]
    C.sort(reverse=True)

    D = [AB[k] + C[0] for k in range(len(AB))]
    E = [0 for k in range(len(AB))]
    j = 0
    for k in range(K):
        hi = 0

        
        for i in range(len(D)):
            if D[hi] < D[i]:
                hi = i
        
        print(D[hi])

        
        E[hi] += 1
        if E[hi] >= len(C):
            D[hi] = -INF
        else:
            D[hi] = AB[hi] + C[E[hi]]


if __name__ == '__main__':
    inputs = read()
    solve(*inputs)

Solution (Python):
import heapq

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

que = [(-(a[0]+b[0]+c[0]), 0, 0, 0)]
is_pushed = {(0, 0, 0): 1}
for _ in range(k):
    v, p, q, r = heapq.heappop(que)
    print(-v)
    if p+1 < x and not is_pushed.get((p+1, q, r)):
        heapq.heappush(que, (-(a[p+1]+b[q]+c[r]), p+1, q, r))
        is_pushed[(p+1, q, r)] = 1
    if q+1 < y and not is_pushed.get((p, q+1, r)):
        heapq.heappush(que, (-(a[p]+b[q+1]+c[r]), p, q+1, r))
        is_pushed[(p, q+1, r)] = 1
    if r+1 < z and not is_pushed.get((p, q, r+1)):
        heapq.heappush(que, (-(a[p]+b[q]+c[r+1]), p, q, r+1))
        is_pushed[(p, q, r+1)] = 1

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

p = [i + k for i, k in product(a, b)]
p.sort(reverse=True)
q = [i + k for i, k in product(p[:k], c)]
q.sort(reverse=True)
print('\n'.join(map(str, q[: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()))
D=[]
for i in A:
  for j in B:
    D.append(i+j)
D=sorted(D)[::-1]
if X*Y>K:
  D=D[:K]
E=[]
for i in D:
  for j in C:
    E.append(i+j)
E=sorted(E)[::-1]
E=E[:K]
for i in E:
  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()))
l = []
for i in a:
    for j in b:
        l.append(i + j)
c.sort(reverse=True)
l.sort(reverse=True)
l = l[:k]
l2 = []
for i in c:
    for j in l:
        l2.append(i + j)
l2.sort(reverse=True)
print(*l2[: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 = list(map(int,input().split()))

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

for c in C:
    for ab in AB:
        ABC.append(c + ab)
ABC = sorted(ABC,reverse=True)        
print(*ABC[:K],sep="\n")

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

X, Y, Z, K = mapint()
Xs = list(mapint())
Ys = list(mapint())
Zs = list(mapint())
Xs.sort(reverse=True)
Ys.sort(reverse=True)
Zs.sort(reverse=True)

x, y, z = 0, 0, 0
from heapq import heappop, heappush
Q = []
heappush(Q, (-(Xs[x]+Ys[y]+Zs[z]), (x, y, z)))
s = set()
s.add((x, y, z))
for _ in range(K):
    ans, (x, y, z) = heappop(Q)
    print(-ans)
    origin = [x, y, z]
    for i in range(3):
        xyz = origin[:]
        xyz[i] += 1
        x, y, z = xyz
        if x>=X or y>=Y or z>=Z: continue
        if not tuple(xyz) in s:
            heappush(Q, (-(Xs[x]+Ys[y]+Zs[z]), (x, y, z)))
            s.add((x, y, z))

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

l = []
for a in A:
    for b in B:
        l.append(a+b)
l = sorted(l, reverse = True)[:K]

ans = []
for c in C:
    for k in l:
        ans.append(c + k)
        
ans = sorted(ans, reverse = True)
print(*ans[:K], sep="\n")

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

l = []
for a in A:
    for b in B:
        l.append(a+b)
l = sorted(l, reverse = True)[:K]

ans = []
for c in C:
    for k in l:
        ans.append(c + k)
        
ans = sorted(ans, reverse = True)
print(*ans[:K], 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)

l = []
for a in A:
    for b in B:
        l.append(a+b)
l = sorted(l, reverse = True)[:K]

ans = []
for c in C:
    for k in l:
        ans.append(c + k)
        
ans = sorted(ans, reverse = True)
print(*ans[:K], sep="\n")

Solution (Python):

import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

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

    q = []
    heappush(q, (-(A[0]+B[0]+C[0]),0,0,0))
    count = 0
    used = set()

    while count < K:
        num, s,t,u = heappop(q)
        print(-num)
        used.add((s,t,u))

        if s+1 < X and (s+1,t,u) not in used:
            heappush(q, (-(A[s+1]+B[t]+C[u]),s+1,t,u))
            used.add((s+1,t,u))
        if t+1 < Y and (s,t+1,u) not in used:
            heappush(q, (-(A[s]+B[t+1]+C[u]),s,t+1,u))
            used.add((s,t+1,u))
        if u+1 < Z and (s,t,u+1) not in used:
            heappush(q, (-(A[s]+B[t]+C[u+1]),s,t,u+1))
            used.add((s,t,u+1))
        count += 1

if __name__ == "__main__":
    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)

hq = [[-a[0]-b[0]-c[0], 0, 0, 0]]
heapq.heapify(hq)
visited = {(0,0,0)}

for i in range(k):
    ii, ia, ib, ic = heapq.heappop(hq)
    print(-ii)
    na = min(ia+1, x-1)
    nb = min(ib+1, y-1)
    nc = min(ic+1, z-1)
    if not (na, ib, ic) in visited:
        heapq.heappush(hq, [-a[na] -b[ib] -c[ic], na, ib, ic])
        visited.add((na, ib, ic))
    if not (ia, nb, ic) in visited:
        heapq.heappush(hq, [-a[ia] -b[nb] -c[ic], ia, nb, ic])
        visited.add((ia, nb, ic))
    if not (ia, ib, nc) in visited:
        heapq.heappush(hq, [-a[ia] -b[ib] -c[nc], ia, ib, nc])
        visited.add((ia, ib, nc))

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=[]
for i in A:
  for j in B:
    D.append(i+j)
D=sorted(D)[::-1]
if X*Y>K:
  D=D[:K]
E=[]
for i in D:
  for j in C:
    E.append(i+j)
E=sorted(E)[::-1]
E=E[:K]
for i in E:
  print(i)

Solution (Python):
import heapq
x,y,z,K=list(map(int,input().split()))
alst=sorted(list(map(int,input().split())),reverse=True)
blst=sorted(list(map(int,input().split())),reverse=True)
clst=sorted(list(map(int,input().split())),reverse=True)

hq=[(-alst[0]-blst[0]-clst[0],0,0,0)]
used=set()

while K>0:
    s,i,j,k=heapq.heappop(hq)
    s*=-1
    if (i,j,k) in used:
        continue
    used.add((i,j,k))
    print(s)

    heapq.heappush(hq,(-s+alst[i]-alst[i+1],i+1,j,k)) if i+1<x else 0
    heapq.heappush(hq,(-s+blst[j]-blst[j+1],i,j+1,k)) if j+1<y else 0
    heapq.heappush(hq,(-s+clst[k]-clst[k+1],i,j,k+1)) if k+1<z else 0

    K-=1

Solution (Python):
X, Y, Z, k = (int(x) for x in input().split())
A = list(int(x) for x in input().split())
B = list(int(x) for x in input().split())
C = list(int(x) for x in input().split())

AB = []
for x in range(X):
    for y in range(Y):
        AB.append(A[x] + B[y])
AB.sort(reverse=True)
C.sort(reverse=True)

ANS = []
for i in range(min(k, X * Y)):
    for z in range(Z):
        ANS.append(AB[i] + C[z])
ANS.sort(reverse=True)

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