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):
def main():
    import sys

    input = sys.stdin.readline

    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)

    ret = []
    for p, a in enumerate(A):
        for q, b in enumerate(B):
            if p * q > k:
                break
            for r, c in enumerate(C):
                if p * q * r > k:
                    break
                ret.append(a + b + c)
    ret.sort(reverse=True)
    print(*ret[:k], sep='\n')


main()

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

    input = sys.stdin.readline

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

    AB = sorted((a + b for a in A for b in B), reverse=True)[:k]
    ret = sorted((ab + c for ab in AB for c in C), reverse=True)[:k]

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


main()

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

    input = sys.stdin.readline

    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 = sorted((a + b for a in A for b in B), reverse=True)[:k]
    ret = sorted((ab + c for ab in AB for c in C), reverse=True)[:k]

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


main()

Solution (Python):
x, y, z, k = [int(n) for n in input().split()]
va = sorted([int(n) for n in input().split()], reverse=True)
vb = sorted([int(n) for n in input().split()], reverse=True)
xy = min(x*y, k)
vab = (sorted([a + b for a in va for b in vb], reverse=True))[:xy]
vc = sorted([int(n) for n in input().split()], reverse=True)
xyz = min(x*y*z, k)
print(*((sorted([ab + c for ab in vab for c in vc], reverse=True))[:xyz]), sep="\n")

Solution (Python):
import sys
import numpy as np

INF = 10 ** 18

