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 as hq
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:
        hq.heappush(ab, -a-b)
abc = []
cnt = 0
while cnt < k and ab:
    cnt += 1
    a = hq.heappop(ab)
    for b in C:
        hq.heappush(abc, a-b)
cnt = 0
while cnt < k:
    print(-hq.heappop(abc))
    cnt += 1

Solution (Python):

import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter


import sys


readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(X, Y, Z, K, A, B, C):
    q = []

    for a in A:
        for b in B:
            if len(q) < K:
                heapq.heappush(q, a+b)
            else:
                heapq.heappushpop(q, a+b)

    qq = []
    for c in C:
        for d in q:
            if len(qq) < K:
                heapq.heappush(qq, d+c)
            else:
                heapq.heappushpop(qq, d+c)

    qq.sort(reverse=True)
    return qq

def main():
    X, Y, Z, K = read_int_n()
    A = read_int_n()
    B = read_int_n()
    C = read_int_n()
    print(*slv(X, Y, Z, K, A, B, C), sep='\n')


if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter


import sys


readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(X, Y, Z, K, A, B, C):
    q = []
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    if len(A) > K:
        A = A[:K]
    if len(B) > K:
        B = B[:K]
    if len(C) > K:
        C = C[:K]

    for a in A:
        for b in B:
            heapq.heappush(q, a+b)
            if len(q) > K:
                heapq.heappop(q)

    qq = []
    for c in C:
        for d in q:
            heapq.heappush(qq, d+c)
            if len(qq) > K:
                heapq.heappop(qq)
    qq.sort(reverse=True)
    return qq

def main():
    X, Y, Z, K = read_int_n()
    A = read_int_n()
    B = read_int_n()
    C = read_int_n()
    print(*slv(X, Y, Z, K, A, B, C), sep='\n')


if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter


import sys


readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(X, Y, Z, K, A, B, C):
    q = []

    for a in A:
        for b in B:
            heapq.heappush(q, a+b)
            if len(q) > K:
                heapq.heappop(q)

    qq = []
    for c in C:
        for d in q:
            heapq.heappush(qq, d+c)
            if len(qq) > K:
                heapq.heappop(qq)
    qq.sort(reverse=True)
    return qq

def main():
    X, Y, Z, K = read_int_n()
    A = read_int_n()
    B = read_int_n()
    C = read_int_n()
    print(*slv(X, Y, Z, K, A, B, C), sep='\n')


if __name__ == '__main__':
    main()

Solution (Python):
x,y,z,k=map(int,input().split())
a=sorted(list(map(int,input().split())))[::-1]
b=sorted(list(map(int,input().split())))[::-1]
c=sorted(list(map(int,input().split())))[::-1]
d=[]
for i in a:
  for j in b:
    d+=[i+j]
ans=[]
d=sorted(d)[::-1][:k]
for i in c:
  for j in d:
    ans+=[i+j]
print(*sorted(ans)[::-1][:k],sep='\n')

Solution (Python):
from heapq import heappop, heappush

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


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



hq = []
u = (A[0] + B[0] + C[0], 0, 0, 0)
heappush(hq, u)


visited = set()
visited.add(u)

for i in range(K):
    x, i, j, k = heappop(hq)
    print(-x)
    
    if i + 1 <= X - 1:
        next_i = (A[i + 1] + B[j] + C[k], i + 1, j, k)
        if next_i not in visited:
            heappush(hq, next_i)
            visited.add(next_i)
    if j + 1 <= Y - 1:
        next_j = (A[i] + B[j + 1] + C[k], i, j + 1, k)
        if next_j not in visited:
            heappush(hq, next_j)
            visited.add(next_j)
    if k + 1 <= Z - 1:
        next_k = (A[i] + B[j] + C[k + 1], i, j, k + 1)
        if next_k not in visited:
            heappush(hq, next_k)
            visited.add(next_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()))


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

ans = []
for ab in tmp:
    for c in C:
        ans.append(ab + c)
ans.sort(reverse=True)

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

Solution (Python):
import os
import sys
from atexit import register
from io import BytesIO

import heapq

sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

x,y,z,k = (int(x) for x in input().split())
a = sorted((int(i) for i in input().split()), reverse=True)
b = sorted((int(i) for i in input().split()), reverse=True)
c = sorted((int(i) for i in input().split()), reverse=True)


