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 = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
AB = [A[x]+B[y] for x in range(X) for y in range(Y)]
AB.sort(reverse=True)
AB = AB[:K]
XY = len(AB)
ABC = [AB[xy]+C[z] for xy in range(XY) for z in range(Z)]
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
AB = [A[x]+B[y] for x in range(X) for y in range(Y)]
AB.sort(reverse=True)
AB = AB[:K]
XY = len(AB)
ABC = [AB[xy]+C[z] for xy in range(XY) for z in range(Z)]
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
AB = [a + b for a in A for b in B]
AB.sort()
AB.reverse()
AB = AB[:min(K, X*Y)]
ABC = [ab + c for ab in AB for c in C]
ABC.sort()
ABC.reverse()
for k in range(min(K, X*Y*Z)):
    print(ABC[k])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

abc = []
for 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=sorted([int(i) for i in input().split()],reverse=True)
b=sorted([int(i) for i in input().split()],reverse=True)
c=sorted([int(i) for i in input().split()],reverse=True)
ab=[a[i]+b[j] for j in range(y) for i in range(x)]
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[ab[i]+c[j] for j in range(z) for i in range(l)]
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=sorted([int(i) for i in input().split()],reverse=True)
b=sorted([int(i) for i in input().split()],reverse=True)
c=sorted([int(i) for i in input().split()],reverse=True)
ab=[]
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[]
for i in range(l):
    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=sorted([int(i) for i in input().split()],reverse=True)
ab=[]
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[]
for i in range(l):
    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_list = [int(i) for i in input().split()]
b_list = [int(i) for i in input().split()]
c_list = [int(i) for i in input().split()]

# list top K of a+b
a_plus_b_list = []
for a in a_list:
    for b in b_list:
        a_plus_b_list.append(a+b)
a_plus_b_list.sort(reverse = True)

# list top K of a+b+c
d_list = []
for a_b in a_plus_b_list:
    for c in c_list:
        d_list.append(a_b + c)
d_list.sort(reverse = True)
for i in range(K):
    print(d_list[i])

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
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=sorted([int(i) for i in input().split()],reverse=True)
ab_max=[]
for i in range(x):
    for j in range(y):
        ab_max.append(a[i]+b[j])
ab_max.sort()
def unti(t_sub):
    global x,y,z,k
    ret=0
    for i in range(x*y):
        h=z-bisect_left(c,t_sub-ab_max[i])
        if h==0 or ret>=k:
            return ret
        else:
            ret+=h
    return ret
l,r=0,ab_max[0]+max(c)
while r>l+1:
    t=(l+r)//2
    if unti(t)>=k:
        r=t
    else:
        l=t
ans=[]
f=False
for i in range(x*y):
    for j in range(z):
        u=ab_max[i]+c[j]
        if u>=r:
            ans.append(u)
        else:
            if j==0:f=True
            break
    if f:
        break
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
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=sorted([int(i) for i in input().split()],reverse=True)
ab_max=[]
for i in range(x):
    for j in range(y):
        ab_max.append(a[i]+b[j])
ab_max.sort()
def unti(t_sub):
    ret=0
    for i in range(x*y):
        h=z-bisect_left(c,t_sub-ab_max[i])
        if h!=0:
            ret+=h
        else:
            return ret
    return ret
l,r=0,ab_max[0]+max(c)
while r>l+1:
    t=(l+r)//2
    if unti(t)>=k:
        r=t
    else:
        l=t
ans=[]
f=False
for i in range(x*y):
    for j in range(z):
        u=ab_max[i]+c[j]
        if u>=r:
            ans.append(u)
        else:
            if j==0:f=True
            break
    if f:
        break
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
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=sorted([int(i) for i in input().split()],reverse=True)
ab_max=[]
for i in range(x):
    for j in range(y):
        ab_max.append(a[i]+b[j])
