Problem:


Score: 400 points

Problem Statement

The Patisserie AtCoder sells cakes with number-shaped candles.
There are X, Y and Z kinds of cakes with 1-shaped, 2-shaped and 3-shaped candles, respectively.
Each cake has an integer value called deliciousness, as follows:

The deliciousness of the cakes with 1-shaped candles are A_1, A_2, ..., A_X.

The deliciousness of the cakes with 2-shaped candles are B_1, B_2, ..., B_Y.

The deliciousness of the cakes with 3-shaped candles are C_1, C_2, ..., C_Z.

Takahashi decides to buy three cakes, one for each of the three shapes of the candles, to celebrate ABC 123.

There are X \times Y \times Z such ways to choose three cakes.

We will arrange these X \times Y \times Z ways in descending order of the sum of the deliciousness of the cakes.

Print the sums of the deliciousness of the cakes for the first, second, ..., K-th ways in this list.

Constraints

1 \leq X \leq 1 \ 000

1 \leq Y \leq 1 \ 000

1 \leq Z \leq 1 \ 000

1 \leq K \leq \min(3 \ 000, X \times Y \times Z)

1 \leq A_i \leq 10 \ 000 \ 000 \ 000

1 \leq B_i \leq 10 \ 000 \ 000 \ 000

1 \leq C_i \leq 10 \ 000 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z K
A_1 \ A_2 \ A_3 \ ... \ A_X
B_1 \ B_2 \ B_3 \ ... \ B_Y
C_1 \ C_2 \ C_3 \ ... \ C_Z

Output

Print K lines. The i-th line should contain the i-th value stated in the problem statement.

Sample Input 1

2 2 2 8
4 6
1 5
3 8

Sample Output 1

19
17
15
14
13
12
10
8

There are 2 \times 2 \times 2 = 8 ways to choose three cakes, as shown below in descending order of the sum of the deliciousness of the cakes:

(A_2, B_2, C_2): 6 + 5 + 8 = 19

(A_1, B_2, C_2): 4 + 5 + 8 = 17

(A_2, B_1, C_2): 6 + 1 + 8 = 15

(A_2, B_2, C_1): 6 + 5 + 3 = 14

(A_1, B_1, C_2): 4 + 1 + 8 = 13

(A_1, B_2, C_1): 4 + 5 + 3 = 12

(A_2, B_1, C_1): 6 + 1 + 3 = 10

(A_1, B_1, C_1): 4 + 1 + 3 = 8

Sample Input 2

3 3 3 5
1 10 100
2 20 200
1 10 100

Sample Output 2

400
310
310
301
301

There may be multiple combinations of cakes with the same sum of the deliciousness. For example, in this test case, the sum of A_1, B_3, C_3 and the sum of A_3, B_3, C_1 are both 301.
However, they are different ways of choosing cakes, so 301 occurs twice in the output.

Sample Input 3

10 10 10 20
7467038376 5724769290 292794712 2843504496 3381970101 8402252870 249131806 6310293640 6690322794 6082257488
1873977926 2576529623 1144842195 1379118507 6003234687 4925540914 3902539811 3326692703 484657758 2877436338
4975681328 8974383988 2882263257 7690203955 514305523 6679823484 4263279310 585966808 3752282379 620585736

Sample Output 3

23379871545
22444657051
22302177772
22095691512
21667941469
21366963278
21287912315
21279176669
21160477018
21085311041
21059876163
21017997739
20703329561
20702387965
20590247696
20383761436
20343962175
20254073196
20210218542
20150096547

Note that the input or output may not fit into a 32-bit integer type.



Sample input:
2 2 2 8
4 6
1 5
3 8


Sample output:
19
17
15
14
13
12
10
8

Submission status: Time Limit Exceeded

Solution code:
import heapq

class Heapq:
    def __init__(self, arr, desc=False):
        if desc:
            arr = [-a for a in arr]
        self.sign = -1 if desc else 1
        self.hq = arr
        heapq.heapify(self.hq)

    def pop(self):
        return heapq.heappop(self.hq) * self.sign

    def push(self, a):
        heapq.heappush(self.hq, a * self.sign)

    def top(self):
        return self.hq[0] * self.sign

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