def main():
    x, y, z, K = map(int, sys.stdin.readline().split())
    abc = np.array(sys.stdin.read().split(), dtype=np.int64)
    a = np.append(abc[:x], INF)
    b = np.append(abc[x:x + y], INF)
    c = np.append(abc[x + y:x + y+ z], INF)
    a = np.sort(a)[::-1]
    b = np.sort(b)[::-1]
    c = np.sort(c)[::-1]
 
    res = []
    for i in range(1, min(K, x) + 1):
        for j in range(1, min(y, K // i) + 1):
            for k in range(1, min(z, K // (i * j)) + 1):
                res.append(a[i] + b[j] + c[k])

    res = sorted(res, reverse=True)[:K]
    print('\n'.join(map(str, res)))

if __name__ == "__main__":
    main()

Solution (Python):
import sys
import numpy as np

def main():
    x, y, z, K = map(int, sys.stdin.readline().split())
    abc = np.array(sys.stdin.read().split(), dtype=np.int64)
    a = abc[:x]
    b = abc[x:x + y]
    c = abc[x + y:x + y+ z]

    ab = np.sort(np.array([a[i] + b for i in range(x)]).reshape(-1))[::-1][:K]
    res = np.sort(np.array([c[i] + ab for i in range(z)]).reshape(-1))[::-1][:K]

    print('\n'.join(map(str, res)))

if __name__ == "__main__":
    main()

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


def main():
    x, y, z, K = map(int, sys.stdin.readline().split())
    abc = np.array(sys.stdin.read().split(), dtype=np.int64)
    a = abc[:x]
    b = abc[x:x + y]
    c = abc[x + y:x + y+ z]
    a = np.sort(a)[::-1]
    b = np.sort(b)[::-1]
    c = np.sort(c)[::-1]

    hq = []
    heappush(hq, (-(a[0]+b[0]+c[0]), 0, 0, 0))
    res = []
    added_before = set()
    
    added_before.add((0, 0, 0))

    for _ in range(K):
        sum_deli, i, j, k = heappop(hq)
        res.append(-sum_deli)
        if i < x - 1 and not (i+1, j, k) in added_before:
            heappush(hq, (-(a[i+1]+b[j]+c[k]), i+1, j, k))
            added_before.add((i+1, j, k))
        if j < y - 1 and not (i, j+1, k) in added_before:
            heappush(hq, (-(a[i]+b[j+1]+c[k]), i, j+1, k))
            added_before.add((i, j+1, k))
        if k < z - 1 and not (i, j, k+1) in added_before:
            heappush(hq, (-(a[i]+b[j]+c[k+1]), i, j, k+1))
            added_before.add((i, j, k+1))

    for s_deli in res:
        print(s_deli)

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]
ans=[]
for a in range(len(A)):
  for b in range(len(B)):
    for c in range(len(C)):
      if (a+1)*(b+1)*(c+1)>K:
        break
      ans.append(A[a]+B[b]+C[c])

ans=sorted(ans)[::-1]
for i in range(min(K,len(ans))):
  print(ans[i])

Solution (Python):

a,b,c,k = map(int,input().split())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
z = list(map(int,input().split()))
xy = []
for x0 in x:
  for y0 in y:
    xy.append(x0+y0)
xy.sort(reverse=True)
if a * b > 3000:
  xy = xy[:3000]
xyz = []
for xy0 in xy:
  for z0 in z:
    xyz.append(xy0+z0)
xyz.sort(reverse=True)
for i in range(k):
  print(xyz[i])

Solution (Python):
import heapq

X, Y, Z, K = map(int, 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)
Q = []
dic = {}
heapq.heappush(Q, (-(A[0] + B[0] + C[0]), 0, 0, 0))
i = 0
while i < K and len(Q) > 0:
    t = heapq.heappop(Q)
    print(-t[0])
    a, b, c = t[1:]
    if a + 1 < X and (a + 2) * (b + 1) * (c + 1) <= K and not (a + 1, b, c) in dic.keys():
        heapq.heappush(Q, (-(A[a + 1] + B[b] + C[c]), a + 1, b, c))
        dic[(a + 1, b, c)] = 1
    if b + 1 < Y and (a + 1) * (b + 2) * (c + 1) <= K and not (a, b + 1, c) in dic.keys():
        heapq.heappush(Q, (-(A[a] + B[b + 1] + C[c]), a, b + 1, c))
        dic[(a, b + 1, c)] = 1
    if c + 1 < Z and (a + 1) * (b + 1) * (c + 2) <= K and not (a, b, c + 1) in dic.keys():
        heapq.heappush(Q, (-(A[a] + B[b] + C[c + 1]), a, b, c + 1))
        dic[(a, b, c + 1)] = 1
    i += 1

Solution (Python):
X, Y, Z, K = map(int, 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)
D = []
for i in range(min(K, X)):
    for j in range(min(K, Y)):
        if (i + 1) * (j + 1) > K:
            break
        for k in range(min(K, Z)):
            if (i + 1) * (j + 1) * (k + 1) > K:
                break
            else:

                D.append(A[i] + B[j] + C[k])

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

Solution (Python):
X, Y, Z, K = map(int, 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)
D = []
for i in range(min(K, X)):
    for j in range(min(K, Y)):
        if (i + 1) * (j + 1) > K:
            break
        for k in range(min(K, Z)):
            if (i + 1) * (j + 1) * (k + 1) > K:
                break
            else:

                D.append(A[i] + B[j] + C[k])

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

Solution (Python):
import numpy as np

X, Y, Z, K = map(int, input().split())
A = np.array([int(i) for i in input().split()])
B = np.array([int(i) for i in input().split()])
C =np.array([int(i) for i in input().split()])
A = -np.sort(-A) 
B = -np.sort(-B)
C = -np.sort(-C)
D = []
for i in range(min(K, X)):
    for j in range(min(K, Y)):
        if (i + 1) * (j + 1) > K:
            break
        for k in range(min(K, Z)):
            if (i + 1) * (j + 1) * (k + 1) > K:
                break
            else:

                D.append(A[i] + B[j] + C[k])

X = np.array(D)
X = -np.sort(-X)
for i in range(K):
    print(X[i])

Solution (Python):

x, y, z, k = map(int,input().split())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
Z = list(map(int, input().split()))
mul_x_y = []
for i in X:
    for j in Y:
        mul_x_y.append(i+j)
mul_x_y.sort()
mul_x_y.reverse()
Z.sort()
Z.reverse()
mul_x_y = mul_x_y[:k]
Z = Z[:k]
ans = []
for i in mul_x_y:
    for j in Z:
        ans.append(i + j)
ans.sort()
ans.reverse()
for i in range(k):
    print(ans[i])

Solution (Python):
X,Y,Z,K=map(int,input().split())
A=[int(x) for x in input().split()]
B=[int(x) for x in input().split()]
C=[int(x) for x in input().split()]
A.sort(reverse =True)
B.sort(reverse =True)
C.sort(reverse =True)
answer=[]
for p in range(min(X,K)):
  for q in range(min(Y,K)):
    if p*q>K:
      break
    for r in range(min(Z,K)):
      if p*q*r>K:
        break
      answer +=[A[p]+B[q]+C[r]]
answer.sort()
for i in range(K):
  print(answer[-i-1])

Solution (Python):
import heapq








class Avltree:
    def __init__(self,key=None):
        self.key = key
        self.left = None
        self.right = None 
        self.balance = "Even"
    def search(self,key):
        if self.key == None:
            return None
        if self.key > key:
            if self.left == None:
                return None
            else:
                return self.left.search(key)
        if self.key < key:
            if self.right == None:
                return None
            else:
                return self.right.search(key)
        return self 
    def DoubleRightRotation(self):
        tl = self.left
        self.left = tl.right.right
        tl.right.right = self 
        tlr = tl.right
        tl.right = tlr.left
        tlr.left = tl
        if tlr.balance == "Left":
            tlr.right.balance = "Right"
            tlr.left.balance = "Even"
        elif tlr.balance == "Right":
            tlr.right.balance = "Even"
            tlr.left.balance = "Left"
        elif tlr.balance == "Even":
            tlr.right.balance = "Even"
            tlr.left.balance = "Even"
        tlr.balance = "Even"
        return tlr
    def DoubleLeftRotation(self):
        tr = self.right
        self.right = tr.left.left
        tr.left.left = self
        trl = tr.left
        tr.left = trl.right
        trl.right = tr
        if trl.balance == "Right":
            trl.left.balance = "Left"
            trl.right.balance = "Even"
        elif trl.balance == "Left":
            trl.left.balance = "Even"
            trl.right.balance = "Right"
        elif trl.balance == "Even":
            trl.left.balance = "Even"
            trl.right.balance = "Even"
        trl.balance = "Even"
        return trl
    def SingleLeftRotation(self):
        tr = self.right
        tr.balance = "Even"
        self.balance = "Even"
        self.right = tr.left
        tr.left = self
        return tr
    def SingleRightRotation(self):
        tl = self.left
        tl.balance = "Even"
        self.balance = "Even"
        self.left = tl.right
        tl.right = self
        return tl
    def replace(self,p,v): 
        if p.left == self:
            p.left = v
        else :
            p.right = v 
    def insert(self,key): 
        if self.key == None: 
            self.key = key
            return self
        if key < self.key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                self.left.insertx(self,key)
        elif key > self.key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                self.right.insertx(self,key)
        else: 
            pass 
    def insertx(self,p,key): 
        if self.key > key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                if not self.left.insertx(self, key): 
                    return False 
            if self.balance == "Right":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Left"
                return True 
            elif self.balance == "Left":
                if self.left.balance == "Right":
                    self.replace(p,self.DoubleRightRotation())
                elif self.left.balance == "Left":
                    self.replace(p,self.SingleRightRotation())
                return False 
        if self.key < key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                if not self.right.insertx(self, key):
                    return False 
            if self.balance == "Left":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Right"
                return True
            elif self.balance == "Right":
                if self.right.balance == "Left": 
                    self.replace(p,self.DoubleLeftRotation())
                elif self.right.balance == "Right":
                    self.replace(p,self.SingleLeftRotation())
                return False
        return False 

    def to_s(self):
        return self.key
    def left_s(self):
        if self.left == None:
            return None
        return (self.left).key
    def right_s(self):
        if self.right == None:
            return None
        return (self.right).key


def Search(x,y,z):
    global dict
    global root
    global q
    p = a[x] + b[y] + c[z]
    u = ((1001)**2) * x + ((1001)**1) * y + z
    s = root.search(u)
    if s == None:
        root.insert(u)
        heapq.heappush(q, -p)
        if p not in dict:
            dict[p] = [(x, y, z)]
        else:
            dict[p].append((x, y, z))


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

t = k
root = Avltree()
q = []
heapq.heapify(q)
dict = {}
count = 0
while t != 0:
    if t == k:
        p = a[x-1]+b[y-1]+c[z-1]
        print(p)
        x -= 1
        y -= 1
        z -= 1
        u = ((1001)**2) * x + ((1001)**1) * y + z
        root.insert(u)
        
        if x != 0:
            Search(x-1,y,z)
        if y != 0:
            Search(x,y-1,z)
        if z != 0:
            Search(x,y,z-1)

        if x == 0 and y == 0 and z == 0:
            break
        
        t -= 1
    else:
        p = heapq.heappop(q)*(-1)
        print(p)
        x, y, z = dict[p].pop()

        if x != 0:
            Search(x-1,y,z)
        if y != 0:
            Search(x,y-1,z)
        if z != 0:
            Search(x,y,z-1)

        if x == 0 and y == 0 and z == 0:
            break

        t -= 1

Solution (Python):
import heapq
import numpy as np
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()))
flag = np.ones((X,Y,Z),dtype=np.bool)
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

ABC = []
heapq.heappush(ABC,(-1 * (A[0]+B[0]+C[0]),0,0,0))
flag[0][0][0] = False
ctr = 0
while ctr < K and ABC:
    s,a,b,c = heapq.heappop(ABC)
    print(-s)
    if a+1<X and flag[a+1][b][c]:
        heapq.heappush(ABC,(-1 * (A[a+1]+B[b]+C[c]),a+1,b,c))
        flag[a+1][b][c] = False
    if b+1<Y and flag[a][b+1][c]:
        heapq.heappush(ABC,(-1 * (A[a]+B[b+1]+C[c]),a,b+1,c))
        flag[a][b+1][c] = False
    if c+1<Z and flag[a][b][c+1]:
        heapq.heappush(ABC,(-1 * (A[a]+B[b]+C[c+1]),a,b,c+1))
        flag[a][b][c+1] = False
    ctr += 1

Solution (Python):

import sys
import heapq

def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    A = sorted(A,reverse = True)
    B = sorted(B,reverse = True)
    C = sorted(C,reverse = True)

    d = []
    heapq.heappush(d, (-(A[0]+B[0]+C[0]), 0, 0, 0)) 
    ijk_set = set((0,0,0))

    for _ in range(K):
        tmp, i, j, k = heapq.heappop(d)
        print(-tmp)
        if (i+1,j,k) not in ijk_set and i+1 <= X-1:
            heapq.heappush(d, (-(A[i+1]+B[j]+C[k]), i+1, j, k))
            ijk_set.add((i+1,j,k))
        if (i,j+1,k) not in ijk_set and j+1 <= Y-1:
            heapq.heappush(d, (-(A[i]+B[j+1]+C[k]), i, j+1, k))
            ijk_set.add((i,j+1,k))
        if (i,j,k+1) not in ijk_set and k+1 <= Z-1:
            heapq.heappush(d, (-(A[i]+B[j]+C[k+1]), i, j, k+1))
            ijk_set.add((i,j,k+1))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  
    Y = int(next(tokens))  
    Z = int(next(tokens))  
    K = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(X)]  
    B = [int(next(tokens)) for _ in range(Y)]  
    C = [int(next(tokens)) for _ in range(Z)]  
    solve(X, Y, Z, K, A, B, C)

