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:
#!/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 = max(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()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

X, Y, Z, K = map(int, input().split())
a = map(int, input().split())
b = list(map(int, input().split()))
c = map(int, input().split())

d = (aa + bb for aa in a for bb in b)
d = sorted(d, reverse=True)[:K]

e = (cc + dd for cc in c for dd in d)
e = sorted(e, reverse=True)[:K]

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

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

X, Y, Z, K = map(int, input().split())
a = map(int, input().split())
b = list(map(int, input().split()))
c = map(int, input().split())

d = (aa + bb for aa in a for bb in b)
d = sorted(d, reverse=True)

e = (cc + dd for cc in c for dd in d[:K])
e = sorted(e, reverse=True)[:K]

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

Submission status: Time Limit Exceeded

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

d = (aa + bb for aa in a for bb in b)
d = sorted(d, reverse=True)

e = (cc + dd for cc in c for dd in d[:K])
e = sorted(e, reverse=True)

for idx, ee in enumerate(e):
    if idx == K: break
    print(ee)

Submission status: Time Limit Exceeded

Solution code:
import itertools

X, Y, Z, K = list(map(int, input().split()))
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))

AB_list = [a + b for (a, b) in list(itertools.product(A_list, B_list))]
AB_list.sort(reverse=True)
ABC_list = [ab + c for (ab, c) in list(itertools.product(AB_list[:min(3000, X*Y*Z)], C_list))]
ABC_list.sort(reverse=True)
for i in range(K):
  print(ABC_list[i])

Submission status: Time Limit Exceeded

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

d = []
for aa in a:
    for bb in b:
        d.append(aa + bb)

d = list(sorted(d, reverse=True)[:K])

e = []
for cc in c:
    for dd in d:
        e.append(cc + dd)

for ee in sorted(e, reverse=True)[:K]:
    print(ee)

Submission status: Time Limit Exceeded

Solution code:
import itertools

X, Y, Z, K = list(map(int, input().split()))
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))

AB_list = [a + b for (a, b) in itertools.product(A_list, B_list)]
AB_list.sort(reverse=True)
ABC_list = [ab + c for (ab, c) in itertools.product(AB_list[:min(3000, X*Y*Z)], C_list)]
ABC_list.sort(reverse=True)
for i in range(K):
  print(ABC_list[i])

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter

K = int(input().split()[-1])

A = sorted([int(e) for e in input().split()], reverse=True)
B = sorted([int(e) for e in input().split()], reverse=True)
C = sorted([int(e) for e in input().split()], reverse=True)


def get_next_iter(i, _list):
    if i < len(_list) - 1:
        return i + 1
    else:
        return i


values = []

ia = ib = ic = 0


def get_value(ia, ib, ic):
    return A[ia] + B[ib] + C[ic]


def get_next(ia, ib, ic):
    ia_n = get_next_iter(ia, A)
    ib_n = get_next_iter(ib, B)
    ic_n = get_next_iter(ic, C)
    s = set()
    s.add((ia_n, ib, ic, get_value(ia_n, ib, ic)))
    s.add((ia, ib_n, ic, get_value(ia, ib_n, ic)))
    s.add((ia, ib, ic_n, get_value(ia, ib, ic_n)))
    return s


values.append((ia, ib, ic, get_value(ia, ib, ic)))
candidate = get_next(0, 0, 0)

for k in range(K - 1):
    vs = sorted(candidate, reverse=True, key=itemgetter(3))
    for i, v in enumerate(vs):
        if v not in values:
            values.append(v)
            candidate.remove(v)
            for each in get_next(v[0], v[1], v[2]):
                candidate.add(each)
            break
        else:
            continue

for each in values:
    print(each[3])

Submission status: Time Limit Exceeded

Solution code:
# https://img.atcoder.jp/abc123/editorial.pdf
# 問題D: Cake 123 解法 #3 - さらに高速な解法、priority-queue を用いて O(K log K)

from heapq import heappush, nlargest

def solve(X, Y, Z, K, A, B, C):
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)

    Q = []
    for a in A:
        for b in B:
            heappush(Q, a+ b)
    AB = nlargest(K, Q)

    Q2 = []
    for ab in AB:
        for c in C:
            heappush(Q2, ab + c)
    ABC = nlargest(K, Q2)
    print("\n".join(map(str, ABC)))

