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:
def d_cake_123_binary_search(X, Y, Z, K, A, B, C):
    # editionalの解法4 O(K^2log(max(P))
    # 参考: https://atcoder.jp/contests/abc123/submissions/4871511
    import bisect
    ab = sorted([e1 + e2 for e2 in B for e1 in A])  # A, Bの要素は全部調べる
 
    # rejected 以上の値は、美味しさの合計がそれ以上である個数がK個未満である
    accepted, rejected = -1, 10**11
    while abs(accepted - rejected) > 1:
        mid = (accepted + rejected) // 2
        count = sum([len(ab) - bisect.bisect_left(ab, mid - e) for e in C])
        if count >= K:
            accepted = mid
        else:
            rejected = mid
 
    ans = []
    for e in C:
        idx = bisect.bisect_left(ab, accepted - e)
        for i in range(idx, len(ab)):
            ans.append(e + ab[i])
    ans.sort(reverse=True)
    return '\n'.join(map(str, ans[:K]))
 
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()]
print(d_cake_123_binary_search(X, Y, Z, K, A, B, C))

Submission status: Time Limit Exceeded

Solution code:
x = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
y =  []
for i in range (len(a)):
  for j in range(len(b)):
    for k in range(len(c)):
      y.append(a[i]+b[j]+c[k])

y.sort(reverse = True)
for i in range (x[3]):
  print(y[i])

Submission status: Time Limit Exceeded

Solution code:
[X,Y,Z,K] = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
ABC = []
for ia,a in enumerate(A):
    for ib,b in enumerate(B):
        for ic,c in enumerate(C):
            if ia*ib*ic <= K:
                ABC.append(a+b+c)
ABC.sort(reverse=True)
ABC = ABC[:K]
for i in ABC:print(i)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def ca():
    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()]
    c.sort(reverse=True)
    AB=[i+j for i in a for j in b]
    AB.sort(reverse=True)
    ans=[i+j for i in AB[:k]for j in c]
    ans.sort(reverse=True)
    for i in ans[:k]:
        print(i)
    exit()
print(ca())

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k=map(int,input().split())
a=[int(i)for i in input().split()]
b=[int(i)for i in input().split()]
c=[int(i)for i in input().split()]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ans=[]
for A in a:
    for B in b:
        ans+=[A+B]
ans.sort(reverse=True)
ANS=[]
for K in ans[:k]:
    for C in c:
        ANS+=[C+K]
ANS.sort(reverse=True)
for i in range(k):
    print(ANS[i])

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K = map(int,input().split())
abc= [list((map(int,input().split()))) for _ in range(3)]
from itertools import product as pro
ans = []
for a in pro(abc[0],abc[1],abc[2]):
    ans.append(int(a[0])+int(a[1])+int(a[2]))
ans.sort(reverse=1)
for i in range(K):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
N = list(map(int, input().split()))[-1]
A = sorted(list(map(int, input().split())), reverse=True)[:N]
B = sorted(list(map(int, input().split())), reverse=True)[:N]
C = sorted(list(map(int, input().split())), reverse=True)[:N]

result = [A[i]+B[j]+C[k] 
          for i in range(len(A)) 
          for j in range(len(B)) 
          for k in range(len(C))
          if i+j+k <= N
         ]
  
result = sorted(result, reverse=True)
[print(result[i]) for i in range(N)]

Submission status: Time Limit Exceeded

Solution code:
N = list(map(int, input().split()))[-1]
A = sorted(list(map(int, input().split())))[::-1][:N]
B = sorted(list(map(int, input().split())))[::-1][:N]
C = sorted(list(map(int, input().split())))[::-1][:N]

result = [A[i]+B[j]+C[k] 
          for i in range(len(A)) 
          for j in range(len(B)) 
          for k in range(len(C))
          if i+j+k <= N
         ]
  
result = sorted(result)[::-1]
[print(result[i]) for i in range(N)]

Submission status: Time Limit Exceeded