sorted_A = []
A_q = Heapq(A, True)
for i in range(min(X, K)):
    sorted_A.append(A_q.pop())
sorted_B = []
B_q = Heapq(B, True)
for i in range(min(Y, K)):
    sorted_B.append(B_q.pop())
sorted_C = []
C_q = Heapq(C, True)
for i in range(min(Z, K)):
    sorted_C.append(C_q.pop())
# pop_c_list = []
# pop_b_list = []
#
# max_list = []
# for i in range(min(X, K)):
#     a = A_q.pop()
#     for j in range(min(Y, K)):
#         b = B_q.pop()
#         pop_b_list.append(b)
#         for k in range(min(Z, K)):
#             c = C_q.pop()
#             pop_c_list.append(c)
#             max_list.append(a+b+c)
#         for pop_c in pop_c_list:
#             C_q.push(pop_c)
#     for pop_b in pop_b_list:
#         B_q.push(pop_b)

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

max_list = []
for a in sorted_A:
    for b in sorted_B:
        for c in sorted_C:
            max_list.append(a + b + c)

max_list_q = Heapq(max_list, True)
for i in range(K):
    print(max_list_q.pop())

Submission status: Time Limit Exceeded

Solution code:
import heapq

class Heapq:
    def __init__(self, arr, desc=False):
        if desc:
            arr = [-a for a in arr]
        self.sign = -1 if desc else 1
        self.hq = arr
        heapq.heapify(self.hq)

    def pop(self):
        return heapq.heappop(self.hq) * self.sign

    def push(self, a):
        heapq.heappush(self.hq, a * self.sign)

    def top(self):
        return self.hq[0] * self.sign

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

sorted_A = []
A_q = Heapq(A, True)
for i in range(X):
    sorted_A.append(A_q.pop())
sorted_B = []
B_q = Heapq(B, True)
for i in range(Y):
    sorted_B.append(B_q.pop())
sorted_C = []
C_q = Heapq(C, True)
for i in range(Z):
    sorted_C.append(C_q.pop())
# pop_c_list = []
# pop_b_list = []
#
# max_list = []
# for i in range(min(X, K)):
#     a = A_q.pop()
#     for j in range(min(Y, K)):
#         b = B_q.pop()
#         pop_b_list.append(b)
#         for k in range(min(Z, K)):
#             c = C_q.pop()
#             pop_c_list.append(c)
#             max_list.append(a+b+c)
#         for pop_c in pop_c_list:
#             C_q.push(pop_c)
#     for pop_b in pop_b_list:
#         B_q.push(pop_b)

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

max_list = []
for a in sorted_A[:K]:
    for b in sorted_B[:K]:
        for c in sorted_C[:K]:
            max_list.append(a + b + c)

max_list_q = Heapq(max_list, True)
for i in range(K):
    print(max_list_q.pop())

Submission status: Time Limit Exceeded

Solution code:
import heapq

class Heapq:
    def __init__(self, arr, desc=False):
        if desc:
            arr = [-a for a in arr]
        self.sign = -1 if desc else 1
        self.hq = arr
        heapq.heapify(self.hq)

    def pop(self):
        return heapq.heappop(self.hq) * self.sign

    def push(self, a):
        heapq.heappush(self.hq, a * self.sign)

    def top(self):
        return self.hq[0] * self.sign

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_q = Heapq(A, True)
# B_q = Heapq(B, True)
# C_q = Heapq(C, True)
# pop_c_list = []
# pop_b_list = []
#
# max_list = []
# for i in range(min(X, K)):
#     a = A_q.pop()
#     for j in range(min(Y, K)):
#         b = B_q.pop()
#         pop_b_list.append(b)
#         for k in range(min(Z, K)):
#             c = C_q.pop()
#             pop_c_list.append(c)
#             max_list.append(a+b+c)
#         for pop_c in pop_c_list:
#             C_q.push(pop_c)
#     for pop_b in pop_b_list:
#         B_q.push(pop_b)

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

max_list = []
for a in A[:K]:
    for b in B[:K]:
        for c in C[:K]:
            max_list.append(a + b + c)

