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

Submission status: Time Limit Exceeded

Solution code:
def dfs(count, ans, ll):
    if count == 2:
        for cc in c:
            cccc = ans + cc
            if cccc is not None:
                ll.append(cccc)
    if count == 1:
        for bb in b:
            bbbb = dfs(count+1, ans+bb, ll)
            if bbbb is not None:
                ll.append(bbbb)
    if count == 0:
        for aa in a:
            aaaa = dfs(count+1, ans+aa, ll)
            if aaaa is not None:
                ll.append(aaaa)
        return ll

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()]
aaa = []
ans = dfs(0, 0, aaa)
ans.sort()
ans = list(reversed(ans))
for i in range(k):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
x, y, z, k = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
ll = []
if x >= k:
    a = a[k-1:]
if y >= k:
    b = b[k-1:]
if z >= k:
    c = c[k-1:]
for aa in a:
    for bb in b:
        for cc in c:
            ll.append(aa+bb+cc)
ll.sort()
ll = list(reversed(ll))
for i in range(k):
    print(ll[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()]
ll = []
for aa in a:
    for bb in b:
        for cc in c:
            ll.append(aa+bb+cc)
ll.sort()
ll = list(reversed(ll))
for i in range(k):
    print(ll[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=1)
b.sort(reverse=1)
c.sort(reverse=1)
m=[]
for i in range(x):
  for j in range(y):
    for k in range(z):
      if (i+1)*(j+1)*(k+1)<=K:
        m.append(a[i]+b[j]+c[k])
m.sort(reverse=1)
for i in m[: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=1)
b.sort(reverse=1)
c.sort(reverse=1)
m=[]
for i in range(x):
  for j in range(y):
    for k in range(z):
      if i*j*k>K:
        continue
      m.append(a[i]+b[j]+c[k])
m.sort(reverse=1)
for i in m[:K]:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = [ int(it) for it in input().split() ]
al = [ int(it) for it in input().split() ]
bl = [ int(it) for it in input().split() ]
cl = [ int(it) for it in input().split() ]

abl = [ a + b for a in al for b in bl]
abl.sort()
abl.reverse()
abl=abl[:k]

abcl = [ ab + c for ab in abl for c in cl]
abcl.sort()
abcl.reverse()
abcl=abcl[:k]
print ( "\n".join( [ str(it) for it in abcl ]) )

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = [ int(it) for it in input().split() ]
al = [ int(it) for it in input().split() ]
bl = [ int(it) for it in input().split() ]
cl = [ int(it) for it in input().split() ]

abl = [ a + b for a in al for b in bl]
abl.sort()
abl.reverse()
abl=abl[:k]

abcl = [ ab + c for ab in abl for c in cl]
abcl.sort()
abcl.reverse()
abcl=abcl[:k]
for it in abcl:
  print (it)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for l in [b,c]:
    p = []
    for i in a:
        for j in l:
            p.append(i+j)
    a = sorted(p, reverse=True)[:k]

for i in a:
    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()))

r = []

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

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

sums = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1)*(j+1)*(k+1) <= K:
                sums.append(A[i]+B[j]+C[k])