ab_max.sort()
def unti(t_sub):
    ret=0
    for i in range(x*y):
        h=z-bisect_left(c,t_sub-ab_max[i])
        if h!=0:
            ret+=h
        else:
            return ret
    return ret
l,r=0,ab_max[0]+max(c)
while r>l+1:
    t=(l+r)//2
    if unti(t)>=k:
        r=t
    else:
        l=t
ans=[]
for i in range(x*y):
    for j in range(z):
        u=ab_max[i]+c[j]
        if u>=r:
            ans.append(u)
        else:
            break
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
def num():
    return int(input())
def nums():
    return list(map(int,input().split()))

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

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

AB = [bi + ai for bi in B for ai in A]
AB.sort(reverse=1)
res = []
for ab_sum in AB[:K]:
    for ci in C[:K]:
        res.append(ab_sum + ci)
res.sort(reverse=True)
for ans in res[:K]:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq

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)

que = []
heapq.heappush(que, [(A[0]+B[0]+C[0])*-1,0,0,0])
data = [[0,0,0]]
for i in range(K):
    tmp = heapq.heappop(que)
    print(tmp[0]*-1)
    if tmp[1]+1 < len(A) and [(A[tmp[1]+1]+B[tmp[2]]+C[tmp[3]])*-1,tmp[1]+1,tmp[2],tmp[3]] not in data:
        heapq.heappush(que, [(A[tmp[1]+1]+B[tmp[2]]+C[tmp[3]])*-1,tmp[1]+1,tmp[2],tmp[3]])
        data.append([(A[tmp[1]+1]+B[tmp[2]]+C[tmp[3]])*-1,tmp[1]+1,tmp[2],tmp[3]])
    if tmp[2]+1 < len(B) and [(A[tmp[1]]+B[tmp[2]+1]+C[tmp[3]])*-1,tmp[1],tmp[2]+1,tmp[3]] not in data:
        heapq.heappush(que, [(A[tmp[1]]+B[tmp[2]+1]+C[tmp[3]])*-1,tmp[1],tmp[2]+1,tmp[3]])
        data.append([(A[tmp[1]]+B[tmp[2]+1]+C[tmp[3]])*-1,tmp[1],tmp[2]+1,tmp[3]])
    if tmp[3]+1 < len(C) and [(A[tmp[1]]+B[tmp[2]]+C[tmp[3]+1])*-1,tmp[1],tmp[2],tmp[3]+1] not in data:
        heapq.heappush(que, [(A[tmp[1]]+B[tmp[2]]+C[tmp[3]+1])*-1,tmp[1],tmp[2],tmp[3]+1])
        data.append([(A[tmp[1]]+B[tmp[2]]+C[tmp[3]+1])*-1,tmp[1],tmp[2],tmp[3]+1])

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

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 = sorted([s+t for s,t in product(a,b)], reverse=True)[:3000]
e = sorted([s+t for s,t in product(c,d)], reverse=True)[:k]
[print(i) for i in e]

Submission status: Time Limit Exceeded

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

X, Y, Z, K = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]

ablst = [m + n for n in b for m in a]

ablst.sort(reverse=True)

c.sort(reverse=True)

abclst = [m + n for n in c[:K] for m in ablst[:K]]

abclst.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]

ablst = [m + n for n in b for m in a]

ablst.sort(reverse=True)

c.sort(reverse=True)

abclst = [m + n for n in c[:K] for m in ablst[:K]]

abclst.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]

ablst = [m + n for n in b for m in a]

ablst.sort(reverse=True)

abclst = [m + n for m in ablst[:K] for n in c]

abclst.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]

ablst = [m + n for n in b for m in a]
ablst.sort(reverse=True)

abclst = []
abclst = [m + n for n in c for m in ablst[:K]]
abclst.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]

c.sort(reverse=True)

ablst = [m + n for n in b for m in a]

ablst.sort(reverse=True)
ablst1 = ablst[:K]

abclst = []

for m in ablst1:
    for n in c:
        abclst.append(m + n)