max_list_q = Heapq(max_list, True)
for i in range(K):
    print(max_list_q.pop())

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)

max_list = []
for a in A[:K]:
    for b in B[:K]:
        for c in C[:K]:
            max_list.append(a+b+c)
max_list.sort(reverse=True)
for max in max_list[:K]:
    print(max)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys

    input = sys.stdin.readline

    x, y, z, k = map(int, input().split())
    A = sorted(map(int, input().split()), reverse=True)
    B = sorted(map(int, input().split()), reverse=True)
    C = sorted(map(int, input().split()), reverse=True)

    def is_ok(p: int):
        cnt = 0
        for a in A:
            for b in B:
                for c in C:
                    if a + b + c < p:
                        break
                    cnt += 1
                    if cnt >= k:
                        return True
        return False

    def binary_search():
        ng = 10 ** 10 + 10
        ok = 0
        while abs(ng - ok) > 1:
            mid = (ng + ok) // 2
            if is_ok(mid):
                ok = mid
            else:
                ng = mid
        return ok

    def logger(p: int):
        ret = []
        for a in A:
            for b in B:
                for c in C:
                    if a + b + c < p:
                        break
                    ret.append(a + b + c)
        return ret

    p = binary_search()  # k個以上のボーダー
    ret = logger(p + 1)
    if len(ret) < k:
        ret.extend([p] * (k - len(ret)))
    ret.sort(reverse=True)
    print(*ret[:k], sep='\n')


main()

# O(K^2logmax)
# https://img.atcoder.jp/abc123/editorial.pdf

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys

    input = sys.stdin.readline

    x, y, z, k = map(int, input().split())
    A = sorted(map(int, input().split()), reverse=True)
    B = sorted(map(int, input().split()), reverse=True)
    C = sorted(map(int, input().split()), reverse=True)

    ret = sorted(
        (a + b + c
         for p, a in enumerate(A)
         for q, b in enumerate(B)
         for r, c in enumerate(C)
         if p * q * r <= k
         ), reverse=True)[:k]

    print(*ret, sep='\n')


main()

# O(Klog^3K)
# https://img.atcoder.jp/abc123/editorial.pdf

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys

    input = sys.stdin.readline

    x, y, z, k = map(int, input().split())
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))
    C = tuple(map(int, input().split()))

    AB = sorted((a + b for a in A for b in B), reverse=True)[:k]
    ret = sorted((ab + c for ab in AB for c in C), reverse=True)[:k]

    print(*ret, sep='\n')


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())))
D = []
E = []
for i in range(x):
  for j in range(y):
    D.append(A[i]+B[j])
D = sorted(D,reverse=True)[:k]
for j in range(len(D)):
  for i in range(z):
    E.append(C[i]+D[j])
E = sorted(E, reverse=True)
for i in range(k):
  print(E[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())))
D = []
E = []
for i in range(x):
  for j in range(y):
    D.append(A[i]+B[j])
D = sorted(D, reverse=True)
for j in range(len(D)):
  for i in range(z):
    E.append(C[i]+D[j])
E = sorted(E, reverse=True)
for i in range(k):
  print(E[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())))
D = []
E = []
for i in range(x):
  for j in range(y):
    D.append(A[i]+B[j])
D.sort()
for i in range(z):
  for j in range(min(len(D),k)):
    E.append(C[i]+D[::-1][j])