Solution code:
N = list(map(int, input().split()))[-1]
A = sorted(list(map(int, input().split())))[::-1]
B = sorted(list(map(int, input().split())))[::-1]
C = sorted(list(map(int, input().split())))[::-1]

result = [A[i]+B[j]+C[k] 
          for i in range(len(A)) 
          for j in range(len(B)) 
          for k in range(len(C))
          if i+j+k <= N
         ]
  
result = sorted(result)[::-1]
[print(result[i]) for i in range(N)]

Submission status: Time Limit Exceeded

Solution code:
# TODO D - Cake 123
# https://atcoder.jp/contests/abc123/tasks/abc123_d

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

A = map(int,raw_input().split())
B = map(int,raw_input().split())
C = map(int,raw_input().split())

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

for i in ABC[:K]:
    print '{}'.format(i)

Submission status: Time Limit Exceeded

Solution code:
N = list(map(int, input().split()))[-1]
A = sorted(list(map(int, input().split())))[::-1]
B = sorted(list(map(int, input().split())))[::-1]
C = sorted(list(map(int, input().split())))[::-1]

result = []
for i in range(len(A)):
  for j in range(len(B)):
    for k in range(len(C)):
      if i+j+k <= N:
        result.append(A[i]+B[j]+C[k])
  
result = sorted(result)[::-1]
for i in range(N):
  print(result[i])

Submission status: Time Limit Exceeded

Solution code:
# TODO D - Cake 123
# https://atcoder.jp/contests/abc123/tasks/abc123_d

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

A = [int(x) for x in raw_input().split()]
B = [int(y) for y in raw_input().split()]
C = [int(z) for z in raw_input().split()]

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

for i in ABC[:K]:
    print '{}'.format(i)

Submission status: Time Limit Exceeded

Solution code:
N = list(map(int, input().split()))[-1]
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

first = [i+j for i in A for j in B]
first = sorted(first)[::-1]
second = [first[i]+j for i in range(len(first)) for j in C if i <= N]

print("\n".join([str(value) for value in sorted(second)[::-1][:N]]))

Submission status: Time Limit Exceeded

Solution code:
N = list(map(int, input().split()))[-1]
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

first = [i+j for i in A for j in B]
first = sorted(first)[::-1]
second = [i+j for i in first for j in C]

print("\n".join([str(value) for value in sorted(second)[::-1][:N]]))

Submission status: Time Limit Exceeded

Solution code:
# TODO D - Cake 123
# https://atcoder.jp/contests/abc123/tasks/abc123_d

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

A = [int(x) for x in raw_input().split()]
B = [int(y) for y in raw_input().split()]
C = [int(z) for z in raw_input().split()]

AB = list()

for x in range(X):
    for y in range(Y):
        AB.append(A[x] + B[y])

AB.sort(reverse=True)

N = min(X*Y, K)

ABC = list()
for ab in range(N):
    for z in range(Z):
        ABC.append(AB[ab] + C[z])
ABC.sort(reverse=True)

for i in range(K):
    print '{}'.format(ABC[i])

Submission status: Time Limit Exceeded

Solution code:
# TODO D - Cake 123
# https://atcoder.jp/contests/abc123/tasks/abc123_d

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

A = [int(x) for x in raw_input().split()]
B = [int(y) for y in raw_input().split()]
C = [int(z) for z in raw_input().split()]

AB = list()

for x in range(X):
    for y in range(Y):
        AB.append(A[x] + B[y])

AB.sort(reverse=True)

N = min(X*Y, K)
ABC = list()

for xy in range(N):
    for z in range(Z):
        ABC.append(AB[xy] + C[z])

top_k = sorted(ABC, reverse=True)[:K]

for i in range(K):
    print '{:d}'.format(top_k[i])

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = [int(elm) for elm in input().split(" ")]
Xs = [X, Y, Z]
A = sorted([int(elm) for elm in input().split(" ")], reverse=True)
B = sorted([int(elm) for elm in input().split(" ")], reverse=True)
C = sorted([int(elm) for elm in input().split(" ")], reverse=True)