abclst.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
from heapq import heapify, heappush, heappop, heapreplace
from collections import deque
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)
D = deque([(0,0,0)])
F = set()
H = [0]
heapify(H)
k = 1
while D:
    p = D.popleft()
    if p in F:
        continue
    F.add(p)
    x, y, z = p
    d = A[x] + B[y] + C[z]
    if k < K:
        heappush(H, d)
        k += 1
    elif H[0] > d:
        continue
    else:
        heapreplace(H, d)
    if x < X - 1: D.append((x + 1, y, z))
    if y < Y - 1: D.append((x, y + 1, z))
    if z < Z - 1: D.append((x, y, z + 1))

print(*sorted(H, reverse=True), sep="\n")

Submission status: Time Limit Exceeded

Solution code:
from heapq import heapify, heappush, heappop, heapreplace
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()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
D = deque([(0,0,0)])
F = set()
H = [0]
heapify(H)
k = 1
while D:
    p = D.popleft()
    if p in F:
        continue
    F.add(p)
    x, y, z = p
    d = A[x] + B[y] + C[z]
    if k < K:
        heappush(H, d)
        k += 1
    elif H[0] > d:
        continue
    else:
        heapreplace(H, d)
    if x < X - 1: D.append((x + 1, y, z))
    if y < Y - 1: D.append((x, y + 1, z))
    if z < Z - 1: D.append((x, y, z + 1))

print(*sorted(H, reverse=True), 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()))
AB = sorted((a + b for a in A for b in B), reverse=True)[:min(X*Y, K)]
C.sort()
ABC = sorted((ab + c for ab in AB for c in C), reverse=True)[:K]
print('\n'.join(map(str, ABC)))

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

ans = []
for k in range(len(a_b)):
    for l in range(Z):
        ans.append(a_b[k]+C[l])
ans = sorted(ans, reverse=True)
for m in range(K):
    print(ans[m])

Submission status: Time Limit Exceeded

Solution code:
import heapq
X,Y,Z,K=[int(s) for s in input().split()]
x=sorted([-int(s) for s in input().split()])
y=sorted([-int(s) for s in input().split()])
z=sorted([-int(s) for s in input().split()])
count=[[[0 for _ in range(X)]for _ in range(Y)]for _ in range(Z)]
count[0][0][0]=1
Q=[[x[0]+y[0]+z[0],0,0,0]]
heapq.heapify(Q)
X-=1
Y-=1
Z-=1
for i in range(K):
  cake,a,b,c=heapq.heappop(Q)
  print(-cake)
  if a<X and count[a+1][b][c]==0:
    heapq.heappush(Q,[x[a+1]+y[b]+z[c],a+1,b,c])
    count[a+1][b][c]=1
  if b<Y and count[a][b+1][c]==0:
    heapq.heappush(Q,[x[a]+y[b+1]+z[c],a,b+1,c])
    count[a][b+1][c]=1
  if c<Z and count[a][b][c+1]==0:
    heapq.heappush(Q,[x[a]+y[b]+z[c+1],a,b,c+1])
    count[a][b][c+1]=1

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K=[int(s) for s in input().split()]
xls=sorted([int(s) for s in input().split()],reverse=True)
yls=sorted([int(s) for s in input().split()],reverse=True)
zls=sorted([int(s) for s in input().split()],reverse=True)
xyls=[]
for x in xls[:K]:
  for y in yls[:K]:
    xyls.append(x+y)
xyls=sorted(xyls,reverse=True)
cake=[]
for xy in xyls[:K]:
  for z in zls[:K]:
    cake.append(xy+z)
cake=sorted(cake,reverse=True)
for i in range(K):
  print(cake[i])

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K=[int(s) for s in input().split()]
xls=[int(s) for s in input().split()]
yls=[int(s) for s in input().split()]
zls=[int(s) for s in input().split()]
xyls=[]
for x in xls:
  for y in yls:
    xyls.append(x+y)
