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):
x,y,z,k=map(int,input().split())
a=sorted(list(map(int,input().split())),reverse=True)
b=sorted(list(map(int,input().split())),reverse=True)
c=sorted(list(map(int,input().split())),reverse=True)
ans=[]
for ai in a:
  for bi in b:
    ans.append(-ai-bi)
ans.sort()
ans=ans[:k] if len(ans)>=k else ans+[0]*(k-len(ans))
ans1=[]
from heapq import heapify,heappop,heappush
for ci in c:
  for ansi in ans:
    heappush(ans1,ansi-ci)
for i in range(k):
  print(-heappop(ans1))

Solution (Python):
x,y,z,k=map(int,input().split())
a=sorted(list(map(int,input().split())),reverse=True)
b=sorted(list(map(int,input().split())),reverse=True)
c=sorted(list(map(int,input().split())),reverse=True)
ans=[]
for ai in a:
  for bi in b:
    ans.append(-ai-bi)
ans.sort()
ans=ans[:k] if len(ans)>=k else ans+[0]*(k-len(ans))
ans1=[]
from heapq import heapify,heappop,heappush
for ci in c:
  for ansi in ans:
    heappush(ans1,ansi-ci)
for i in range(k):
  print(-heappop(ans1))

Solution (Python):
from itertools import product
from heapq import *

X,Y,Z,K,*f = map(int, open(0).read().split())
A = f[:X]
B = f[X:X+Y]
C = f[X+Y:]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
AB = [sum(x) * (-1) for x in product(A,B)]
heapify(AB)
AB_picked = []
for i in range(K):
    if AB:
        AB_picked.append(heappop(AB)*(-1))
    else:
        break
ABC = [sum(x) * (-1) for x in product(AB_picked,C)]
heapify(ABC)
for i in range(K):
    print(heappop(ABC)*(-1))

Solution (Python):
import heapq
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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)

    def bfs():
        used = set()
        used.add((0, 0, 0))

        q = []
        heapq.heapify(q)
        heapq.heappush(q, (-(A[0] + B[0] + C[0]), 0, 0, 0))
        res = []
        while q:
            v, i, j, k = heapq.heappop(q)
            res.append(-v)

            if len(res) == K:
                return res

            for di, dj, dk in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
                ni = i + di
                nj = j + dj
                nk = k + dk

                if ni >= X or nj >= Y or nk >= Z:
                    continue

                if (ni, nj, nk) in used:
                    continue

                used.add((ni, nj, nk))
                heapq.heappush(q, (-(A[ni] + B[nj] + C[nk]), ni, nj, nk))

        return res

    res = bfs()
    res.sort(reverse=True)
    for i in range(K):
        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)


hq = []


arg_hash = {}


val = -(a_list[0] + b_list[0] + c_list[0])
heappush(hq, (val, 0, 0, 0))


ans_arr = []







for i in range(K):
    
    ans_val, p, q, r = heappop(hq)
    ans_arr.append(-ans_val)
    
    
    arg_a = (p+1, q, r)
    arg_b = (p, q+1, r)
    arg_c = (p, q, r+1)

    
    
    
    if (p < X-1) and (arg_a not in arg_hash):
        
        arg_hash[arg_a] = 1
        
        heappush(hq, (-(a_list[p+1] + b_list[q] + c_list[r]), ) + arg_a)
    
    if (q < Y-1) and (arg_b not in arg_hash):
        
        arg_hash[arg_b] = 1
        
        heappush(hq, (-(a_list[p] + b_list[q+1] + c_list[r]), ) + arg_b)
    
    if (r < Z-1) and (arg_c not in arg_hash):
        
        arg_hash[arg_c] = 1
        
        heappush(hq, (-(a_list[p] + b_list[q] + c_list[r+1]), ) + arg_c)


for i in range(len(ans_arr)):
    print(ans_arr[i])

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 *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

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





X, Y, Z, K = getNM()
A = getList()
B = getList()
C = getList()
A.sort(reverse = True)
B.sort(reverse = True)
C.sort(reverse = True)

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

