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.



Sample input:
2 2 2 8
4 6
1 5
3 8


Sample output:
19
17
15
14
13
12
10
8

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split(" "))
A, B, C = [list(map(int, input().split(" "))) for _ in range(3)]
# X, Y, Z, K = 1000, 1000, 1000, 3000
# A, B, C = [[i*j for j in range(1000)] for i in range(3)]

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

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

abc_cakes = []
for ab in ab_cakes:
    for c in C:
        abc_cakes.append(ab + c)

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

for abc in abc_cakes:
    print(abc)

Submission status: Time Limit Exceeded

Solution code:
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 x in range(X):
    if x >= K:
        break
    for y in range(Y):
        if x + y >= K:
            break
        for z in range(Z):
            if x + y + z >= K:
                break

            value = A[x] + B[y] + C[z]
            ans.append(value)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

import heapq

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


        


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from heapq import *
import sys
input = sys.stdin.readline
x,y,z,K = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ans = []
lim = min(K,x*y*z)
cnt = 0
Q = [[-a[0]-b[0]-c[0],0,0,0]]
while cnt <= lim:
  heapify(Q)
  if Q == []:
    break
  M = heappop(Q)
  M[0] = -M[0]
  ans.append(M[0])
  i,j,k = M[1],M[2],M[3]
  if i+1 < x and Q.count([-a[i+1]-b[j]-c[k],i+1,j,k]) == 0:
    heappush(Q,[-a[i+1]-b[j]-c[k],i+1,j,k])
  if j+1 < y and Q.count([-a[i]-b[j+1]-c[k],i,j+1,k]) == 0:
    heappush(Q,[-a[i]-b[j+1]-c[k],i,j+1,k])
  if k+1 < z and Q.count([-a[i]-b[j]-c[k+1],i,j,k+1]) == 0:
    heappush(Q,[-a[i]-b[j]-c[k+1],i,j,k+1])
  cnt += 1
for i in ans[:lim]:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
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()))

W = sorted([x + y for x in B for y in C], reverse = True)
Z = sorted([x + y for x in A for y in W], reverse = True)

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
X, Y, Z, K = map(int, input().split())
A = deque(list(map(int, input().split())))
B = deque(list(map(int, input().split())))
C = deque(list(map(int, input().split())))

L = sorted(deque([x + y + z for x in A for y in B for z in C]), reverse = True)
for i in range(K):
  print (L[i])

Submission status: Time Limit Exceeded

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

L = sorted([x + y + z for x in A for y in B for z in C], reverse = True)
for i in range(K):
  print (L[i])

Submission status: Time Limit Exceeded

Solution code:

from collections import deque, defaultdict
import copy
import bisect
#sys.setrecursionlimit(10 ** 9)
import math
import heapq


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


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)

kouho = []
all_set = []
heapq.heappush(kouho, [-(A[0] + B[0] + C[0]), 0, 0, 0])
all_set.append([-(A[0] + B[0] + C[0]), 0, 0, 0])
for i in range(K):
	comb = heapq.heappop(kouho)
	print(-comb[0])
	if comb[1] < X - 1 and not [-(A[comb[1] + 1] + B[comb[2]] + C[comb[3]]), comb[1] + 1, comb[2], comb[3]] in all_set:
		heapq.heappush(kouho,  [-(A[comb[1] + 1] + B[comb[2]] + C[comb[3]]), comb[1] + 1, comb[2], comb[3]])
		all_set.append([-(A[comb[1] + 1] + B[comb[2]] + C[comb[3]]), comb[1] + 1, comb[2], comb[3]])
	if comb[2] < Y - 1 and not [-(A[comb[1]] + B[comb[2] + 1] + C[comb[3]]), comb[1], comb[2] + 1, comb[3]] in all_set:
		heapq.heappush(kouho,  [-(A[comb[1]] + B[comb[2] + 1] + C[comb[3]]), comb[1], comb[2] + 1, comb[3]])
		all_set.append([-(A[comb[1]] + B[comb[2] + 1] + C[comb[3]]), comb[1], comb[2] + 1, comb[3]])
	if comb[3] < Z - 1 and not [-(A[comb[1]] + B[comb[2]] + C[comb[3] + 1]), comb[1], comb[2], comb[3] + 1] in all_set:
		heapq.heappush(kouho,  [-(A[comb[1]] + B[comb[2]] + C[comb[3] + 1]), comb[1], comb[2], comb[3] + 1])
		all_set.append([-(A[comb[1]] + B[comb[2]] + C[comb[3] + 1]), comb[1], comb[2], comb[3] + 1])

