Problem:


Score: 400 points

Problem Statement

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

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

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

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

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

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

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

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

Constraints

1 \leq X \leq 1 \ 000

1 \leq Y \leq 1 \ 000

1 \leq Z \leq 1 \ 000

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

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

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

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

2 2 2 8
4 6
1 5
3 8

Sample Output 1

19
17
15
14
13
12
10
8

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

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

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

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

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

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

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

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

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

Sample Input 2

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

Sample Output 2

400
310
310
301
301

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

Sample Input 3

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

Sample Output 3

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

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



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


Sample output:
19
17
15
14
13
12
10
8

Submission status: Time Limit Exceeded

Solution code:
import math
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)
l=[]
stat=int(math.sqrt(10**7//X))
for i in range(X):
    for j in range(min(stat,Y)):
        for k in range(min(stat,Z)):
            l.append(A[i] + B[j] + C[k])
end1=stat
stat2=int(math.sqrt(10**7//Y))
for i in range(min(stat,X),min(stat+stat2,X)):
    for j in range(Y):
        for k in range(min(stat,Z),min(stat+stat2,Z)):
            l.append(A[i] + B[j] + C[k])
end2=stat+stat2
stat3=int(math.sqrt(10**7//Z))
for i in range(min(stat+stat2,X),min(stat+stat2+stat3,X)):
    for j in range(min(stat+stat2,Y),min(stat+stat2+stat3,Y)):
        for k in range(Z):
            l.append(A[i] + B[j] + C[k])

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
x, y, z, k = map(int, input().split())
A = map(int, input().split())
B = map(int, input().split())
C = map(int, input().split())
AB = [sum(ab) for ab in product(A, B)]
AB.sort(reverse=True)
AB = AB[:k]
ABC = [sum(abc) for abc in product(AB, C)]
ABC.sort(reverse=True)
for i in range(k):
    print(ABC[i])

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = map(int,input().split())
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))
c_list = list(map(int,input().split()))
a_list.sort(reverse = True)
b_list.sort(reverse = True)
c_list.sort(reverse = True)
def check(p):
  count = 0
  for i in range(x):
    for j in range(y):
      for m in range(z):
        if a_list[i] + b_list[j] + c_list[m] < p:
          break
        count += 1
        if count >= k:
          return True
  return False

left = 1
right = 10**20
while right - left > 1:
  middle = (right + left)//2
  if check(middle) == True:
    left = middle
  else:
    right = middle
ans_list = []
for i in range(x):
    for j in range(y):
      for m in range(z):
        if a_list[i] + b_list[j] + c_list[m] < left -1:
          break
        if a_list[i] + b_list[j] + c_list[m] >= left+1:
          ans_list.append(a_list[i] + b_list[j] + c_list[m])
nokori = k - len(ans_list)
for i in range(nokori):
  ans_list.append(left)
  
ans_list.sort(reverse = True)
for ans in ans_list:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
x, y, z, k = map(int,input().split())
alist = [int(i) for i in input().split()]
blist = [int(i) for i in input().split()]
clist = [int(i) for i in input().split()]


# print(x,y,z,k, alist, blist, clist)

#%%
# x,y,z,k,alist,blist,clist = 2, 2, 2, 8, [4, 6],[1, 5],[3, 8]


#%%
alist.sort()
blist.sort()
clist.sort()
alist = alist[::-1][:k]
blist = blist[::-1][:k]
clist = clist[::-1][:k]

#%%
import itertools




#%%
ans = []
for i in list(itertools.product(alist, blist, clist)):
    ans.append((sum(i)))

ans.sort()
#%%
for i in ans[::-1][:k]:
    print(i)

#%%

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc123/tasks/abc123_d

def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

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

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

E = []
for a in A:
    for b in B:
        E.append(a+b)
# E.sort(reverse=True)

ans = []

for c in C:
    for e in E:
        ans.append(c+e)
ans.sort(reverse=True)

for i, a in enumerate(ans):
    if i < K:
        print(a)
    else:
        break

Submission status: Time Limit Exceeded

Solution code:
# D - Cake 123

import heapq
import itertools

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

a = list(int(x) for x in input().split())
b = list(int(x) for x in input().split())
c = list(int(x) for x in input().split())

anslist = []
ip = list(itertools.product(a, b, c))   
for i in range(len(ip)):
    heapq.heappush(anslist, sum(ip[i])*-1)

for i in range(k):
    print(heapq.heappop(anslist)*-1)

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 = []
e = []
for m in range(x):
    for n in range(y):
        d.append(a[m]+b[n])
d.sort(reverse = True)
for m in range(z):
    for n in range(min(3000, len(d))):
        e.append(c[m]+d[n])
e.sort(reverse = True)
for n in range(k):
    print(e[n])

Submission status: Time Limit Exceeded

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

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

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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

    num = 0
    for a in A:
        last_num = num
        j = len(C) - 1
        for i in B:
            while i + C[j] < m - a:
                j -= 1
                if j < 0: break
            else:
                num += j + 1
                continue
            break
        if last_num == num: break
    if num > K:
        l = m
    else:
        h = m

ans = []
cnt = 0
for a in A:
    j = len(C) - 1
    last_cnt = cnt
    for i in B:

        while i + C[j] < l - a:
            j -= 1
            if j < 0: break
        else:
            for k in range(j+1):
                ans.append(a+i+C[k])
                cnt += 1
            continue
        break
    if last_cnt == cnt: break
ans.sort(reverse=True)
print (*ans[:K],sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

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

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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

    num = 0
    for a in A:
        j = len(C) - 1
        for i in B:
            while i + C[j] < m - a:
                j -= 1
                if j < 0: break
            else:
                num += j + 1
                continue
            break
    if num > K:
        l = m
    else:
        h = m

ans = []
for a in A:
    j = len(C) - 1
    for i in B:
        while i + C[j] < l - a:
            j -= 1
            if j < 0: break
        else:
            for k in range(j+1):
                ans.append(a+i+C[k])
            continue
        break
ans.sort(reverse=True)
print (*ans[:K],sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
sys.setrecursionlimit(10**7)
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())

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

if X > Y and X > Z:
    A,C = C,A
elif Y > X and Y > Z:
    B,C = C,B

AB = [a + b for a in A for b in B]
AB.sort(reverse=True)
C.sort(reverse=True)
max_abc = AB[0] + C[0]

l = 0
h = max_abc

while h > l+1:
    m = (h+l) // 2

    num = 0
    j = len(C) - 1
    for i in AB:
        while i + C[j] < m:
            j -= 1
            if j < 0: break
        else:
            num += j + 1
            continue
        break
    if num > K:
        l = m
    else:
        h = m

ans = []
j = len(C) - 1
for i in AB:
    while i + C[j] < l:
        j -= 1
        if j < 0: break
    else:
        for k in range(j+1):
            ans.append(i+C[k])
        continue
    break
ans.sort(reverse=True)
print (*ans[:K],sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

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


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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

    num = 0
    for a in A:
        j = len(C) - 1
        for i in B:
            while i + C[j] < m - a:
                j -= 1
                if j < 0: break
            else:
                num += j + 1
                continue
            break
    if num > K:
        l = m
    else:
        h = m

ans = []
for a in A:
    j = len(C) - 1
    for i in B:
        while i + C[j] < l - a:
            j -= 1
            if j < 0: break
        else:
            for k in range(j+1):
                ans.append(a+i+C[k])
ans.sort(reverse=True)
print (*ans[:K],sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

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


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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

    num = 0
    for a in A:
        for i in B:
            j = len(C) - 1
            while i + C[j] < m - a:
                j -= 1
                if j < 0: break
            else:
                num += j + 1
                continue
            break
    if num > K:
        l = m
    else:
        h = m

ans = []
for a in A:
    for i in B:
        j = len(C) - 1
        while i + C[j] < l - a:
            j -= 1
            if j < 0: break
        else:
            for k in range(j+1):
                ans.append(a+i+C[k])
ans.sort(reverse=True)
print (*ans[:K],sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
sys.setrecursionlimit(10**7)
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())

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

if len (C) > len(A) and len(C) > len(B):
    A,C = C,A
elif len(B) > len(A) and len(B) > len(C):
    A,B = B,A

AB = [a + b for a in A for b in B]
AB.sort(reverse=True)
C.sort(reverse=True)
max_abc = AB[0] + C[0]

l = 0
h = max_abc

while h > l+1:
    m = (h+l) // 2

    num = 0
    for i in AB:
        j = len(C) - 1
        while i + C[j] < m:
            j -= 1
            if j < 0: break
        else:
            num += j + 1
            continue
        break
    if num > K:
        l = m
    else:
        h = m

ans = []
for i in AB:
    j = len(C) - 1
    while i + C[j] < l:
        j -= 1
        if j < 0: break
    else:
        for k in range(j+1):
            ans.append(i+C[k])
ans.sort(reverse=True)
print (*ans[:K],sep='\n')

Submission status: Time Limit Exceeded

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

a = sorted(a, reverse = True)
b = sorted(b, reverse = True)
c = sorted(c, reverse = True)

ans = [[-(a[0] + b[0] + c[0]), 1, 1, 1]]
heapify(ans)
log = [[[0] * z for i in range(y)] for j in range(x)]
log[0][0][0] = 1

d = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
for i in range(k):
    temp, s, t, u = heappop(ans)
    temp = temp * (-1)
    print(temp)
    for ds, dt, du in d:
        ns = s + ds
        nt = t + dt
        nu = u + du
        if not(0 <= ns <= x and 0 <= nt <= y and 0 <= nu <= z):continue
        if log[ns - 1][nt - 1][nu - 1]:continue
        heappush(ans, [-(a[ns - 1] + b[nt - 1] + c[nu - 1]), ns, nt, nu])
        log[ns - 1][nt - 1][nu - 1] = 1

Submission status: Time Limit Exceeded

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

a = sorted(a, reverse = True)
b = sorted(b, reverse = True)
c = sorted(c, reverse = True)

ans = [[-(a[0] + b[0] + c[0]), 1, 1, 1]]
heapify(ans)
log = [[[0] * z for i in range(y)] for j in range(x)]
log[0][0][0] = 1

d = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
for i in range(k):
    temp, s, t, u = heappop(ans)
    temp = temp * (-1)
    print(temp)
    for ds, dt, du in d:
        ns = s + ds
        nt = t + dt
        nu = u + du
        if not(0 <= ns <= x and 0 <= nt <= y and 0 <= nu <= z):continue
        if log[ns - 1][nt - 1][nu - 1]:continue
        heappush(ans, [-(a[ns - 1] + b[nt - 1] + c[nu - 1]), ns, nt, nu])
        log[ns - 1][nt - 1][nu - 1] = 1

Submission status: Time Limit Exceeded

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

c = sorted(c, reverse=True)
ab = []
for i in range(x):
    for j in range(y):
        temp = a[i] + b[j]
        ab.append(temp)

ab = sorted(ab, reverse=True)
ans = []
for i in range(z):
    for j in range(min(x * y, k)):
        temp = c[i] + ab[j]
        ans += [temp]
ans = sorted(ans, 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())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

c = sorted(c, reverse=True)
ab = []
for i in range(x):
    for j in range(y):
        temp = a[i] + b[j]
        ab.append(temp)
ab = sorted(ab, reverse=True)
ans = []
for i in range(z):
    for j in range(x * y):
        temp = c[i] + ab[j]
        ans += [temp]
ans = sorted(ans, 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())
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)

K=min(K,3000)

AB=[a+b for a in A for b in B]
AB.sort(reverse=True)

ABC=[ab+c for ab in AB[:K] 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()))


K=min(K,3000)

AB=[a+b for a in A for b in B]
AB.sort(reverse=True)

ABC=[ab+c for ab in AB[:K] 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()))

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

K=min(K,3000)
ab=[0]*K
abc=[]
i=0



for x in range(X):
  for y in range(Y):
    if i<=K-1:
    	ab[i]=A[x]+B[y]
    else:
      	ab.append(A[x]+B[y])
    i +=1

ab.sort(reverse=True)

abc=[ab[k]+C[z] for k in range(K) 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()))

K=min(K,3000)
ab=[0]*K
abc=[]
i=0

for x in range(X):
  for y in range(Y):
    if i<=K-1:
    	ab[i]=A[x]+B[y]
    else:
      	ab.append(A[x]+B[y])
    i +=1

ab.sort(reverse=True)


abc=[ab[k]+C[z] for k in range(K) 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=[0]*K
abc=[]
i=0

for x in range(X):
  for y in range(Y):
    if i<=K-1:
    	ab[i]=A[x]+B[y]
    else:
      	ab.append(A[x]+B[y])
    i +=1

ab.sort(reverse=True)

K=min(K,3000)

for k in range(K):
  for z in range(Z):
    abc.append(ab[k]+C[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=[0]*K
abc=[]
i=0

for x in range(X):
  for y in range(Y):
    if i<=K-1:
    	ab[i]=A[x]+B[y]
    else:
      	ab.append(A[x]+B[y])
    i +=1

ab.sort(reverse=True)

for k in range(K):
  for z in range(Z):
    abc.append(ab[k]+C[z])

abc.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
readline = sys.stdin.buffer.readline

X, Y, Z, K = map(int, readline().split())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))
C = list(map(int, readline().split()))
AB = [a+b for (a,b) in list(itertools.product(A,B))]
AB.sort(reverse=True)
ans = [ab + c for (ab,c) in list(itertools.product(AB[:K],C))]
ans.sort(reverse=True)
for i in range(K):
  print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def main():
    X, Y, Z, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))
    C = list(map(int, readline().split()))
    AB = [a+b for (a,b) in list(itertools.product(A,B))]
    AB.sort(reverse=True)
    ans = [ab + c for (ab,c) in list(itertools.product(AB[:K],C))]
    ans.sort(reverse=True)
    for i in range(K):
        print(ans[i])


if __name__ == '__main__':
    main()

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

A1=[-1*i for i in A]
B1=[-1*i for i in B]
C1=[-1*i for i in C]


list=[]
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            list.append(A1[i]+B1[j]+C1[k])
heapq.heapify(list)
for l in range(K):
    print(-1*heapq.heappop(list))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
from bisect import bisect_left, bisect_right
import itertools
from heapq import heapify, heappop, heappush

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 二分探索, 大きい方から１つずつheapに入れる、XとYは全探索
# AとBは全探索
X, Y, Z, K = lr()
A = lr(); A.sort()
B = lr(); B.sort()
C = lr(); C.sort()
D = []
for a, b in itertools.product(A, B):
    D.append(a+b)
D.sort()

def check(x):
    length = len(D)
    count = 0
    for y in C:
        i = bisect_left(D, x-y)
        count += length - i
    return count >= K

ok = 0; ng = 10 ** 11
while abs(ng-ok) > 1:
    mid = (ok+ng) // 2
    if check(mid):
        ok = mid
    else:
        ng = mid

# ok以上の値
answer = []
for y in C:
    index = bisect_left(D, ok-y)
    for i in range(index, len(D)):
        heappush(answer, -(D[i]+y))

for i in range(K):
    print(-heappop(answer))

# 02

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
from bisect import bisect_left, bisect_right
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 二分探索, 大きい方から１つずつheapに入れる、XとYは全探索
# AとBは全探索
X, Y, Z, K = lr()
A = lr(); A.sort()
B = lr(); B.sort()
C = lr(); C.sort()
D = []
for a, b in itertools.product(A, B):
    D.append(a+b)
D.sort()

def check(x):
    length = len(D)
    count = 0
    for y in C:
        i = bisect_left(D, x-y)
        count += length - i
    return count >= K

ok = 0; ng = 10 ** 11
while abs(ng-ok) > 1:
    mid = (ok+ng) // 2
    if check(mid):
        ok = mid
    else:
        ng = mid

# ok以上の値
answer = []
for y in C:
    index = bisect_left(D, ok-y)
    for i in range(index, len(D)):
        answer.append(D[i]+y)
answer.sort(reverse=True)
for i in range(K):
    print(answer[i])

# 02

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()))
if X<Y and X<Z:
  X,Z=Z,X
  A,C=C,A