alta.sort(reverse = True)
alta = alta[:K]
ans = []
for i in range(len(alta)):
    for j in range(Z):
        ans.append(alta[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 = 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)

D = []
for i in range(X):
    for j in range(Y):
        D.append(A[i]+B[j])
D.sort(reverse=True)

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

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

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache





MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b: a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





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

ans = []
q = [(-A[0]-B[0]-C[0],0,0,0)]
f = [0]
dx = [-1,1,0,0,0,0]
dy = [0,0,-1,1,0,0]
dz = [0,0,0,0,-1,1,]
while K:
  K -= 1
  v,x,y,z = heapq.heappop(q)
  ans.append(-v)
  for i in range(6):
    nx,ny,nz = x+dx[i],y+dy[i],z+dz[i]
    if nx<0 or nx>=X or ny<0 or ny>=Y or nz<0 or nz>=Z: continue
    if (nx*Y*Z + ny*Z + nz) in f: continue
    heapq.heappush(q,(-A[nx]-B[ny]-C[nz],nx,ny,nz))
    f.append(nx*Y*Z + ny*Z + nz)
    
for a in ans:
  print(a)

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):
numx,numy,numz,k=list(map(int,input().split()))
X=list(map(int,input().split()))
Y=list(map(int,input().split()))
Z=list(map(int,input().split()))