Submission status: Time Limit Exceeded

Solution code:
import itertools

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 = [a + b for (a,b) in list(itertools.product(A,B))]
AB.sort(reverse = True)
ans = [ab + c for (ab,c) in list(itertools.product(AB[:min(3000,x*y*z)],C))]
ans.sort(reverse = True)
for i in range(k):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
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())) 
e=[]
d=[]
 
a.sort()
b.sort()
c.sort()
 
for i in range(1,x+1):
    for j in range(1,y+1):
        e.append(a[-i]+b[-j])
e.sort()

del e[:-k]
for i in range(1,len(e)+1):
    for j in range(1,z+1):
        d.append(e[-i]+c[-j])
        
d.sort()
for i in range(1,k+1):
    print(d[-i])

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
def f(A, B, k):
    S = [a+b for a, b in product(A, B)]
    S.sort(reverse = True)
    return S[:k]
x, y, z, k = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
S = f(f(A, B, k), C, k)
for s in S: print(s)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,K = map(int,input().split())
ans = 0
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)

ans = []
for i in range(x):
    for j in range(y):
        for k in range(z):
            if (i+1)*(j+1)*(k+1)<=K:
                ans.append(a[i]+b[j]+c[k])
ans.sort(reverse=True)
for i in range(K):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
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)

ans = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1)*(j+1)*(k+1) <= K: ans.append(A[i]+B[j]+C[k])
ans.sort(reverse=True)
for i in range(K): print(ans[i])

Submission status: Time Limit Exceeded

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

AB = AB[:K]
C.sort(reverse=True)
C = C[:K]
ABC = []
i = 0
for c in C:
    # if i >= K: break
    for ab in AB:
        ABC.append(ab+c)
        i += 1

ABC.sort(reverse=True)

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

Submission status: Time Limit Exceeded

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

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

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

ABC.sort(reverse=True)

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

Submission status: Time Limit Exceeded

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

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

AB = AB[:K]
C.sort(reverse=True)
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])

Submission status: Time Limit Exceeded

Solution code:
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 h in range(min(x,k)):
    for i in range(y):
        if h*i <= k:
            for j in range(z):
                if h*i*j <= k:
                    p.append(a[h]+b[i]+c[j])

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

Submission status: Time Limit Exceeded

Solution code:
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 a:
    for j in b:
        p.append(i+j)
p.sort(reverse=True)
p = p[:min(3001, x*y*z+1)]
q = []
for i in p:
    for j in c:
        q.append(i+j)
q.sort(reverse=True)
for i in range(k):
    print(q[i])

Submission status: Time Limit Exceeded

Solution code:
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)
p = []
for i in a:
    for j in b:
        p.append(i+j)
p.sort(reverse=True)
p = p[:min(3001, x*y*z+1)]
q = []
for i in p:
    for j in c:
        q.append(i+j)
q.sort(reverse=True)
for i in range(k):
    print(q[i])

Submission status: Time Limit Exceeded

Solution code:
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)
ans_list = []
for i in a:
    for j in b:
        ans_list.append(i+j)
ans_list.sort(reverse=True)
ans_list = ans_list[:min(3001, x*y*z+1)]
ans_list_2 = []
for i in ans_list:
    for j in c:
        ans_list_2.append(i+j)