elif Y<X and Y<Z:
  Y,Z=Z,Y
  B,C=C,Y
C=sorted(C,reverse=True)
k=min(K,X*Y)
AB=sorted([a+b for a in A for b in B],reverse=True)[:k]
ABC=sorted([ab+c for ab in AB for c in C[:K]],reverse=True)[:K]
for x in ABC:
  print(x)

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()))
if X>Y and X>Z:
  X,Z=Z,X
  A,C=C,A
elif Y>X and Y>Z:
  Y,Z=Z,Y
  B,C=C,Y
C=sorted(C,reverse=True)
k=min(K,X*Y)
AB=sorted([a+b for a in A for b in B],reverse=True)[:k]
ABC=sorted([ab+c for ab in AB for c in C[:K]],reverse=True)[:K]
for x in ABC:
  print(x)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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()))
k=min(K,X*Y)
AB=sorted([a+b for a in A for b in B],reverse=True)[:k]
ABC=sorted([ab+c for ab in AB for c in C],reverse=True)[:K]
for x in ABC:
  print(x)

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 av in a:
    for bv in b:
        ab.append(av + bv)
ab = sorted(ab, reverse=True)[:k]
abc = []
for abv in ab:
    for cv in c:
        abc.append(abv + cv)
abc = sorted(abc, reverse=True)[:k]
for ans in abc:
    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()))
