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 sys

def main():
    sinputl = sys.stdin.readline

    _, _, _, k = [int(x) for x in sinputl().split()]

    input = [[int(x) for x in sinputl().split()] for _ in range(3)]

    answer = []

    for i in range(3):
        del input[i][k:]
        input[i].sort(reverse=True)

    for a in input[0]:
        for b in input[1]:
            for c in input[2]:
                answer.append(a + b + c)

    answer.sort(reverse=True)

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    sinputl = sys.stdin.readline

    _, _, _, k = [int(x) for x in sinputl().split()]

    input = [[int(x) for x in sinputl().split()] for _ in range(3)]

    answer = []

    input[0] = input[0][:k]
    input[1] = input[1][:k]
    input[2] = input[2][:k]


    for a in input[0]:
        for b in input[1]:
            for c in input[2]:
                answer.append(a + b + c)

    answer.sort(reverse=True)

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    sinputl = sys.stdin.readline

    _, _, _, k = [int(x) for x in sinputl().split()]

    input = [[int(x) for x in sinputl().split()] for _ in range(3)]

    answer = []

    for a in input[0]:
        for b in input[1]:
            for c in input[2]:
                answer.append(a + b + c)

    answer.sort(reverse=True)

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


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 = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)

D = []
for i in range(X):
    for j in range(Y):
        D.append(A[i] + B[j])

D.sort(reverse=True)
E = []
for i in range(min(K, len(D))):
    for j in range(Z):
        E.append(D[i] + C[j])

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

Submission status: Time Limit Exceeded

Solution code:
# abcde    s=input()    s='abcde'
# abcde    s=list(input())    s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)    a=int(input())    a=5
# 1 2    | x,y = s_inpl()   |    x=1,y=2
# 1 2 3 4 5 ... n 　    li = input().split()    li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　    li = inpl()    li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　    li = input().split('T')    li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import math
import copy
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
from bisect import bisect_left, bisect_right
# import numpy as np

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]

def i_inpl(): return int(input())
def s_inpl(): return map(int,input().split())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")
MAX_DIGIT = 50

############
############
############

X, Y, Z, K = l_inpl()
A = l_inpl()
B = l_inpl()
C = l_inpl()

top_k_AB = []
for i in range(X):
    for j in range(Y):
        top_k_AB.append(A[i] + B[j])

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

top_k_ABC = []
for i in range(len(top_k_AB)):
    for j in range(Z):
        top_k_ABC.append(top_k_AB[i]+C[j])

top_k_ABC.sort(reverse=True)
print("\n".join([ str(s) for s in top_k_ABC[:K]]))

Submission status: Time Limit Exceeded

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

C = sorted(C, reverse=True)
AB = sorted([a + b for a in A for b in B], reverse=True)

ABC_k = sorted([ab + c for ab in AB[:K] for c in C], reverse=True)

for ans in ABC_k[:K]:
    print(ans)

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 = []
for a in A:
    for b in B:
        AB.append(a+b)
AB.sort(reverse=True)
AB = AB[:K]
for c in range(min(len(C),K)):
    for ab in AB:
        ABC.append(ab+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 = 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)
# m = A[0]+B[0]+C[0]
AB = []
ABC = []
for a in A:
    for b in B:
        AB.append(a+b)
AB.sort(reverse=True)
AB = AB[:K]
for c in C:
    for ab in AB:
        ABC.append(c+ab)
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)
# m = A[0]+B[0]+C[0]
AB = []
for a in A:
    for b in B:
        AB.append(a+b)
ABC = []
for c in C:
    for ab in AB:
        ABC.append(c+ab)
ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])

Submission status: Time Limit Exceeded

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

def solve():
    X, Y, Z, K, A, B, C = get_input()
    A = MergeSort(A)[:K]
    B = MergeSort(B)[:K]
    C = MergeSort(C)[:K]
    lB = len(B)
    lC = len(C)
    sumAB = []
    for i in range(lB):
        tmpA = [ele + B[i] for ele in A]
        sumAB = MergeArray(sumAB, tmpA)[:K]
    sumABC = []
    for i in range(lC):
        tmpAB = [ele + C[i] for ele in sumAB]
        sumABC = MergeArray(sumABC, tmpAB)[:K]
    for i in range(K):
        print sumABC[i]