print("\n".join(map(str,sorted(E)[::-1][: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()))
D = []
E = []
for i in range(x):
  for j in range(y):
    D.append(A[i]+B[j])
for i in range(z):
  for j in range(len(D)):
    E.append(C[i]+D[j])
print("\n".join(map(str,sorted(E)[::-1][: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()))
D = []
E = []
for i in range(x):
  for j in range(y):
    D.append(A[i]+B[j])
for i in range(z):
  for j in range(len(D)):
    E.append(C[i]+D[j])
print("\n".join(map(str,sorted(E)[::-1][:min(3000,k)])))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

# sum of deliciousness が border以上の組み合わせが K個以上あればTrue
def is_ok(a, b, c, X, Y, Z, K, border):
    cnt = 0
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                if a[i] + b[j] + c[k] < border:
                    break
                cnt += 1
                if cnt >= K:
                    return True
    return False

def main():
    x, y, z, K = map(int, sys.stdin.readline().split())
    abc = np.array(sys.stdin.read().split(), dtype=np.int64)
    a = abc[:x]
    b = abc[x:x + y]
    c = abc[x + y:x + y+ z]
    a = np.sort(a)[::-1]
    b = np.sort(b)[::-1]
    c = np.sort(c)[::-1]

    l, r = 3, 3 * 10 ** 10
    while l <= r:
        border = (l + r) // 2
        if is_ok(a, b, c, x, y, z, K, border):
            l = border + 1
        else:
            r = border - 1

    res = []
    cnt = 0
    for i in range(x):
        for j in range(y):
            for k in range(z):
                sum_deli = a[i] + b[j] + c[k]
                if sum_deli < r:
                    break
                res.append(sum_deli)
                cnt += 1
                if cnt >= K:
                    break
                

    res = sorted(res, reverse=True)[:K]
    print('\n'.join(map(str, res)))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

# sum of deliciousness が border以上の組み合わせが K個以上あればTrue
def is_ok(a, b, c, X, Y, Z, K, border, found=False):
    cnt = 0
    res = []
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                sum_deli = a[i] + b[j] + c[k]
                if sum_deli < border:
                    break
                cnt += 1
                res.append(sum_deli)
                if cnt >= K:
                    if not found:
                        return True
                    else:
                        return res
    return False

def main():
    x, y, z, K = map(int, sys.stdin.readline().split())
    abc = np.array(sys.stdin.read().split(), dtype=np.int64)
    a = abc[:x]
    b = abc[x:x + y]
    c = abc[x + y:x + y+ z]
    a = np.sort(a)[::-1]
    b = np.sort(b)[::-1]
    c = np.sort(c)[::-1]

    l, r = 3, 3 * 10 ** 10
    while l <= r:
        border = (l + r) // 2
        if is_ok(a, b, c, x, y, z, K, border):
            l = border + 1
        else:
            r = border - 1

    res = is_ok(a, b, c, x, y, z, K, r, found=True)
    res = sorted(res, reverse=True)[:K]
    print('\n'.join(map(str, res)))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

x, y, z, k = map(int,input().split())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
Z = list(map(int, input().split()))
mul_x_y = []
for i in X:
    for j in Y:
        mul_x_y.append(i+j)
mul_x_y.sort()
mul_x_y.reverse()
Z.sort()
Z.reverse()
mul_x_y = mul_x_y[:k]
Z = Z[:k]
ans = []
for i in mul_x_y:
    for j in Z:
        ans.append(i + j)
ans.sort()
ans.reverse()
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(x) for x in input().split()]
B=[int(x) for x in input().split()]
C=[int(x) for x in input().split()]
A.sort(reverse =True)
B.sort(reverse =True)
C.sort(reverse =True)
answer=[]
for p in range(min(X,K)):
  for q in range(min(Y,K)):
    for r in range(min(Z,K)):
      answer +=[A[p]+B[q]+C[r]]
answer.sort()
for i in range(K):
  print(answer[-i-1])

Submission status: Time Limit Exceeded

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

C.sort(reverse=True)
AB = [0 for _ in range(X*Y)]
ctr = 0
for x in range(X):
    for y in range(Y):
        AB[ctr] = A[x]+B[y]
        ctr += 1
AB.sort(reverse=True)
ABC = [0 for _ in range(min(len(AB),K)*Z)]
ctr = 0
for ab in range(min(len(AB),K)):
    for z in range(Z):
        ABC[ctr] = AB[ab]+C[z]
        ctr += 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()))
C.sort(reverse=True)
AB = []
ctr = 0
for x in range(X):
    for y in range(Y):
        AB.append(A[x]+B[y])
        ctr += 1
AB.sort(reverse=True)
ABC = []
ctr = 0
for ab in range(min(len(AB),K)):
    for z in range(Z):
        ABC.append(AB[ab]+C[z])
        ctr += 1
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])

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

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

    ABC = []
    for k in range(Z):
        for ab_index in range(len(AB)):
            ABC.append(C[k]+AB[ab_index])

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


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

Submission status: Time Limit Exceeded

Solution code:
import heapq

tmp = list(map(int, input().split()))
x = tmp[0]
y = tmp[1]
z = tmp[2]
k = tmp[3]
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)

#log = [[[False for _ in range(z)] for _ in range(y)] for _ in range(x)]
log = [[[False] * z for _ in range(y)] for _ in range(x)]
cnt = 0
h = []
heapq.heappush(h, (-(a[0]+b[0]+c[0]), 0, 0, 0))
for i in range(k): 
  tmp = heapq.heappop(h)
  print(-tmp[0])
  if tmp[1]+1 < x and log[tmp[1]+1][tmp[2]][tmp[3]] == False:
    heapq.heappush(h, (-(a[tmp[1]+1]+b[tmp[2]]+c[tmp[3]]), tmp[1]+1, tmp[2], tmp[3]))
    log[tmp[1]+1][tmp[2]][tmp[3]] = True
  if tmp[2]+1 < y and log[tmp[1]][tmp[2]+1][tmp[3]] == False:
    heapq.heappush(h, (-(a[tmp[1]]+b[tmp[2]+1]+c[tmp[3]]), tmp[1], tmp[2]+1, tmp[3]))
    log[tmp[1]][tmp[2]+1][tmp[3]] = True
  if tmp[3]+1 < z and log[tmp[1]][tmp[2]][tmp[3]+1] == False:
    heapq.heappush(h, (-(a[tmp[1]]+b[tmp[2]]+c[tmp[3]+1]), tmp[1], tmp[2], tmp[3]+1))
    log[tmp[1]][tmp[2]][tmp[3]+1] = True

Submission status: Time Limit Exceeded

Solution code:
import heapq

tmp = list(map(int, input().split()))
x = tmp[0]
y = tmp[1]
z = tmp[2]
k = tmp[3]
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)

log = [[[False for _ in range(z)] for _ in range(y)] for _ in range(x)]

cnt = 0
h = []
heapq.heappush(h, (-(a[0]+b[0]+c[0]), 0, 0, 0))
for i in range(k): 
  tmp = heapq.heappop(h)
  print(-tmp[0])
  if tmp[1]+1 < x and log[tmp[1]+1][tmp[2]][tmp[3]] == False:
    heapq.heappush(h, (-(a[tmp[1]+1]+b[tmp[2]]+c[tmp[3]]), tmp[1]+1, tmp[2], tmp[3]))
    log[tmp[1]+1][tmp[2]][tmp[3]] = True
  if tmp[2]+1 < y and log[tmp[1]][tmp[2]+1][tmp[3]] == False:
    heapq.heappush(h, (-(a[tmp[1]]+b[tmp[2]+1]+c[tmp[3]]), tmp[1], tmp[2]+1, tmp[3]))
    log[tmp[1]][tmp[2]+1][tmp[3]] = True
  if tmp[3]+1 < z and log[tmp[1]][tmp[2]][tmp[3]+1] == False:
    heapq.heappush(h, (-(a[tmp[1]]+b[tmp[2]]+c[tmp[3]+1]), tmp[1], tmp[2], tmp[3]+1))
    log[tmp[1]][tmp[2]][tmp[3]+1] = True

Submission status: Time Limit Exceeded

Solution code:
import heapq

tmp = list(map(int, input().split()))
x = tmp[0]
y = tmp[1]
z = tmp[2]
k = tmp[3]
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)

log = [[[False for _ in range(z)] for _ in range(y)] for _ in range(x)]