ans = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            ans.append(A[i]+B[j]+C[k])

for i in heapq.nlargest(K, ans):
    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()))
ans = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            ans.append(A[i]+B[j]+C[k])
ans.sort(reverse = True)
for i in ans[:K]:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
# pypyでギリ。
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

x, y, z, k = map(int, readline().split())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))
C = list(map(int, readline().split()))
goukei = []
for a in A:
    for b in B:
        goukei.append(a + b)
goukei.sort()
goukei2 = goukei[-k:]
goukei3 = []
for c in C:
    for ab in goukei2:
        goukei3.append(ab + c)
goukei3.sort(reverse=True)
print(*goukei3[:k], sep="\n")

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop

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()))
used = [[[False] * (z + 1) for _ in range(y + 1)] for _ in range(x + 1)]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
kouho = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]


def calc(a, b, c):
    if not used[a][b][c]:
        used[a][b][c] = True
        heappush(kouho, ((-A[a] - B[b] - C[c]), a, b, c))


used[0][0][0] = True
for i in range(k):
    temp, p, q, r = heappop(kouho)
    temp *= -1
    print(temp)
    if p + 1 < x:
        calc(p + 1, q, r)
    if q + 1 < y:
        calc(p, q + 1, r)
    if r + 1 < z:
        calc(p, q, r + 1)