def solve2():
    X, Y, Z, K, A, B, C = get_input()
    A.sort(reverse = True)
    B.sort(reverse = True)
    C.sort(reverse = True)
    sumABC = []
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                if (i + 1) * (j + 1) * (k + 1) <= K:
                    sumABC.append(A[i] + B[j] + C[k])
    sumABC.sort(reverse = True)
    for i in range(K):
        print sumABC[i]

if __name__ == '__main__':
    solve2()

Submission status: Time Limit Exceeded

Solution code:
def MergeSort(arr):
    if len(arr) <= 1:
        return arr
    mid = len(arr) // 2
    left = arr[:mid]
    right = arr[mid:]
    left = MergeSort(left)
    right = MergeSort(right)
    return MergeArray(left, right)

def MergeArray(left, right):
    merged = []
    li, ri = 0, 0
    while li < len(left) and ri < len(right):
        if left[li] > right[ri]:
            merged.append(left[li])
            li += 1
        else:
            merged.append(right[ri])
            ri += 1
    if li < len(left):
        merged.extend(left[li:])
    elif ri < len(right):
        merged.extend(right[ri:])
    return merged

def solve():
    X, Y, Z, K = [int(_) for _ in raw_input().split()]
    A = [int(_) for _ in raw_input().split()]
    B = [int(_) for _ in raw_input().split()]
    C = [int(_) for _ in raw_input().split()]
    A = MergeSort(A)[:K]
    B = MergeSort(B)[:K]
    C = MergeSort(C)[:K]
    lB = len(B)
    lC = len(C)
    sumAB = []
    for i in range(lB):
        tmpA = [ele + B[i] for ele in A]
        sumAB = MergeArray(sumAB, tmpA)
    sumABC = []
    for i in range(lC):
        tmpAB = [ele + C[i] for ele in sumAB]
        sumABC = MergeArray(sumABC, tmpAB)
    for i in range(K):
        print sumABC[i]

if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
# To avoid the use of reverse, define inverse order intentionally
def merge(a, b, ab):
        i, j = 0, 0
        
        while(i < len(a) or j < len(b)):
                if(j >= len(b) or (i < len(a) and a[i] > b[j])): # take from a if a[i] is larger than b[j]
                        ab[i+j] = a[i]
                        i += 1
                else:
                        ab[i+j] = b[j]
                        j += 1

