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


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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

lst = []
for i in A:
    for j in B:
        for k in C:
            lst += [(i + j + k)]

lst.sort(reverse = True)

count = 1
while count <= K:
    print (lst[count-1])
    count += 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()))

a.sort(key=lambda x: -x)
b.sort(key=lambda x: -x)
c.sort(key=lambda x: -x)

xyz = []

for ia in range(len(a)):
    if ia > k:
        break
    for ib in range(len(b)):
        if ia*ib > k:
            break
        for ic in range(len(c)):
            if (ia*ib*ic) <= k:
                xyz.append(a[ia]+b[ib]+c[ic])
            else:
                break

xyz.sort(key=lambda x: -x)
for l in xyz[:k]:
    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()))

a.sort(key=lambda x: -x)
b.sort(key=lambda x: -x)
c.sort(key=lambda x: -x)

xyz = []

for ia in range(len(a)):
    for ib in range(len(b)):
        for ic in range(len(c)):
            if (ia*ib*ic) <= k:
                xyz.append(a[ia]+b[ib]+c[ic])
            else:
                break

xyz.sort(key=lambda x: -x)
for l in xyz[:k]:
    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()))

a.sort(key=lambda x: -x)
b.sort(key=lambda x: -x)
c.sort(key=lambda x: -x)

xy = []
xyz = []
m = min(3000, x*y*z)


for ia in a:
    for ib in b:
        xy.append(ia+ib)
xy.sort(key=lambda x: -x)
xy = xy[:m]

for ix in xy:
    for ic in c:
        xyz.append(ix+ic)


ans = sorted(xyz, key=lambda x: -x)
ans = ans[:k]

for a in ans:
    print(a)

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)
LIS = []
for i in range(X):
    for j in range(Y):
        for k in range(min((3000//(i+1)*(j+1)),Z)):
            SUM = A[i] + B[j] + C[k]
            LIS.append(SUM)
LIS.sort(reverse = True)
for i in range(K):
    print(LIS[i])

Submission status: Time Limit Exceeded

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


def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    AB = list( itertools.product(A,B) )

    ABS = list(map(lambda c: sum(c), AB))
    ABS.sort(reverse=True)
    ABS2 = ABS[0:K]

    ABC = list( itertools.product(ABS2,C) )
    ABCS = list(map(lambda c: sum(c), ABC))
    ABCS.sort(reverse=True)

    for k in range(K):
        print(ABCS[k])



# Generated by 1.1.4 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:
#!/usr/bin/env python3
import sys
import itertools


def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)

    AB = list( itertools.product(A,B) )

    ABS = list(map(lambda c: sum(c), AB))
    ABS.sort(reverse=True)
    ABS2 = ABS[0:K]

    ABC = list( itertools.product(ABS2,C) )
    ABCS = list(map(lambda c: sum(c), ABC))
    ABCS.sort(reverse=True)

    for k in range(K):
        print(ABCS[k])



# Generated by 1.1.4 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:
#!/usr/bin/env python3
import sys
import itertools


def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)

    com = list( itertools.product(A,B,C) )

    S = list(map(lambda c: sum(c), com))
    S.sort(reverse=True)

    for k in range(K):
        print(S[k])
    return


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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A.sort()
B.sort()
C.sort()
A.reverse()
B.reverse()
C.reverse()
LIS = []
LISA = []
LISB = []
LISC = []
for s in range(X):
    for t in range(Y):
        if (s+1)*(t+1) <= 3000:
            for u in range(Z):
                if 3000//(u+1) + 1 > min(s,t):
                    LISA.append(s)
                    LISB.append(t)
                    LISC.append(u)
for i in range(len(LISA)):
    PR = A[LISA[i]]+B[LISB[i]]+C[LISC[i]]
    LIS.append(PR)
LIS.sort()
LIS.reverse()

for i in range(min(len(LIS),K)):
    print(LIS[i])

Submission status: Time Limit Exceeded

Solution code:
def solve(p):
    cnt = 0
    for i in range(len(A)):
        for j in range(len(B)):
            for k in range(len(C)):
                if A[i] + B[j] + C[k] < p:
                    break
                cnt +=1
                if cnt >=k:
                    return True
    return False

def find(p):
    num = []
    for i in range(len(A)):
        for j in range(len(B)):
            for k in range(len(C)):
                if A[i] + B[j] + C[k] <= p:
                    break
                else:
                    num.append(A[i] + B[j] + C[k])
    return num

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)

max_taste = max(A) + max(B)+max(C)

M = max_taste
m = 0
n = (M+m) //2
while m!=n:
    if solve(n):
        M = n
    else:
        m = n
    n = (M+m) //2
    #print(M, m, n)
        