ans_list_2.sort(reverse=True)
for i in range(k):
    print(ans_list_2[i])

Submission status: Time Limit Exceeded

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

ans_list = []
for i in a:
    for j in b:
        ans_list.append(i+j)
ans_list.sort(reverse=True)
ans_list = ans_list[:min(3001, x*y*z+1)]
ans_list_2 = []
for i in ans_list:
    for j in c:
        ans_list_2.append(i+j)
ans_list_2.sort(reverse=True)
for i in range(k):
    print(ans_list_2[i])

Submission status: Time Limit Exceeded

Solution code:
from heapq import heapify,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)
ans = []
lim = min(K,x*y*z)
cnt = 0
Q = [[-a[0]-b[0]-c[0],0,0,0]]
while cnt <= lim:
  heapify(Q)
  if Q == []:
    break
  M = heappop(Q)
  M[0] = -M[0]
  ans.append(M[0])
  i,j,k = M[1],M[2],M[3]
  if i+1 < x and i+1 < K and Q.count([-a[i+1]-b[j]-c[k],i+1,j,k]) == 0:
    heappush(Q,[-a[i+1]-b[j]-c[k],i+1,j,k])
  if j+1 < y and j+1 < K and Q.count([-a[i]-b[j+1]-c[k],i,j+1,k]) == 0:
    heappush(Q,[-a[i]-b[j+1]-c[k],i,j+1,k])
  if k+1 < z and k+1 < K and Q.count([-a[i]-b[j]-c[k+1],i,j,k+1]) == 0:
    heappush(Q,[-a[i]-b[j]-c[k+1],i,j,k+1])
  cnt += 1
for i in ans[:lim]:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
from heapq import *
x,y,z,K = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ans = []
lim = min(K,x*y*z)
cnt = 0
Q = [[-a[0]-b[0]-c[0],0,0,0]]
while cnt <= lim:
  heapify(Q)
  if Q == []:
    break
  M = heappop(Q)
  M[0] = -M[0]
  ans.append(M[0])
  i,j,k = M[1],M[2],M[3]
  if i+1 < x and Q.count([-a[i+1]-b[j]-c[k],i+1,j,k]) == 0:
    heappush(Q,[-a[i+1]-b[j]-c[k],i+1,j,k])
  if j+1 < y and Q.count([-a[i]-b[j+1]-c[k],i,j+1,k]) == 0:
    heappush(Q,[-a[i]-b[j+1]-c[k],i,j+1,k])
  if k+1 < z and Q.count([-a[i]-b[j]-c[k+1],i,j,k+1]) == 0:
    heappush(Q,[-a[i]-b[j]-c[k+1],i,j,k+1])
  cnt += 1
for i in ans[:lim]:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
from heapq import *
import numpy
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)
ans = []
check = numpy.array([[[0]*K]*K]*K)
check[0][0][0] = 1
lim = min(K,x*y*z)
cnt = 0
Q = [[-a[0]-b[0]-c[0],0,0,0]]
while cnt <= lim:
  heapify(Q)
  if Q == []:
    break
  M = heappop(Q)
  M[0] = -M[0]
  ans.append(M[0])
  i,j,k = M[1],M[2],M[3]
  if i+1 < x and i+1 < K and check[i+1][j][k] != 1:
    heappush(Q,[-a[i+1]-b[j]-c[k],i+1,j,k])
    check[i+1][j][k] = 1
  if j+1 < y and j+1 < K and check[i][j+1][k] != 1:
    heappush(Q,[-a[i]-b[j+1]-c[k],i,j+1,k])
    check[i][j+1][k] = 1
  if k+1 < z and k+1 < K and check[i][j][k+1] != 1:
    heappush(Q,[-a[i]-b[j]-c[k+1],i,j,k+1])
    check[i][j][k+1] = 1
  cnt += 1