Submission status: Time Limit Exceeded

Solution code:
x, y, z, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
goukei = []
for i in range(x):
    for j in range(y):
        goukei.append(A[i] + B[j])
goukei.sort()
goukei2 = goukei[-k:]
goukei3 = []
for c in C:
    for ab in goukei2:
        goukei3.append(ab + c)
goukei3.sort(reverse=True)
print(*goukei3[:k], sep="\n")

Submission status: Time Limit Exceeded

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

a, b, c = (sorted(map(int, sys.stdin.readline().split()), reverse=True) for _ in range(3))

abc = [[[0 for _ in range(z)] for __ in range(y)] for ___ in range(x)]

stu = [[-a[0]-b[0]-c[0], 0,0,0]]
heapq.heapify(stu)
result = []
while k > 0:
    out_p, n, m, l = heapq.heappop(stu)
    if n+1<x and abc[n+1][m][l]==0:
        abc[n+1][m][l]=1
        heapq.heappush(stu, [-a[n+1]-b[m]-c[l], n+1, m, l])
    if m+1<y and abc[n][m+1][l]==0:
        abc[n][m+1][l]=1
        heapq.heappush(stu, [-a[n]-b[m+1]-c[l], n, m+1, l])
    if l+1<z and abc[n][m][l+1]==0:
        abc[n][m][l+1]=1
        heapq.heappush(stu, [-a[n]-b[m]-c[l+1], n, m, l+1])
    result.append(-out_p)
    k -= 1