h = []
heapq.heappush(h, (-a[0]-b[0]-c[0],(0,0,0)))
visited = dict()
cnt = 0
while True:
    cost, node = heapq.heappop(h)
    if visited.get(node, False):
        continue
    visited[node] = True
    cnt += 1
    print(-cost)
    if cnt == k:
        break
    if node[0] + 1 != x:
        heapq.heappush(h,(cost+a[node[0]] - a[node[0] + 1], (node[0]+1, node[1], node[2])))
    if node[1] + 1 != y:
        heapq.heappush(h,(cost+b[node[1]] - b[node[1] + 1], (node[0], node[1]+1, node[2])))
    if node[2] + 1 != z:
        heapq.heappush(h,(cost+c[node[2]] - c[node[2] + 1], (node[0], node[1], node[2]+1)))

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

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

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

AB = AB[0:K]

C = list(map(int, input().split()))
ABC = []
for ab in AB:
  for c in C:
    ABC.append(ab+c)
    
ABC.sort(reverse = True)

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

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)

A.append(-int(1e+100))
B.append(-int(1e+100))
C.append(-int(1e+100))

Aq = deque(A)
Bq = deque(B)
Ar = [Aq.popleft()]
Br = [Bq.popleft()]
maximum = min((len(A)-1) + (len(B)-1), 3001)
preA = Ar[0]
preB = Br[0]

while len(Ar) + len(Br) < maximum:
    if  Ar[0] - Aq[0] > Br[0] - Bq[0]:
        Br.append(Bq.popleft())
        preB = Br[-1]
    else:
        Ar.append(Aq.popleft())
        preA = Ar[-1]

AB = [Ar[i] + Br[j] for i in range(len(Ar)) for j in range(len(Br))]
AB = sorted(AB, reverse=True)

AB.append(-int(1e+15))

ABq = deque(AB)
Cq = deque(C)
ABr = [ABq.popleft()]
Cr = [Cq.popleft()]
maximum = min((len(AB)-1) + (len(C)-1), 3001)
preAB = ABr[0]
preC = Cr[0]
while len(ABr) + len(Cr) < maximum:
    if ABr[0] - ABq[0] > Cr[0] - Cq[0]:
        Cr.append(Cq.popleft())
        preC = Cr[-1]
    else:
        ABr.append(ABq.popleft())
        preAB = ABr[-1]

ABC = [ABr[i] + Cr[j] for i in range(len(ABr)) for j in range(len(Cr))]
ABC = sorted(ABC, reverse=True)

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

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)

A.append(-int(1e+15))
B.append(-int(1e+15))
C.append(-int(1e+15))

Aq = deque(A)
Bq = deque(B)
Ar = [Aq.popleft()]
Br = [Bq.popleft()]
maximum = min((len(A)-1) + (len(B)-1), K+1)
preA = Ar[0]
preB = Br[0]

while len(Ar) + len(Br) < maximum:
    if  Ar[0] - Aq[0] > Br[0] - Bq[0]:
        Br.append(Bq.popleft())
        preB = Br[-1]
    else:
        Ar.append(Aq.popleft())
        preA = Ar[-1]

AB = [Ar[i] + Br[j] for i in range(len(Ar)) for j in range(len(Br))]
AB = sorted(AB, reverse=True)

AB.append(-int(1e+15))

ABq = deque(AB)
Cq = deque(C)
ABr = [ABq.popleft()]
Cr = [Cq.popleft()]
maximum = min((len(AB)-1) + (len(C)-1), K+1)
preAB = ABr[0]
preC = Cr[0]
while len(ABr) + len(Cr) < maximum:
    if ABr[0] - ABq[0] > Cr[0] - Cq[0]:
        Cr.append(Cq.popleft())
        preC = Cr[-1]
    else:
        ABr.append(ABq.popleft())
        preAB = ABr[-1]

ABC = [ABr[i] + Cr[j] for i in range(len(ABr)) for j in range(len(Cr))]
ABC = sorted(ABC, reverse=True)

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

Solution (Python):










def solve(X, Y, Z, K, As, Bs, Cs):
    ABs = []
    for A in As:
        for B in Bs:
            ABs.append(A + B)
    ABs.sort(reverse=True)
    ABCs = []
    for i in range(min(X * Y, K)):
        AB = ABs[i]
        for C in Cs:
            ABCs.append(AB + C)
    ABCs.sort(reverse=True)
    for i in range(K):
        print(ABCs[i])