if __name__ == "__main__":
    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()))
    solve(X, Y, Z, K, A, B, C)

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)

#降順にソート

d = []
#美味しさの合計の配列

for i in range(X):
    for j in range(Y):
        for k in range(Z):
            d.append(a[i] + b[j] + c[k])

d.sort(reverse=True)
#dを降順に

for i in range(K):
    print(d[i])
#dをkまで出力

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

sum_ab = [ a[i] + b[j] for i in range(x) for j in range(y) ]
sum_ab.sort(reverse=True)

sum_abc = [ c[i] + sum_ab[j] for i in range(z) for j in range(min(k, x*y)) ]
sum_abc.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K=[int(i) for i in input().split()]
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
C=[int(i) for i in input().split()]
A.sort(key=lambda x:-x)
B.sort(key=lambda y:-y)
C.sort(key=lambda z:-z)
pri=[]
def dp(l):
  return A[l[0]]+B[l[1]]+C[l[2]]

def priq(a,b,c):
  if [a,b,c] not in pri:

    for i in range(len(pri)-1,-1,-1):
      if dp(pri[i])<dp([a,b,c]):
        pri.insert(i+1,[a,b,c])
        return
    pri.insert(0,[a,b,c])

pri.append([0,0,0])
for i in range(K):
  x,y,z=pri.pop(-1)
  print(dp([x,y,z]))
  if x<X-1:
    priq(x+1,y,z)
  if y<Y-1:
    priq(x,y+1,z)
  if z<Z-1:
    priq(x,y,z+1)

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K=[int(i) for i in input().split()]
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
C=[int(i) for i in input().split()]
A.sort(key=lambda x:-x)
B.sort(key=lambda y:-y)
C.sort(key=lambda z:-z)
pri=[]
primemo={}
def dp(l):
  return A[l[0]]+B[l[1]]+C[l[2]]

def priq(a,b,c):
  if [a,b,c] not in pri:
    for i in range(len(pri)):
      if dp(pri[i])<dp([a,b,c]):
        pri.insert(i,[a,b,c])
        return
    pri.append([a,b,c])

pri.append([0,0,0])
for i in range(K):
  x,y,z=pri.pop(0)
  print(dp([x,y,z]))
  if x<X-1:
    priq(x+1,y,z)
  if y<Y-1:
    priq(x,y+1,z)
  if z<Z-1:
    priq(x,y,z+1)

Submission status: Time Limit Exceeded

Solution code:
def int_raw():
    return int(input())

def ss_raw():
    return input().split()

def ints_raw():
    return list(map(int, input().split()))

X,Y,Z,K = ints_raw()

A = ints_raw()
B= ints_raw()
C= ints_raw()
#A.sort(reverse=True)
#B.sort(reverse=True)
#C.sort(reverse=True)
def main():
    D = [a+b for a in A for b in B]
    D.sort(reverse=True)
    D = D[:K+1]
    E =[d+c for d in D for c in C]
    E.sort(reverse=True)
    for e in E[:K]:
        print (e)

main()

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 = [ai + bi for (ai, bi) in product(a, b)]
ab.sort(reverse=True)
abc = [abi + ci for (abi, ci) in product(ab[: min(k, x * y * z)], c)]
abc.sort(reverse=True)

for abci in abc[: min(k, x * y * z)]:
    print(abci)

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
 
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))
 
AB_list = [0]*(X*Y)
for i in range(X):
    for j in range(Y):
        AB_list[i*Y+j] = A_list[i] + B_list[j]
AB_list_s = sorted(AB_list)
if len(AB_list_s) > 3000:
    AB_list_s = AB_list_s[-3000:]
W = len(AB_list_s)
ABC_list = [0]*(W*Z)
for i in range(W):
    for j in range(Z):
        ABC_list[i*Z+j] = AB_list_s[i] + C_list[j]
ABC_list_s = sorted(ABC_list)
for i in range(K):
    print(ABC_list_s[-(i+1)])

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