# merge sort with inverse order
def merge_sort(l):
        if(len(l) == 1): return None
        
        l1 = l[:(len(l)//2)]
        l2 = l[(len(l)//2):]

        merge_sort(l1)
        merge_sort(l2)
        merge(l1, l2, l)

        return None

XYZK = list(map(int, input().split()))
tastes = []

for n in range(3): tastes.append(list(map(int, input().split())))

ab_tastes = []

for i in range(XYZK[0]):
        for j in range(XYZK[1]):
                ab_tastes.append(tastes[0][i]+tastes[1][j])

merge_sort(ab_tastes)
fin = []

for i in range(min(XYZK[0]*XYZK[1], XYZK[3])):
        for j in range(XYZK[2]):
                fin.append(ab_tastes[i]+tastes[2][j])

merge_sort(fin)

for i in range(XYZK[3]):
        print(fin[i])

Submission status: Time Limit Exceeded

Solution code:
def main():
    x, y, z, k = [int(i) for i in input().split()]
    a = [int(i) for i in input().split()]
    b = [int(i) for i in input().split()]
    c = [int(i) for i in input().split()]
    ab = []
    for aa in a:
        for bb in b:
            ab.append(aa+bb)
    ab.sort(reverse=True)
    abc = []
    for i in range(min(k,len(ab))):
        for j in range(len(c)):
            abc.append(ab[i]+c[j])
    abc.sort(reverse=True)
    for i in range(k):
        print(abc[i])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    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()]
    ans = []
    for aa in a:
        for bb in b:
            for cc in c:
                ans.append(aa+bb+cc)
    ans.sort(reverse=True)
    for aa in ans[:k]:
        print(aa)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# To avoid the use of reverse, define inverse order intentionally
def merge(a, b):
        i, j = 0, 0
        merged = []
        
        while(i < len(a) or j < len(b)):
                if(j >= len(b) or (i < len(a) and a[i] > b[j])): # take from a if a[i] is larger than b[j]
                        merged.append(a[i])
                        i += 1
                else:
                        merged.append(b[j])
                        j += 1
        
        return merged

# merge sort with inverse order
def merge_sort(l):
        if(len(l) == 1): return l
        
        l1 = l[0:(len(l)//2)]
        l2 = l[(len(l)//2):]

        ll1 = merge_sort(l1)
        ll2 = merge_sort(l2)

        return merge(ll1, ll2)

XYZK = list(map(int, input().split()))
tastes = []

for n in range(3): tastes.append(list(map(int, input().split())))

ab_tastes = []

for i in range(XYZK[0]):
        for j in range(XYZK[1]):
                ab_tastes.append(tastes[0][i]+tastes[1][j])

ab_tastes = merge_sort(ab_tastes)
final = []

for i in range(min(XYZK[0]*XYZK[1], XYZK[3])):
        for j in range(XYZK[2]):
                final.append(ab_tastes[i]+tastes[2][j])

final = merge_sort(final)

for i in range(XYZK[3]):
        print(final[i])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
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()))

out = deque()
out2 = deque()

for i in range(X):
    for j in range(Y):
        out.append(A[i]+B[j])
out = sorted(out, reverse=True)
del out[K:]
for i in range(len(out)):
    for j in range(Z):
        out2.append(out[i]+C[j])
out2 = sorted(out2, reverse=True)
for i in range(K):
    print(out2[i])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
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()))

out = deque()
out2 = deque()

for i in range(X):
    for j in range(Y):
        out.append(A[i]+B[j])
out = list(out)
out.sort(reverse = True)
del out[K:]
for i in range(len(out)):
    for j in range(Z):
        out2.append(out[i]+C[j])
out2 = list(out2)
out2.sort(reverse = True)
for i in range(K):
    print(out2[i])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
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()))

out = deque()

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

Submission status: Time Limit Exceeded

Solution code:
XYZK = list(map(int, input().split()))
tastes = []

for n in range(3): tastes.append(list(map(int, input().split())))

ab_tastes = []

for i in range(XYZK[0]):
    for j in range(XYZK[1]):
        ab_tastes.append(tastes[0][i]+tastes[1][j])

ab_tastes.sort()
ab_tastes.reverse()
final = []

for i in range(min(XYZK[0]*XYZK[1], XYZK[3])):
    for j in range(XYZK[2]):
        final.append(ab_tastes[i]+tastes[2][j])

final.sort()
final.reverse()

for i in range(XYZK[3]):
    print(final[i])

Submission status: Time Limit Exceeded

Solution code:
def sm(a, b):
    for i in range(3):
        if(a[i] < b[i]): return True
        elif(a[i] > b[i]): return False
    
    return False

def ins_point(array, v):
    l = 0
    r = len(array) - 1

    if(sm(v, array[0])): return 0
    elif(sm(array[r], v)): return r+1

    while(l < r):
        m = (l + r) // 2
        if(m == 0): return 1
        elif(sm(array[m], v)): l = m + 1
        elif(sm(v, array[m])): r = m - 1
        else: return m
    
    return l

def child(v, ind):
    ans = []
    dec = False

    for i in range(3):
        if(i == ind and v[ind] > 0):
            ans.append(v[i]-1)
            dec = True
        else: ans.append(v[i])
    
    return ans, dec

def search(array, v):
    if(len(array) == 0): return False
    l = 0
    r = len(array) - 1

    while(l < r):
        m = (l + r) // 2
        if(sm(array[m], v)): l = m + 1
        elif(sm(v, array[m])): r = m - 1
        else: return True
    
    return array[l] == v

XYZK = list(map(int, input().split()))
tastes = []

for n in range(3):
    tmp = list(map(int, input().split()))
    tmp.sort()
    tastes.append(tmp)

maxima = sum(XYZK[0:3]) - 3
inds = []
for i in range(maxima+1): inds.append([])

nex = [i-1 for i in XYZK[0:3]]

for i in range(XYZK[3]):
    summ = 0
    for j in range(3): summ += tastes[j][nex[j]]
    print(summ)

    # nex をその合計の箇所に insert
    # それぞれの階層でソートしながら(大小関係を考えながら) insert
    # もちろん空だったらただ入れるだけでいい
    if(len(inds[sum(nex)]) == 0): inds[sum(nex)].append(nex)
    else: inds[sum(nex)].insert(ins_point(inds[sum(nex)], nex), nex)
    
    # inds の検索
    # 階層が空でなかったら各要素の子供ベクトルを逐次見ていく
    # 子供ベクトルがアーカイブ (inds) に含まれているか否かで判定
    # m_tmp と next を更新していく
    m_tmp = 0
    j = maxima

    while(len(inds[j]) > 0 and j > 0):
        for ji in range(len(inds[j])):
            for jj in range(3):
                v, dec = child(inds[j][ji], jj)
                if(dec and not(search(inds[sum(v)], v))):
                    tmp = 0
                    for n in range(3): tmp += tastes[n][v[n]]
                    if(m_tmp <= tmp):
                        nex = v
                        m_tmp = tmp
        j -= 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(reverse=True)
#b.sort(reverse=True)
c.sort(reverse=True)
E = [0] * x*y

total = [0] * (x*y + z*k)
idx = 0
for i in range(x):
      for j in range(y):
            E[idx] = a[i] + b[j]
            idx += 1
E.sort(reverse=True)
idx = 0
for i in range(min(x*y,k)):
      for j in range(min(z,k)):
            total[idx] = E[i] + c[j]
            idx += 1

total.sort(reverse=True)
for i in range(k):
      print(total[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()))
l=[]
for a in A:
    for b in B:
        for c in C:
            s=a+b+c
            l.append(s)
m=sorted(l,reverse=True)
[print(m[i]) for i in range(K) ]

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

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

r = [i+j for i,j in list(itertools.product(a, b))]
r.sort(reverse=True)

r = [i+j for i,j in list(itertools.product(r[0:min(3001,x*y*z)], c))]
r.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
import itertools


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 = [a + b for (a, b) in list(itertools.product(A, B))]
AB.sort(reverse=True)
ans = [ab + c for (ab, c) in list(itertools.product(AB[:min(3000, X*Y*Z)], C))]
ans.sort(reverse=True)

print(*ans[:K],sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import itertools


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 = [a + b for (a, b) in list(itertools.product(A, B))]
AB.sort(reverse=True)
ans = [ab + c for (ab, c) in list(itertools.product(AB[:min(3000, X*Y*Z)], C))]
ans.sort(reverse=True)
for i in range(K):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

r = [i+j for i,j in list(itertools.product(a, b))]
r.sort(reverse=True)

r = [i+j for i,j in list(itertools.product(r[0:K], c))]
r.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

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

r = [i+j for i,j in list(itertools.product(a, b))]
r.sort(reverse=True)

r = [i+j for i,j in list(itertools.product(r[0:K], c))]
r.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
import heapq
#Aの中でa番目、Bの中でb番目、Cの中でc番目のものがabc > Kの時、大きい順のK番目以内にabcの組が入ることはない
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 = []
check = [[[0] * Z for _ in range(Y)] for _ in range(X)]
heapq.heappush(h, [-(A[0]+B[0]+C[0]), 0, 0, 0])
check[0][0][0] = 1
for count in range(K):
    ans = heapq.heappop(h)
    i, j , k = ans[1], ans[2], ans[3]
    print (-ans[0])
    if i + 1 < X:
        if check[i+1][j][k] == 0:
            heapq.heappush(h, [-(A[i+1]+B[j]+C[k]), i+1, j, k])
            check[i+1][j][k] = 1
    if j + 1 < Y:
        if check[i][j+1][k] == 0:
            heapq.heappush(h, [-(A[i]+B[j+1]+C[k]), i, j+1, k])
            check[i][j+1][k] = 1
    if k + 1 < Z:
        if check[i][j][k+1] == 0:
            heapq.heappush(h, [-(A[i]+B[j]+C[k+1]), i, j, k+1])
            check[i][j][k+1] = 1

Submission status: Time Limit Exceeded

Solution code:
#Aの中でa番目、Bの中でb番目、Cの中でc番目のものがabc > Kの時、大きい順のK番目以内にabcの組が入ることはない
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)

lst = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1) * (j+1) * (k+1) <= K:
                lst += [A[i]+B[j]+C[k]]