if __name__ == '__main__':
    
    
    X, Y, Z, K = map(int, input().split())
    As = [int(i) for i in input().split()]
    Bs = [int(i) for i in input().split()]
    Cs = [int(i) for i in input().split()]
    
    
    
    
    
    solve(X, Y, Z, K, As, Bs, Cs)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
from math import sqrt
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7





"""
N, M = getNM()
weight = []
key = []
for _ in range(M):
    a, b = getNM()
    weight.append(a)
    c = getList()
    key.append(c)

dp = [float('inf')] * (1 << N)
dp[0] = 0
for i in range(M):
    bit = 0
    for item in key[i]:
        bit |= (1 << (item - 1))
    for j in range(1 << N):
        dp[j | bit] = min(dp[j | bit], dp[j] + weight[i])

print(dp)
"""

"""
ARC028 B-特別賞
N, K = getNM()
A = getList()
young = [0] * N

for i in range(N):
    young[A[i] - 1] = i

A = [-i for i in A]

prized = []
for i in range(K):
    prized.append(A[i])
heapq.heapify(prized)


now = heapq.heappop(prized)
print(young[-now - 1] + 1)

for i in range(K, N):
    
    if A[i] > now:
        
        heapq.heappush(prized, A[i])
        
        now = heapq.heappop(prized)
        print(young[-now - 1] + 1)
    
    else:
        print(young[-now - 1] + 1)
"""

"""
ABC062 D - 3N Numbers
N = getN()
A = getList()


fore = A[:N]

back = A[2 * N:]
back = [-i for i in back]
for_sum = sum(fore)
back_sum = sum(back)
heapq.heapify(fore)
heapq.heapify(back)

fore_list = []
back_list = []
for i in range(N):
    fore_list.append(for_sum)
    back_list.append(back_sum)
    in_fore = A[N + i]
    heapq.heappush(fore, in_fore)
    out_fore = heapq.heappop(fore)
    for_sum += in_fore - out_fore

    in_back = (-1) * A[-N - i - 1]
    heapq.heappush(back, in_back)
    out_back = heapq.heappop(back)
    back_sum += in_back - out_back

fore_list.append(for_sum)
back_list.append(back_sum)

ans = -float('inf')
for i in range(N + 1):
    opt = fore_list[i] + back_list[N - i]
    ans = max(ans, opt)
print(ans)
"""

X, Y, Z, K = getNM()
A = sorted([-i for i in getList()])
B = sorted([-i for i in getList()])
C = sorted([-i for i in getList()])

pos = []
heapify(pos)
dict = defaultdict(int)
u = (A[0] + B[0] + C[0], 0, 0, 0)
heappush(pos, u)
dict[u] = 1

for i in range(K):
    p, i, j, l = heappop(pos)
    print(-p)
    
    if i + 1 < X:
        opt_a = (A[i + 1] + B[j] + C[l], i + 1, j, l)
        if dict[opt_a] == 0:
            heappush(pos, opt_a)
            dict[opt_a] = 1
    if j + 1 < Y:
        opt_b = (A[i] + B[j + 1] + C[l], i, j + 1, l)
        if dict[opt_b] == 0:
            heappush(pos, opt_b)
            dict[opt_b] = 1
    if l + 1 < Z:
        opt_c = (A[i] + B[j] + C[l + 1], i, j, l + 1)
        if dict[opt_c] == 0:
            heappush(pos, opt_c)
            dict[opt_c] = 1

Solution (Python):
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)
AB=[]
for a in A:
    for b in B:
        AB.append(a+b)
AB=sorted(AB,reverse=True)[:K]
ans=[]
for ab in AB:
    for c in C:
        ans.append(ab+c)
ans=sorted(ans,reverse=True)[:K]
print(*ans,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 a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse=True)
ab = ab[:k]
abc = []
for i in ab:
    for j in c:
        abc.append(i+j)
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])

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=sorted(list(map(int,input().split())))[::-1]

d=sorted([q+p for q in a for p in b],reverse=True)[:K]

e=sorted([q+p for q in d for p in c],reverse=True)[:K]
for i in e:
  print(i)

Solution (Python):
from heapq import*

x,y,z,K=map(int,input().split())
a=sorted(list(map(int,input().split())))[::-1]
b=sorted(list(map(int,input().split())))[::-1]
c=sorted(list(map(int,input().split())))[::-1]