for i in ans[:lim]:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
from heapq import *
import numpy
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)
ans = []
check = numpy.array([[[0]*x]*y]*z)
check[0][0][0] = 1
lim = min(K,x*y*z)
cnt = 0
Q = [[-a[0]-b[0]-c[0],0,0,0]]
while cnt <= lim:
  heapify(Q)
  if Q == []:
    break
  M = heappop(Q)
  M[0] = -M[0]
  ans.append(M[0])
  i,j,k = M[1],M[2],M[3]
  if i+1 < x and check[i+1][j][k] != 1:
    heappush(Q,[-a[i+1]-b[j]-c[k],i+1,j,k])
    check[i+1][j][k] = 1
  if j+1 < y and check[i][j+1][k] != 1:
    heappush(Q,[-a[i]-b[j+1]-c[k],i,j+1,k])
    check[i][j+1][k] = 1
  if k+1 < z and check[i][j][k+1] != 1:
    heappush(Q,[-a[i]-b[j]-c[k+1],i,j,k+1])
    check[i][j][k+1] = 1
  cnt += 1
for i in ans[:lim]:
  print(i)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
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]
lim = min(k,x*y)
ab.sort(reverse=True)
abc = [0]*lim*z
for i in range(lim):
  for j in range(z):
    abc[i*z+j] = ab[i] + c[j]
abc.sort(reverse=True)
lim = min(x*y*z,k)

for i in abc[0:lim]:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
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())),reverse=True)
ab=[]
for i in a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse=True)
ans=[]
for i in range(len(ab)):
    for j in range(len(c)):
        ans.append(ab[i]+c[j])
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Time Limit Exceeded

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

A = sorted(A, reverse = True)
B = sorted(B, reverse = True)
C = sorted(C, reverse = True)

ans = []

for i in range(min(K,X)):
  for j in range(min(K,Y)):
    for k in range(min(K,Z)):
      ans.append(A[i]+B[j]+C[k])
    
ans = sorted(ans, reverse=True)

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

Submission status: Time Limit Exceeded

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

C = sorted(C, reverse = True)

AB=[]

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

ABl = len(AB)
for i in range(ABl):
  for j in range(min(K,Z)):
    ans.append(AB[i]+C[j])

ans = sorted(ans, reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
x, y, z, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ab = []
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort(reverse=True)
abc = []
for i in range(min(x*y, 3000)):
    for j in range(z):
        abc.append(ab[i]+c[j])
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K = (int(i) for i in input().split())  
A = [int(i) for i in input().split()] 
B = [int(i) for i in input().split()] 
C = [int(i) for i in input().split()] 

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

ans = []
for a in A:
	for b in B:
		for c in C:
			ans.append(a+b+c)

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

Submission status: Time Limit Exceeded

Solution code:
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)
ans = []
for i in range(X):
	for j in range(Y):
		for k in range(Z):
			if ((i+1)*(j+1)*(k+1) <= K):
				ans.append(A[i]+B[j]+C[k])

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

Submission status: Time Limit Exceeded

Solution code:
import heapq
x, y, z, k = [ int(v) for v in input().split() ]
x_list = sorted([ int(v) for v in input().split() ], reverse = True)
y_list = sorted([ int(v) for v in input().split() ], reverse = True)
z_list = sorted([ int(v) for v in input().split() ], reverse = True)
check_list = [ [ [ False for i in range(x) ] for j in range(y) ]  for l in range(z)]
check_list_x, check_list_y, check_list_z = [ 0 for i in range(x) ], [ 0 for i in range(y) ], [ 0 for i in range(z) ]
cake_list = []

def maling_cake(a,b,c):
    d = x_list[a]+y_list[b]+z_list[c]
    return (-d,a,b,c)

for i in range(k):
    if i == 0:
        print(-maling_cake(0,0,0)[0])
        check_list[0][0][0] = 1
        if x > 1: 
            heapq.heappush(cake_list, maling_cake(1,0,0))
            check_list[1][0][0] = 1
        if y > 1: 
            heapq.heappush(cake_list, maling_cake(0,1,0))
            check_list[0][1][0] = 1
        if z > 1: 
            heapq.heappush(cake_list, maling_cake(0,0,1))
            check_list[0][0][1] = 1
        
    else:
        di, ai, bi, ci = heapq.heappop(cake_list)
        if ai < x - 1:
            if check_list[ai+1][bi][ci] == 0:
                heapq.heappush(cake_list, maling_cake(ai+1,bi,ci))
                check_list[ai+1][bi][ci] = 1
        if bi < y - 1:
            if check_list[ai][bi+1][ci] == 0:
                heapq.heappush(cake_list, maling_cake(ai,bi+1,ci))
                check_list[ai][bi+1][ci] = 1
        if ci < z - 1:
            if check_list[ai][bi][ci+1] == 0:
                heapq.heappush(cake_list, maling_cake(ai,bi,ci+1))
                check_list[ai][bi][ci+1] = 1

        print(-di)

Submission status: Time Limit Exceeded

Solution code:
import heapq
x, y, z, k = [ int(v) for v in input().split() ]
x_list = sorted([ int(v) for v in input().split() ], reverse = True)
y_list = sorted([ int(v) for v in input().split() ], reverse = True)
z_list = sorted([ int(v) for v in input().split() ], reverse = True)
check_list = [ [ [ False for i in range(x) ] for j in range(y) ]  for l in range(z)]
cake_list = []
def maling_cake(a,b,c):
    d = x_list[a]+y_list[b]+z_list[c]
    return (-d,a,b,c)


for i in range(k):
    if i == 0:
        print(-maling_cake(0,0,0)[0])
        check_list[0][0][0] = True
        if x > 1: 
            heapq.heappush(cake_list, maling_cake(1,0,0))
            check_list[1][0][0] = True
        if y > 1: 
            heapq.heappush(cake_list, maling_cake(0,1,0))
            check_list[0][1][0] = True
        if z > 1: 
            heapq.heappush(cake_list, maling_cake(0,0,1))
            check_list[0][0][1] = True
        
    else:
        di, ai, bi, ci = heapq.heappop(cake_list)
        if ai < x - 1:
            if check_list[ai+1][bi][ci] == False:
                heapq.heappush(cake_list, maling_cake(ai+1,bi,ci))
                check_list[ai+1][bi][ci] = True
        if bi < y - 1:
            if check_list[ai][bi+1][ci] == False:
                heapq.heappush(cake_list, maling_cake(ai,bi+1,ci))
                check_list[ai][bi+1][ci] = True
        if ci < z - 1:
            if check_list[ai][bi][ci+1] == False:
                heapq.heappush(cake_list, maling_cake(ai,bi,ci+1))
                check_list[ai][bi][ci+1] = True

        print(-di)

Submission status: Time Limit Exceeded

Solution code:

import heapq
x, y, z, k = [ int(v) for v in input().split() ]
x_list = sorted([ int(v) for v in input().split() ], reverse = True)
y_list = sorted([ int(v) for v in input().split() ], reverse = True)
z_list = sorted([ int(v) for v in input().split() ], reverse = True)
check_list = [ [ [ False for i in range(x) ] for j in range(y) ]  for l in range(z)]
cake_list = []
def maling_cake(a,b,c):
    d = x_list[a]+y_list[b]+z_list[c]
    return (-d,a,b,c)


for i in range(k):
    if i == 0:
        print(-maling_cake(0,0,0)[0])
        check_list[0][0][0] = True
        if x > 1: 
            heapq.heappush(cake_list, maling_cake(1,0,0))
            check_list[1][0][0] = True
        if y > 1: 
            heapq.heappush(cake_list, maling_cake(0,1,0))
            check_list[0][1][0] = True
        if z > 1: 
            heapq.heappush(cake_list, maling_cake(0,0,1))
            check_list[0][0][1] = True
        
    else:
        di, ai, bi, ci = heapq.heappop(cake_list)
        if ai < x - 1:
            if check_list[ai+1][bi][ci] == False:
                heapq.heappush(cake_list, maling_cake(ai+1,bi,ci))
                check_list[ai+1][bi][ci] = True
        if bi < y - 1:
            if check_list[ai][bi+1][ci] == False:
                heapq.heappush(cake_list, maling_cake(ai,bi+1,ci))
                check_list[ai][bi+1][ci] = True
        if ci < z - 1:
            if check_list[ai][bi][ci+1] == False:
                heapq.heappush(cake_list, maling_cake(ai,bi,ci+1))
                check_list[ai][bi][ci+1] = True

        print(-di)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = map(int,input().split())
lia = list(map(int,input().split()))
lib = list(map(int,input().split()))
lic = list(map(int,input().split()))

lia.sort(reverse = True)
lib.sort(reverse = True)
lic.sort(reverse = True)

li_add = []
if k >= 15:
    lia = lia[0:k]
    lib = lib[0:k]
    lic = lic[0:k]

for i in range(len(lia)):
    for j in range(len(lib)):
        for h in range(len(lic)):
            li_add.append(lia[i] + lib[j] + lic[h])
li_add.sort(reverse = True)
for i in range(k):
    print(li_add[i])

Submission status: Time Limit Exceeded

Solution code:

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

Q = []
topK = []
dic = {}

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

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

Q.append([X-1,Y-1,Z-1])

while len(Q) > 0:

    now = Q.pop(0)

    if len(topK) < K:
        
        if ( now[0] , now[1] , now[2]) not in dic:
            dic[ ( now[0] , now[1] , now[2]) ] = 1
            topK.append( A[now[0]] + B[now[1]] + C[now[2]] )
            topK.sort()

        if now[0] != 0 : Q.append([ now[0]-1 , now[1] , now[2] ])
        if now[1] != 0 : Q.append([ now[0] , now[1]-1 , now[2] ])
        if now[2] != 0 : Q.append([ now[0] , now[1] , now[2]-1 ])    

    elif topK[0] < A[now[0]] + B[now[1]] + C[now[2]]:

        if ( now[0] , now[1] , now[2] ) not in dic:

            dic[ (now[0] , now[1] , now[2])] = 1
            #topK.append( A[now[0]] + B[now[1]] + C[now[2]] )
            del topK[0]
            topK.append(A[now[0]] + B[now[1]] + C[now[2]])
            topK.sort()

        if now[0] != 0 : Q.append([ now[0]-1 , now[1] , now[2] ])
        if now[1] != 0 : Q.append([ now[0] , now[1]-1 , now[2] ])
        if now[2] != 0 : Q.append([ now[0] , now[1] , now[2]-1 ])          

topK.sort()
#print (dic)

for i in range(K):

    print (topK[-1-i])

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
a_cakes = list(map(int, input().split()))
b_cakes = list(map(int, input().split()))
c_cakes = list(map(int, input().split()))
a_b_cakes = []
for a in a_cakes:
  for b in b_cakes:
    a_b_cakes.append(a+b)
a_b_cakes.sort(reverse=True)
a_b_c_cakes = []
for a_b in a_b_cakes[:K]:
  for c in c_cakes:
    a_b_c_cakes.append(a_b+c)
a_b_c_cakes.sort(reverse=True)
for ans in a_b_c_cakes[:K]:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
AiList = list(map(int, input().split()))
BiList = list(map(int, input().split()))
CiList = list(map(int, input().split()))
# print("---------")
# print(X, Y, Z, K)
# print(AiList)
# print(BiList)
# print(CiList)

resultList = []
for x in range(0, X):
    for y in range(0, Y):
        for z in range(0, Z):
            resultList.append(AiList[x] + BiList[y] + CiList[z])
resultList.sort(reverse=True)
for i in range(0, K):
    print(resultList[i])