if __name__ == '__main__':
    main()

Solution (Python):

import sys


def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    AB = []

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

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

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


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  
    Y = int(next(tokens))  
    Z = int(next(tokens))  
    K = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(X)]  
    B = [int(next(tokens)) for _ in range(Y)]  
    C = [int(next(tokens)) for _ in range(Z)]  
    solve(X, Y, Z, K, A, B, C)

if __name__ == '__main__':
    main()

Solution (Python):
tmp = list(map(int, input().split()))
x = tmp[0]
y = tmp[1]
z = tmp[2]
k = tmp[3]
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)

ansl = []
for p in range(x):
  if p > k:
    break
  for q in range(y):
    if p * q > k:
      break
    for r in range(z):
      if p * q * r > k:
        break
      ansl.append(a[p] + b[q] + c[r])

ansl.sort(reverse=True)

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

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heapify,heappop,heappush
import sys,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

x,y,z,k = inpl()
a = inpl()
b = inpl()
c = inpl()
ab = []
for i in a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse=True)
A = min(x*y,k)
res = []

for i in range(A):
    for j in range(z):
        res.append(ab[i]+c[j])
res.sort(reverse=True)
for i in range(k):
    print(res[i])

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

x,y,z,k = inpl()
a = inpl()
b = inpl()
c = inpl()
ab = []
for i in a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse=True)
abc = []
for i in range(min(k, len(ab))):
    for j in range(z):
        abc.append(ab[i] + c[j])