item = sorted(find(n), reverse=True)
if len(item) >=K:
    for i in range(K):
        print(item[i])
else:
    for i in range(len(item)):
        print(item[i])
    for _ in range(K- len(item)):
        print(n)

Submission status: Time Limit Exceeded

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

L=[]
K = min(K, 3000, X*Y*Z)

for x in range(X):
    for y in range(Y):
        for z in range(Z):
            L.append(A[x]+B[y]+C[z])

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

list_a.sort(reverse=True)
list_b.sort(reverse=True)
list_c.sort(reverse=True)

list = []

for la in list_a:
    for lb in list_b:
        list.append([la,lb,la+lb])

list.sort(key=lambda x:x[2],reverse=True)

list2 = []

for a,b,n in list:
    for lc in list_c:
        list2.append(n+lc)

list2.sort(reverse=True)

cnt = 0

for i in list2:
    print(i)
    cnt += 1
    if cnt == K:
        exit()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
D - Cake 123
https://atcoder.jp/contests/abc123/tasks/abc123_d

"""
import sys


from itertools import product

def solve(X, Y, Z, K, A, B, C):
    res_AB = sorted([sum(p) for p in product(A, B)], reverse=True)[:K]
    return sorted([sum(p) for p in product(res_AB, C)], reverse=True)[:K]


def main(args):
    X, Y, Z, K = map(int, input().split())
    A = [int(a) for a in input().split()]
    B = [int(b) for b in input().split()]
    C = [int(c) for c in input().split()]
    ans = solve(X, Y, Z, K, A, B, C)
    print(*ans, sep='\n')


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
D - Cake 123
https://atcoder.jp/contests/abc123/tasks/abc123_d

"""
import sys


from itertools import product

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

    while len(A)-1 * len(B)-1 * len(C)-1 > K and len(A) * len(B) * len(C) >= 10000:
        m = sorted([[A[0], 'a'], [B[0], 'b'], [C[0], 'c']])
        _, mi = m.pop(0)
        if mi == 'a':
            A.pop(0)
        elif mi == 'b':
            B.pop(0)
        else:
            C.pop(0)

    return sorted([sum(p) for p in product(A, B, C)], reverse=True)[:K]


def main(args):
    X, Y, Z, K = map(int, input().split())
    A = [int(a) for a in input().split()]
    B = [int(b) for b in input().split()]
    C = [int(c) for c in input().split()]
    ans = solve(X, Y, Z, K, A, B, C)
    print(*ans, sep='\n')


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
D - Cake 123
https://atcoder.jp/contests/abc123/tasks/abc123_d

"""
import sys


from itertools import product

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

    while len(A)-1 * len(B)-1 * len(C)-1 > K:
        m = sorted([[A[0], 'a'], [B[0], 'b'], [C[0], 'c']])
        _, mi = m.pop(0)
        if mi == 'a':
            A.pop(0)
        elif mi == 'b':
            B.pop(0)
        else:
            C.pop(0)

    return sorted([sum(p) for p in product(A, B, C)], reverse=True)[:K]


def main(args):
    X, Y, Z, K = map(int, input().split())
    A = [int(a) for a in input().split()]
    B = [int(b) for b in input().split()]
    C = [int(c) for c in input().split()]
    ans = solve(X, Y, Z, K, A, B, C)
    print(*ans, sep='\n')


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Time Limit Exceeded

Solution code:
x, y, z, k = [int(i) for i in input().split()]
p = [int(a) for a in input().split()]
q = [int(b) for b in input().split()]
r = [int(c) for c in input().split()]
p = sorted(p, reverse=True)
q = sorted(q, reverse=True)
r = sorted(r, reverse=True)
pqr = []
for a in range(len(p)):
    if a+1 > k:
        continue
    for b in range(len(q)):
        if b+1 > k:
            continue
        elif (a+1)*(b+1) > k:
            continue
        for c in range(len(r)):
            if c+1 > k:
                continue
            elif (a+1)*(c+1) > k:
                continue
            elif (b+1)*(c+1) > k:
                continue
            elif (a+1)*(b+1)*(c+1) > k:
                continue
            pqr.append(p[a] + q[b] + r[c])
pqr = sorted(pqr, reverse=True)
for i in range(k):
    print(pqr[i])

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
A = [int(x) for x in input().split()]
A.sort(reverse=True)
B = [int(x) for x in input().split()]
B.sort(reverse=True)
C = [int(x) for x in input().split()]
C.sort(reverse=True)


bi_comb = []
for i in A[:K]:
    for j in B[:K]:
        bi_comb.append(i+j)
bi_comb.sort(reverse=True)
tri_comb = []
for k in bi_comb[:K]:
    for l in C[:K]:
        tri_comb.append(k + l)
tri_comb.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = [int(x) for x in input().split()]
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [int(c) for c in input().split()]
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
ABC = []
for a in range(len(A)):
    if a+1 > K:
        continue
    for b in range(len(B)):
        if b+1 > K:
            continue
        elif (a+1)*(b+1) > K:
            continue
        for c in range(len(C)):
            if c+1 > K:
                continue
            elif (a+1)*(c+1) > K:
                continue
            elif (b+1)*(c+1) > K:
                continue
            elif (a+1)*(b+1)*(c+1) > K:
                continue
            ABC.append(A[a] + B[b] + C[c])
ABC = sorted(ABC, reverse=True)
for i in range(K):
    print(ABC[i])

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = [int(x) for x in input().split()]
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [int(c) for c in input().split()]
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
ABC = []
for a in range(len(A)):
    if a+1 > K:
        continue
    for b in range(len(B)):
        if (a+1)*(b+1) > K:
            continue
        for c in range(len(C)):
            if (a+1)*(b+1)*(c+1) > K:
                continue
            ABC.append(A[a] + B[b] + C[c])
ABC = sorted(ABC, reverse=True)
for i in range(K):
    print(ABC[i])

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = [int(x) for x in input().split()]
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [int(c) for c in input().split()]
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
ABC = []
for a in range(len(A)):
    for b in range(len(B)):
        if (a+1)*(b+1) > K:
            continue
        for c in range(len(C)):
            if (a+1)*(b+1)*(c+1) > K:
                continue
            ABC.append(A[a] + B[b] + C[c])
ABC = sorted(ABC, reverse=True)
for i in range(K):
    print(ABC[i])

Submission status: Time Limit Exceeded

Solution code:
l = [int(x) for x in input().split()]
x = [int(x) for x in input().split()]
y = [int(x) for x in input().split()]
z = [int(x) for x in input().split()]

result_l = []
for i in x:
    for j in y:
        for k in z:
            result_l.append(i+j+k)
result_l.sort(reverse=True)
for ind in range(l[-1]):
    print(result_l[ind])

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

for i in a:
  for j in b:
    ab.append(i + j)

ab = sorted(ab, reverse=True)
ab_k = ab[:k]

for i in ab_k:
  for j in c:
    ans.append(i + j)

ans = sorted(ans, reverse=True)

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

#37

Submission status: Time Limit Exceeded

Solution code:
import sys

w1 = input()
x,y,z,k = w1.split(" ")

x = int(x)
y = int(y)
z = int(z)
k = int(k)

x1 = []
wx1 = input()
wx1 = wx1.split()

wy1 = input()
wy1 = wy1.split()

wz1 = input()
wz1 = wz1.split()

for i in range(x):
    x1.append(int(wx1[i]))
x1.sort(reverse=True)

y1 = []
for i in range(y):
    y1.append(int(wy1[i]))
y1.sort(reverse=True)

z1 = []
for i in range(y):
    z1.append(int(wz1[i]))
z1.sort(reverse=True)

xy = []
for i in x1:
	for j in y1:
		xy.append(i+j)

xy.sort(reverse = True)

xyz = []
n = 0
for i in z1:
    for j in xy:
        xyz.append((j + i))
        n += 1
        if n == k:
            break

xyz.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

comb_ab = []
for a, b in itertools.product(A, B):
    comb_ab.append(sum([a, b]))

comb_ab_top = sorted(comb_ab, reverse=True)[:K]

comb_abc = []
for ab, c in itertools.product(comb_ab_top, C):
    comb_abc.append(sum([ab, c]))

ans = sorted(comb_abc, reverse=True)[:K]
print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

comb = []
for a, b, c in itertools.product(A, B, C):
    comb.append(sum([a, b, c]))

print(*sorted(comb, reverse=True)[: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)

a_b = []
for i in range(x):
    for j in range(y):
        if (i+1)*(j+1) <= k:
            a_b.append(a[i] + b[j])
        else:
            break
a_b.sort(reverse=True)
ans = []
for i in range(min(k, len(a_b))):
    for j in range(min(k, z)):
        ans.append(a_b[i] + c[j])

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

Submission status: Time Limit Exceeded

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

a_b = []
for i in range(x):
    for j in range(y):
        a_b.append(a[i] + b[j])
a_b.sort(reverse=True)
ans = []
for i in range(min(k, len(a_b))):
    for j in range(min(k, z)):
        ans.append(a_b[i] + c[j])

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

Submission status: Time Limit Exceeded

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

a_b = []
for i in range(x):
    for j in range(y):
        a_b.append(a[i] + b[j])
a_b.sort(reverse=True)
ans = []
for i in range(len(a_b)):
    for j in range(z):
        ans.append(a_b[i] + c[j])
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Time Limit Exceeded

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

alist.sort(reverse=True)
blist.sort(reverse=True)
clist.sort(reverse=True)

ablist = []
for i in alist:
  for j in blist:
      ablist.append(i+j)
      
ablist.sort(reverse=True)
ablist = ablist[0:K]

abclist = []
for ij in ablist:
  for k in clist:
    abclist.append(ij+k)
    
abclist.sort(reverse=True)
for ijk in range(K):
  print(abclist[ijk])

Submission status: Time Limit Exceeded

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

ans = []
for i in range(x):
    for j in range(y):
        for l in range(z):
            if i * j * l <= k:
                ans.append(a[i] + b[j] + c[l])
            else:
                break
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 = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
a = list(reversed(sorted(a)))
b = list(reversed(sorted(b)))
c = list(reversed(sorted(c)))
ans = []
for i in range(x):
    for j in range(y):
        for l in range(z):
            if i * j * l <= k:
                ans.append(a[i] + b[j] + c[l])
            else:
                break
ans = list(reversed(sorted(ans)))
for i in range(k):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
import heapq

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)

q = [[(A[0]+B[0]+C[0])*(-1),0,0,0]]
heapq.heapify(q)
count = 0
alr = [[[False]*Z for i in range(Y)] for i in range(X)]
alr[0][0][0] = True

while count < K:
    poped = heapq.heappop(q)
    ans = poped[0]*(-1)
    print(ans)
    i, j, k = poped[1], poped[2], poped[3]
    if i+1 < X and not alr[i+1][j][k]:
        alr[i+1][j][k] = True
        heapq.heappush(q, [(A[i+1]+B[j]+C[k])*(-1), i+1, j, k])
    if j+1 < Y and not alr[i][j+1][k]:
        alr[i][j+1][k] = True
        heapq.heappush(q, [(A[i]+B[j+1]+C[k])*(-1), i, j+1, k])
    if k+1 < Z and not alr[i][j][k+1]:
        alr[i][j][k+1] = True
        heapq.heappush(q, [(A[i]+B[j]+C[k+1])*(-1), i, j, k+1])
    count += 1

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
K1 = min(K, len(A))

B = sorted(list(map(int, input().split())), reverse=True)
K2 = min(K, len(B))

C = sorted(list(map(int, input().split())), reverse=True)
K3 = min(K, len(C))

K12 = min(K1 * K2, K)

M1 = [0] * (K1 * K2)
k = 0
for i in range(0, K1):
    for j in range(0, K2):
        M1[k] = A[i] + B[j]
        k += 1

M1.sort(reverse=True)

M = [0] * (K12 * K3)
k = 0
for i in range(0, K12):
    for j in range(0, K3):
        M[k] = M1[i] + C[j]
        k += 1

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

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
K1 = min(K, len(A))

B = list(map(int, input().split()))
B.sort(reverse=True)
K2 = min(K, len(B))

C = list(map(int, input().split()))
C.sort(reverse=True)
K3 = min(K, len(C))

K12 = min(K1 * K2, K)

M1 = [0] * (K1 * K2)
k = 0
for i in range(0, K1):
    for j in range(0, K2):
        M1[k] = A[i] + B[j]
        k += 1

M1.sort(reverse=True)

M = [0] * (K12 * K3)
k = 0
for i in range(0, K12):
    for j in range(0, K3):
        M[k] = M1[i] + C[j]
        k += 1

M.sort(reverse=True)

for i in range(0, K):
    print(M[i])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = input().split()
X = int(X)
Y = int(Y)
Z = int(Z)
K = int(K)
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [int(c) for c in input().split()]
M = [len(A), len(B), len(C)]
if max(M) == len(A):
    if min(M) == len(B):
        P, Q, R = A, C, B
    else:
        P, Q, R = A, B, C
elif max(M) == len(B):
    if min(M) == len(C):
        P, Q, R = B, A, C
    else:
        P, Q, R = B, C, A
else:
    if min(M) == len(A):
        P, Q, R = C, B, A
    else:
        P, Q, R = C, A, B
PQ = []
for p in range(len(P)):
    for q in range(len(Q)):
        PQ.append(P[p] + Q[q])
PQ = sorted(PQ, reverse=True)
PQR = []
for pq in range(min(K, len(PQ))):
    for r in range(len(R)):
        PQR.append(PQ[pq] + R[r])
PQR = sorted(PQR, reverse=True)
for i in range(K):
    print(PQR[i])