xyls=sorted(xyls,reverse=True)
M=max([X*Y,K])
cake=[]
for xy in xyls[:M]:
  for z in zls:
    cake.append(xy+z)
cake=sorted(cake,reverse=True)
for i in range(K):
  print(cake[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()))

ab = sorted(x+y for x in a for y in b)[:-k-1:-1]
abc = sorted(ab+z for ab in ab[:k] for z in c)[:-k-1:-1]

print(*abc, sep="\n")

Submission status: Time Limit Exceeded

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

x,y,z,k = map(int,input().split())
lisa = list(map(int,input().split()))
lisb = list(map(int,input().split()))
lisc = list(map(int,input().split()))

ab = sorted(a+b for a in lisa for b in lisb)[:-k-1:-1]
abc = sorted(ab+c for ab in ab[:k] for c in lisc)[:-k-1:-1]

print(*abc, sep="\n")

Submission status: Time Limit Exceeded

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

x,y,z,k = map(int,input().split())
lisa = list(map(int,input().split()))
lisb = list(map(int,input().split()))
lisc = list(map(int,input().split()))


lisab = sorted(a+b for a in lisa for b in lisb)[:-k-1:-1]
lisabc = sorted(ab+c for ab in lisab[:k] for c in lisc)[:-k-1:-1]

print(*lisabc, sep="\n")

Submission status: Time Limit Exceeded

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

x,y,z,k = map(int,input().split())
lisA = list(map(int,input().split()))
lisB = list(map(int,input().split()))
lisC = list(map(int,input().split()))
lisAns = []
lisAns2 = []

for i in range(x):
  for j in range(y):
    lisAns.append(lisA[i]+lisB[j])

for i in range(len(lisAns)):
  for j in range(z):
    lisAns2.append(lisAns[i]+lisC[j])