def sum_(kouho):
    a, b, c = kouho
    return sum([A[a], B[b], C[c]])

kouhos = []

kouhos = [[0, 0, 0]]
cur = kouhos[0]
sum_cur = sum_(cur)
kouhos_vals = [sum_cur]
#all_ = {}
all0_ = {}
all1_ = {}
if sum_cur % 2 == 0:
    all0_[sum_cur] = [cur]
else:
    all1_[sum_cur] = [cur]

def add_kouhos(kouhos, kouhos_vals, new_kouhos):
    if len(kouhos) == 0:
        kouhos = new_kouhos
        kouhos_vals = [sum_(kouho) for kouho in new_kouhos]
        return kouhos, kouhos_vals

    len_kouhos = len(kouhos)

    new_i = 1
    new_kouho = new_kouhos[-new_i] # 後ろ(小さいkouho)から追加していく
    sum_new_kouho = sum_(new_kouho)

    n_kouhos = len(new_kouhos)

    last_kouho_i = len_kouhos - 1
    while True:
        if sum_new_kouho <= sum_(kouhos[-1]):
            #kouhos.append(new_kouho)
            kouhos.insert(last_kouho_i+1, new_kouho)
            new_i += 1
            if new_i == n_kouhos+1:
                return kouhos, kouhos_vals        
            new_kouho = new_kouhos[-new_i]
            sum_new_kouho = sum_(new_kouho)
        else:
            break

    for cur_kouho_i in reversed(range(0, len_kouhos)):
        loop = True
        sum_cur_kouho = sum_(kouhos[cur_kouho_i])
        while loop:
            if sum_new_kouho < sum_cur_kouho: # 下からkouhosを辿って行き、超えられなかった一つ手前にinsert
                kouhos.insert(cur_kouho_i+1, new_kouho)
                kouhos_vals.insert(cur_kouho_i+1, sum_(new_kouho))
                new_i += 1
                if new_i == n_kouhos+1:
                    return kouhos, kouhos_vals
                new_kouho = new_kouhos[-new_i]
                sum_new_kouho = sum_(new_kouho)
            else:
                loop = False

    while True:
        kouhos.insert(0, new_kouho) # ひたすら先頭にnew_kouhosを小さいものから追加していく
        kouhos_vals.insert(0, sum_new_kouho)
        new_i += 1
        if new_i == n_kouhos+1:
            return kouhos, kouhos_vals
        else:
            new_kouho = new_kouhos[-new_i]
            sum_new_kouho = sum_(new_kouho)

def make_new_kouhos(all1_, all2_):
    new_kouhos = []
    tmp_dict = {}
    for i in range(3):
        new_kouho = cur.copy()
        new_kouho[i] += 1

        prop_new_kouho = new_kouho
        
        cond2 = new_kouho[i] < Xs[i]
        if not cond2:
            continue
        sum_val = sum_(prop_new_kouho)

        if sum_val % 2 ==0:
            all_ = all0_
        else:
            all_ = all1_
        
        #cond11 = sum_val not in all_.keys()
        cond11 = sum_val not in all_.keys()
        #try:
        #    all[sum_val]
        #    cond11 = False
        #except:
        #    cond11 = True

        if cond11:
            cond1 = cond11
        else:
            cond12 = new_kouho not in all_[sum_val]
            cond1 = cond12
        
        if cond1: # ここが計算大
            new_kouhos.append(new_kouho)
            tmp_dict[tuple(new_kouho)] = sum_val
            if cond11:
                all_[sum_val] = [new_kouho]
            else:
                all_[sum_val].append(new_kouho)
    new_kouhos = sorted(new_kouhos, key=lambda x:-tmp_dict[tuple(x)]) ## 案
    return all0_, all1_, new_kouhos

