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 = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

AB_list = []
for i in range(X):
    for j in range(Y):
        out = A[i] + B[j]
        AB_list.append(out)
AB_list = sorted(AB_list, reverse=True)[:K+1]

ans = []
for AB in AB_list:
    for i in range(Z):
        out = AB + C[i]
        ans.append(out)

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

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = 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())))

AB_list = []
for i in range(X):
    for j in range(Y):
        out = A[i] + B[j]
        AB_list.append(out)
AB_list = sorted(AB_list, reverse=True)[:K+1]

ans = []
for AB in AB_list:
    for i in range(Z):
        out = AB + C[i]
        ans.append(out)

for i in range(K):
    print(sorted(ans, reverse=True)[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()
B.sort()
sublist = []
for i in range(max(X-K-1, 0), X):
    for j in range(max(Y - K - 1, 0), Y):
        sublist.append(A[i]+B[j])

sublist.sort()
subanslist = sublist[-K::1]
anslist = []

for i in subanslist:
    for j in range(max(Z - K - 1, 0), Z):
        anslist.append(i+C[j])

anslist.sort()
ans = anslist[:-K-1:-1]
for i in ans:
    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()))

A.sort()
B.sort()
sublist = []
for i in range(max(X-K-1, 0), X):
    for j in range(max(Y - K - 1, 0), Y):
        sublist.append(A[i]+B[j])

sublist.sort()
subanslist = sublist[-K::1]
anslist = []

for i in subanslist:
    for j in range(max(Z - K - 1, 0), Z):
        anslist.append(i+C[j])

anslist.sort()
for i in anslist[:-K-1:-1]:
    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()))
A.sort(reverse = True)
B.sort(reverse = True)
C.sort(reverse = True)
ans = [A[i]+B[j]+C[k] for k in range(Z) for j in range(Y) for i in range(X) if i*j*k <= 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)
t = []
for i in set(range(x)):
    for j in set(range(y)):
        for l in set(range(z)):
            if (i+1) * (j+1) * (l+1) <= k:
                t.append(a[i]+b[j]+c[l])
            else:
                break
t.sort(reverse=True)
for i in range(k):
    print(t[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)
t = []
for i in set(range(x)):
    for j in set(range(y)):
        for l in set(range(z)):
            if (i+1) * (j+1) * (l+1) <= k:
                t.append(a[i]+b[j]+c[l])
            else:
                break
t.sort(reverse=True)
for i in range(k):
    print(t[i])

Submission status: Time Limit Exceeded

Solution code:

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

A = [0]*X
A[:] = map(int, input().split())
B = [0]*Y
B[:] = map(int, input().split())
C = [0]*Z
C[:] = map(int, input().split())

AB = [0]*X*Y
cnt=0
for i, a in enumerate(A):
    for j, b in enumerate(B):
        AB[cnt] = a+b
        cnt+=1
AB.sort(reverse=True)
AB_top = AB[:K]
            
ABC = [0]*K*Z

cnt=0
for i, ab in enumerate(AB_top):
    for j, c in enumerate(C):
        ABC[cnt] = ab+c
        cnt+=1
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])

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

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()))
result = [a + b  for (a,b) in list(itertools.product(A, B))]
result.sort(reverse = True)
result = [s + c for (s,c) in list(itertools.product(result[:K], 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(' ')))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
t = []
for i in range(x):
    for j in range(y):
        for l in range(z):
            if i * j * l < k:
                t.append(a[i]+b[j]+c[l])
            else:
                break
t.sort(reverse=True)
for i in range(k):
    print(t[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)
t = []
for i in range(x):
    for j in range(y):
        for l in range(z):
            if i * j * l < k:
                t.append(a[i]+b[j]+c[l])
t.sort(reverse=True)
for i in range(k):
    print(t[i])

Submission status: Time Limit Exceeded

Solution code:

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

A = [0]*X
A[:] = map(int, input().split())
B = [0]*Y
B[:] = map(int, input().split())
C = [0]*Z
C[:] = map(int, input().split())

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

AB = [0]*K
for i, a in enumerate(A):
    for j, b in enumerate(B):
        if AB[-1] > a+b:
            break
        else:
            AB[-1] = max(AB[-1], a+b)
            AB.sort(reverse=True)
            
ABC = [0]*K
for i, ab in enumerate(AB):
    for j, c in enumerate(C):
        if ABC[-1] > ab+c:
            break
        else:
            ABC[-1] = max(ABC[-1], ab+c)
            ABC.sort(reverse=True)
            
for k in range(K):
    print(ABC[k])

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()))
import itertools
combi=list(itertools.product(a,b[:k]))
ans=[0]*len(combi)
for i in range(len(combi)):
    ans[i]= combi[i][0]+combi[i][1]