list.sort(sums, reverse=True)
for i in range(K):
    print(sums[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 A:
    for j in B:
        AB.append(i + j)
AB = sorted(AB, reverse=True)[:K]
ABC = []
for i in AB:
    for j in C:
        ABC.append(i + j)

ABC = sorted(ABC, reverse=True)[:K]
for a in ABC:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = input().split()
X = int(X)
Y = int(Y)
Z = int(Z)
K = int(K)
A = [int(a) for a in input().split()]
A = sorted(A, reverse=True)
B = [int(b) for b in input().split()]
B = sorted(B, reverse=True)
C = [int(c) for c in input().split()]
C = sorted(C, reverse=True)
ABC = []
AK, BK, CK = K, K, K
if len(A) <= K:
    AK = len(A)
if len(B) <= K:
    BK = len(B)
if len(C) <= K:
    CK = len(C)
for i in range(AK):
    for j in range(BK):
        for k in range(CK):
            ABC.append(A[i] + B[j] + C[k])
ABC_sort = sorted(ABC, reverse=True)
for x in range(K):
    print(ABC_sort[x])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

import heapq


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

index_hstr = []
deliciouses = []
# priority queue
pq = []
heapq.heappush(pq, (-a[0] - b[0] - c[0], 0, 0, 0))
index_hstr.append((0, 0, 0))

for t in range(k):
    deli, i, j, k = heapq.heappop(pq)
    if i+1<x and (i+1, j, k) not in index_hstr:
        heapq.heappush(pq, (-a[i+1] - b[j] - c[k], i+1, j, k))
        index_hstr.append((i+1, j, k))
    if j+1<y and (i, j+1, k) not in index_hstr:
        heapq.heappush(pq, (-a[i] - b[j+1] - c[k], i, j+1, k))
        index_hstr.append((i, j+1, k))
    if k+1<z and (i, j, k+1) not in index_hstr:
        heapq.heappush(pq, (-a[i] - b[j] - c[k+1], i, j, k+1))
        index_hstr.append((i, j, k+1))
    deliciouses.append(-deli)


print('\n'.join(str(i) for i in deliciouses))

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
A_array = list(map(int, input().split()))
B_array = list(map(int, input().split()))
C_array = list(map(int, input().split()))

A_array.sort()
B_array.sort()
C_array.sort()

use_array = []
sum_array = []
candidate_array = []

max_sum = A_array[-1] + B_array[-1] + C_array[-1]

candidate_array = [[X - 1, Y - 1, Z - 1, max_sum]]

use_array.append((X - 1, Y - 1, Z - 1))

for i in range(K):
    candidate_array = sorted(candidate_array, key=lambda x: -x[3])

    x, y, z, s = candidate_array.pop(0)

    sum_array.append(s)
    if x != 0 and (x - 1, y, z) not in use_array:
        use_array.append((x - 1, y, z))
        diff = A_array[x] - A_array[x - 1]
        candidate_array.append([x - 1, y, z, s - diff])

    if y != 0 and (x, y - 1, z) not in use_array:
        use_array.append((x, y - 1, z))
        diff = B_array[y] - B_array[y - 1]
        candidate_array.append([x, y - 1, z, s - diff])

    if z != 0 and (x, y, z - 1) not in use_array:
        use_array.append((x, y, z - 1))
        diff = C_array[z] - C_array[z - 1]
        candidate_array.append([x, y, z - 1, s - diff])

for ans in sum_array:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq
X, Y, Z, K = map(int, input().split())
A_array = list(map(int, input().split()))
B_array = list(map(int, input().split()))
C_array = list(map(int, input().split()))

A_array.sort()
B_array.sort()
C_array.sort()

use_array = [[[0] * Z for i in range(Y)] for i in range(X)]
sum_array = []

candidate = []
heapq.heapify(candidate)

max_sum = A_array[-1] + B_array[-1] + C_array[-1]

heapq.heappush(candidate, (0, X - 1, Y - 1, Z - 1))

for i in range(K):

    s, x, y, z = heapq.heappop(candidate)

    sum_array.append(s)
    if x != 0 and use_array[x - 1][y][z] == 0:
        use_array[x - 1][y][z] = 1
        diff = A_array[x] - A_array[x - 1]
        heapq.heappush(candidate, (s + diff, x - 1, y, z))

    if y != 0 and use_array[x][y - 1][z] == 0:
        use_array[x][y - 1][z] = 1
        diff = B_array[y] - B_array[y - 1]
        heapq.heappush(candidate, (s + diff, x, y - 1, z))

    if z != 0 and use_array[x][y][z - 1] == 0:
        use_array[x][y][z - 1] = 1
        diff = C_array[z] - C_array[z - 1]
        heapq.heappush(candidate, (s + diff, x, y, z - 1))

for ans in sum_array:
    print(max_sum - ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq
X, Y, Z, K = map(int, input().split())
A_array = list(map(int, input().split()))
B_array = list(map(int, input().split()))
C_array = list(map(int, input().split()))

A_array.sort()
B_array.sort()
C_array.sort()

use_array = [[[0] * Z for i in range(Y)] for i in range(X)]
sum_array = []

candidate = []

max_sum = A_array[-1] + B_array[-1] + C_array[-1]

heapq.heappush(candidate, (0, X - 1, Y - 1, Z - 1))

for i in range(K):

    s, x, y, z = heapq.heappop(candidate)

    sum_array.append(s)
    if x != 0 and use_array[x - 1][y][z] == 0:
        use_array[x - 1][y][z] = 1
        diff = A_array[x] - A_array[x - 1]
        heapq.heappush(candidate, (s + diff, x - 1, y, z))

    if y != 0 and use_array[x][y - 1][z] == 0:
        use_array[x][y - 1][z] = 1
        diff = B_array[y] - B_array[y - 1]
        heapq.heappush(candidate, (s + diff, x, y - 1, z))

    if z != 0 and use_array[x][y][z - 1] == 0:
        use_array[x][y][z - 1] = 1
        diff = C_array[z] - C_array[z - 1]
        heapq.heappush(candidate, (s + diff, x, y, z - 1))

for ans in sum_array:
    print(max_sum - ans)

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
A_array = list(map(int, input().split()))
B_array = list(map(int, input().split()))
C_array = list(map(int, input().split()))

A_array.sort()
B_array.sort()
C_array.sort()

use_array = [[[0] * Z for i in range(Y)] for i in range(X)]
sum_array = []
candidate_array = []

max_sum = A_array[-1] + B_array[-1] + C_array[-1]

candidate_array = [[X - 1, Y - 1, Z - 1, max_sum]]

for i in range(K):
    candidate_array = sorted(candidate_array, key=lambda x: -x[3])

    x, y, z, s = candidate_array.pop(0)

    sum_array.append(s)
    if x != 0 and use_array[x - 1][y][z] == 0:
        use_array[x - 1][y][z] = 1
        diff = A_array[x] - A_array[x - 1]
        candidate_array.append([x - 1, y, z, s - diff])

    if y != 0 and use_array[x][y - 1][z] == 0:
        use_array[x][y - 1][z] = 1
        diff = B_array[y] - B_array[y - 1]
        candidate_array.append([x, y - 1, z, s - diff])

    if z != 0 and use_array[x][y][z - 1] == 0:
        use_array[x][y][z - 1] = 1
        diff = C_array[z] - C_array[z - 1]
        candidate_array.append([x, y, z - 1, s - diff])

for ans in sum_array:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq
X,Y,Z,K = map(int,input().split())
As = sorted(list(map(int,input().split())),reverse=True)
Bs = sorted(list(map(int,input().split())),reverse=True)
Cs = sorted(list(map(int,input().split())),reverse=True)
q = []
heapq.heapify(q)
heapq.heappush(q,(-(As[0]+Bs[0]+Cs[0]),0,0,0))
list_ijk = [(0,0,0)]
for i in range(K):
    # valueのマイナス値の小さい順にpop(優先度付きqueue)
    v,i,j,k = heapq.heappop(q)
    if i < len(As)-1 and (i+1,j,k) not in list_ijk:
        heapq.heappush(q,(-(As[i+1]+Bs[j]+Cs[k]),i+1,j,k))
        list_ijk.append((i+1,j,k))
    if j < len(Bs)-1 and (i,j+1,k) not in list_ijk:
        heapq.heappush(q,(-(As[i]+Bs[j+1]+Cs[k]),i,j+1,k))
        list_ijk.append((i,j+1,k))
    if k < len(Cs)-1 and (i,j,k+1) not in list_ijk:
        heapq.heappush(q,(-(As[i]+Bs[j]+Cs[k+1]),i,j,k+1))
        list_ijk.append((i,j,k+1))
    print(-v)

Submission status: Time Limit Exceeded

Solution code:

import heapq
X,Y,Z,K = map(int,input().split())
As = sorted(list(map(int,input().split())),reverse=True)
Bs = sorted(list(map(int,input().split())),reverse=True)
Cs = sorted(list(map(int,input().split())),reverse=True)
q = []
heapq.heapify(q)
heapq.heappush(q,(-(As[0]+Bs[0]+Cs[0]),0,0,0))
list_ijk = [(0,0,0)]
for i in range(K):
    # valueのマイナス値の小さい順にpop(優先度付きqueue)
    v,i,j,k = heapq.heappop(q)
    if i < len(As)-1 and (i+1,j,k) not in list_ijk:
        heapq.heappush(q,(-(As[i+1]+Bs[j]+Cs[k]),i+1,j,k))
        list_ijk.append((i+1,j,k))
    if j < len(Bs)-1 and (i,j+1,k) not in list_ijk:
        heapq.heappush(q,(-(As[i]+Bs[j+1]+Cs[k]),i,j+1,k))
        list_ijk.append((i,j+1,k))
    if k < len(Cs)-1 and (i,j,k+1) not in list_ijk:
        heapq.heappush(q,(-(As[i]+Bs[j]+Cs[k+1]),i,j,k+1))
        list_ijk.append((i,j,k+1))
    print(-v)

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

bc = [be+ce for be in b for ce in c]
bc.sort(reverse=True)

bc = bc[:min(k, len(bc))]

abc = [ae+bce for bce in bc for ae in a]
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)

bc = [be+ce for be in b for ce in c]
bc.sort(reverse=True)

bc = bc[:min(k, len(bc))]

abc = [ae+bce for bce in bc for ae in a]
abc.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
import heapq
import itertools


class MaxHeap:
    def __init__(self):
        self.heap = []

    def push(self, x):
        heapq.heappush(self.heap, -x)

    def pop(self):
        return -heapq.heappop(self.heap)


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


max_heap = MaxHeap()
[max_heap.push(sum(v)) for v in itertools.product(A, B, C)]
for _ in range(K):
    print(max_heap.pop())

Submission status: Time Limit Exceeded

Solution code:
import heapq


class MaxHeap:
    def __init__(self):
        self.heap = []

    def push(self, x):
        heapq.heappush(self.heap, -x)

    def pop(self):
        return -heapq.heappop(self.heap)


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


max_heap = MaxHeap()
for a in A:
    for b in B:
        for c in C:
            max_heap.push(a + b + c)

for _ in range(K):
    print(max_heap.pop())

Submission status: Time Limit Exceeded

Solution code:
###############################################################################

from bisect import bisect_left as binl

def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * sy) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


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


def main():
    x, y, z, k = intin()
    alist = intina()
    blist = intina()
    clist = intina()

    ablist = []
    for a in alist:
        for b in blist:
            ablist.append(a + b)

    ablist.sort(reverse=True)

    abclist = []
    for ab in ablist[:(k+1)]:
        for c in clist:
            abclist.append(ab + c)

    abclist.sort(reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
ListA = [int(i) for i in input().split()]
ListB = [int(i) for i in input().split()]
ListC = [int(i) for i in input().split()]
ListA.sort(reverse = True)
ListB.sort(reverse = True)
ListC.sort(reverse = True)
ListAns =[]
for a in range(min(X, K)):
    for b in range(min(Y, K)):
        for c in range(min(Z, K)):
            ans = ListA[a] + ListB[b] + ListC[c]
            ListAns.append(ans)
ListAns.sort(reverse = True)
for i in range(K):
    print(ListAns[i])

Submission status: Time Limit Exceeded

Solution code:
x,y,z,N =(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()]

sa=sorted(a,reverse=True)
sb=sorted(b,reverse=True)
sc=sorted(c,reverse=True)
l=[]#端点情報
n=0
i=0
j=0
k=0

while n<N:
  print(sa[i]+sb[j]+sc[k])
  l.append([i,j,k])
  maxl=0
  maxijk=[len(sa)-1,len(sb)-1,len(sc)-1]
  for w in l:
    if w[0]<len(sa)-1:
      if not ([w[0]+1,w[1],w[2]] in l):
        if maxl<=sa[w[0]+1]+sb[w[1]]+sc[w[2]]:
          maxl=sa[w[0]+1]+sb[w[1]]+sc[w[2]]
          maxijk=[w[0]+1,w[1],w[2]]
    if w[1]<len(sb)-1:
      if not([w[0],w[1]+1,w[2]] in l):
        if maxl<=sa[w[0]]+sb[w[1]+1]+sc[w[2]]:
          maxl=sa[w[0]]+sb[w[1]+1]+sc[w[2]]
          maxijk=[w[0],w[1]+1,w[2]]
    if w[2]<len(sc)-1:
      if not([w[0],w[1],w[2]+1] in l):
        if maxl<=sa[w[0]]+sb[w[1]]+sc[w[2]+1]:
          maxl=sa[w[0]]+sb[w[1]]+sc[w[2]+1]
          maxijk=[w[0],w[1],w[2]+1]
    i=maxijk[0]
  j=maxijk[1]
  k=maxijk[2]
  n+=1

Submission status: Time Limit Exceeded

Solution code:
x,y,z,N =(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()]

sa=sorted(a,reverse=True)
sb=sorted(b,reverse=True)
sc=sorted(c,reverse=True)
l=[]#端点情報
n=0
i=0
j=0
k=0

while n<N:
  print(sa[i]+sb[j]+sc[k])
  l.append([i,j,k])
  maxl=0
  maxijk=[len(sa)-1,len(sb)-1,len(sc)-1]
  for w in l:
    if w[0]>=len(sa)-1:continue
    if [w[0]+1,w[1],w[2]] in l:continue
    if maxl<=sa[w[0]+1]+sb[w[1]]+sc[w[2]]:
      maxl=sa[w[0]+1]+sb[w[1]]+sc[w[2]]
      maxijk=[w[0]+1,w[1],w[2]]
  for w in l:
    if w[1]>=len(sb)-1:continue
    if [w[0],w[1]+1,w[2]] in l:continue
    if maxl<=sa[w[0]]+sb[w[1]+1]+sc[w[2]]:
      maxl=sa[w[0]]+sb[w[1]+1]+sc[w[2]]
      maxijk=[w[0],w[1]+1,w[2]]
  for w in l:
    if w[2]>=len(sc)-1:continue
    if [w[0],w[1],w[2]+1] in l:continue
    if maxl<=sa[w[0]]+sb[w[1]]+sc[w[2]+1]:
      maxl=sa[w[0]]+sb[w[1]]+sc[w[2]+1]
      maxijk=[w[0],w[1],w[2]+1]
  i=maxijk[0]
  j=maxijk[1]
  k=maxijk[2]
  n+=1

Submission status: Time Limit Exceeded

Solution code:
x,y,z,N =(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()]

answer=[]
for i in range(len(a)):
  for j in range(len(b)):
    for k in range(len(c)):
      answer.append(a[i]+b[j]+c[k])
sortedAnswer=sorted(answer)
for i in range(x*y*z-1,x*y*z-N-1,-1):
  print(sortedAnswer[i])

Submission status: Time Limit Exceeded

Solution code:
import heapq

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()
B.sort()
C.sort()

queue = [(A[0]+B[0]+C[0], 0, 0, 0)]
flg = [[[0 for i in range(Z)] for j in range(Y)] for k in range(X)]
flg[0][0][0] = 1

for i in range(K):
    cake, a, b, c = heapq.heappop(queue)
    print(-cake)
    for j in range(3):
        aa = a + [1, 0, 0][j]
        bb = b + [0, 1, 0][j]
        cc = c + [0, 0, 1][j]
        if aa < X and bb < Y and cc < Z:
            if flg[aa][bb][cc] == 0:
                flg[aa][bb][cc] = 1
                heapq.heappush(queue, (A[aa]+B[bb]+C[cc], aa, bb, cc))

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 = []
for a in A:
    for b in B:
        D.append(a+b)
D = sorted(D,reverse=True)[:3000]

E = []
for d in D:
    for c in C:
        E.append(c+d)
E = sorted(E,reverse=True)[:K]

for e in E:
    print(e)

Submission status: Time Limit Exceeded

Solution code:
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    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()] 
  else:
    A = [1,10,100]
    B = [2,20,200]
    C = [1,10,100]
    #A = [4,6]
    #B = [1,5]
    #C = [3,8]
    X = len(A)
    Y = len(B)
    Z = len(C)
    K = 5
    #K = 8

  # Processing
  # A.sort(reverse=True)
  # B.sort(reverse=True)
  
  AB = []
  for i in A:
    for j in B:
      AB.append(i+j)
  
  AB.sort(reverse=True)
  #if 'get_ipython' in globals(): 
  #  print(AB)
  
  ABC = []
  C.sort(reverse=True)
  for i in AB[0:K]:
    for j in C[0:K]:
      ABC.append(i+j)  
  
  ABC.sort(reverse=True)
  
  #if 'get_ipython' in globals(): 
  #  print(ABC)

  for i in ABC[0:K]:
    print(i)      

main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    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()] 
  else:
    A = [1,10,100]
    B = [2,20,200]
    C = [1,10,100]
    #A = [4,6]
    #B = [1,5]
    #C = [3,8]
    X = len(A)
    Y = len(B)
    Z = len(C)
    K = 5
    #K = 8

  # Processing
  # A.sort(reverse=True)
  # B.sort(reverse=True)
  
  AB = []
  for i in A:
    for j in B:
      AB.append(i+j)
  
  AB.sort(reverse=True)
  if 'get_ipython' in globals(): 
    print(AB)
  
  ABC = []
  C.sort(reverse=True)
  for i in AB[0:K]:
    for j in C[0:K]:
      ABC.append(i+j)  
  
  ABC.sort(reverse=True)
  
  if 'get_ipython' in globals(): 
    print(ABC)

  for i in ABC[0:K]:
    print(i)      