for _ in range(K):
    cur = kouhos[0]
    print(sum_(cur))
    kouhos = kouhos[1:]
    kouhos_vals = kouhos_vals[1:]

    all0_, all1_,  new_kouhos = make_new_kouhos(all0_, all1_)

    if len(new_kouhos) > 0:
        kouhos, kouhos_vals = add_kouhos(kouhos, kouhos_vals, new_kouhos)

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = [int(elm) for elm in input().split(" ")]
Xs = [X, Y, Z]
A = sorted([int(elm) for elm in input().split(" ")], reverse=True)
B = sorted([int(elm) for elm in input().split(" ")], reverse=True)
C = sorted([int(elm) for elm in input().split(" ")], reverse=True)


def sum_(kouho):
    a, b, c = kouho
    return sum([A[a], B[b], C[c]])

kouhos = []

kouhos = [[0, 0, 0]]
cur = kouhos[0]
sum_cur = sum_(cur)
kouhos_vals = [sum_cur]
all_ = {}
all_[sum_cur] = [cur]

def add_kouhos(kouhos, kouhos_vals, new_kouhos):
    if len(kouhos) == 0:
        kouhos = new_kouhos
        kouhos_vals = [sum_(kouho) for kouho in new_kouhos]
        return kouhos, kouhos_vals

    len_kouhos = len(kouhos)

    new_i = 1
    new_kouho = new_kouhos[-new_i] # 後ろ(小さいkouho)から追加していく
    sum_new_kouho = sum_(new_kouho)

    n_kouhos = len(new_kouhos)

    last_kouho_i = len_kouhos - 1
    while True:
        if sum_new_kouho <= sum_(kouhos[-1]):
            #kouhos.append(new_kouho)
            kouhos.insert(last_kouho_i+1, new_kouho)
            new_i += 1
            if new_i == n_kouhos+1:
                return kouhos, kouhos_vals        
            new_kouho = new_kouhos[-new_i]
            sum_new_kouho = sum_(new_kouho)
        else:
            break

    for cur_kouho_i in reversed(range(0, len_kouhos)):
        loop = True
        sum_cur_kouho = sum_(kouhos[cur_kouho_i])
        while loop:
            if sum_new_kouho < sum_cur_kouho: # 下からkouhosを辿って行き、超えられなかった一つ手前にinsert
                kouhos.insert(cur_kouho_i+1, new_kouho)
                kouhos_vals.insert(cur_kouho_i+1, sum_(new_kouho))
                new_i += 1
                if new_i == n_kouhos+1:
                    return kouhos, kouhos_vals
                new_kouho = new_kouhos[-new_i]
                sum_new_kouho = sum_(new_kouho)
            else:
                loop = False

    while True:
        kouhos.insert(0, new_kouho) # ひたすら先頭にnew_kouhosを小さいものから追加していく
        kouhos_vals.insert(0, sum_new_kouho)
        new_i += 1
        if new_i == n_kouhos+1:
            return kouhos, kouhos_vals
        else:
            new_kouho = new_kouhos[-new_i]
            sum_new_kouho = sum_(new_kouho)

def make_new_kouhos(all_):
    new_kouhos = []
    tmp_dict = {}
    for i in range(3):
        new_kouho = cur.copy()
        new_kouho[i] += 1

        prop_new_kouho = new_kouho
        
        cond2 = new_kouho[i] < Xs[i]
        if not cond2:
            continue
        sum_val = sum_(prop_new_kouho)
        
        #cond11 = sum_val not in all_.keys()
        cond11 = sum_val not in all_.keys()
        #try:
        #    all[sum_val]
        #    cond11 = False
        #except:
        #    cond11 = True

        if cond11:
            cond1 = cond11
        else:
            cond12 = new_kouho not in all_[sum_val]
            cond1 = cond12
        
        if cond1: # ここが計算大
            new_kouhos.append(new_kouho)
            tmp_dict[tuple(new_kouho)] = sum_val
            if cond11:
                all_[sum_val] = [new_kouho]
            else:
                all_[sum_val].append(new_kouho)
    new_kouhos = sorted(new_kouhos, key=lambda x:-tmp_dict[tuple(x)]) ## 案
    return all_, new_kouhos