XY=[ x + y for x in X for y in Y]
XY.sort(reverse=True)
XY=XY[:k]
XYZ=[xy + z for xy in XY for z in Z]
XYZ.sort(reverse=True)

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

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

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()))
    
    
    
    
    
    
    ans = []
    for i in a:
        for j in b:
            ans.append(i+j)
    ans.sort()
    ans.reverse()
    ans = ans[:k]
    res = []
    for i in c:
        for j in ans:
            res.append(i+j)
    res.sort()
    res.reverse()
    res = res[:k]
    print(*res, sep="\n")
    
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)
q = [[(A[0]+B[0]+C[0])*-1, 0, 0, 0]]
heapq.heapify(q)
used = {}
for i in range(K):
    if len(q) == 0:
        break
    a, b, c, d = heapq.heappop(q)
    print(a*(-1))
    if (b+1, c, d) in used:
        pass
    elif b+1 < X:
        heapq.heappush(q, [(A[b+1]+B[c]+C[d])*(-1), b+1, c, d])
        used[(b+1, c, d)] = 1
    if (b, c+1, d) in used:
        pass
    elif c+1 < X:
        heapq.heappush(q, [(A[b]+B[c+1]+C[d])*(-1), b, c+1, d])
        used[(b, c+1, d)] = 1
    if (b, c, d+1) in used:
        pass
    elif d+1 < X:
        heapq.heappush(q, [(A[b]+B[c]+C[d+1])*(-1), b, c, d+1])
        used[(b, c, d+1)] = 1

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

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
P = []
for i in range(min(3000, X)):
    for j in range(min(3000//(i+1) + 1, Y)):
        for z in range(min(3000//(i+1)//(j+1) +1, Z) ):
            P.append(A[i]+B[j]+C[z])
P.sort(reverse=True)
for i in range(K):
    print(P[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)
P = []
for i in range(min(3000, X)):
    for j in range(min(3000//(i+1) + 1, X)):
        for z in range(min(3000//(i+1)//(j+1) +1, X) ):
            P.append(A[i]+B[j]+C[z])
P.sort(reverse=True)
for i in range(K):
    print(P[i])

Solution (Python):




import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

def main():
    x,y,z,k=MI()
    set_=set()
    A=sorted(LI(),reverse=True)
    B=sorted(LI(),reverse=True)
    C=sorted(LI(),reverse=True)
    Q=[[(A[0]+B[0]+C[0])*(-1),0,0,0]]
    heapify(Q)
    set_.add((0,0,0))
    i=0
    ans=[]
    xl,yl,zl=0,0,0
    al=[]
    al=[[0,0,0]]
    while i<k:
        
        a,u,v,w=heappop(Q)
        ans.append([-a,u,v,w])
        print(-a)
        i+=1
        if u+1<x and (u+1,v,w) not in set_:
            heappush(Q,[-A[u+1]-B[v]-C[w],u+1,v,w])
            set_.add((u+1,v,w))
        if v+1<y and (u,v+1,w) not in set_:
            heappush(Q,[-A[u]-B[v+1]-C[w],u,v+1,w])
            set_.add((u,v+1,w))
        if w+1<z and (u,v,w+1) not in set_:
            heappush(Q,[-A[u]-B[v]-C[w+1],u,v,w+1])
            set_.add((u,v,w+1))
if __name__=="__main__":
    main()

Solution (Python):




import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7


x,y,z,k=MI()
set_=set()
A=sorted(LI(),reverse=True)
B=sorted(LI(),reverse=True)
C=sorted(LI(),reverse=True)
Q=[[(A[0]+B[0]+C[0])*(-1),0,0,0]]
heapify(Q)
set_.add((0,0,0))
i=0
ans=[]
xl,yl,zl=0,0,0
al=[]
al=[[0,0,0]]
while i<k:
    a,u,v,w=heappop(Q)
    ans.append([-a,u,v,w])
    print(-a)
    i+=1
    if u+1<x and (u+1,v,w) not in set_:
        heappush(Q,[-A[u+1]-B[v]-C[w],u+1,v,w])
        set_.add((u+1,v,w))
    if v+1<y and (u,v+1,w) not in set_:
        heappush(Q,[-A[u]-B[v+1]-C[w],u,v+1,w])
        set_.add((u,v+1,w))
    if w+1<z and (u,v,w+1) not in set_:
        heappush(Q,[-A[u]-B[v]-C[w+1],u,v,w+1])
        set_.add((u,v,w+1))

Solution (Python):




import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

def main():
    x,y,z,k=MI()
    set_=set()
    A=sorted(LI(),reverse=True)
    B=sorted(LI(),reverse=True)
    C=sorted(LI(),reverse=True)
    Q=[[(A[0]+B[0]+C[0])*(-1),0,0,0]]
    heapify(Q)
    set_.add((0,0,0))
    
    i=0
    ans=[]
    
    
    
    xl,yl,zl=0,0,0
    al=[]
    al=[[0,0,0]]
    while i<k:
        
        a,u,v,w=heappop(Q)
        ans.append([-a,u,v,w])
        print(-a)
        i+=1
        if u+1<x and (u+1,v,w) not in set_:
            heappush(Q,[-A[u+1]-B[v]-C[w],u+1,v,w])
            set_.add((u+1,v,w))
            
            
            
        if v+1<y and (u,v+1,w) not in set_:
            heappush(Q,[-A[u]-B[v+1]-C[w],u,v+1,w])
            set_.add((u,v+1,w))
            
            
        if w+1<z and (u,v,w+1) not in set_:
            heappush(Q,[-A[u]-B[v]-C[w+1],u,v,w+1])
            set_.add((u,v,w+1))
            
if __name__=="__main__":
    main()

Solution (Python):




import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

x,y,z,k=MI()
A=LI()
B=LI()
C=LI()
lis=[]
for i in range(x):
    for j in range(y):
        lis.append(A[i]+B[j])
lis.sort(reverse=True)
lisan=[]
for i in range(min(k,x*y)):
    for j in range(z):
        lisan.append(lis[i]+C[j])
lisan.sort(reverse=True)
for i in range(k):
    print(lisan[i])

Solution (Python):
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))



def main():
    X,Y,Z,K=mi()
    A,B,C=list(mi()), list(mi()), list(mi())
    
    W = []
    for i in range(X):
        for j in range(Y):
            W.append(A[i]+B[j])

    W.sort(reverse=True)

    ans = []
    for w in W[:K]:
        for c in C:
            ans.append(w+c)
    
    ans.sort(reverse=True)
    print(*ans[:K],sep='\n')


if __name__ == "__main__":
    main()

Solution (Python):
import heapq
X,Y,Z,K = map(int,input().split())
K = min(K,X*Y*Z)
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)
set_ = set()
hq = [(-(A[0]+B[0]+C[0]),0,0,0)]
heapq.heapify(hq)
set_.add((0,0,0))
while K:
    val,i,j,k = heapq.heappop(hq)
    print(-val)
    K-=1
    if i+1<len(A) and (i+1,j,k) not in set_: 
        heapq.heappush(hq,(-(A[i+1]+B[j]+C[k]),i+1,j,k))
        set_.add((i+1,j,k))
    if j+1<len(B) and (i,j+1,k) not in set_: 
        heapq.heappush(hq,(-(A[i]+B[j+1]+C[k]),i,j+1,k))
        set_.add((i,j+1,k))
    if k+1<len(C) and (i,j,k+1) not in set_: 
        heapq.heappush(hq,(-(A[i]+B[j]+C[k+1]),i,j,k+1))
        set_.add((i,j,k+1))

Solution (Python):
X,Y,Z,K = map(int,input().split())
K = min(K,X*Y*Z)
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)
sum2 = []
for a in A:
    for b in B:
        sum2.append(a+b)
sum2.sort(reverse = True)
sum2 = sum2[:K]
idx = 0
sum3 = []
for c in C:
    for s in sum2:
        sum3.append(c+s)
sum3.sort(reverse=True)
for i in range(K):
    print(sum3[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 = [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]*min(K, len(AB))*Z
i, j = 0, 0
for i in range(min(K, len(AB))):
    for c in C:
        ans[j] = AB[i] + c
        j += 1
ans.sort(reverse=True)

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

Solution (Python):

from heapq import heappop, heappush
import sys
def main(X, Y, Z, K, A, B, C):
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    used = set()
    q = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
    used.add((0, 0, 0))
    for i in range(K):
        v, ai, bi, ci = heappop(q)
        print(-v)
        for nai, nbi, nci in [(ai + 1, bi, ci), (ai, bi + 1, ci), (ai, bi, ci + 1)]:
            if nai >= X or nbi >= Y or nci >= Z: continue
            if (nai, nbi, nci) in used: continue
            used.add((nai, nbi, nci))
            heappush(q, (-(A[nai] + B[nbi] + C[nci]), nai, nbi, nci))

if __name__ == '__main__':
    input = sys.stdin.readline
    X, Y, Z, K = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())
    *C, = map(int, input().split())
    main(X, Y, Z, K, A, B, C)

Solution (Python):

from itertools import product
import sys
def main(X, Y, Z, K, A, B, C):
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    ans = []
    for x in range(1, X + 1):
        if x > K: break
        for y in range(1, Y + 1):
            if x * y > K: break
            for z in range(1, Z + 1):
                if x * y * z > K: break
                ans.append(A[x - 1] + B[y - 1] + C[z - 1])
    ans.sort(reverse=True)
    for i in range(K):
        print(ans[i])

if __name__ == '__main__':
    input = sys.stdin.readline
    X, Y, Z, K = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())
    *C, = map(int, input().split())
    main(X, Y, Z, K, A, B, C)

Solution (Python):

from itertools import product
import sys
def main(X, Y, Z, K, A, B, C):
    AB = sorted([a + b for a, b in product(A, B)], reverse=True)
    ABC = sorted([ab + c for ab, c in product(AB[:K], C)], reverse=True)
    for k in range(K):
        print(ABC[k])

if __name__ == '__main__':
    input = sys.stdin.readline
    X, Y, Z, K = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())
    *C, = map(int, input().split())
    main(X, Y, Z, K, A, B, C)

Solution (Python):






from heapq import heapify, heappop, heappush










import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

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

    h=[]
    heapify(h)
    ac,bc,cc=0,0,0

    heappush(h, (-(A[ac]+B[bc]+C[cc]), ac,bc,cc))
    checked=set()

            
    checked.add((-(A[ac]+B[bc]+C[cc]),ac,bc,cc))


    for _ in range(k):
        (p,ac,bc,cc)=heappop(h)
        print(-p)
        if ac+1<x:
            if (-(A[ac+1]+B[bc]+C[cc]), ac+1,bc,cc) not in checked:
                heappush(h, (-(A[ac+1]+B[bc]+C[cc]), ac+1,bc,cc))
                checked.add((-(A[ac+1]+B[bc]+C[cc]), ac+1,bc,cc))

        if bc+1<y:  
                        
            if (-(A[ac]+B[bc+1]+C[cc]), ac,bc+1,cc) not in checked:
                heappush(h, (-(A[ac]+B[bc+1]+C[cc]), ac,bc+1,cc))
                checked.add((-(A[ac]+B[bc+1]+C[cc]), ac,bc+1,cc))
        if cc+1<z:
            if (-(A[ac]+B[bc]+C[cc+1]), ac,bc,cc+1) not in checked:
                heappush(h, (-(A[ac]+B[bc]+C[cc+1]), ac,bc,cc+1))
                checked.add((-(A[ac]+B[bc]+C[cc+1]), ac,bc,cc+1))




    


    


    
resolve()

Solution (Python):
import collections
import heapq

x,y,z,t = map(int, raw_input().split(' '))
mat = [map(int,raw_input().split(' ')) for _ in range(3)]
for ii in range(len(mat)): mat[ii].sort(key = lambda x:-x)

seen,heap = set([(0,0,0)]),[(-mat[0][0]-mat[1][0]-mat[2][0], 0,0,0)]


while(heap and t):
	u,i,j,k = heapq.heappop(heap)
	print -u
	t-=1
	for nei in [(i+1,j,k), (i,j+1,k), (i,j,k+1)]:
		if nei not in seen and all([0<=nei[ii]<len(mat[ii]) for ii in range(3)]):
			seen.add(nei)
			vv = -sum([mat[ii][vvv] for ii,vvv in enumerate(nei)])
			heapq.heappush(heap,(vv,nei[0],nei[1],nei[2]))

Solution (Python):
import collections
import heapq

x,y,z,t = map(int, raw_input().split(' '))
xs = map(int,raw_input().split(' '))
xs.sort(key = lambda x:-x)
ys = map(int,raw_input().split(' '))
ys.sort(key = lambda x:-x)
zs = map(int,raw_input().split(' '))
zs.sort(key = lambda x:-x)
mat = [xs,ys,zs]
heap = [(-mat[0][0]-mat[1][0]-mat[2][0], 0,0,0)]
seen = set([(0,0,0)])
res = []
while(heap and t):
	u,i,j,k = heapq.heappop(heap)
	print -u
	res.append(-u)
	t-=1
	for nei in [(i+1,j,k), (i,j+1,k), (i,j,k+1)]:
		if nei not in seen and all([0<=nei[ii]<len(mat[ii]) for ii in range(3)]):
			seen.add(nei)
			vv = mat[0][nei[0]] + mat[1][nei[1]] + mat[2][nei[2]]
			vv *= -1
			heapq.heappush(heap,(vv,nei[0],nei[1],nei[2]))

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)

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

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

Solution (Python):
from heapq import *

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)




Q = []
his = set()

def pushq(p,q,r):
  if (p,q,r) in his:
    return
  if p >= X or q >= Y or r >= Z:
    return
  his.add((p,q,r))
  heappush(Q, ((-1) * (A[p] + B[q] + C[r]), (p, q, r)))
  return

pushq(0,0,0)
for _ in range(K):
  V = heappop(Q)
  print((-1)*V[0])
  p,q,r = V[1]
  pushq(p+1,q,r)
  pushq(p,q+1,r)
  pushq(p,q,r+1)

Solution (Python):
x, y, z, k = map(int, input().split())
a = map(int, input().split())
b = list(map(int, input().split()))
c = map(int, input().split())
d = sorted((ai + bi for ai in a for bi in b), reverse=True)[:k]
e = sorted((ci + di for ci in c for di in d), reverse=True)[:k]
print(*e)

Solution (Python):
import heapq
x,y,z,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
q = []
s = set()
heapq.heappush(q,(-a[0]-b[0]-c[0],0,0,0))

for i in range(k):
    ans,ai,bi,ci = heapq.heappop(q)
    print(-ans)
    if ai < x-1 and ((ai+1, bi, ci) not in s):
        heapq.heappush(q, (-a[ai+1]-b[bi]-c[ci],ai+1,bi,ci))
        s.add((ai+1, bi, ci))
    if bi < y-1 and ((ai, bi+1, ci) not in s):
        heapq.heappush(q, (-a[ai]-b[bi+1]-c[ci],ai,bi+1,ci))
        s.add((ai, bi+1, ci))
    if ci < z-1 and ((ai, bi, ci+1) not in s):
        heapq.heappush(q, (-a[ai]-b[bi]-c[ci+1],ai,bi,ci+1))
        s.add((ai, bi, ci+1))