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:
import itertools

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

#a=[int(i) for i in range(1000,-1,-1)]
#b=[int(i) for i in range(1000,-1,-1)]
#c=[int(i) for i in range(1000,-1,-1)]
x=list(itertools.product(a,b))
for i in range(len(x)):
  x[i]=x[i][0]+x[i][1]
x.sort(reverse=True)
y=[]
length=min(len(x),3000)
for i in range(length):
  y+=[x[i]]
#print(len(y))
y=list(itertools.product(y,c))
#print(y)
for i in range(len(y)):
  y[i]=y[i][0]+y[i][1]
y.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = map(int,input().split())
li = [list(map(int,input().split())) for _ in range(3)]
li2 = []
li3 = []

for i in range(x):
    for j in range(y):
        li2.append(li[0][i]+li[1][j])
li2.sort(reverse = True)
li2[:min(k,len(li2))]

for i in range(z):
    for j in range(min(k,len(li2))):
        li3.append(li[2][i]+li2[j])
li3.sort(reverse = True)

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

Submission status: Time Limit Exceeded

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
#
import math
import sys


# input
X,Y,Z,K = map(int, input().split())
A = list(map(int, sys.stdin.readline().rstrip().split()))
B = list(map(int, sys.stdin.readline().rstrip().split()))
C = list(map(int, sys.stdin.readline().rstrip().split()))

# solve
AB = [0 for i in range(X*Y)]
i = 0
for a in A:
  for b in B:
    AB[i] = a+b
    i += 1
AB = sorted(AB, key=lambda x:-x)[:K]

ABC = [0 for i in range(len(AB)*Z)]
i = 0
for ab in AB:
  for c in C:
    ABC[i] = ab+c
    i += 1
ABC = sorted(ABC, key=lambda x:-x)[:K]

for ans in ABC:
  print(ans)

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

AA = []
BB = []
CC = []
count = 0

for k in range(X):
	maxA = max(A)
	A.remove(maxA)
	AA.append(maxA)
for k in range(Y):
	maxB = max(B)
	B.remove(max(B))
	BB.append(maxB)
for k in range(Z):
	maxC = max(C)
	C.remove(maxC)
	CC.append(maxC)

#print(AA)
#print()
#print(BB)
#print()
#print(CC)


indexlist  = []
for k in range(min(X, K)):
	for l in range(min(Y, K)):
		for m in range(min(Z, K)):
			indexlist.append([k, l, m])
#print(indexlist)


dictionary = {}
for P in indexlist:
	p = AA[P[0]] + BB[P[1]] + CC[P[2]]
	dictionary.update({str(P[0]) +" , " + str(P[1]) +" , " + str(P[2]): p})

#print(dictionary)


Ans = sorted(dictionary.values())
for k in range(K):
	print(Ans[-k-1])

Submission status: Time Limit Exceeded

Solution code:
import heapq,sys


def main():
    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+Bでのベスト枠K個求めるO(XYlogK)
    AandB = []
    for x in A:
        for y in B:
            heapq.heappush(AandB,x+y)
            #要素がK+1個以上なら一々はじく！！！ことでO(logK)
            if len(AandB) > K:
                heapq.heappop(AandB)
    #上のとCでのベスト枠K個を求める(ZKlogK)
    heap_queue = []
    for z in C:
        for elt in AandB:
            heapq.heappush(heap_queue,z+elt)
            if len(heap_queue) > K:
                heapq.heappop(heap_queue)
    heap_queue.sort(reverse = True)
    for e in heap_queue:
        print(e)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import heapq,sys


def main():
    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+Bでのベスト枠K個求めるO(XYlogK)
    AandB = []
    for i in range(X):
        for j in range(Y):
            heapq.heappush(AandB, A[i] + B[j])
            #要素がK+1個以上なら一々はじく！！！ことでO(logK)
            if len(AandB) > K:
                heapq.heappop(AandB)
    #上のとCでのベスト枠K個を求める(ZKlogK)
    heap_queue = []
    for p in range(Z):
        for h in range(len(AandB)):
            heapq.heappush(heap_queue,C[p] + AandB[h])
            if len(heap_queue) > K:
                heapq.heappop(heap_queue)
    heap_queue.sort(reverse = True)
    for e in heap_queue:
        print(e)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import heapq


def heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap) - 1)


def heappop_max(heap):
    lastelt = heap.pop()
    if heap:
        return_item = heap[0]
        heap[0] = lastelt
        heapq._siftup_max(heap, 0)
        return return_item
    return lastelt


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)
ans = []
heap_queue = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            heappush_max(heap_queue, A[i] + B[j] + C[k])
for k in range(K):
    print(heappop_max(heap_queue))

Submission status: Time Limit Exceeded

Solution code:
great_list = []
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)

for i in range(min(k,len(a))):
	for j in range(min(k,len(b))):
		for l in range(min(k,len(c))):
			great_list.append(a[i] + b[j] + c[l])

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

mylist=[]
for h in range(len(a)):
  for i in range(len(b)):
    for j in range(len(c)):
      if (h+1)*(i+1)*(j+1) <= k:
        mylist.append(a[h]+b[i]+c[j])

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

Submission status: Time Limit Exceeded

Solution code:
import heapq,sys


def main():
    input = sys.stdin.readline
    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)
    ans = []
    heap_queue = []
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                heapq.heappush(heap_queue, A[i] + B[j] + C[k])
                #要素がK個以上ならはじく！！！ことでO(logK)
                if len(heap_queue) > K:
                    heapq.heappop(heap_queue)
    heap_queue.sort(reverse = True)
    for e in heap_queue:
        print(e)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import heapq,sys



def heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap) - 1)


def heappop_max(heap):
    lastelt = heap.pop()
    if heap:
        return_item = heap[0]
        heap[0] = lastelt
        heapq._siftup_max(heap, 0)
        return return_item
    return lastelt

def main():
    input = sys.stdin.readline
    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)
    ans = []
    heap_queue = []
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                heappush_max(heap_queue, A[i] + B[j] + C[k])
    for k in range(K):
        print(heappop_max(heap_queue))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

ab=list(itertools.product(a,b))
ab.sort(reverse=True)
sumab=[0 for i in range(3010)]
#print(ab)

mynum=min(len(ab),3010)
for i in range(mynum):
  sumab[i]=ab[i][0]+ab[i][1]
  
abc=list(itertools.product(sumab,c))
#abc.sort(reverse=True)
sumabc=[0 for i in range(len(abc))]
for i in range(len(abc)):
  sumabc[i]=abc[i][0]+abc[i][1]
  
sumabc.sort(reverse=True)

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

Submission status: Time Limit Exceeded

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

    import itertools
    D = sorted([a+b for a, b in itertools.product(A, B)], reverse=True)
    D = D[:K]

    E = sorted([a+b for a, b in itertools.product(C, D)], reverse=True)
    E = E[:K]
    for x in E:
        print(x)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
_,_,_,k = list(map(int,input().split(' ')))
ds = [list(map(int,input().split(' '))) for _ in range(3)]

xys = []

for y in ds[0]:
  for x in ds[1]:
    xys.append(x + y)
xys = sorted(xys)[::-1][:k]

xyzs = []
for xy in xys:
  for z in ds[2]:
    xyzs.append(xy + z)

ans = sorted(xyzs)[::-1][:k]

for d in ans:
  print(d)

Submission status: Time Limit Exceeded

Solution code:
_,_,_,k = list(map(int,input().split(' ')))
ds = [list(map(int,input().split(' '))) for _ in range(3)]

xys = sum([[x + y for x in ds[0]] for y in ds[1]],[])
xys = sorted(xys)[::-1][:k]

#print(xys)

xyzs = sum([[xy + z for xy in xys] for z in ds[2]],[])


#print(xyzs)

ans = sorted(xyzs)[::-1][:k]

for d in ans:
  print(d)

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K = map(int, input().split())
a_list = [int(i) for i in input().split()]
b_list = [int(i) for i in input().split()]
c_list = [int(i) for i in input().split()]
a_list.sort(reverse = True)
b_list.sort(reverse = True)
c_list.sort(reverse = True)
total_list = [0 for _ in range(X*Y*Z)]
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1)*(j+1)*(k+1) <= K:
                total_list[i*(Z*Y)+j*Z+k] = (a_list[i] + b_list[j] + c_list[k])
            else:
                break
total_list =  sorted(total_list, reverse = True)
for i in range(K):
    print(total_list[i])

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = [int(i) for i in input().split()]
ABCList = [sorted([int(i) for i in input().split()], reverse=True) for _ in range(3)]

ABSum = sorted([i + j for i in ABCList[0] for j in ABCList[1]], reverse=True)[:K]
for ans in sorted([i + j for i in ABSum for j in ABCList[2]], reverse=True)[:K]:
    print(ans)

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_B = []