abc.sort(reverse=True)
for i in range(k):
    print(abc[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):
        if (i+1) * (j+1) > k:
            break
        for l in range(z):
            if (i+1) * (j+1) * (l+1) > k:
                break
            res.append(a[i]+b[j]+c[l])
res.sort(reverse=True)
for i in range(k):
    print(res[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):
        if (i+1) * (j+1) > k:
            break
        for l in range(z):
            if (i+1) * (j+1) * (l+1) > k:
                break
            res.append(a[i]+b[j]+c[l])
res.sort(reverse=True)
for i in range(k):
    print(res[i])

Solution (Python):
import heapq
x, y, z,  k = map(int, 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())

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

ab_1 = []
for i in a:
    for j in b:
        heapq.heappush(ab_1, -i-j)
key1 = min([k, x*y])
ab_2 = []
for i in range(key1):
    value = heapq.heappop(ab_1)
    ab_2.append(-value)

abc = []
for p in ab_2:
    for q in c:
        heapq.heappush(abc, -p-q)
key2 = min([k, x*y*z])
for i in range(key2):
    value = heapq.heappop(abc)
    print(-value)

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

al = list(map(int,input().split()))
bl = list(map(int,input().split()))
cl = list(map(int,input().split()))

al = sorted(al)
bl = sorted(bl)
cl = sorted(cl)
rank = []
flag = False
for xx in range(x-1,-1,-1):
    tmp = al[xx]+bl[-1]+cl[-1]
    if flag and rank[-1] >= tmp:
        break
    for yy in range(y-1, -1, -1):
        tmp = al[xx]+bl[yy]+cl[-1]
        if flag and rank[-1] >= tmp:
            break
        for zz in range(z-1,-1,-1):
            if len(rank) < k:
                rank.append(al[xx]+bl[yy]+cl[zz])
                rank = sorted(rank, reverse=True)
            else:
                tmp = al[xx]+bl[yy]+cl[zz]
                if rank[-1] < tmp:
                    flag = True
                    rank.pop()
                    rank.append(tmp)
                    rank = sorted(rank, reverse=True)
                else:
                    break

for i in rank:
    print(i)

Solution (Python):
import heapq
import collections

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

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

heap = []
heapq.heapify(heap)
heapq.heappush(heap,(-(A[0]+B[0]+C[0]),0,0,0))

dictionary = collections.defaultdict(bool)
dictionary[(0,0,0)] = True

i = 0

while i<k:
    M = heapq.heappop(heap)
    print(-M[0])
    i += 1
    if not dictionary[(M[1]+1,M[2],M[3])] and M[1]+1<x:
        heapq.heappush(heap,(-(A[M[1]+1]+B[M[2]]+C[M[3]]),M[1]+1,M[2],M[3]))
        dictionary[(M[1]+1,M[2],M[3])] = True
    if not dictionary[(M[1],M[2]+1,M[3])] and M[2]+1<y:
        heapq.heappush(heap,(-(A[M[1]]+B[M[2]+1]+C[M[3]]),M[1],M[2]+1,M[3]))
        dictionary[(M[1],M[2]+1,M[3])] = True
    if not dictionary[(M[1],M[2],M[3]+1)] and M[3]+1<z:
        heapq.heappush(heap,(-(A[M[1]]+B[M[2]]+C[M[3]+1]),M[1],M[2],M[3]+1))
        dictionary[(M[1],M[2],M[3]+1)] = True

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    x,y,z,K=map(int,input().split())
    A=sorted(map(int,input().split()),reverse=1)
    B=sorted(map(int,input().split()),reverse=1)
    C=sorted(map(int,input().split()),reverse=1)

    
    from itertools import product
    ind=[]
    for i in range(1,x+1):
        if(i>K): break
        for j in range(1,y+1):
            if(i*j>K): break
            for k in range(1,z+1):
                if(i*j*k>K): break
                ind.append((i-1,j-1,k-1))

    S=[]
    for i,j,k in ind:
        S.append(A[i]+B[j]+C[k])
    S.sort(reverse=1)
    for i in range(K):
        print(S[i])

resolve()

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
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()))
    S=[]
    T=[]
    from itertools import product
    for a,b in product(A,B):
        S.append(a+b)
    S.sort(reverse=1)
    S=S[:k]
    for s,c in product(S,C):
        T.append(s+c)
    T.sort(reverse=1)
    for i in range(k):
        print(T[i])