for _ in range(K):
    cur = kouhos[0]
    print(sum_(cur))
    kouhos = kouhos[1:]
    kouhos_vals = kouhos_vals[1:]

    all_, new_kouhos = make_new_kouhos(all_)

    if len(new_kouhos) > 0:
        kouhos, kouhos_vals = add_kouhos(kouhos, kouhos_vals, new_kouhos)

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = [int(elm) for elm in input().split(" ")]
Xs = [X, Y, Z]
A = sorted([int(elm) for elm in input().split(" ")], reverse=True)
B = sorted([int(elm) for elm in input().split(" ")], reverse=True)
C = sorted([int(elm) for elm in input().split(" ")], reverse=True)


def sum_(kouho):
    a, b, c = kouho
    return sum([A[a], B[b], C[c]])

kouhos = []

kouhos = [[0, 0, 0]]
cur = kouhos[0]
sum_cur = sum_(cur)
kouhos_vals = [sum_cur]
#all_ = [cur]
all_ = {}
all_[sum_cur] = [cur]

def add_kouhos(kouhos, kouhos_vals, new_kouhos):
    if len(kouhos) == 0:
        kouhos = new_kouhos
        kouhos_vals = [sum_(kouho) for kouho in new_kouhos]
        return kouhos, kouhos_vals

    len_kouhos = len(kouhos)

    new_i = 1
    new_kouho = new_kouhos[-new_i] # 後ろ(小さいkouho)から追加していく
    sum_new_kouho = sum_(new_kouho)

    n_kouhos = len(new_kouhos)

    last_kouho_i = len_kouhos - 1
    while True:
        if sum_new_kouho <= sum_(kouhos[-1]):
            #kouhos.append(new_kouho)
            kouhos.insert(last_kouho_i+1, new_kouho)
            new_i += 1
            if new_i == n_kouhos+1:
                return kouhos, kouhos_vals        
            new_kouho = new_kouhos[-new_i]
            sum_new_kouho = sum_(new_kouho)
        else:
            break

    #for kouho_i in range(1, len_kouhos+1):
    for cur_kouho_i in reversed(range(0, len_kouhos)):
        loop = True
        sum_cur_kouho = sum_(kouhos[cur_kouho_i])
        while loop:
            #if sum_(new_kouho) > sum_(kouhos[-kouho_i]):
            if sum_new_kouho < sum_cur_kouho: # 下からkouhosを辿って行き、超えられなかった一つ手前にinsert
                kouhos.insert(cur_kouho_i+1, new_kouho)
                kouhos_vals.insert(cur_kouho_i+1, sum_(new_kouho))
                #kouhos.insert(-kouho_i, new_kouho)
                #kouhos_vals.insert(-kouho_i, sum_(new_kouho))
                new_i += 1
                if new_i == n_kouhos+1:
                    return kouhos, kouhos_vals
                new_kouho = new_kouhos[-new_i]
                sum_new_kouho = sum_(new_kouho)
            else:
                loop = False
                #new_i += 1
                #new_kouho = new_kouhos[-new_i]

    while True:
        #print("add mae", new_kouho)
        kouhos.insert(0, new_kouho) # ひたすら先頭にnew_kouhosを小さいものから追加していく
        kouhos_vals.insert(0, sum_new_kouho)
        new_i += 1
        if new_i == n_kouhos+1:
            return kouhos, kouhos_vals
        else:
            new_kouho = new_kouhos[-new_i]
            sum_new_kouho = sum_(new_kouho)