lisAns2.sort(reverse = True)
for i in range(k):
  print(lisAns2[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()
b.sort()
c.sort()

bc = [0] * (y * z)
q = 0
for i in range(y):
    for j in range(z):
        bc[q] = b[i] + c[j]
        q += 1

bc.sort(reverse=True)
r = len(bc)
bc = bc[:min(k, r)]

abc = [0] * (x * min(k, r))
q = 0
for i in range(min(k, r)):
    for j in range(x):
        abc[q] = bc[i] + a[j]
        q += 1

abc.sort(reverse=True)
for i in range(min(k, x*y*z)):
    print(abc[i])

Submission status: Time Limit Exceeded

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

D = [[0,0,0]]
used = []
ans = []
for k in range(K):
  #print(k,D)
  mxd = 0
  mxi = 0
  for i in range(len(D)):
    d = A[D[i][0]]+B[D[i][1]]+C[D[i][2]]
    if d >= mxd:
      mxd = d
      mxi = i
  ans.append(mxd)
  if D[mxi][0]+1 < X and [D[mxi][0]+1,D[mxi][1],D[mxi][2]] not in D and [D[mxi][0]+1,D[mxi][1],D[mxi][2]] not in used:
    D.append([D[mxi][0]+1,D[mxi][1],D[mxi][2]])
  if D[mxi][1]+1 < Y and [D[mxi][0],D[mxi][1]+1,D[mxi][2]] not in D and [D[mxi][0],D[mxi][1]+1,D[mxi][2]] not in used:
    D.append([D[mxi][0],D[mxi][1]+1,D[mxi][2]])
  if D[mxi][2]+1 < Z and [D[mxi][0],D[mxi][1],D[mxi][2]+1] not in D and [D[mxi][0],D[mxi][1],D[mxi][2]+1] not in used:
    D.append([D[mxi][0],D[mxi][1],D[mxi][2]+1])
  used.append(D[mxi])
  del D[mxi]
#print(D)
for a in ans:
  print(a)

Submission status: Time Limit Exceeded

Solution code:
import sys
from heapq import heapify, heappushpop
from bisect import bisect_left as bisect

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

X, Y, Z, K = MAP()
x = MAP()
y = MAP()
z = MAP()

if X > Z:
	x, z = z, x
	X, Z = Z, X
if Y > Z:
	y, z = z, y
	Y, Z = Z, Y

x.sort()
y.sort()
z.sort()

nums = [0] * K
heapify(nums)

for i in range(X):
	for j in range(Y):
		xy = x[i]+y[j]
		z_idx = bisect(z, nums[0]-xy)
		# print(nums)
		# print(xy, z, z_idx)
		for k in range(z_idx, Z):
			heappushpop(nums, xy+z[k])

nums.sort(reverse=True)
for n in nums:
	print(n)

Submission status: Time Limit Exceeded

Solution code:
from heapq import nlargest
x,y,z,k=map(int,input().split())
A,B,C=[list(map(int,input().split()))for _ in[0]*3]
E=nlargest(k,(a+b+c for a in A for b in B for c in C))
for i in E:
    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()
b.sort()
c.sort()
a.reverse()
b.reverse()
c.reverse()

from heapq import heappop,heappush
q=[]
heappush(q, (-(a[0]+b[0]+c[0]),0,0,0))

ispushed = [[[False]*z for _ in range(y)] for __ in range(x)]
ispushed[0][0][0]=True

for _ in range(k):
	ans,i,j,k = heappop(q)
	print(-ans)
	if i<x-1:
		if not ispushed[i+1][j][k]:
			ispushed[i+1][j][k]=True
			heappush(q,(-(a[i+1]+b[j]+c[k]),i+1,j,k))
	
	if j<y-1:
		if not ispushed[i][j+1][k]:
			ispushed[i][j+1][k]=True
			heappush(q,(-(a[i]+b[j+1]+c[k]),i,j+1,k))
	
	if k<z-1:
		if not ispushed[i][j][k+1]:
			ispushed[i][j][k+1]=True
			heappush(q,(-(a[i]+b[j]+c[k+1]),i,j,k+1))

Submission status: Time Limit Exceeded

Solution code:
import heapq
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()))

m=[]

for i in range(x):
    for j in range(y):
        for n in range(z):
            m.append(-(a[i]+b[j]+c[n]))


heapq.heapify(m)

for i in range(k):
    print(-(heapq.heappop(m)))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import sys
from bisect import bisect_left

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def bisearch_max(mn, mx, func):
    """ 条件を満たす最大値を見つける二分探索 """

    ok = mn
    ng = mx
    while ok+1 < ng:
        mid = (ok+ng) // 2
        if func(mid):
            # 上を探しに行く
            ok = mid
        else:
            # 下を探しに行く
            ng = mid
    return ok

X, Y, Z, K = MAP()
A = LIST()
B = LIST()
C = sorted(LIST())

# K個以上の3つ組が達成できるような最大の値mを探す
def check(m):
    sm = 0
    for a in A:
        for b in B:
            cnt = Z - bisect_left(C, m-a-b)
            sm += cnt
    return sm >= K

res = bisearch_max(0, 10**18, check)

ans = []
for a in A:
    for b in B:
        idx = bisect_left(C, res-a-b)
        for i in range(Z-1, idx-1, -1):
            c = C[i]
            ans.append(a+b+c)
ans.sort(reverse=1)
[print(a) for a in ans]

Submission status: Time Limit Exceeded

Solution code:
import heapq

X,Y,Z,K = map(int,input().split(' '))
An = list(map(int,input().split(' ')))
Bn = list(map(int,input().split(' ')))
Cn = list(map(int,input().split(' ')))
output = []

for a in An:
    for b in Bn:
        for c in Cn:
            heapq.heappush(output,(a+b+c)*(-1))