cnt = 0
h = []
heapq.heappush(h, (-(a[0]+b[0]+c[0]), 0, 0, 0))
while len(h) != 0 and cnt < k: 
  tmp = heapq.heappop(h)
  print(-tmp[0])
  cnt += 1
  if tmp[1]+1 < x and log[tmp[1]+1][tmp[2]][tmp[3]] == False:
    heapq.heappush(h, (-(a[tmp[1]+1]+b[tmp[2]]+c[tmp[3]]), tmp[1]+1, tmp[2], tmp[3]))
    log[tmp[1]+1][tmp[2]][tmp[3]] = True
  if tmp[2]+1 < y and log[tmp[1]][tmp[2]+1][tmp[3]] == False:
    heapq.heappush(h, (-(a[tmp[1]]+b[tmp[2]+1]+c[tmp[3]]), tmp[1], tmp[2]+1, tmp[3]))
    log[tmp[1]][tmp[2]+1][tmp[3]] = True
  if tmp[3]+1 < z and log[tmp[1]][tmp[2]][tmp[3]+1] == False:
    heapq.heappush(h, (-(a[tmp[1]]+b[tmp[2]]+c[tmp[3]+1]), tmp[1], tmp[2], tmp[3]+1))
    log[tmp[1]][tmp[2]][tmp[3]+1] = True

Submission status: Time Limit Exceeded

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

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

ab_1 = []
for i in range(x):
    for j in range(y):
        ab_1.append(a[i]+b[j])
ab_1.sort(reverse=True)   
key = min([k, x*y*z])
ab_2 = ab_1[:key]

abc = []
for p in ab_2:
    for q in c:
        abc.append(p+q)
abc.sort(reverse=True)
for i in range(key):
    print(abc[i])

Submission status: Time Limit Exceeded

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

al = list(map(int,input().split()))
bl = list(map(int,input().split()))
cl = list(map(int,input().split()))

al = sorted(al)
bl = sorted(bl)
cl = sorted(cl)
rank = []
for xx in range(x-1,-1,-1):
    tmp = al[xx]+bl[-1]+cl[-1]
    if len(rank) >= k:
        if rank[-1] >= tmp:
            break
    for yy in range(y-1, -1, -1):
        tmp = al[xx]+bl[yy]+cl[-1]
        if len(rank) >= k:
            if rank[-1] >= tmp:
                break
        for zz in range(z-1,-1,-1):
            if len(rank) < k:
                rank.append(al[xx]+bl[yy]+cl[zz])
                rank = sorted(rank, reverse=True)
            else:
                tmp = al[xx]+bl[yy]+cl[zz]
                if rank[-1] < tmp:
                    rank.pop()
                    rank.append(tmp)
                    rank = sorted(rank, reverse=True)
                else:
                    break

for i in rank:
    print(i)

Submission status: Time Limit Exceeded

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

al = list(map(int,input().split()))
bl = list(map(int,input().split()))
cl = list(map(int,input().split()))

al = sorted(al)
bl = sorted(bl)
cl = sorted(cl)
rank = []
for xx in range(x-1,-1,-1):
    for yy in range(y-1, -1, -1):
        for zz in range(z-1,-1,-1):
            if len(rank) < k:
                rank.append(al[xx]+bl[yy]+cl[zz])
                rank = sorted(rank, reverse=True)
            else:
                tmp = al[xx]+bl[yy]+cl[zz]
                if rank[-1] < tmp:
                    rank.pop()
                    rank.append(tmp)
                    rank = sorted(rank, reverse=True)
                else:
                    break

for i in rank:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    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=1)
    B.sort(reverse=1)
    C.sort(reverse=1)
    A=A[:k]
    B=B[:k]
    C=C[:k]
    S=[]
    from itertools import product
    for i,j,l in product(range(k),repeat=3):
        if(i>=x or j>=y or l>=z): continue
        S.append(A[i]+B[j]+C[l])
    S.sort(reverse=1)
    for i in range(k):
        print(S[i])
resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    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()))
    S=[]
    from itertools import product
    for a,b,c in product(A,B,C):
        S.append(a+b+c)
    S.sort(reverse=1)
    for i in range(k):
        print(S[i])
resolve()

Submission status: Time Limit Exceeded

Solution code:
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

x,y,z,k = MAP()
a = MAP()
b = MAP()
c = MAP()

a.sort()
b.sort()

l = []
for i in a:
    for j in b:
        l.append(i+j)

l.sort()
c.sort()
l2 = []
for i in l[-k:]:
    for j in c[-k:]:
        l2.append(i+j)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