def make_new_kouhos(all_):
    new_kouhos = []
    tmp_dict = {}
    for i in range(3):
        new_kouho = cur.copy()
        new_kouho[i] += 1

        prop_new_kouho = new_kouho
        
        cond2 = new_kouho[i] < Xs[i]
        if not cond2:
            continue
        sum_val = sum_(prop_new_kouho)
        
        cond11 = sum_val not in all_.keys()
        #cond1 = cond11 or cond12
        if cond11:
            cond1 = cond11
        else:
            cond12 = new_kouho not in all_[sum_val]
            cond1 = cond12
        #cond1 = (new_kouho not in all_)
        #cond2 = new_kouho[i] < Xs[i]
        
        #if cond1 and cond2: # ここが計算大
        if cond1: # ここが計算大
            # todo all= dict[val] = [], val = sum_(x)というリストを作って探索範囲を下げる
            new_kouhos.append(new_kouho)
            tmp_dict[tuple(new_kouho)] = sum_val
            if cond11:
                all_[sum_val] = [new_kouho]
            else:
                all_[sum_val].append(new_kouho)
    #new_kouhos = sorted(new_kouhos, key=lambda x:-sum_(x)) ## ここも変更
    new_kouhos = sorted(new_kouhos, key=lambda x:-tmp_dict[tuple(x)]) ## 案
    return all_, new_kouhos

#while True:
for _ in range(K):
    #print("cur_kouho", kouhos[0])
    cur = kouhos[0]
    print(sum_(cur))
    kouhos = kouhos[1:]
    kouhos_vals = kouhos_vals[1:]

    #new_kouhos = make_new_kouhos()
    all_, new_kouhos = make_new_kouhos(all_)
    #all_ += new_kouhos ## TODOここも修正

    if len(new_kouhos) > 0:
        #print("before add kouhos", kouhos)
        #print("new_kouhos", new_kouhos)
        kouhos, kouhos_vals = add_kouhos(kouhos, kouhos_vals, new_kouhos)
        #print("after add kouhos", kouhos)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
#一番高い組み合わせから、値が一番低いものを、次点で高いものに変えれば良い
x,y,z,k=map(int, input().split())#=3,3,3,5
a=list(map(int, input().split()))#=[1,10,100]#
b=list(map(int, input().split()))#=[2,20,200]#
c=list(map(int, input().split()))#=[1,10,100]
a=sorted(a)[::-1]
b=sorted(b)[::-1]
c=sorted(c)[::-1]
ans=np.array([a[0],b[0],c[0]])
ans_list=[]
ans_list.append((a[0],b[0],c[0]))
que=[[0,0,0]]

def bfs(que):
    #与えられたqueを全部探索し、次のqueを作ったら再帰
    next_que = []
    exit = False
    #print(que)
    for q in que:
        point = (a[q[0]],b[q[1]],c[q[2]])
        if point not in ans_list:
            ans_list.append(point)
        #print(a[q[0]],b[q[1]],c[q[2]])
        #print(ans_list)
        if len(ans_list)==3*k:
            return 0
        for i in range(3):
            if i==0 and q[0]+1!=len(a):#aiを変更
                next_que.append([q[0]+1,q[1],q[2]])
            elif i==1 and q[1]+1!=len(b):#biを変更
                next_que.append([q[0],q[1]+1,q[2]])
            elif i==2 and q[2]+1!=len(c):
                next_que.append([q[0],q[1],q[2]+1])
            elif q[0]+1==len(a) and q[1]+1==len(b) and q[2]+1==len(c):
                exit=True
            #else:
             #   next_que.append([len(a)-1,len(b)-1,len(c)-1])
    if not exit:
        bfs(next_que)
    return 0

bfs(que)
point_list = []
for i in range(len(ans_list)):
    point_list.append(ans_list[i][0]+ans_list[i][1]+ans_list[i][2])
point_list=sorted(point_list)[::-1]
for i in range(k):
    print(point_list[i])

Submission status: Time Limit Exceeded

Solution code:
def cake(s1,s2,s3):
    ans1 = sorted(i+j for i in s1 for j in s2)
    ans2 = sorted(i+j for i in ans1 for j in s3)
    return ans2    
    