lst.sort(reverse = True)
for l in range(K):
    print (lst[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(reverse = True)
B.sort(reverse = True)
C.sort(reverse = True)

lst = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1) * (j+1) * (k+1) <= K:
                lst += [A[i]+B[j]+C[k]]
lst.sort(reverse = True)
for l in range(K):
    print (lst[l])

Submission status: Time Limit Exceeded

Solution code:
#単純な全探索-->TLE-->Cの配列を扱うときに工夫
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:
        lst += [i + j]

lst.sort(reverse = True)
C.sort(reverse = True)

ans = []
for i in range(min(K, len(lst))):
    for j in range(min(K, len(C))):
        ans += [lst[i]+C[j]]

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

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]"):
    import heapq
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    h = []
    heapq.heappush(h, [-(A[0]+B[0]+C[0]), 0, 0, 0])
    ans = []
    used = [[[False for _ in range(K)] for _ in range(K)] for _ in range(K)]
    used[0][0][0] = True
    for _ in range(K):
        e, i, j, k = heapq.heappop(h)
        ans.append(-e)
        if i+1 < X and not used[i+1][j][k]:
            used[i+1][j][k] = True
            heapq.heappush(h, [-(A[i+1]+B[j]+C[k]), i+1, j, k])
        if j+1 < Y and not used[i][j+1][k]:
            used[i][j+1][k] = True
            heapq.heappush(h, [-(A[i]+B[j+1]+C[k]), i, j+1, k])
        if k+1 < Z and not used[i][j][k+1]:
            used[i][j][k+1] = True
            heapq.heappush(h, [-(A[i]+B[j]+C[k+1]), i, j, k+1])
    for elem in ans:
        print(elem)





#def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
#    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:
#!/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]"):
    import heapq
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    h = []
    heapq.heappush(h, [-(A[0]+B[0]+C[0]), 0, 0, 0])
    ans = []
    used = [[[False for _ in range(Z)] for _ in range(Y)] for _ in range(X)]
    used[0][0][0] = True
    for _ in range(K):
        e, i, j, k = heapq.heappop(h)
        ans.append(-e)
        if i+1 < X and not used[i+1][j][k]:
            used[i+1][j][k] = True
            heapq.heappush(h, [-(A[i+1]+B[j]+C[k]), i+1, j, k])
        if j+1 < Y and not used[i][j+1][k]:
            used[i][j+1][k] = True
            heapq.heappush(h, [-(A[i]+B[j+1]+C[k]), i, j+1, k])
        if k+1 < Z and not used[i][j][k+1]:
            used[i][j][k+1] = True
            heapq.heappush(h, [-(A[i]+B[j]+C[k+1]), i, j, k+1])
    for elem in ans:
        print(elem)





#def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
#    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:
#!/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]"):
    import heapq
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    h = heapq.heapify([])
    h = []
    heapq.heappush(h, [-(A[0]+B[0]+C[0]), 0, 0, 0])
    ans = []
    used = [[[False for _ in range(Z)] for _ in range(Y)] for _ in range(X)]
    used[0][0][0] = True
    while len(ans) < K:
        e, i, j, k = heapq.heappop(h)
        ans.append(-e)
        if i+1 < X and not used[i+1][j][k]:
            used[i+1][j][k] = True
            heapq.heappush(h, [-(A[i+1]+B[j]+C[k]), i+1, j, k])
        if j+1 < Y and not used[i][j+1][k]:
            used[i][j+1][k] = True
            heapq.heappush(h, [-(A[i]+B[j+1]+C[k]), i, j+1, k])
        if k+1 < Z and not used[i][j][k+1]:
            used[i][j][k+1] = True
            heapq.heappush(h, [-(A[i]+B[j]+C[k+1]), i, j, k+1])
    for elem in ans:
        print(elem)





#def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
#    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()