x,y,z,K=map(int,input().split())
listsa=list(map(int,input().split()))#長さはｘ
listsb=list(map(int,input().split()))#長さはy
listsc=list(map(int,input().split()))#長さはz
if x*y>3000:
    suballlist=[]
    for i in listsa:
        for j in listsb:
            suballlist.append(i+j)
    suballlist=sorted(suballlist,reverse=True)
    uselist=suballlist[:3000]
    alllist=[]
    
    for i in uselist:
        for j in listsc:
            alllist.append(i+j)
            
    alllist=sorted(alllist,reverse=True)
    for i in range(K):
        print(alllist[i])
    
elif x*y<=3000:
    alllist=[]
    for i in listsa:
        for j in listsb:
            for k in listsc:
                alllist.append(i+j+k)
                
    alllist=sorted(alllist,reverse=True)
    for i in range(K):
        print(alllist[i])

Submission status: Time Limit Exceeded

Solution code:
x,y,z,K=map(int,input().split())
listsa=list(map(int,input().split()))#長さはｘ
listsb=list(map(int,input().split()))#長さはy
listsc=list(map(int,input().split()))#長さはz
if x*y>3000:
    suballlist=[]
    for i in listsa:
        for j in listsb:
            suballlist.append(i+j)
    suballlist=sorted(suballlist,reverse=True)
    uselist=suballlist[:3000]
    alllist=[]
    for i in uselist:
        for j in listsc:
            alllist.append(i+j)
    alllist=sorted(alllist,reverse=True)
    for i in range(K):
        print(alllist[i])
    
elif x*y<=3000:
    alllist=[]
    for i in listsa:
        for j in listsb:
            for k in listsc:
                alllist.append(i+j+k)
                
    alllist=sorted(alllist,reverse=True)
    for i in range(K):
        print(alllist[i])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

x,y,z,k = inpl()
a = inpl()
b = inpl()
c = inpl()
a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
q = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
heapify(q)
old = [(0,0,0)]
for i in range(k):
    k,l,m,n = heappop(q)
    print(-k)
    if l + 1 < x and not (l+1,m,n) in old:
        heappush(q,[-(a[l+1] + b[m] + c[n]), l+1, m, n])
        old.append((l+1,m,n))
    if m + 1 < y and not(l,m+1,n) in old:
        heappush(q,[-(a[l] + b[m+1] + c[n]), l, m+1, n])
        old.append((l,m+1,n))
    if n + 1 < z and not (l,m,n+1)  in old:
        heappush(q,[-(a[l] + b[m] + c[n+1]), l, m, n+1])
        old.append((l,m,n+1))
    # print(q)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import heapq

def main():
    X, Y, Z, K = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]
    B = [int(x) for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = []
    for a in A:
        for b in B:
            for c in C:
                heapq.heappush(ans, -(a + b + c))

    for _ in range(K):
        print(-heapq.heappop(ans))



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

def main():
    X, Y, Z, K = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]
    B = [int(x) for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = []
    for a in A:
        for b in B:
            for c in C:
                ans.append(a + b + c)

    ans_s = sorted(ans, reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k=map(int,input().split())
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
c=[int(x) for x in input().split()]

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

s=list()

for l in range(x):
    for m in range(y):
      for n in range(z):
        if l*m*n>k:
           break
        s.append(a[l]+b[m]+c[n])
 
s.sort(reverse=True)
for i in range(k):
  print(s[i])

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k=map(int,input().split())
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
c=[int(x) for x in input().split()]

t=list()
for i in range(x):
  for j in range(y):
    t.append(a[i]+b[j])

s=list()
t.sort(reverse=True)
for i in range(min(k,x*y)):
  for j in range(z):
    s.append(t[i]+c[j])

s.sort(reverse=True)

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

Submission status: Time Limit Exceeded

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

t=list()
for i in range(x):
  for j in range(y):
    t.append(a[i]+b[j])

s=list()
t.sort(reverse=True)
for i in range(min(k,x*y)):
  for j in range(z):
    s.append(t[i]+c[j])

s.sort(reverse=True)

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