x, y, z, k = list(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 = [ai + bi for (ai, bi) in product(a, b)]
ab.sort(reverse=True)
abc = [abi + ci for (abi, ci) in product(ab[: min(k, x * y * z)], c)]
abc.sort(reverse=True)

for abci in abc[: min(k, x * y * z)]:
    print(abci)

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=[0]*(X*Y)
for i in range(X):
    for j in range(Y):
        A_B[i*Y+j]=A[i]+B[j]
A_B_s=sorted(A_B,reverse=True)
if len(A_B_s)>3000:
    A_B_s=A_B_s[:3000]
W=len(A_B_s)
A_B_C=[0]*(W*Z)
for i in range(W):
    for j in range(Z):
        A_B_C[Z*i+j]=A_B_s[i]+C[j]
A_B_C_s=sorted(A_B_C,reverse=True)
for i in range(K):
    print(A_B_C_s[i])

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_B=[0]*(X*Y)
for i in range(X):
    for j in range(Y):
        A_B[i*Y+j]=A[i]+B[j]
A_B.sort(reverse=True)
if len(A_B)>3000:
    A_B=A_B[:3000]
W=len(A_B)
A_B_C=[0]*(W*Z)
for i in range(W):
    for j in range(Z):
        A_B_C[Z*i+j]=A_B[i]+C[j]
A_B_C.sort(reverse=True)
for i in range(K):
    print(A_B_C[i])

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

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)

a = []
for i, j in product(A[0:K+1], B[0:K+1]):
    a.append(i + j)

ans_list = []
for i, j in product(a, C[0:K+1]):
    ans_list.append(i + j)
ans_list.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

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 = []
mXY = min(X, Y)
for i, j in product(A, B):
    a.append(i + j)
a.sort(reverse=True)
a1 = a[0:K]

ans_list = []
for i, j in product(a1, C[0:K]):
    ans_list.append(i + j)
ans_list.sort(reverse=True)

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

Submission status: Time Limit Exceeded

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

Dtotal = []
Etotal = []

#Dtotalは、AとBの合計を大きい順にソートする
for i in range(x):
    for j in range(y):
        Dtotal.append(A[i]+B[j])
Dtotal = sorted(Dtotal, reverse=True)

#Etotalをソートする
for i in range(x*y):
    for j in range(z):
        Etotal.append(Dtotal[i]+C[j])
Etotal = sorted(Etotal, reverse=True)


#結果を出力する
for i in range(k):
    print(Etotal[i])

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

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

a1 = []
for i, j in product(A, B):
    a1.append(i + j)
a1.sort(reverse=True)

ans_list = []
for i, j in product(a1[0:min(X*Y*Z, 3000)], C):
    ans_list.append(i + j)
ans_list.sort(reverse=True)

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

Submission status: Time Limit Exceeded

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

a = list(reversed(sorted(a)))
b = list(reversed(sorted(b)))
c = list(reversed(sorted(c)))

ab = []
for i in range(min(len(a), k)):
    ax = a[i]
    for j in range(min(len(b), k)):
        bx = b[j]
        ab.append(ax+bx)
ab = list(reversed(sorted(ab)))

abc = []
for i in range(min(k, len(ab))):
    abx = ab[i]
    for j in range(min(k, len(c))):
        cx = c[j]
        abc.append(abx + cx)

abc = list(reversed(sorted(abc)))

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

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

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

Submission status: Time Limit Exceeded

Solution code:
# https://img.atcoder.jp/abc123/editorial.pdf
# 問題D: Cake 123 解法 #2 - 「ある条件」を満たす最大106307通りを全部ソート

def solve(X, Y, Z, K, A, B, C):
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)

    ABC = [a + b + c
           for i1, a in enumerate(A, start=1)
           for i2, b in enumerate(B, start=1)
           for i3, c in enumerate(C, start=1)
           if i1 * i2 * i3 <= K]
    ABC.sort(reverse=True)
    ABC = ABC[:K]

    print("\n".join(map(str, ABC)))

if __name__ == "__main__":
    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()))
    solve(X, Y, Z, K, A, B, C)

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 ax in a:
    for bx in b:
        ab.append(ax+bx)
ab = list(reversed(sorted(ab)))

abc = []
for i in range(min(k, len(ab))):
    abx = ab[i]
    for cx in c:
        abc.append(abx + cx)

abc = list(reversed(sorted(abc)))

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

Submission status: Time Limit Exceeded

Solution code:
# https://img.atcoder.jp/abc123/editorial.pdf
# 問題D: Cake 123 解法 #2 - 「ある条件」を満たす最大106307通りを全部ソート

def solve(X, Y, Z, K, A, B, C):
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)

    ABC = []
    for i1, a in enumerate(A, start=1):
        for i2, b in enumerate(B, start=1):
            for i3, c in enumerate(C, start=1):
                if i1 * i2 * i3 <= K:
                    ABC.append(a + b + c)
    ABC.sort(reverse=True)
    ABC = ABC[:K]

    print("\n".join(map(str, ABC)))

if __name__ == "__main__":
    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()))
    solve(X, Y, Z, K, A, B, C)

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)

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

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

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

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = 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 = []
abc = []
if x>k:
    x=k
if y>k:
    y=k
if z>k:
    z=k

for a in range(x):
    for b in range(y):
        ab.append(A[a]+B[b])
ab.sort(reverse=True)

for l in ab:
  for c in range(z):
      abc.append(l+C[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 = 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 a in A:
    for b in B:
        AB.append(a + b)
AB.sort(reverse=True)

ABC = []
for ab in AB[:K]:
    for c in C[:K]:
        ABC.append(ab + 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())
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 i in range(X) for j in range(Y) for k in range(Z) if (i+1)*(j+1)*(k+1) <= 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)

aa = []
if x>k:
  x=k
if y>k:
  y=k
if z>k:
  z=k
  
for a in range(x):
    for b in range(y):
        for c in range(z):
            aa.append(A[a]+B[b]+C[c])
aa.sort(reverse=True)
for i in range(k):
    print(aa[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)

ABK = []

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

ABK.sort(reverse=True)
ABK = ABK[:k]
ABC = []

for ab in ABK:
    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:
# https://img.atcoder.jp/abc123/editorial.pdf 解法 #1 - 工夫したソートで O(K^2 log K)

#import functools
#import operator
#import itertools

def solve(X, Y, Z, K, A, B, C):
#   AB = [functools.reduce(operator.add, x) for x in itertools.product(A, B)]
    AB = []
    for a in A:
        for b in B:
            AB.append(a + b)
    AB.sort(reverse=True)
    AB = AB[:K]

    ABC = []
    for ab in AB:
        for c in C:
            ABC.append(ab + c)
#   ABC = [functools.reduce(operator.add, x) for x in itertools.product(AB, C)]
    ABC.sort(reverse=True)
    ABC = ABC[:K]

    print("\n".join(map(str, ABC)))

if __name__ == "__main__":
    X, Y, Z, K = list(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)
    solve(X, Y, Z, K, A, B, C)

Submission status: Time Limit Exceeded

Solution code:
# https://img.atcoder.jp/abc123/editorial.pdf 解法 #1 - 工夫したソートで O(K^2 log K)

import functools
import operator
import itertools

def solve(X, Y, Z, K, A, B, C):
    AB = [functools.reduce(operator.add, x) for x in itertools.product(A, B)]
    AB.sort(reverse=True)
    AB = AB[:K]
    ABC = [functools.reduce(operator.add, x) for x in itertools.product(AB, C)]
    ABC.sort(reverse=True)
    ABC = ABC[:K]
    print("\n".join(map(str, ABC)))

if __name__ == "__main__":
    X, Y, Z, K = list(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)
    solve(X, Y, Z, K, A, B, C)

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+1)*(j+1)*(k+1) <= K]
ans.sort(reverse=True)
for i in range(K):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
x,y,z,p = 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()
c.sort()
"""
if x*y*z <= 3*10**6:
    abc = []
    for i in range(x):
        for j in range(y):
            for k in range(z):
                abc.append(a[i]+b[j]+c[k])
    abc.sort()
    abc = abc[::-1]
    for i in range(p):
        print(abc[i])  
    exit()
"""
ab = []
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort()
c = c[::-1]
ab = ab[::-1]
ab.append(-2**62)
ans = []
miru = [0]*z
for i in range(z):
    if i + 1 == z:
        d = -2**60
    else:
        d = c[i+1]+ab[0]
    for j in range(i+1):
        flag = 0
        while ab[miru[j]] + c[j] >= d and (len(ans)<= p or ab[miru[j]] + c[j] >= ans[0]):
             ans.append(ab[miru[j]] + c[j])
             miru[j]+=1
             flag = 1
        if flag and len(ans) >p:
            ans.sort()