ans.sort(reverse=1)
combi2=list(itertools.product(ans[:k],c))
ans2=[0]*len(combi2)
for j in range(len(combi2)):
    ans2[j]=combi2[j][0]+combi2[j][1]
ans2.sort(reverse=True)
for x in ans2[:k]:
    print(x)

Submission status: Time Limit Exceeded

Solution code:

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

A = [0]*X
A[:] = map(int, input().split())
B = [0]*Y
B[:] = map(int, input().split())
C = [0]*Z
C[:] = map(int, input().split())

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

AB = [0]*X*Y
cnt=0
for i, a in enumerate(A):
    for j, b in enumerate(B):
        AB[cnt] = a+b
        cnt+=1
AB.sort(reverse=True)
AB_top = AB[:K]
            
ABC = [0]*K

cnt=0
for i, ab in enumerate(AB_top):
    for j, c in enumerate(C):
        if ABC[-1] > ab+c:
            break
        else:
            ABC[-1] = max(ABC[-1], ab+c)
            ABC.sort(reverse=True)
            
for k in range(K):
    print(ABC[k])

Submission status: Time Limit Exceeded

Solution code:
import sys; sys.setrecursionlimit(50000)
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)

result = [A[x]+B[y]+C[z] for x in range(X) for y in range(Y) for z in range(Z) if x * y * z <= K]
result.sort(reverse=True)
for i in range(K):
    print(result[i])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 16 20:52:46 2019

@author: Owner
"""


import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy
import bisect

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())

    
all(nstr.count(c) for c in '753')

# 複数配列を並び替え
ABT = zip(A, B, totAB)
result = 0
# itemgetterには何番目の配列をキーにしたいか渡します
sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)

# 2進数のbit判定
(x >> i) & 1

# dp最小化問題
dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break
#２分探索
li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    


#ソート関数
org_list = [3, 1, 4, 5, 2]
new_list = sorted(org_list)
print(org_list)
print(new_list)
# [3, 1, 4, 5, 2]
# [1, 2, 3, 4, 5]

"""

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

A = [0]*X
A[:] = map(int, input().split())
B = [0]*Y
B[:] = map(int, input().split())
C = [0]*Z
C[:] = map(int, input().split())

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

AB = [0]*X*Y
cnt=0
for i, a in enumerate(A):
    for j, b in enumerate(B):
        AB[cnt] = a+b
        cnt+=1
AB.sort(reverse=True)
AB_top = AB[:K]
            
ABC = [0]*K*Z

cnt=0
for i, ab in enumerate(AB_top):
    for j, c in enumerate(C):
        ABC[cnt] = ab+c
        cnt+=1
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])

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で総当たり、上位K個をスタック
tmp = [a+b for a in A for b in B]
tmp.sort(reverse=True)
tmp = tmp[:K]

# 残りのc個に対しても総当たり
ans = [t+c for t in tmp for c in C]
ans.sort(reverse=True)
ans = ans[:K]

for s in ans:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys; sys.setrecursionlimit(50000)
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)

result = []
for x in range(X):
    for y in range(Y):
        for z in range(Z):
            if x * y * z > K: break
            result.append(A[x]+B[y]+C[z])
result.sort(reverse=True)
for i in range(K):
    print(result[i])

Submission status: Time Limit Exceeded

Solution code:
import sys; sys.setrecursionlimit(50000)
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)

result = []
for x in range(X):
    for y in range(Y):
        for z in range(Z):
            if x * y * z > K: continue
            result.append(A[x]+B[y]+C[z])
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()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

# まずA, Bで総当たり、上位K個をスタック
tmp = []
for a in A:
    for b in B:
        tmp.append(a+b)
tmp = sorted(tmp, reverse=True)[:K]
# 残りのc個に対しても総当たり
ans = []
for s in tmp:
    for c in C:
        ans.append(s+c)
ans = sorted(ans, reverse=True)[:K]
for s in ans:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys; sys.setrecursionlimit(50000)
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)

result = []
for x in range(X):
    for y in range(Y):
        for z in range(Z):
            if x + y + z > K: continue
            result.append(A[x]+B[y]+C[z])
result.sort(reverse=True)
for i in range(K):
    print(result[i])

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()))
result = [a + b + c for (a,b,c) in list(itertools.product(A, B, 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()))
import itertools
combi=list(itertools.product(a,b))
ans=[0]*len(combi)
for i in range(len(combi)):
    ans[i]= sum(combi[i])
ans.sort(reverse=True)
combi2=list(itertools.product(ans[:k],c))
ans2=[0]*len(combi2)
for j in range(len(combi2)):
    ans2[j]=sum(combi2[j ])
ans2.sort(reverse=True)
for x in ans2[:k]:
    print(x)

Submission status: Time Limit Exceeded

Solution code:
def 通るかな():
    X,Y,Z,K = [int(_) for _ in input().split()]
    #aA = sorted((int(_) for _ in input().split()),reverse=True)
    #aB = sorted((int(_) for _ in input().split()),reverse=True)
    #aC = sorted((int(_) for _ in input().split()),reverse=True)
    aA = [int(_) for _ in input().split()]
    aB = [int(_) for _ in input().split()]
    aC = [int(_) for _ in input().split()]

    aD =[0] *(X*Y)
    i=0
    for a in aA:
        for b in aB:
            aD[i]=a+b
            i+=1
    aD.sort(reverse=True)
    if 3000 < X*Y :
        aD=aD[:3000]
    iL = len(aD)
    aE = [0] * (iL*Z)
    i = 0
    for d in aD:
        for c in aC:
            aE[i] = d+c
            i+=1
    aE.sort(reverse=True)
    print(*aE[:K],sep="\n")
通るかな()

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()))
result = [a + b + c for c in C for b in B for a in A]
result.sort(reverse = True)
for i,j in enumerate(result):
    if i == K:
        exit()
    else:print(j)

Submission status: Time Limit Exceeded

Solution code:
# solution 1
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)
AB = []
for i in range(X):
    for j in range(Y):
        AB.append(A[i]+B[j])
AB.sort(reverse=True)
ABC = []
for k in range(Z):
    for ij in range(min(K, len(AB))):
        ABC.append(AB[ij]+C[k])
ABC.sort(reverse=True)
for l in range(K):
    print(ABC[l])

Submission status: Time Limit Exceeded

Solution code:
# solution 1
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)
AB = []
for i in range(X):
    for j in range(Y):
        AB.append(A[i]+B[j])
AB.sort(reverse=True)
ABC = []
for k in range(Z):
    for ij in range(min(K, len(AB))):
        ABC.append(AB[ij]+C[k])
ABC.sort(reverse=True)
for l in range(K):
    print(ABC[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()))
a.sort(key=lambda x:-x)
b.sort(key=lambda x:-x)
c.sort(key=lambda x:-x)
# queue=[beauty, indexa, indexb, indexc]
queue=[(a[0]+b[0]+c[0],0,0,0)]
canAdd=[[[1 for _ in range(z)]for _ in range(y)]for _ in range(x)]
beauty=[0 for _ in range(k)]
for index in range(k):
    queue.sort(key=lambda x:-x[0])
    # print(queue)
    beauty[index],indexa,indexb,indexc=queue[0]
    queue.pop(0)
    for da,db,dc in [(1,0,0),(0,1,0),(0,0,1)]:
        nexta,nextb,nextc=indexa+da,indexb+db,indexc+dc
        if nexta<x and nextb<y and nextc<z and (nexta+1)*(nextb+1)*(nextc+1)<=k:
            if canAdd[nexta][nextb][nextc]:
                queue.append((a[nexta]+b[nextb]+c[nextc],nexta,nextb,nextc))
                canAdd[nexta][nextb][nextc]=0
for i in range(k):
    print(beauty[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(key=lambda x:-x)
b.sort(key=lambda x:-x)
c.sort(key=lambda x:-x)
# queue=[beauty, indexa, indexb, indexc]
queue=[(a[0]+b[0]+c[0],0,0,0)]
canAdd=[[[1 for _ in range(z)]for _ in range(y)]for _ in range(x)]
beauty=[0 for _ in range(k)]
for index in range(k):
    queue.sort(key=lambda x:-x[0])
    # print(queue)
    beauty[index],indexa,indexb,indexc=queue[0]
    queue.pop(0)
    for da,db,dc in [(1,0,0),(0,1,0),(0,0,1)]:
        if indexa+da<x and indexb+db<y and indexc+dc<z:
            if canAdd[indexa+da][indexb+db][indexc+dc]:
                queue.append((a[indexa+da]+b[indexb+db]+c[indexc+dc],indexa+da,indexb+db,indexc+dc))
                canAdd[indexa+da][indexb+db][indexc+dc]=0
for i in range(k):
    print(beauty[i])

Submission status: Time Limit Exceeded

Solution code:
x, y, z, k = map(int, input().split())
X = [int(i) for i in input().split()]
Y = [int(i) for i in input().split()]
Z = [int(i) for i in input().split()]
X.sort(reverse=1)
Y.sort(reverse=1)
Z.sort(reverse=1)

answer = []

for i in range(min(x,k)):
    for j in range(min(y,k)):
        for l in range(min(z,k)):
            if (i+1) * (j+1) * (l+1) <= k:
                answer.append(X[i]+Y[j]+Z[l])

answer.sort(reverse=1)

for p in answer[:k]:
    print(p)

Submission status: Time Limit Exceeded

Solution code:
x, y, z, k = map(int, input().split())
X = [int(i) for i in input().split()]
Y = [int(i) for i in input().split()]
Z = [int(i) for i in input().split()]
X.sort(reverse=1)
Y.sort(reverse=1)
Z.sort(reverse=1)

answer = []
for i in X[:k]:
    for j in Y[:k]:
        answer.append(i+j)
answer.sort(reverse=1)

answer2 = []
for l in Z[:k]:
    for a in answer[:k]:
        answer2.append(l+a)
answer2.sort(reverse=1)

for p in answer2[:k]:
    print(p)

Submission status: Time Limit Exceeded

Solution code:
x, y, z, k = map(int, input().split())
X = [int(i) for i in input().split()]
Y = [int(i) for i in input().split()]
Z = [int(i) for i in input().split()]


answer = []
for i in X:
    for j in Y:
        answer.append(i+j)
answer.sort(reverse=1)

answer2 = []
for l in Z[:k]:
    for a in answer[:k]:
        answer2.append(l+a)
answer2.sort(reverse=1)

for p in answer2[:k]:
    print(p)

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

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)
ab = ab[0:k]
abc = []
for i in ab:
  for j in c:
    abc.append(i+j)
abc.sort(reverse=True)
abc = abc[0:k]
for i in abc:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
import heapq
import sys
from itertools import product
X, Y, Z, K = [int(i) for i in sys.stdin.readline().split()]
A_ls = [int(i) for i in sys.stdin.readline().split()]
B_ls = [int(i) for i in sys.stdin.readline().split()]
C_ls = [int(i) for i in sys.stdin.readline().split()]
_sum_ls = []
for a, b, c in product(A_ls, B_ls, C_ls):
    _sum_ls.append(- (a + b + c))
heapq.heapify(_sum_ls)
for i in range(K):
    k = heapq.heappop(_sum_ls)
    print(-k)

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

import itertools
AB = [a + b for a, b in itertools.product(A,B)]
AB.sort(reverse=True)
ABC = [ab + c for ab, c in itertools.product(AB[:3000],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()))

e = [i + j for i in a for j in b]
e.sort(reverse=True)
e = e[:k]
ans = [i + j for i in e for j in 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()))

import itertools
AB = [a + b for a, b in itertools.product(A,B)]
ABC = [ab + c for ab, c in itertools.product(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()))

e = [i + j for i in a for j in b]
e.sort(reverse=True)
e = e[:k]
ans = [i + j for i in e for j in c]
ans.sort(reverse=True)
ans = ans[:k]

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(key=lambda x:-x)
b.sort(key=lambda x:-x)
c.sort(key=lambda x:-x)
sumAB=[0 for _ in range(x*y)]
index=0
imax=min(k,x)
jmax=min(k,y)
for i in range(imax):
    for j in range(jmax):
        sumAB[index]=a[i]+b[j]
        index+=1
sumAB.sort(key=lambda x:-x)
beauty=[0 for _ in range(k*k)]
index=0
imax=min(k,x*y)
jmax=min(k,z)
for i in range(imax):
    for j in range(jmax):
        beauty[index]=sumAB[i]+c[j]
        index+=1
beauty.sort(key=lambda x:-x)
for i in range(k):
    print(beauty[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()))
sumAB=[0 for _ in range(x*y)]
index=0
imax=min(k,x)
jmax=min(k,y)
for i in range(imax):
    for j in range(jmax):
        sumAB[index]=a[i]+b[j]
        index+=1
sumAB.sort(key=lambda x:-x)
c.sort(key=lambda x:-x)
beauty=[0 for _ in range(k*k)]
index=0
imax=min(k,x*y)
jmax=min(k,z)
for i in range(imax):
    for j in range(jmax):
        beauty[index]=sumAB[i]+c[j]
        index+=1
beauty.sort(key=lambda x:-x)
for i in range(k):
    print(beauty[i])

Submission status: Time Limit Exceeded

Solution code:
line = [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(" ")]

score = []
for i in A:
  for j in B:
    for k in C:
      score.append(i + j + k)

score.sort(reverse = True)

for i in score[:line[3]]:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product


X, Y, Z, K = map(int, input().split())
lst_A = list(map(int, input().split()))
lst_B = list(map(int, input().split()))
lst_C = list(map(int, input().split()))
lst_C = lst_C[:K]

lst_AB = [a + b for a, b in product(lst_A, lst_B)]
lst_AB.sort(reverse=True)
K_ = min(3000, X*Y*Z)
lst_ABC = [ab + c for ab, c in list(product(lst_AB[:K_], lst_C))]
lst_ABC.sort(reverse=True)


print(*lst_ABC[:K], sep="\n")

Submission status: Time Limit Exceeded

Solution code:
from itertools import product


X, Y, Z, K = map(int, input().split())
lst_A = list(map(int, input().split()))
lst_B = list(map(int, input().split()))
lst_C = list(map(int, input().split()))


lst_AB = [a + b for a, b in product(lst_A, lst_B)]
lst_AB.sort(reverse=True)


lst_ABC = [ab + c for ab, c in list(product(lst_AB[:min(3000, X*Y*Z)], lst_C))]
lst_ABC.sort(reverse=True)


print(*lst_ABC[:K], sep="\n")

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)

C.sort(reverse=True)

ab_scores = sorted([a + b for a in A for b in B], reverse=True)[:K]
scores = sorted([ab + c for ab in ab_scores for c in C], reverse=True)

for i in scores[:K]:
    print(i)

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

A = sorted(A)[::-1][:K]
B = sorted(B)[::-1][:K]
C = sorted(C)[::-1][:K]

T = [A, B]
score = []
for cand in itertools.product(*T):
  score.append(sum(cand))
  
T = [sorted(score)[::-1][:K], C]
res = []
for cand in itertools.product(*T):
  res.append(sum(cand))

res = sorted(res)[::-1][:K]
for i in range(K):
    print(res[i])

Submission status: Time Limit Exceeded

Solution code:
from itertools import product


X, Y, Z, K = map(int, input().split())
lst_A = list(map(int, input().split()))
lst_B = list(map(int, input().split()))
lst_C = list(map(int, input().split()))


lst_AB = [a + b for a, b in product(lst_A, lst_B)]
lst_AB.sort(reverse=True)


lst_ABC = [ab + c for ab, c in product(lst_AB[:min(3000, X*Y*Z)], lst_C)]
lst_ABC.sort(reverse=True)


print(*lst_ABC[:K], sep="\n")

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.sort(reverse=True)

ab_scores = sorted([a + b for a in A for b in B], reverse=True)[:K]
scores = sorted([ab + c for ab in ab_scores for c in C], reverse=True)

for i in scores[:K]:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

X, Y, Z, K = map(int, input().split())
lst_A = list(map(int, input().split()))
lst_B = list(map(int, input().split()))
lst_C = list(map(int, input().split()))


lst_AB = [a+b for a, b in product(lst_A, lst_B)]
lst_AB.sort(reverse=True)
lst_AB_K = lst_AB[:min(3000, X*Y*Z)]


lst_ABC = [ab+c for ab, c in product(lst_AB_K, lst_C)]
lst_ABC.sort(reverse=True)


print(*lst_ABC[:K], sep="\n")

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

A = sorted(A)[::-1]
B = sorted(B)[::-1]
C = sorted(C)[::-1]

T = [A[:K], B[:K]]
score = []
for cand in itertools.product(*T):
  score.append(sum(cand))

score = sorted(score)[::-1][:K]
  
T = [score, C[:K]]
res = []
for cand in itertools.product(*T):
  res.append(sum(cand))

res = sorted(res)[::-1][:K]
for i in range(K):
    print(res[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.sort(reverse=True)

ab_scores = sorted([a + b for a in A for b in B], reverse=True)[:K]
scores = sorted([ab + c for ab in ab_scores for c in C], reverse=True)

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