main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    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()] 
  else:
    A = [1,10,100]
    B = [2,20,200]
    C = [1,10,100]
    #A = [4,6]
    #B = [1,5]
    #C = [3,8]
    X = len(A)
    Y = len(B)
    Z = len(C)
    K = 5
    #K = 8

  # Processing
  # 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)
  if 'get_ipython' in globals(): 
    print(AB)
  
  ABC = []
  for i in AB[0:K]:
    for j in C:
      ABC.append(i+j)  
  
  ABC.sort(reverse=True)
  
  if 'get_ipython' in globals(): 
    print(ABC)

  for i in ABC[0:K]:
    print(i)      

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()))
L = []
for x in A:
    for y in B:
        L.append((x+y,x,y))
L.sort()
L = L[::-1]
L = L[:3001]
M = []
for x in L:
    for y in C:
        M.append(x[0]+y)
M = sorted(M)[::-1]
for i in range(K):
    print(M[i])

Submission status: Time Limit Exceeded

Solution code:
 # import sys  sys.exit()を使う時に必要

def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    # N = int(input())
    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()] 
    # S = [input() for _ in range(N)]  縦持ち／行数N
    # t = [0] * N  　　ti xi yi 縦持ち/行数N
    # x = [0] * N
    # y = [0] * N
    # for i in range(N):
    #   t[i], x[i], y[i] = map(int, input().split())
  else:
    #A = [1,10,100]
    #B = [2,20,200]
    #C = [1,10,100]
    A = [4,6]
    B = [1,5]
    C = [3,8]
    X = len(A)
    Y = len(B)
    Z = len(C)
    # K = 5
    K = 8

  ans = []
  # Processing
  A.sort(reverse=True)
  B.sort(reverse=True)
  C.sort(reverse=True)
      
  for i in A:
    if i >= A[min(K,X)-1]:
      for j in B:
        if j >= B[min(K,Y)-1] :
          for k in C:
            if k >= C[min(K,Z)-1]:
              ans.append(i+j+k)
            else:
              break
        else:
          break
    else:
      break
    
  ans.sort(reverse=True)
  if 'get_ipython' in globals(): 
    print(ans)

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

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)
cor = []
for i in range(X):
    for j in range(Y):
        cor.append(A[i]+B[j])

cor.sort(reverse = True)
cor = cor[:min(K,X*Y)]
anslist = []
for i in range(min(K,X*Y)):
    for j in range(Z):
        anslist.append(C[j]+cor[i])

anslist.sort(reverse = True)
for i in range(K):
    print(anslist[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)
cor = []
for i in range(X):
    for j in range(Y):
        cor.append(A[i]+B[j])

cor.sort(reverse = True)
if K <= X*Y:
    cor = cor[:K]
anslist = []
for i in range(min(K,X*Y)):
    for j in range(Z):
        anslist.append(C[j]+cor[i])
        if len(anslist) == K:
            break

anslist.sort(reverse = True)
for i in range(K):
    print(anslist[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()]
m1 = []
m2 = []
m1_sort = []
for ai in a:
  for bi in b:
    m1.append(ai+bi)
if x * y > k:
  l = k
else:
  l = x * y    
for i in range(l):
  m1_sort.append(max(m1))
  m1.remove(max(m1))

for m1_sorti in m1_sort:
  for ci in c:
    m2.append(m1_sorti + ci)
for i in range(k):
  ans = max(m2)
  print(ans)
  m2.remove(ans)