resolve()

Solution (Python):
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

x,y,z,k = MAP()
a = MAP()
b = MAP()
c = MAP()

a.sort()
b.sort()

l = []
for i in a:
    for j in b:
        l.append(i+j)

l.sort()
c.sort()
l2 = []
for i in l[-k:]:
    for j in c[-k:]:
        l2.append(i+j)

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

Solution (Python):
import sys
input = sys.stdin.readline
x,y,z,K=map(int,input().split())
listsa=list(map(int,input().split()))
listsb=list(map(int,input().split()))
listsc=list(map(int,input().split()))
if x*y>3000:
    suballlist=[]
    for i in listsa:
        for j in listsb:
            suballlist.append(i+j)
    suballlist=sorted(suballlist,reverse=True)
    uselist=suballlist[:3000]
    alllist=[]
    
    for i in uselist:
        for j in listsc:
            alllist.append(i+j)
            
    alllist=sorted(alllist,reverse=True)
    for i in range(K):
        print(alllist[i])
    
elif x*y<=3000:
    alllist=[]
    for i in listsa:
        for j in listsb:
            for k in listsc:
                alllist.append(i+j+k)
                
    alllist=sorted(alllist,reverse=True)
    for i in range(K):
        print(alllist[i])

Solution (Python):
import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines



import numpy as np




















def main():
	x,y,z,k=map(int,input().split())
	ABC=np.array(read().split(),np.int64)
	A=ABC[:x]
	B=ABC[x:-z]
	C=ABC[-z:]
	AB=np.add.outer(A,B)
	AB=AB.ravel()
	AB.sort()
	AB=AB[-k:]
	
	ans=np.add.outer(AB,C)
	ans=ans.ravel()
	ans.sort()
	
	for i in range(k):
		print(ans[-i-1])
	
if __name__ == '__main__':
	main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines



import numpy as np




















def main():
	x,y,z,k=map(int,input().split())
	ABC=np.array(read().split(),np.int64)
	A=ABC[:x]
	B=ABC[x:-z]
	C=ABC[-z:]
	AB=np.add.outer(A,B)
	AB=AB.ravel()
	AB.sort()
	AB=AB[-3000:]
	
	ans=np.add.outer(AB,C)
	ans=ans.ravel()
	ans.sort()
	
	for i in range(k):
		print(ans[-i-1])
	
if __name__ == '__main__':
	main()

Solution (Python):
from heapq import nlargest
x,y,z,k=map(int,input().split())
A,B,C=[list(map(int,input().split()))for _ in[0]*3]
D=nlargest(k,(a+b for a in A for b in B))
E=nlargest(k,(d+c for d in D for c in C))
for i in E:
    print(i)