for _ in range(K):
    print(heapq.heappop(output) * (-1))

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k,*L=map(int,open(0).read().split())
a=L[:x];b=L[x:x+y];c=sorted(L[x+y:])[::-1]
ab=sorted(i+j for i in a for j in b)[::-1][:min(x*y,k)]
print(*sorted(i+j for i in ab for j in c)[::-1][:k],sep="\n")

Submission status: Time Limit Exceeded

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

f1 =[]
for i in A:
  for j in B:
    f1.append( i+j )
f1.sort(reverse = True)
f1 = f1[:min(3000,X*Y*Z+1)]

f2 = []
for i in f1:
  for j in C:
    f2.append(i+j)
f2.sort(reverse = True)

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

Submission status: Time Limit Exceeded

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

f1 =[]
for i in A:
  for j in B:
    f1.append( i+j )
f1.sort(reverse = True)

f2 = []
for i in f1[:min(3000,X*Y*Z+1)]:
  for j in C:
    f2.append(i+j)
f2.sort(reverse = True)

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

Submission status: Time Limit Exceeded

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

f1 =[]
for i in A:
  for j in B:
    f1.append( i+j )
f1.sort(reverse = True)

f2 = []
for i in f1[:min(3000,K)]:
  for j in C:
    f2.append(i+j)
f2.sort(reverse = True)

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

Submission status: Time Limit Exceeded

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

f1 =[]
for i in A:
  for j in B:
    f1.append( i+j )
f1.sort(reverse = True)

f2 = []
for i in f1[:min(3000,K)]:
  for j in C:
    f2.append(i+j)
f2.sort(reverse = True)

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

Submission status: Time Limit Exceeded

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

f1 =[]
for i in A:
  for j in B:
    f1.append( i+j )
f1.sort(reverse = True)

f2 = []
for i in f1[:min(3000,K)]:
  for j in C:
    f2.append(i+j)
f2.sort(reverse = True)

for i in f2[:K]:
  print(i)

Submission status: Time Limit Exceeded

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

f1 =[]
for i in A:
  for j in B:
    f1.append( i+j )
f1 = sorted(f1,reverse = True)[:min(3000,K)]

f2 = []
for i in f1:
  for j in C:
    f2.append(i+j)
  
for i in sorted(f2, reverse = True)[:K]:
  print(i)

Submission status: Time Limit Exceeded

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

s = sorted(sum([[i+j for i in A] for j in B],[]), reverse = True)[:min(3001, K)]
ans = sorted(sum([[i+j for i in s] for j in C],[]), reverse = True)[:K]
for i in ans:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = map(int,input().split())
As = sorted(list(map(int,input().split())))
Bs = sorted(list(map(int,input().split())))
Cs = sorted(list(map(int,input().split())))
mi = 99999
count = 0
ans = []
for a in As[::-1]:
  for b in Bs[::-1]:
    for c in Cs[::-1]:
      cake = a+b+c
      if count < 3000:
        ans.append(cake)
        if cake < mi:
          mi = cake
      elif cake > mi:
        ans.append(cake)
      count += 1
ans.sort()
for a in ans[-1:-k-1:-1]:
  print(a)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = map(int,input().split())
As = sorted(list(map(int,input().split())))
Bs = sorted(list(map(int,input().split())))
Cs = sorted(list(map(int,input().split())))

ans = []
for a in As:
  for b in Bs:
    for c in Cs:
      ans.append(a+b+c)
ans.sort()
for a in ans[-1:-k-1:-1]:
  print(a)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[1]:


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


# In[14]:


AB = []
for x in range(X):
    for y in range(Y):
        AB.append(A[x] + B[y])
AB.sort(reverse=True)
if K < X * Y:
    AB = AB[:K]
W = len(AB)
AB


# In[16]:


ABC = []
for w in range(W):
    for z in range(Z):
        ABC.append(AB[w] + C[z])
ABC.sort(reverse=True)
if K < W * Z:
    ABC = ABC[:K]
ABC


# In[17]:


print('\n'.join(map(str, ABC)))


# In[ ]:





# In[ ]: