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:
from itertools import product, chain
from collections import deque


def solver1(a, b, c, k):
    apb_sorted = sorted([a + b for a, b in product(a, b)])[-k:]
    ans_list = sorted(
        list(chain.from_iterable([[_c + ab for ab in apb_sorted] for _c in c[:k]]))
    )
    it = reversed(ans_list)
    for _ in range(k):
        print(next(it))


def solver2(a, b, c, k):
    d = deque()
    n = len(a)
    m = min(k, n)
    for p in range(m):
        for q in range(m):
            for r in range(m):
                if (p + 1) * (q + 1) * (r + 1) > k:
                    continue
                d.append(a[-p - 1] + b[-q - 1] + c[-r - 1])
    it = reversed(sorted(d))
    for _ in range(k):
        print(next(it))


if __name__ == "__main__":
    x, y, z, k = list(map(int, input().split(" ")))
    a = sorted(list(map(int, input().split(" "))))
    b = sorted(list(map(int, input().split(" "))))
    c = sorted(list(map(int, input().split(" "))))
    solver2(a, b, c, k)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product, chain
from collections import deque


def solver1(a, b, c, k):
    apb_sorted = sorted([a + b for a, b in product(a, b)])[-k:]
    ans_list = sorted(
        list(chain.from_iterable([[_c + ab for ab in apb_sorted] for _c in c[:k]]))
    )
    it = reversed(ans_list)
    for _ in range(k):
        print(next(it))


def solver2(a, b, c, k):
    d = deque()
    n = len(a)
    for p in range(n):
        for q in range(n):
            for r in range(n):
                if (p + 1) * (q + 1) * (r + 1) > k:
                    continue
                d.append(a[-p - 1] + b[-q - 1] + c[-r - 1])
    it = reversed(sorted(d))
    for _ in range(k):
        print(next(it))


if __name__ == "__main__":
    x, y, z, k = list(map(int, input().split(" ")))
    a = sorted(list(map(int, input().split(" "))))
    b = sorted(list(map(int, input().split(" "))))
    c = sorted(list(map(int, input().split(" "))))
    solver2(a, b, c, k)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k=map(int,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()]
ans=[]
for A in a:
    for B in b:
        for C in c:
            ans+=[A+B+C]
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
from itertools import product, chain

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

apb_sorted = sorted([a + b for a, b in product(a, b)])
ans_list = sorted(
    list(chain.from_iterable([[_c + ab for ab in apb_sorted] for _c in c[:k]]))
)
it = reversed(ans_list)
for _ in range(k):
    print(next(it))

Submission status: Time Limit Exceeded

Solution code:
from itertools import product, chain

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

apb_sorted = sorted([a + b for a, b in product(a, b)])
ans_list = sorted(list(chain.from_iterable([[_c + ab for ab in apb_sorted] for _c in c])))
it = reversed(ans_list)
for _ in range(k):
    print(next(it))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

x,y,z,n=map(int,input().split())
X_list= [int(x) for x in input().split()]
Y_list= [int(x) for x in input().split()]
Z_list= [int(x) for x in input().split()]

Cake_point = [0 for i in range(x*y*z)]
for i in range(x):
    for j in range(y):
        for k in range(z):
            Cake_point[i*y*z + j*z +k] = (X_list[i] + Y_list[j] + Z_list[k])

Cake_point.sort(reverse=True)

for i in range(n):
    print(Cake_point[i])

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import re

x,y,z,n=map(int,input().split())#標準入力を行う

X_list= [int(x) for x in input().split()]
Y_list= [int(x) for x in input().split()]
Z_list= [int(x) for x in input().split()]

Cake_point = [0 for i in range(x*y*z)]
for i in range(x):
    for j in range(y):
        for k in range(z):
            Cake_point[i*y*z + j*z +k] = (X_list[i] + Y_list[j] + Z_list[k])

Cake_point.sort(reverse=True)

for i in range(n):
    print(Cake_point[i])

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import re
x,y,z,n=map(int,input().split())

X_list=input()
X_list = re.split(" ", X_list)
for i in range(x):
    X_list[i] = int(X_list[i])

Y_list=input()
Y_list = re.split(" ", Y_list)
for i in range(x):
    Y_list[i] = int(Y_list[i])

Z_list=input()
Z_list = re.split(" ", Z_list)
for i in range(x):
    Z_list[i] = int(Z_list[i])


Cake_point = [0 for i in range(x*y*z)]
for i in range(x):
    for j in range(y):
        for k in range(z):
            Cake_point[i*y*z + j*z +k] = (X_list[i] + Y_list[j] + Z_list[k])

Cake_point.sort(reverse=True)

for i in range(n):
    print(Cake_point[i])

Submission status: Time Limit Exceeded

Solution code:
import bisect

x, y, z, til = (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)

rem = []
for i in range(x):
    for j in range(y):
        for k in range(z):
            if (i+1) * (y+1) * (z+1) > til:
                bisect.insort(rem, a[i] + b[j] + c[k])
            else:
                break

for i in range(1, til+1):
    print(rem[i * -1])

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=[]
if(k>len(a)):
    indA=len(a)
else:
    indA=k
if(k>len(b)):
    indB=len(b)
else:
    indB=k
if(k>len(c)):
    indC=len(c)
else:
    indC=k

for i in a[:indA]:
    for j in b[:indB]:
        ab.append(i+j)
ab.sort(reverse=True)

abc=[]
for i in ab[:k]:
    for j in c[:indC]:
        abc.append(i+j)
abc.sort(reverse=True)

for i in abc[:k]:
    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=list(map(int,input().split()))

ab=[]

for i in a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse=True)

abc=[]
for i in ab[:k]:
    for j in c:
        abc.append(i+j)
abc.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = [int(elm) for elm in input().split(" ")]
Xs = [X, Y, Z]
A = sorted([int(elm) for elm in input().split(" ")], reverse=True)
B = sorted([int(elm) for elm in input().split(" ")], reverse=True)
C = sorted([int(elm) for elm in input().split(" ")], reverse=True)


def sum_(kouho):
    a, b, c = kouho
    return sum([A[a], B[b], C[c]])

kouhos = []

kouhos = [[0, 0, 0]]
cur = kouhos[0]
kouhos_vals = [sum_(cur)]
all_ = [cur]

def add_kouhos(kouhos, kouhos_vals, new_kouhos):
    if len(kouhos) == 0:
        kouhos = new_kouhos
        kouhos_vals = [sum_(kouho) for kouho in new_kouhos]
        return kouhos, kouhos_vals

    len_kouhos = len(kouhos)

    new_i = 1
    new_kouho = new_kouhos[-new_i] # 後ろ(小さいkouho)から追加していく
    sum_new_kouho = sum_(new_kouho)

    n_kouhos = len(new_kouhos)

    last_kouho_i = len_kouhos - 1
    while True:
        if sum_new_kouho <= sum_(kouhos[-1]):
            #kouhos.append(new_kouho)
            kouhos.insert(last_kouho_i+1, new_kouho)
            new_i += 1
            if new_i == n_kouhos+1:
                return kouhos, kouhos_vals        
            new_kouho = new_kouhos[-new_i]
            sum_new_kouho = sum_(new_kouho)
        else:
            break

    #for kouho_i in range(1, len_kouhos+1):
    for cur_kouho_i in reversed(range(0, len_kouhos)):
        loop = True
        sum_cur_kouho = sum_(kouhos[cur_kouho_i])
        while loop:
            #if sum_(new_kouho) > sum_(kouhos[-kouho_i]):
            if sum_new_kouho < sum_cur_kouho: # 下からkouhosを辿って行き、超えられなかった一つ手前にinsert
                kouhos.insert(cur_kouho_i+1, new_kouho)
                kouhos_vals.insert(cur_kouho_i+1, sum_(new_kouho))
                #kouhos.insert(-kouho_i, new_kouho)
                #kouhos_vals.insert(-kouho_i, sum_(new_kouho))
                new_i += 1
                if new_i == n_kouhos+1:
                    return kouhos, kouhos_vals
                new_kouho = new_kouhos[-new_i]
                sum_new_kouho = sum_(new_kouho)
            else:
                loop = False
                #new_i += 1
                #new_kouho = new_kouhos[-new_i]

    while True:
        #print("add mae", new_kouho)
        kouhos.insert(0, new_kouho) # ひたすら先頭にnew_kouhosを小さいものから追加していく
        kouhos_vals.insert(0, sum_new_kouho)
        new_i += 1
        if new_i == n_kouhos+1:
            return kouhos, kouhos_vals
        else:
            new_kouho = new_kouhos[-new_i]
            sum_new_kouho = sum_(new_kouho)

def make_new_kouhos():
    new_kouhos = []
    for i in range(3):
        new_kouho = cur.copy()
        new_kouho[i] += 1
        if new_kouho not in all_ and new_kouho[i] < Xs[i]:
            new_kouhos.append(new_kouho)
    new_kouhos = sorted(new_kouhos, key=lambda x:-sum_(x))
    return new_kouhos

#while True:
for _ in range(K):
    #print("cur_kouho", kouhos[0])
    cur = kouhos[0]
    print(sum_(cur))
    kouhos = kouhos[1:]
    kouhos_vals = kouhos_vals[1:]

    new_kouhos = make_new_kouhos()
    all_ += new_kouhos

    if len(new_kouhos) > 0:
        #print("before add kouhos", kouhos)
        #print("new_kouhos", new_kouhos)
        kouhos, kouhos_vals = add_kouhos(kouhos, kouhos_vals, new_kouhos)
        #print("after add kouhos", kouhos)

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = [int(elm) for elm in input().split(" ")]
Xs = [X, Y, Z]
A = sorted([int(elm) for elm in input().split(" ")], reverse=True)
B = sorted([int(elm) for elm in input().split(" ")], reverse=True)
C = sorted([int(elm) for elm in input().split(" ")], reverse=True)


def sum_(kouho):
    a, b, c = kouho
    return sum([A[a], B[b], C[c]])

kouhos = []

kouhos = [[0, 0, 0]]
cur = kouhos[0]
kouhos_vals = [sum_(cur)]
all_ = [cur]

def add_kouhos(kouhos, kouhos_vals, new_kouhos):
    if len(kouhos) == 0:
        kouhos = new_kouhos
        kouhos_vals = [sum_(kouho) for kouho in new_kouhos]
        return kouhos, kouhos_vals

    len_kouhos = len(kouhos)

    new_i = 1
    new_kouho = new_kouhos[-new_i] # 後ろ(小さいkouho)から追加していく

    n_kouhos = len(new_kouhos)

    last_kouho_i = len_kouhos - 1
    while True:
        if sum_(new_kouho) <= sum_(kouhos[-1]):
            #kouhos.append(new_kouho)
            kouhos.insert(last_kouho_i+1, new_kouho)
            new_i += 1
            if new_i == n_kouhos+1:
                return kouhos, kouhos_vals        
            new_kouho = new_kouhos[-new_i]
        else:
            break

    #for kouho_i in range(1, len_kouhos+1):
    for cur_kouho_i in reversed(range(0, len_kouhos)):
        loop = True
        sum_cur_kouho = sum_(kouhos[cur_kouho_i])
        while loop:
            #if sum_(new_kouho) > sum_(kouhos[-kouho_i]):
            if sum_(new_kouho) < sum_cur_kouho: # 下からkouhosを辿って行き、超えられなかった一つ手前にinsert
                kouhos.insert(cur_kouho_i+1, new_kouho)
                kouhos_vals.insert(cur_kouho_i+1, sum_(new_kouho))
                #kouhos.insert(-kouho_i, new_kouho)
                #kouhos_vals.insert(-kouho_i, sum_(new_kouho))
                new_i += 1
                if new_i == n_kouhos+1:
                    return kouhos, kouhos_vals
                new_kouho = new_kouhos[-new_i]
            else:
                loop = False
                #new_i += 1
                #new_kouho = new_kouhos[-new_i]

    while True:
        #print("add mae", new_kouho)
        kouhos.insert(0, new_kouho) # ひたすら先頭にnew_kouhosを小さいものから追加していく
        kouhos_vals.insert(0, sum_(new_kouho))
        new_i += 1
        if new_i == n_kouhos+1:
            return kouhos, kouhos_vals
        else:
            new_kouho = new_kouhos[-new_i]

def make_new_kouhos():
    new_kouhos = []
    for i in range(3):
        new_kouho = cur.copy()
        new_kouho[i] += 1
        if new_kouho not in all_ and new_kouho[i] < Xs[i]:
            new_kouhos.append(new_kouho)
    new_kouhos = sorted(new_kouhos, key=lambda x:-sum_(x))
    return new_kouhos

#while True:
for _ in range(K):
    #print("cur_kouho", kouhos[0])
    cur = kouhos[0]
    print(sum_(cur))
    kouhos = kouhos[1:]
    kouhos_vals = kouhos_vals[1:]

    new_kouhos = make_new_kouhos()
    all_ += new_kouhos

    if len(new_kouhos) > 0:
        #print("before add kouhos", kouhos)
        #print("new_kouhos", new_kouhos)
        kouhos, kouhos_vals = add_kouhos(kouhos, kouhos_vals, new_kouhos)
        #print("after add kouhos", kouhos)

Submission status: Time Limit Exceeded

Solution code:
#4
x,y,z,k=map(int,input().split())
listx = [int(i) for i in input().split()]
listy = [int(i) for i in input().split()]
listz = [int(i) for i in input().split()]

import numpy as np
listxy = np.zeros([x*y,],dtype=np.int)
c = 0
for i in range(0,x):
    for j in range(0,y):
        listxy[c] = listx[i]+listy[j]
        c+=1

listxy = sorted(listxy,reverse=True)
#print(listxy)
if x*y>k:
    listxy=listxy[:k]
    listxyz =np.zeros([k*z,],dtype=np.int)
    c = 0
    for i in range(0,k):
        for j in range(0,z):
            listxyz[c] = listxy[i]+listz[j]
            c+=1
else:
    listxyz =np.zeros([x*y*z,],dtype=np.int)
    c = 0
    for i in range(0,x*y):
        for j in range(0,z):
            listxyz[c] = listxy[i]+listz[j]
            c+=1

listxyz=sorted(listxyz,reverse=True)

for i in range(0,k):
    print(listxyz[i])

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k=map(int,input().split())
listx = [int(i) for i in input().split()]
listy = [int(i) for i in input().split()]
listz = [int(i) for i in input().split()]

import numpy as np
listxy = np.zeros([x*y,],dtype=np.int)
c = 0
for i in range(0,x):
    for j in range(0,y):
        listxy[c] = listx[i]+listy[j]
        c+=1

listxy.sort()
#print(listxy)
if x*y>k:
    listxy=listxy[-k:]
    listxyz =np.zeros([k*z,],dtype=np.int)
    c = 0
    for i in range(0,k):
        for j in range(0,z):
            listxyz[c] = listxy[i]+listz[j]
            c+=1
else:
    listxyz =np.zeros([x*y*z,],dtype=np.int)
    c = 0
    for i in range(0,x*y):
        for j in range(0,z):
            listxyz[c] = listxy[i]+listz[j]
            c+=1

listxyz.sort()

for i in range(0,k):
    print(listxyz[-(i+1)])

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k=map(int,input().split())
listx = [int(i) for i in input().split()]
listy = [int(i) for i in input().split()]
listz = [int(i) for i in input().split()]

import numpy as np
listxy = np.zeros([x*y,],dtype=np.int)
c = 0
for i in range(0,x):
    for j in range(0,y):
        listxy[c] = listx[i]+listy[j]
        c+=1

listxy.sort()
#print(listxy)
if x*y>k:
    listxy=listxy[-k:]
    listxyz =np.zeros([k*z,],dtype=np.int)
    c = 0
    for i in range(0,k):
        for j in range(0,z):
            listxyz[c] = listxy[i]+listz[j]
            c+=1
else:
    listxyz =np.zeros([x*y*z,],dtype=np.int)
    c = 0
    for i in range(0,x*y):
        for j in range(0,z):
            listxyz[c] = listxy[i]+listz[j]
            c+=1

listxyz.sort()
if len(listxyz)>k:
    listxyz=listxyz[-k:]
for i in range(0,k):
    print(listxyz[-(i+1)])

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k=map(int,input().split())
listx = [int(i) for i in input().split()]
listy = [int(i) for i in input().split()]
listz = [int(i) for i in input().split()]

listxy = []
for i in range(0,x):
    for j in range(0,y):
        listxy.append(listx[i]+listy[j])
listxy.sort()
#print(listxy)
if x*y>k:
    listxy=listxy[-k:]
    listxyz = []
    for i in range(0,k):
        for j in range(0,z):
            listxyz.append(listxy[i]+listz[j])
else:
    listxyz = []
    for i in range(0,x*y):
        for j in range(0,z):
            listxyz.append(listxy[i]+listz[j])

listxyz.sort()
if len(listxyz)>k:
    listxyz=listxyz[-k:]
for i in range(0,k):
    print(listxyz[-(i+1)])

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))
bc = sorted(i+j for i in b for j in c)[::-1][:k]
abc = sorted(i+j for i in sorted(a) for j in bc)[::-1][:k]
for i in abc:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
lx, ly, lz, k = [int(item) for item in input().split()]
xs = [int(item) for item in input().split()]
ys = [int(item) for item in input().split()]
zs = [int(item) for item in input().split()]
arr = []
for x in xs:
  for y in ys:
    arr.append(x+y)