ans.sort()
ans = ans[::-1]
for i in range(p):
    print(ans[i])

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

for i in list_cake_1:
	for j in list_cake_2:
		for k in list_cake_3:
			total_ijk = i+j+k
			list_total.append(total_ijk)
				
list_total.sort()

for i in range(1, K+1):
	print(list_total[-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)

ABC = []
for a in range(X):
    for b in range(Y):
        if (a+1) * (b+1) > K:
            break
        for c in range(Z):
            if (a+1) * (b+1) * (c+1) > K:
                break
            ABC.append(A[a] + B[b] + C[c])

for i in range(K):
    print(sorted(ABC, 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()))

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

ans = []
for AB in AB_list:
    if K < len(C):
        for i in range(K):
            out = AB + C[i]
            ans.append(out)
    else:
        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:
from heapq import heappush, nlargest

def main():
    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()]
    h = []
    for i in A:
        for j in B:
            for k in C:
                heappush(h, i + j + k)
    for i in nlargest(K, h):
        print(i)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K = map(int,input().split())
A = [i for i in map(int,input().split())]
B = [i for i in map(int,input().split())]
C = [i for i in map(int,input().split())]
AB = []
for i in range(X):
    for j in range(Y):
        AB.append(A[i]+B[j])
AB.sort(reverse=True)
ABC = []
for i in range(min(K,len(AB))):
    for j in range(Z):
        ABC.append(AB[i]+C[j])
ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
x,y,z,p = 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()
c.sort()
"""
if x*y*z <= 3*10**6:
    abc = []
    for i in range(x):
        for j in range(y):
            for k in range(z):
                abc.append(a[i]+b[j]+c[k])
    abc.sort()
    abc = abc[::-1]
    for i in range(p):
        print(abc[i])  
    exit()
"""
ab = []
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort()
c = c[::-1]
ab = ab[::-1]
ab.append(-2**62)
ans = []
miru = [0]*z
for i in range(z):
    if i + 1 == z:
        d = -2**60
    else:
        d = c[i+1]+ab[0]
    for j in range(i+1):
        flag = 0
        while ab[miru[j]] + c[j] >= d and (len(ans)<= p or ab[miru[j]] + c[j] >= ans[0]):
             ans.append(ab[miru[j]] + c[j])
             miru[j]+=1
             flag = 1
        if flag:
            ans.sort()
ans.sort()
ans = ans[::-1]
for i in range(p):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
x,y,z,p = 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()
c.sort()
"""
if x*y*z <= 3*10**6:
    abc = []
    for i in range(x):
        for j in range(y):
            for k in range(z):
                abc.append(a[i]+b[j]+c[k])
    abc.sort()
    abc = abc[::-1]
    for i in range(p):
        print(abc[i])  
    exit()
"""
ab = []
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort()
c = c[::-1]
ab = ab[::-1]
ab.append(-2**62)
ans = []
miru = [0]*z
for i in range(z):
    if i + 1 == z:
        d = -2**60
    else:
        d = c[i+1]+ab[0]
    for j in range(i+1):
        ans.sort()
        while ab[miru[j]] + c[j] >= d and (len(ans)<= p or ab[miru[j]] + c[j] >= ans[0]):
             ans.append(ab[miru[j]] + c[j])
             miru[j]+=1
ans.sort()
ans = ans[::-1]
for i in range(p):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K = map(int,input().split())
A = [i for i in map(int,input().split())]
B = [i for i in map(int,input().split())]
C = [i for i in map(int,input().split())]
AB = []
for a in A:
    for b in B:
        AB.append(a+b)
AB.sort(reverse=True)
ABC = []
for i in range(min(K,len(AB))):
    for c in C:
        ABC.append(AB[i]+c)
ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])