for i in range(X):
	for j in range(Y):
		ans = a[i] + b[j]
		A_B.append(ans)
		

A_B_up = sorted(A_B, reverse=True)

A_B_up1 = A_B_up[0:K]

A_B_count = len(A_B_up1)


A_B_rank = []

for i in range(A_B_count):
	for j in range(Z):
		ans1 = A_B_up1[i] + c[j]
		A_B_rank.append(ans1)


A_B_rank1 = sorted(A_B_rank, reverse=True)

for i in range(K):
	print(A_B_rank1[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_B = []

for i in range(X):
	for j in range(Y):
		ans = a[i] + b[j]
		A_B.append(ans)
		

A_B_up = sorted(A_B, reverse=True)

A_B_count = len(A_B_up)

A_B_rank = []

for i in range(A_B_count):
	for j in range(Z):
		ans1 = A_B_up[i] + c[j]
		A_B_rank.append(ans1)

A_B_rank1 = sorted(A_B_rank, reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
# from collections import deque 

X, Y, Z, K = map(int, input().split())
aa = [int(s) for s in input().split()]
ba = [int(s) for s in input().split()]
ca = [int(s) for s in input().split()]

# sum_ab_a = deque()
# for a in aa:
#     for b in ba:
#         sum_ab_a.append(a+b)

aa.sort(reverse=True)
ba.sort(reverse=True)

aa = aa[0:K]
ba = ba[0:K]

sum_ab_a = [a+b for a in aa for b in ba]

sum_ab_a.sort(reverse=True)
sum_ab_a = sum_ab_a[0:K]

# ans = deque()
# for n in sum_ab_a:
#     for c in ca:
#         ans.append(n+c)

ans = [n+c for n in sum_ab_a for c in ca]

ans = sorted(ans, reverse=True)
for i in range(K):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
# from collections import deque 

X, Y, Z, K = map(int, input().split())
aa = [int(s) for s in input().split()]
ba = [int(s) for s in input().split()]
ca = [int(s) for s in input().split()]

# sum_ab_a = deque()
# for a in aa:
#     for b in ba:
#         sum_ab_a.append(a+b)

aa = aa[0:K]
ba = ba[0:K]

sum_ab_a = [a+b for a in aa for b in ba]

sum_ab_a = sorted(sum_ab_a, reverse=True)
sum_ab_a = sum_ab_a[0:K]

# ans = deque()
# for n in sum_ab_a:
#     for c in ca:
#         ans.append(n+c)

ans = [n+c for n in sum_ab_a for c in ca]

ans = sorted(ans, reverse=True)
for i in range(K):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

ab=list(itertools.product(a,b))
sumab=[0 for i in range(len(ab))]
#print(ab)

for i in range(len(ab)):
  sumab[i]=ab[i][0]+ab[i][1]
  
abc=list(itertools.product(sumab,c))
abc.sort(reverse=True)
sumabc=[0 for i in range(len(abc))]
for i in range(len(abc)):
  sumabc[i]=abc[i][0]+abc[i][1]
  
sumabc.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop

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

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

A = 10 ** 9
B = 10 ** 6
C = 10 ** 3

ans = 0

h = []

s = [[[0 for _ in range(z+1)] for _ in range(y+1)] for _ in range(x+1)]

t = a[0] + b[0] + c[0]

s[0][0][0] = 1

heappush(h, - t * A)

for _ in range(k):
    temp = - heappop(h)
    r = temp % C
    temp //= C
    q = temp % C
    temp //= C
    p = temp % C
    print(temp // C)
    if p < x-1 and s[p+1][q][r] == 0:
        heappush(h, - ((a[p+1] + b[q] + c[r]) * A + (p+1) * B + q * C + r))
        s[p + 1][q][r] = 1
    if q < y-1 and s[p][q+1][r] == 0:
        heappush(h, - ((a[p] + b[q+1] + c[r]) * A + p * B + (q+1) * C + r))
        s[p][q + 1][r] = 1
    if r < z-1 and s[p][q][r+1] == 0:
        heappush(h, - ((a[p] + b[q] + c[r+1]) * A + p * B + q * C + r + 1))
        s[p][q][r + 1] = 1

Submission status: Time Limit Exceeded

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

A = sorted(map(int, input().split(" ")),reverse=True)
B = sorted(map(int, input().split(" ")),reverse=True)
C = sorted(map(int, input().split(" ")),reverse=True)

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

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 s in a:
    for t in b:
       ab.append(s + t)

ab.sort(reverse=True)
c.sort(reverse=True)

ans = []

for i in range(min(k, len(ab))):
    for u in c:
        ans.append(ab[i] + u)

ans.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

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)

ans = sorted([ab + c for ab in AB[:k] for c in C],reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

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

A = sorted(map(int,input().split()))[::-1]
B = sorted(map(int,input().split()))[::-1]
C = sorted(map(int,input().split()))[::-1]

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

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

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

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = map(int,input().split())
 
A = sorted(map(int,input().split()))[::-1]
B = sorted(map(int,input().split()))[::-1]
C = sorted(map(int,input().split()))[::-1]
 
AB = sorted([a+b for a in A for b in B])[::-1]
 
ans = sorted([ab + c for ab in AB[:k] for c in C])[::-1]
 
for i in ans[:k]:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

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

A = sorted(map(int,input().split()))[::-1]
B = sorted(map(int,input().split()))[::-1]
C = sorted(map(int,input().split()))[::-1]

AB = sorted([a+b for a in A for b in B])[::-1]

ans = sorted([ab + c for ab in AB for c in C])[::-1]

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

Submission status: Time Limit Exceeded

Solution code:
def main():
  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)
  res = []
  for i in range(X):
    for j in range(Y):
      for k in range(Z):
        if (i+1)*(j+1)*(k+1) <= K:
          res.append(A[i]+B[j]+C[k])
  res.sort(reverse = True)
  for r in res[:K]:
    print(r)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

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

  AB = [a+b for a in A for b in B]
  AB.sort(reverse = True)
  res = [c+ab for ab in AB[:min(K, len(AB))] for c in C]
  res.sort(reverse = True)
  for r in res[:K]:
    print(r)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

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

  AB = [a+b for a in A for b in B]
  AB.sort(reverse = True)
  res = [c+ab for ab in AB[:K] for c in C]
  res.sort(reverse = True)
  for r in res[:K]:
    print(r)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

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

BC = [i+j for i,j in list(product(B,C))]
BC.sort(reverse=True)

ans = [i+j for i,j in list(product(BC[:min(3000, X*Y*Z)], A))]
ans.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

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

BC = [i+j for i,j in product(B,C)]
BC.sort(reverse=True)

ans = [i+j for i,j in product(BC[:min(3000, X*Y*Z)], A)]
ans.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
import heapq
import copy

def inv(l):
    return list(map(lambda x: x*-1, l))

def solve(A,B,C, K):
    A,B,C = inv(A), inv(B), inv(C)
    A = sorted(A)
    B = sorted(B)
    C = sorted(C)
    ind = set([(0,0,0)])

    h = [(A[0]+ B[0]+ C[0], (0,0,0))]
    res = []

    for _ in range(K):
        h2 = copy.copy(h)
        for (val, (i,j,k)) in h2:
            if i+1 < len(A) and (i+1,j,k) not in ind:
                heapq.heappush(h, (A[i+1]+B[j]+C[k], (i+1, j, k)))
                ind |= set([(i+1,j,k)])
            if j+1 < len(B) and (i,j+1, k) not in ind:
                heapq.heappush(h, (A[i]+B[j+1]+C[k], (i, j+1, k)))
                ind |= set([(i,j+1,k)])
            if k+1 < len(C) and (i,j, k+1) not in ind:
                heapq.heappush(h, (A[i]+B[j]+C[k+1], (i, j, k+1)))
                ind |= set([(i,j,k+1)])
        res += [heapq.heappop(h)]
    for r in res:
        print(r[0]*-1)

la,lb,lc,K = list(map(int, input().split(' ')))
A = list(map(int, input().split(' ')))
B = list(map(int, input().split(' ')))
C = list(map(int, input().split(' ')))

solve(A,B,C,K)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

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)

candidates = []
for i, j, k in product(range(len(A)), range(len(B)), range(len(C))):
    if i * j * k <= K:
        candidates.append(A[i] + B[j] + C[k])
candidates.sort(reverse=True)

for c in candidates[:K]:
    print(c)

Submission status: Time Limit Exceeded

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

list_cake_1 = list(map(int, input().split()))
list_cake_2 = list(map(int, input().split()))
list_cake_3 = list(map(int, input().split()))

list_cake_1 = sorted(list_cake_1, reverse=True)
list_cake_2 = sorted(list_cake_2, reverse=True)
list_cake_3 = sorted(list_cake_3, reverse=True)


list_total_sum = [cake_1 + cake_2 + cake_3 for i, cake_1 in enumerate(list_cake_1, 1) for j, cake_2 in enumerate(list_cake_2, 1) for k, cake_3 in enumerate(list_cake_3, 1) if i*j*k <= K]

for element in sorted(list_total_sum, reverse=True)[:K]:
	print(element)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
 
X, Y, Z, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
 
AB = [a + b for a, b in product(A, B)]
AB.sort(reverse=True)
 
ABC = [ab + c for ab, c in product(AB[:min(3000, X*Y*Z)], C)]
ABC.sort(reverse=True)
 
for abc in ABC[:K]:
    print(abc)

Submission status: Time Limit Exceeded

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

list_cake_1 = list(map(int, input().split()))
list_cake_2 = list(map(int, input().split()))
list_cake_3 = list(map(int, input().split()))

list_cake_1 = sorted(list_cake_1, reverse=True)
list_cake_2 = sorted(list_cake_2, reverse=True)
list_cake_3 = sorted(list_cake_3, reverse=True)


list_total_sum = [list_cake_1[i]+list_cake_2[j]+list_cake_3[k] for i in range(X) for j in range(Y) for k in range(Z) if (i+1)*(j+1)*(k+1) <= K]

list_total_sum = sorted(list_total_sum, reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

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

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

ABC = [ab + c for ab, c in product(AB[:K], C)]
ABC.sort(reverse=True)

for abc in ABC[:K]:
    print(abc)

Submission status: Time Limit Exceeded

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

list_cake_1 = list(map(int, input().split()))
list_cake_2 = list(map(int, input().split()))
list_cake_3 = list(map(int, input().split()))

list_cake_1.sort()
list_cake_2.sort()
list_cake_3.sort()


list_total_sum = [list_cake_1[-(i+1)]+list_cake_2[-(j+1)]+list_cake_3[-(k+1)] for i in range(X) for j in range(Y) for k in range(Z) if (i+1)*(j+1)*(k+1) <= K]


list_total_sum.sort()

for i in range(K):
	print(list_total_sum[-(i+1)])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys

def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    l = [0]*X*Y
    for i in range(X):
        for j in range(Y):
            l[i+j*X] = A[i]+B[j]

    l.sort(reverse=True)
    t = min(3000,X*Y)
    l = l[:t]

    ans = [0] * (t) * Z
    for i in range(t):
        for j in range(Z):
            ans[i+j*t] = l[i] + C[j]

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


    return


# Generated by 1.1.3 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    Y = int(next(tokens))  # type: int
    Z = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(X) ]  # type: "List[int]"
    B = [ int(next(tokens)) for _ in range(Y) ]  # type: "List[int]"
    C = [ int(next(tokens)) for _ in range(Z) ]  # type: "List[int]"
    solve(X, Y, Z, K, A, B, C)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

list_cake_1 = []
list_cake_2 = []
list_cake_3 = []

while len(list_cake_1) < X:
	element = list(map(int, input().split()))
	list_cake_1.extend(element)

while len(list_cake_2) < Y:
	element = list(map(int, input().split()))
	list_cake_2.extend(element)
	
while len(list_cake_3) < Z:
	element = list(map(int, input().split()))
	list_cake_3.extend(element)

list_cake_12 = []

for i in list_cake_1:
	for j in list_cake_2:
		list_cake_12.append(i+j)

list_cake_12.sort()
list_cake_3.sort()

list_cake_123 = []

min_number = min([X*Y, K])

for i in range(1, min_number+1):
	for j in range(Z):
		list_cake_123.append(list_cake_12[-i]+list_cake_3[j])

list_cake_123.sort()

for i in range(1, K+1):
	print(list_cake_123[-i])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys

def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    l = [0]*X*Y
    for i in range(X):
        for j in range(Y):
            l[i+j*X] = A[i]+B[j]

    l.sort()
    t = min(3000, K)
    l = l[-t:]

    ans = [0] * len(l)*Z
    ls = len(l)
    for i in range(ls):
        for j in range(Z):
            ans[i+j*ls] = l[i] + C[j]

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


    return


# Generated by 1.1.3 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    Y = int(next(tokens))  # type: int
    Z = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(X) ]  # type: "List[int]"
    B = [ int(next(tokens)) for _ in range(Y) ]  # type: "List[int]"
    C = [ int(next(tokens)) for _ in range(Z) ]  # type: "List[int]"
    solve(X, Y, Z, K, A, B, C)

if __name__ == '__main__':
    main()