arr.sort(reverse=True)
narr = []
for a in arr[:k]:
  for z in zs:
    narr.append(a+z)
narr.sort(reverse=True)
for v in narr[:k]:
  print(v)

Submission status: Time Limit Exceeded

Solution code:
lx, ly, lz, k = [int(item) for item in input().split()]
xs = [int(item) for item in input().split()]
ys = [int(item) for item in input().split()]
zs = [int(item) for item in input().split()]
arr = []
for x in xs:
  for y in ys:
    arr.append(x+y)
arr.sort(reverse=True)
narr = []
for a in arr[:k]:
  for z in zs:
    narr.append(a+z)
narr.sort(reverse=True)
for i in range(k):
  print(narr[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()]

ab = [ai + bi for ai in a for bi in b]
ab.sort(reverse=True)
ab = ab[:k]

abc = [abi + ci for abi in ab for ci in c]
abc.sort(reverse = True)

for i in range(len(abc)):
  if i < k:
    print(abc[i])
  else:
    break

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
X, Y, Z, K = map(int,input().split(' '))

*a, = map(int,input().split(' '))
*b, = map(int,input().split(' '))
*c, = map(int,input().split(' '))

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

abc = []
for i in range(len(a)):
    if i > K+1:
        break
    for j in range(len(b)):
        if i+j > K+1:
            break
        for k in range(len(c)):
            if i+j+k > K+1:
                break
            abc.append(a[i]+b[j]+c[k])

abc.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 
import math
import sys
import itertools
import numpy as np

 
# なんこか入力
x,y,z,k = map(int, 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()
b.sort()
c.sort()
#cake = [[0 for i in range(3)] for j in range(3000)]
#cake[0][0]=x-1
#cake[0][1]=y-1
#cake[0][2]=z-1
#print(a[x-1]+b[y-1]+c[z-1])
#print(a[cake[0][0]],b[cake[0][1]],c[cake[0][2]])
t = np.array([[-1,-1,-1,-1] for i in range(3*k)]).astype(np.int64)
t[0] = [a[x-1]+b[y-1]+c[z-1],x-1,y-1,z-1]
t_a = 1
#print(s)
 
for i in range(0,k):
    ma = 0
    ma_j = -1
    temp = [-1,-1,-1,-1]
    for j in range(len(t)):
        if ma <= t[j][0] :
            temp = t[j]
            ma = t[j][0]
            ma_j = j
#    print(temp)
    print(temp[0])
    t[ma_j][0]=0
#    print("s=",s)
#    print("t=",t)
#    print(temp)
    f =1
    if temp[1] > 0:
        for j in range(len(t)):
            if temp[1]-1 == t[j][1] and temp[2]==t[j][2] and temp[3]==t[j][3]:
                f = 0
                break
        if f == 1:
            t[t_a] = [a[temp[1]-1]+b[temp[2]]+c[temp[3]],temp[1]-1,temp[2],temp[3]]
            t_a += 1
    f =1
    if temp[2] > 0:
        for j in range(len(t)):
            if temp[1] == t[j][1] and temp[2]-1==t[j][2] and temp[3]==t[j][3]:
                f = 0
                break
        if f == 1:
            t[t_a] = [a[temp[1]]+b[temp[2]-1]+c[temp[3]],temp[1],temp[2]-1,temp[3]]
            t_a += 1
    f =1
    if temp[3] > 0:
        for j in range(len(t)):
            if temp[1] == t[j][1] and temp[2]==t[j][2] and temp[3]-1==t[j][3]:
                f = 0
                break
        if f == 1:
            t[t_a] = [a[temp[1]]+b[temp[2]]+c[temp[3]-1],temp[1],temp[2],temp[3]-1]
            t_a += 1

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N =list(map(int, input().split()))
cake_list = []
cake_list_A = list(map(int, input().split()))
cake_list_B = list(map(int, input().split()))
cake_list_C = list(map(int, input().split()))
cake_list.append(sorted(cake_list_A, reverse=True))
cake_list.append(sorted(cake_list_B, reverse=True))
cake_list.append(sorted(cake_list_C, reverse=True))
seikai=[]

seikai1=[]


for j in range(N[0]):
    for k in range(N[1]):
        seikai1.append(cake_list[0][j]+cake_list[1][k])

seikai1 = sorted(seikai1,reverse=True)
seikai1 = seikai1[:N[3]]

for j in range(min(len(seikai1),N[3])):
    for k in range(N[2]):
        seikai.append(seikai1[j] + cake_list[2][k])

seikai = sorted(seikai,reverse=True)
seikai = seikai[0:]

for j in range(N[3]):
    print(seikai[j])

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] *(len(A) * len(B))
for i in range(len(A)) :
    for j in range(len(B)) :
        AB[len(B) * i + j] = A[i] + B[j]
        
AB.sort(reverse = True)
AB = AB[:K]

ABC = [0] * (len(AB) * len(C))

for i in range(len(AB)) :
    for j in range(len(C)) :
        ABC[len(B) * i + j] = 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 = 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)