h=[]
se=set((0,0,0))

heappush(h,(-a[0]-b[0]-c[0],0,0,0))

for _ in range(K):

  
  
  
  tmp,xx,yy,zz=heappop(h)
  print(-tmp)
  for i in range(2):
    for j in range(2):
      for k in range(2):
        if i+j+k==1:
          xn,yn,zn=xx+i,yy+j,zz+k
          if xn>=x:continue
          if yn>=y:continue
          if zn>=z:continue
          if (xn,yn,zn) in se:
            continue 
          heappush(h,(-a[xn]-b[yn]-c[zn],xn,yn,zn))
          se.add((xn,yn,zn))

Solution (Python):
import sys
import math
import copy
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter



input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)


def solve():
    x, y, z, k = getList()
    an = getList()
    bn = getList()
    cn = getList()

    ans = []
    for aa in an:
        for bb in bn:
            ans.append(aa + bb)

    ans.sort(reverse=True)
    ans = ans[:k]
    anans = []
    for aa in ans:
        for bb in cn:
            anans.append(aa + bb)

    anans.sort(reverse=True)
    for aaa in anans[:k]:
        print(aaa)

def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    
    solve()

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


import numpy as np
x,y,z,K = list(map(int, input().split()))
a = np.array(list(map(int, input().split())), dtype=np.int64)
b = np.array(list(map(int, input().split())), dtype=np.int64)
c = np.array(list(map(int, input().split())), dtype=np.int64)
ab = (a + b.reshape(-1,1)).reshape(-1)
ab.sort()
c.sort()
abk = ab[-K:]
ck = c[-K:]
l = (abk + ck.reshape(-1,1)).reshape(-1)
l.sort()
l = l[::-1]
write("\n".join(map(str, l[:K])))

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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()))
ab = [(a[i]+b[j]) for i in range(x) for j in range(y)]
ab.sort(reverse=True)
c.sort(reverse=True)
abk = ab[:K]
ck = c[:K]
l = [(abk[i]+ck[j]) for i in range(min(len(abk),K)) for j in range(min(len(ck),K))]
l.sort(reverse=True)
write("\n".join(map(str, l[: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()))
ans = []
for i in range(x):
    for j in range(y):
        ans.append(a[i]+b[j])
ans.sort(reverse=True)
ans = ans[:k]
ans2 = []
for i in range(len(ans)):
    for j in range(z):
        ans2.append(ans[i]+c[j])
ans2.sort(reverse=True)
for i in range(k):
    print(ans2[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)

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

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

Solution (Python):

from heapq import heappop, heappush

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

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

pq = []
heappush(pq, (-A[0] - B[0] - C[0], 0, 0, 0))
appeared = set((0, 0, 0))

for _ in range(K):
    
    val, i, j, k = heappop(pq)
    print(-val)

    
    if i + 1 < X and (i + 1, j, k) not in appeared:
        heappush(pq, (-A[i + 1] - B[j] - C[k], i + 1, j, k))
        appeared.add((i + 1, j, k))

    if j + 1 < Y and (i, j + 1, k) not in appeared:
        heappush(pq, (-A[i] - B[j + 1] - C[k], i, j + 1, k))
        appeared.add((i, j + 1, k))

    if k + 1 < Z and (i, j, k + 1) not in appeared:
        heappush(pq, (-A[i] - B[j] - C[k + 1], i, j, k + 1))
        appeared.add((i, j, k + 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()))

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

res.sort(reverse=True)

res2 = []
for v in res[:K]:
    for c in C:
        res2.append(v + c)

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

Solution (Python):

(x, y, z, k), a, b, c = [[*map(int, o.split())] for o in open(0)]
a.sort(); b.sort(); c.sort()

h = [(-(a[-1] + b[-1] + c[-1]), -1, -1, -1)]
s = set(h)

from heapq import*
def push(i, j, k):
    global s, h
    try:
        t = (- (a[i] + b[j] + c[k]), i, j, k)
    except IndexError:
        return
    if t not in s:
        heappush(h, t)
        s |= {t}

for _ in [0] * k:
    p, i, j, k = heappop(h)
    print(-p)
    push(i, j, k - 1)
    push(i, j - 1, k)
    push(i - 1, j, k)

Solution (Python):

(x, y, z, k), a, b, c = [[*map(int, o.split())] for o in open(0)]
a.sort(); b.sort(); c.sort()

p = a[-1] + b[-1] + c[-1]
s, h = {(p, -1, -1, -1)}, [(-p, -1, -1, -1)]

from heapq import*
def push(i, j, k):
    global s, h
    try:
        t = a[i] + b[j] + c[k]
    except IndexError:
        return
    if (t, i, j, k) not in s:
        heappush(h, (-t, i, j, k))
        s |= {(t, i, j, k)}

for _ in [0] * k:
    p, i, j, k = heappop(h)
    print(-p)
    push(i, j, k - 1)
    push(i, j - 1, k)
    push(i - 1, j, k)

Solution (Python):
import sys
from heapq import heappush, heappop

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

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

    hq = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
    seen = set()
    ans = []
    while len(ans) < K:
        s, i, j, k = heappop(hq)
        ans.append(-s)
        if i < X - 1 and (i + 1, j, k) not in seen:
            heappush(hq, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
            seen.add((i + 1, j, k))
        if j < Y - 1 and (i, j + 1, k) not in seen:
            heappush(hq, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
            seen.add((i, j + 1, k))
        if k < Z - 1 and (i, j, k + 1) not in seen:
            heappush(hq, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
            seen.add((i, j, k + 1))

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from heapq import heappush, heappop

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

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

    hq = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
    seen = set()
    ans = []
    while len(ans) < K:
        s, i, j, k = heappop(hq)
        ans.append(-s)
        if i < X - 1 and (i + 1, j, k) not in seen:
            heappush(hq, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
            seen.add((i + 1, j, k))
        if j < Y - 1 and (i, j + 1, k) not in seen:
            heappush(hq, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
            seen.add((i, j + 1, k))
        if k < Z - 1 and (i, j, k + 1) not in seen:
            heappush(hq, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
            seen.add((i, j, k + 1))

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

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

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

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

    ABC.sort(reverse=True)

    print('\n'.join(map(str, ABC[:K])))

    return


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 = [0] * (X * Y)
for i in range(X):
  for j in range(Y):
    AB[i * Y + j] = A[i] + B[j]

AB = sorted(AB, reverse = True)[:K]

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

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

Solution (Python):
from itertools import product
X, Y, Z, K = map(int, input().split())
AB = []
for ab in product(map(int, input().split()), map(int, input().split())):
    AB.append(ab[0] + ab[1])
AB.sort(reverse=True)
AB = AB[:min(K,X*Y)]
ABC = []
for abc in product(AB, map(int, input().split())):
    ABC.append(abc[0]+abc[1])
ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])

Solution (Python):
from itertools import product
X, Y, Z, K = map(int, input().split())
AB = []
for ab in product(map(int, input().split()), map(int, input().split())):
    AB.append(ab[0] + ab[1])
AB.sort(reverse=True)
AB = AB[:K]
ABC = []
for abc in product(AB, map(int, input().split())):
    ABC.append(abc[0]+abc[1])
ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])

Solution (Python):
def binary_search_int(ok, ng, test, k, A, B):
    """
    :param ok: solve(x) = True を必ず満たす点
    :param ng: solve(x) = False を必ず満たす点
    """
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if test(mid, k, A, B):
            ok = mid
        else:
            ng = mid
    return ok

def get_cnt(x, A, B):
    """ a+b>=x となるようなAの要素とBの要素の組の数 (A, Bはソート済み)"""
    l, r = bisect_left(B, x-A[-1]), len(A)-2
    cnt = len(B) - l
    while l < len(B) and r>=0:
        if A[r]+B[l] < x:
            l += 1
        else:
            cnt += len(B) - l
            r -= 1
    return cnt

def test(x, k, A, B):
    return get_cnt(x, A, B) >= k

from bisect 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.sort()
B.sort()
C.sort()

P = []
for k in range(1,K+1):
    tmp = binary_search_int(0, 10**11,test, k, A, B)
    if tmp != 0:
        P.append(tmp)
Q = []
P.sort()
for k in range(1,K+1):
    Q.append(binary_search_int(1, 10**11,test, k, P, C))
print(*Q, 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()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
list1=[]
for i in range(X):
  for j in range(Y):
      list1.append(A[i]+B[j])
list1.sort(reverse=True)
list1=list1[:K+1]
list2=[]
for i in range(min(X*Y,K)):
  for j in range(Z):
    list2.append(list1[i]+C[j])
list2.sort(reverse=True)
for i in range(K):
  print(list2[i])