a,b,c,d = map(int, input().split())

s1=list(map(int, input().split()))
s2=list(map(int, input().split()))
s3=list(map(int, input().split()))

z = sorted(cake(s1,s2,s3),reverse=True)
for i in range(0,d):
    print(z[i])

Submission status: Time Limit Exceeded

Solution code:
def add(s1,s2):
    l = []
    for x in s1:
        for y in s2:
            l.append(x+y)
    return l

def cake(s1,s2,s3):
    ans1 = add(s1,s2)
    ans2 = add(ans1,s3)
    return ans2    
    
a,b,c,d = map(int, input().split())
s1=list(map(int, input().split()))
s2=list(map(int, input().split()))
s3=list(map(int, input().split()))
z = sorted(cake(s1,s2,s3),reverse=True)
for i in range(0,d):
    print(z[i])

Submission status: Time Limit Exceeded

Solution code:
def add(s1,s2):
    l = []
    for i in range(len(s1)):
        for j in range(len(s2)):
            l.append(s1[i]+s2[j])
    return l

def cake(s1,s2,s3):
    ans1 = add(s1,s2)
    ans2 = add(ans1,s3)
    return ans2    
    
a,b,c,d = map(int, input().split())
s1=list(map(int, input().split()))
s2=list(map(int, input().split()))
s3=list(map(int, input().split()))
z = sorted(cake(s1,s2,s3),reverse=True)
for i in range(0,d):
    print(z[i])

Submission status: Time Limit Exceeded

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

x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
 
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
 
ab = sorted(x+y for x in a for y in b)[:-k-1:-1]
c = c[:k]
abc = []
for i in ab:
    for j in c:
        abc.append(i+j)
abc.sort(reverse=True)
 
print(*abc[:k], sep="\n")

Submission status: Time Limit Exceeded

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

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

abc=[]

for i in range(x):
    for j in range(y):
        for l in range(z):
                if((i+1)*(j+1)*(l+1)<=k):
                        abc.append(a[i]+b[j]+c[l])

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

Submission status: Time Limit Exceeded

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

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

ab = sorted(x+y for x in a for y in b)[:-k-1:-1]
c = c[:k]
abc = []
for i in ab:
    for j in c:
        abc.append(i+j)
abc.sort(reverse=True)

print(*abc[:k], sep="\n")

Submission status: Time Limit Exceeded

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

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

ab = []

for i in a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse=True)
ab = ab[:k]
c = c[:k]
abc = []
for i in ab:
    for j in c:
        abc.append(i+j)
abc.sort(reverse=True)

print(*abc[:k],sep="\n")

Submission status: Time Limit Exceeded

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

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

ab=[]

for i in a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse=True)
ab=ab[:k]
c=c[:k]
abc=[]
for i in ab:
    for j in c:
        abc.append(i+j)
abc.sort(reverse=True)

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

Submission status: Time Limit Exceeded

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

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

abc=[]

for i in range(x):
    for j in range(y):
        for l in range(z):
                if((i+1)*(j+1)*(l+1)<=k):
                        abc.append(a[i]+b[j]+c[l])

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

Submission status: Time Limit Exceeded

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

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

ab=[]

for i in a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse=True)

abc=[]
for i in ab[:k]:
    for j in c[:k]:
        abc.append(i+j)
abc.sort(reverse=True)

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

Submission status: Time Limit Exceeded

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

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

ab=[]

for i in a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse=True)

abc=[]
for i in ab[:k]:
    for j in c:
        abc.append(i+j)
abc.sort(reverse=True)

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

Submission status: Time Limit Exceeded

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

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

ab=[]

for i in a[:k]:
    for j in b[:k]:
        ab.append(i+j)
ab.sort(reverse=True)

abc=[]
for i in ab[:k]:
    for j in c[:k]:
        abc.append(i+j)
abc.sort(reverse=True)

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