result.sort(reverse=True)
for i in result:
    print(i)

Submission status: Time Limit Exceeded

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

abc = [[[0 for _ in range(z)] for __ in range(y)] for ___ in range(x)]

stu = [[-a[0]-b[0]-c[0], 0,0,0]]
heapq.heapify(stu)
while k > 0:
    out_p, n, m, l = heapq.heappop(stu)
    if n+1<x and abc[n+1][m][l]==0:
        abc[n+1][m][l]=1
        heapq.heappush(stu, [-a[n+1]-b[m]-c[l], n+1, m, l])
    if m+1<y and abc[n][m+1][l]==0:
        abc[n][m+1][l]=1
        heapq.heappush(stu, [-a[n]-b[m+1]-c[l], n, m+1, l])
    if l+1<z and abc[n][m][l+1]==0:
        abc[n][m][l+1]=1
        heapq.heappush(stu, [-a[n]-b[m]-c[l+1], n, m, l+1])
    print(-out_p)
    k -= 1

Submission status: Time Limit Exceeded

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

abc = []
for s in a:
    for t in b:
        for u in c:
            abc.append(-s-t-u)
heapq.heapify(abc)
for i in range(k):
    print(-heapq.heappop(abc))

Submission status: Time Limit Exceeded

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

abc = []
heapq.heapify(abc)
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
for s in a:
    for t in b:
        for u in c:
            heapq.heappush(abc, -s-t-u)

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

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()))
d = []
da = d.append
for i in range(x):
  for j in range(y):
    da(a[i]+b[j])
f = []
e = sorted(d,reverse=True)[:min(k,x*y)]
fa = f.append
for i in range(min(k,x*y)):
  for j in range(z):
    fa(e[i]+c[j])
f.sort(reverse=True)
for i in range(k):
  print(f[i])