result = []
for i in range(len(A)):
    for j in range(len(B)):
        for k in range(len(C)):
            if (i*j*k) <= K:
                result.append(A[i]+B[j]+C[k])
            else:
                break

result.sort(reverse=True)

for i in range(K):
    print(result[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 i in range(len(A)) :
    for j in range(len(B)) :
        AB.append(A[i] + B[j])

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

ABC = []

for i in range(len(AB)) :
    for j in range(len(C)) :
        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 = 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)

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

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

ABC = []

for i in range(len(AB)) :
    for j in range(len(C)) :
        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 = 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)
idx = 0
for a in A:
    for b in B:
        AB[idx] = a + b
        idx += 1
AB = sorted(AB, reverse = True)
ABC = [0] * (K * Z)
idx = 0
k = min(K, X * Y)
for i in range(k):
    for c in C:
        ABC[idx] = AB[i] + c
        idx += 1
ABC = sorted(ABC, 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()))

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

result = []
ABl = sorted(ABl,reverse=True)[:K]
for ab in ABl:
    for c in C:
        result.append(ab+c)

result.sort(reverse=True)

for i in range(K):
    print(result[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 = []
ABappend = AB.append
for a in A:
    for b in B:
        ABappend(a+b)
AB = sorted(AB, reverse = True)
ABC = []
ABCappend = ABC.append
for ab in AB[:K]:
    for c in C:
        ABCappend(ab + c)
for abc in sorted(ABC, reverse = True)[:K]:
    print(abc)

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 = sorted(AB, reverse = True)[:K]
ABC = []
for ab in AB:
    for c in C:
        ABC.append(ab + c)
for abc in sorted(ABC, reverse = True)[:K]:
    print(abc)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

r = sorted([a_ + b_ + c_ for a_,b_,c_ in itertools.product(a,b,c)])

r.reverse()

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

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache

numbers = input().split()
x = input().split()
y = input().split()
z = input().split()

k = int(numbers[3])


x.sort(reverse=True)
y.sort(reverse=True)
z.sort(reverse=True)

answer = []
for i in x:
    for j in y:
        for m in z:
            price = int(i)+int(j)+int(m)
            answer.append(price)

answer.sort(reverse=True)
del answer[k::]

for l in answer:
    print(l)

Submission status: Time Limit Exceeded

Solution code:
numbers = input().split()
x = input().split()
y = input().split()
z = input().split()

k = int(numbers[3])


def cutting(c, k):
    del c[k::]


x.sort(reverse=True)
cutting(x, k)
y.sort(reverse=True)
cutting(y, k)
z.sort(reverse=True)
cutting(z, k)

answer = []

for i in x:
    for j in y:
        for m in z:
            price = int(i)+int(j)+int(m)
            answer.append(price)

answer.sort(reverse=True)
del answer[k::]

for l in answer:
    print(l)

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

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

AB.sort(reverse=True)

AB = AB[0:K]

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

AB=[]

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

AB.sort(reverse=True)

AB = AB[0:K]

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:
xyzk=list(map(int,input().split()))
x=xyzk[0]
y=xyzk[1]
z=xyzk[2]
k=xyzk[3]
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
d = []
for i in range(len(a)):
    for j in range(len(b)):
        d.append(a[i]+b[j])
e = list(reversed(sorted(d)))
if len(e) > 3000:
    f = e[:3000]
else:
    f = e
g = []
for i in range(len(f)):
    for j in range(len(c)):
        g.append(f[i]+c[j])
h = list(reversed(sorted(g)))
for i in range(k):
    print(h[i])