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=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

mix_ab= [a_+b_ for a_,b_ in itertools.product(a, b)]
mix_ab.sort(reverse=True)
c.sort(reverse=True)

mix_abc= [ab_+c_ for ab_,c_ in itertools.product(mix_ab[:k], c[:k])]
mix_abc.sort(reverse=True)

print(*mix_abc[:k], sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

import itertools

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

amax = max(a)
a_diff = [a[i]-amax for i in range(0,x)]
bmax = max(b)
b_diff = [b[i]-bmax for i in range(0,y)]
cmax = max(c)
c_diff = [c[i]-cmax for i in range(0,z)]

minus= [a_+b_+c_ for a_,b_,c_ in itertools.product(a_diff, b_diff, c_diff)]
minus.sort(reverse=True)

ans0 = amax+bmax+cmax
for i in range(k):
    ans = ans0 + minus[i]
    print(ans)

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)

top = A[0]+B[0]+C[0]

flg = [[[0] * Z for _ in range(Y)] for _ in range(X)]
flg[0][0][0] = 1
q = []
heapq.heappush(q,[0,0,0,0])

cnt = 0

while cnt < K:
	d,a,b,c = heapq.heappop(q)
	print(top-d)
	cnt += 1

	if a+1 < X:
		if flg[a+1][b][c] == 0:
			flg[a+1][b][c] = 1
			heapq.heappush(q,[d+A[a]-A[a+1], a+1, b, c])
	if b+1 < Y:
		if flg[a][b+1][c] == 0:
			flg[a][b+1][c] = 1
			heapq.heappush(q,[d+B[b]-B[b+1], a, b+1, c])
	if c+1 < Z:
		if flg[a][b][c+1] == 0:
			flg[a][b][c+1] = 1
			heapq.heappush(q,[d+C[c]-C[c+1], a, b, c+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 = sorted(list(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[:min(K,len(AB))]

ABC = []
for i in range(min(K,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 = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
taste = []


for i in range(X):
  for j in range(Y):
    for k in range(Z):
      taste.append(A[i]+B[j]+C[k])

taste = sorted(taste, reverse=True)

for i in range(K):
  print(taste[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()))
taste = []

for i in range(X):
  for j in range(Y):
    for k in range(Z):
      taste.append(A[i]+B[j]+C[k])

taste = sorted(taste, reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def solve():

    from heapq import heappop, heappush


    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)

    sum_list = []
    seen_list = set()
    ans = []

    heappush(sum_list, (-(A[0]+B[0]+C[0]), 0, 0, 0))
    seen_list.add((0, 0, 0))

    while sum_list:
        s, i, j, k = heappop(sum_list)
        
        ans.append(-s)

        if i+1 < len(A) and (i+1, j, k) not in seen_list:
            heappush(sum_list, (-(A[i+1]+B[j]+C[k]), i+1, j, k))
            seen_list.add((i+1, j, k))

        if j+1 < len(B) and (i, j+1, k) not in seen_list:
            heappush(sum_list, (-(A[i]+B[j+1]+C[k]), i, j+1, k))
            seen_list.add((i, j+1, k))

        if k+1 < len(C) and (i, j, k+1) not in seen_list:
            heappush(sum_list, (-(A[i]+B[j]+C[k+1]), i, j, k+1))
            seen_list.add((i, j, k+1))

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

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

from heapq import heappop, heappush

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)

sum_list = []
seen_list = set()
ans = []

heappush(sum_list, (-(A[0]+B[0]+C[0]), 0, 0, 0))
seen_list.add((0, 0, 0))

while sum_list:
    s, i, j, k = heappop(sum_list)
    
    ans.append(str(-s))

    if i+1 < len(A) and (i+1, j, k) not in seen_list:
        heappush(sum_list, (-(A[i+1]+B[j]+C[k]), i+1, j, k))
        seen_list.add((i+1, j, k))

    if j+1 < len(B) and (i, j+1, k) not in seen_list:
        heappush(sum_list, (-(A[i]+B[j+1]+C[k]), i, j+1, k))
        seen_list.add((i, j+1, k))

    if k+1 < len(C) and (i, j, k+1) not in seen_list:
        heappush(sum_list, (-(A[i]+B[j]+C[k+1]), i, j, k+1))
        seen_list.add((i, j, k+1))

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

from heapq import heappop, heappush

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)

sum_list = []
seen_list = set()
ans = []

heappush(sum_list, (-(A[0]+B[0]+C[0]), 0, 0, 0))

while sum_list:
    s, i, j, k = heappop(sum_list)
    
    ans.append(-s)

    if i+1 < len(A) and (i+1, j, k) not in seen_list:
        heappush(sum_list, (-(A[i+1]+B[j]+C[k]), i+1, j, k))
        seen_list.add((i+1, j, k))

    if j+1 < len(B) and (i, j+1, k) not in seen_list:
        heappush(sum_list, (-(A[i]+B[j+1]+C[k]), i, j+1, k))
        seen_list.add((i, j+1, k))

    if k+1 < len(C) and (i, j, k+1) not in seen_list:
        heappush(sum_list, (-(A[i]+B[j]+C[k+1]), i, j, k+1))
        seen_list.add((i, j, k+1))

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

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
x = sorted(list(map(int, input().split())))
y = sorted(list(map(int, input().split())))
z = sorted(list(map(int, input().split())))
import heapq
shou = 3 * 10**10 + 1
ans = []
for i in range(X-1, -1, -1):
  for j in range(Y-1, -1, -1):
    for k in range(Z-1, -1, -1):
      if (X-i)*(Y-j)*(Z-k) <= K:
        sub = x[i]+y[j]+z[k]
        ans.append(sub)
ans = list(map(lambda x: x*(-1), ans)) 
heapq.heapify(ans)
for i in range(K):
  print(heapq.heappop(ans)*(-1))

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
x = sorted(list(map(int, input().split())))
y = sorted(list(map(int, input().split())))
z = sorted(list(map(int, input().split())))
import heapq
shou = 3 * 10**10 + 1
ans = []
heapq.heapify(ans)
for i in range(X-1, -1, -1):
  if len(ans) == K:
    sub = x[i]+y[Y-1]+z[Z-1]
    if sub < shou:
      break
  for j in range(Y-1, -1, -1):
    if len(ans) == K:
      sub = x[i]+y[j]+z[Z-1]
      if sub < shou:
        break
    for k in range(Z-1, -1, -1):
      sub = x[i]+y[j]+z[k]
      if len(ans) < K:
        heapq.heappush(ans, sub)
        shou = min(shou, sub)
      else:
        if sub < shou:
          break
        else:
          heapq.heappush(ans, sub)
          show = heapq.heappop(ans)
ans = list(map(lambda x: x*(-1), ans)) 
heapq.heapify(ans)
for i in range(len(ans)):
  print(heapq.heappop(ans)*(-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()))
deli = []
for x in range(X):
    for y in range(Y):
        for z in range(Z):
            heapq.heappush(deli, -1*(A[x]+B[y]+C[z]))
for k in range(K):
    print(-1*(heapq.heappop(deli)))

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    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.sort(reverse=1)
    ab = ab[:k]
    abc = []
    for i in ab:
        for j in c:
            abc.append(i+j)
    abc.sort(reverse=1)
    for i in range(k):
        print(abc[i])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#conding utf-8
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=[]
ans=[]
for i in range(len(a)):
  for j in range(len(b)):
    ab.append(a[i]+b[j])
    
ab.sort()
ab.reverse()
del ab[k:]

for i in range(len(ab)):
  for j in range(len(c)):
    ans.append(ab[i]+c[j])
    
ans.sort()
ans.reverse()

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

Submission status: Time Limit Exceeded

Solution code:
# damedatta
def binsearch(l, r, fn):
    while r - l > 1:
        m = (l + r) // 2
        if fn(m):
            l = m
        else:
            r = m
    return l
def main():
    X, Y, Z, K = map(int, input().split())
    D = sorted(map(int, input().split()), reverse=True)
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    A = sorted((i + j for i in B for j in C), reverse=True)
    def calc(x):
        t = 0
        ii = len(D) - 1
        for i in A:
            while ii >= 0 and D[ii] + i < x:
                ii -= 1
            t += ii + 1
            if t >= K:
                return True
            else:
                return False
    l = binsearch(D[-1] + A[-1], D[0] + A[0], calc)
    ii = len(D) - 1
    rl = []
    for i in A:
        while ii >= 0 and D[ii] + i < l:
            ii -= 1
        if ii < 0:
            break
        rl.extend([i + j for j in D[:ii+1]])
    rl.sort(reverse=True)
    print('\n'.join(str(i) for i in rl[:K]))
main()

Submission status: Time Limit Exceeded

Solution code:
# damedatta
def binsearch(l, r, fn):
    while r - l > 1:
        m = (l + r) // 2
        if fn(m):
            l = m
        else:
            r = m
    return l
def main():
    X, Y, Z, K = map(int, input().split())
    A = sorted(map(int, input().split()), reverse=True)
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    D = sorted((i + j for i in B for j in C), reverse=True)
    def calc(x):
        t = 0
        ii = len(D) - 1
        for i in A:
            while ii >= 0 and D[ii] + i < x:
                ii -= 1
            t += ii + 1
            if t >= K:
                return True
            else:
                return False
    l = binsearch(D[-1] + A[-1], D[0] + A[0], calc)
    ii = len(D) - 1
    rl = []
    for i in A:
        while ii >= 0 and D[ii] + i < l:
            ii -= 1
        if ii < 0:
            break
        rl.extend([i + j for j in D[:ii+1]])
    rl.sort(reverse=True)
    print('\n'.join(str(i) for i in rl[:K]))
main()

Submission status: Time Limit Exceeded

Solution code:
# damedatta
def binsearch(l, r, fn):
    while r - l > 1:
        m = (l + r) // 2
        if fn(m):
            l = m
        else:
            r = m
    return l
import bisect
def main():
    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 = [i + j for i in B for j in C]
    A.sort(reverse=True)
    D.sort(reverse=True)
    def calc(x):
        t = 0
        ii = len(D) - 1
        for i in A:
            while ii >= 0 and D[ii] + i < x:
                ii -= 1
            t += ii + 1
            if t >= K:
                return True
            else:
                return False
    l = binsearch(D[-1] + A[-1], D[0] + A[0], calc)
    ii = len(D) - 1
    rl = []
    for i in A:
        while ii >= 0 and D[ii] + i < l:
            ii -= 1
        if ii < 0:
            break
        rl.extend([i + j for j in D[:ii+1]])
    rl.sort(reverse=True)
    print('\n'.join(str(i) for i in rl[:K]))
main()

Submission status: Time Limit Exceeded

Solution code:
import bisect
def main():
    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 = max(X,Y,Z)
    if Y == m:
        B, A = A, B
    if Z == m:
        C, A = A, C
    D = []
    for i in B:
        for j in C:
            D.append(i+j)
    D.sort()
    A.sort(reverse=True)
    r = max(A) + D[0]
    l = 0
    ti = 0
    while l != r:
        k = (l + r + 1) // 2
        t = 0
        fin = False
        for i in A:
            for j, v in enumerate(D[ti:]):
                if i + v >= k:
                    t += (len(D) - j - ti)
                    ti += j
                    break
            else:
                fin = True
            if fin:
                break
        if t > K:
            l = k
        else:
            r = k - 1
    rl = []
    for i in A:
        ii = bisect.bisect_left(D, k-i)
        rl.extend([w+i for w in D[ii:]])
    rl.sort(reverse=True)
    print('\n'.join(str(i) for i in rl[:K]))
main()

Submission status: Time Limit Exceeded

Solution code:
import bisect
def main():
    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.sort()
    r = max(A) + max(B) + max(C)
    l = 0
    while l != r:
        k = (l + r) // 2
        t = 0
        for i in A:
            for j in B:
                ii = bisect.bisect_left(C, k-i-j)
                t += (Z - ii)
        if t > K:
            l = k
        else:
            r = k - 1
    rl = []
    for i in A:
        for j in B:
            ii = bisect.bisect_left(C, k-i-j)
            rl.extend([w+i+j for w in C[ii:]])
    rl.sort(reverse=True)
    for i in rl:
        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()))

F = []
for i in A:
  for j in B:
    F.append(i + j)
F.sort(reverse=True)
F = F[:min(X*Y, K)]
G = []
for i in C:
  for j in F:
    G.append(i + j)
G.sort(reverse=True)
for i in range(K):
  print(G[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()))

deli = []
for i in range(x):
    for j in range(y):
        for l in range(z):
            deli.append(a[i]+b[j]+c[l])
deli.sort()
deli = deli[::-1]
for i in range(k):
    print(deli[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 = [a + b for a in A for b in B]
AB.sort()
AB = AB[-K:]

ans = [ab + c for ab in AB for c in C]
ans.sort()

for x in ans[-K:][::-1]:
    print(x)

Submission status: Time Limit Exceeded

Solution code:
def main():
    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)
    ans = []
    for i in range(x):
        for j in range(y):
            for m in range(z):
                if (i+1)*(j+1)*(m+1) <= k:
                    ans.append(a[i]+b[j]+c[m])

    ans.sort(reverse=True)
    for i in range(k):
        print(ans[i])
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
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)
ans = []
for i in range(x):
    for j in range(y):
        for m in range(z):
            if (i+1)*(j+1)*(m+1) <= k:
                ans.append(a[i]+b[j]+c[m])

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

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


AB = []

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

AB = sorted(AB, reverse=True)
AB = AB[:min(k, x*y)]
# print(AB)

ABC = []
for l in range(min(k, x*y)):
    for m in range(z):
        ABC.append(AB[l] + C[m])

ABC = sorted(ABC, reverse=True)
for i in range(k):
    print(ABC[i])



# ------------優先度付きキュー----------

# hq = []
# heapq.heapify(hq)
# i, j, l = 0, 0, 0

# heapq.heappush(hq, [(A[i]+B[j]+C[l])*-1, i, j, l])
# Used = []

# for i in range(k):
#     now = heapq.heappop(hq)
#     print(now[0]*-1)
#     i = now[1]
#     j = now[2]
#     l = now[3]

#     if i >= x-1:
#         pass
#     elif [(A[i+1]+B[j]+C[l])*-1, i+1, j, l] not in Used:
#         Used.append([(A[i+1]+B[j]+C[l])*-1, i+1, j, l])
#         heapq.heappush(hq, [(A[i+1]+B[j]+C[l])*-1, i+1, j, l])
        
#     if j >= y-1:
#         pass
#     elif [(A[i]+B[j+1]+C[l])*-1, i, j+1, l] not in Used:
#         Used.append([(A[i]+B[j+1]+C[l])*-1, i, j+1, l])
#         heapq.heappush(hq, [(A[i]+B[j+1]+C[l])*-1, i, j+1, l])
        
#     if l >= z-1:
#         pass
#     elif [(A[i]+B[j]+C[l+1])*-1, i, j, l+1] not in Used:
#         Used.append([(A[i]+B[j]+C[l+1])*-1, i, j, l+1])
#         heapq.heappush(hq, [(A[i]+B[j]+C[l+1])*-1, i, j, l+1])
        
#     # print(hq)

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 = sorted(list(map(int,input().split())))
S = []
for x in A:
    for y in B:
        S.append(x+y)
S.sort(reverse = True)
S1 = []
for i in range(min(K,X*Y)):
    for j in range(min(len(C),K)):
        S1.append(S[i]+C[j])
S1.sort(reverse = True)
for i in range(K):
    print(S1[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()))
S = []
for x in A:
    for y in B:
        S.append(x+y)
S.sort(reverse = True)
S1 = []
for i in range(min(K,X*Y)):
    for z in C:
        S1.append(S[i]+z)
S1.sort(reverse = True)
for i in range(K):
    print(S1[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()))
S = []
for x in A:
    for y in B:
        for z in C:
            S.append(x+y+z)
S.sort(reverse = True)
for i in range(K):
    print(S[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()))
D = [0] * (x * y)
for i in range(x):
  for j in range(y):
    D[i * y + j] = A[i] + B[j]
D.sort(reverse=True)
E = [0] * (z * k)
size = min(x * y, k)
for i in range(z):
  for j in range(size):
    E[i * size + j] = C[i] + D[j]
E.sort(reverse=True)
print(*E[:k], sep='\n')

Submission status: Time Limit Exceeded

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


ab = []
for aa in sorted(a)[::-1][:k]:
    for bb in sorted(b)[::-1][:k]:
        ab.append(aa+bb)

ab = sorted(ab)[::-1][:k]

ans = []
for aabb in ab:
    for cc in sorted(c)[::-1][:k]:
        ans.append(aabb+cc)

for ansi in sorted(ans)[::-1][:k]:
    print(ansi)

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 aa in a:
    for bb in b:
        ab.append(aa+bb)

ab = sorted(ab)[::-1][:k]

ans = []
for aabb in ab:
    for cc in c:
        ans.append(aabb+cc)

for ansi in sorted(ans)[::-1][:k]:
    print(ansi)

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 aa in a:
    for bb in b:
        for cc in c:
            ans.append(aa+bb+cc)

for ansi in sorted(ans)[::-1][:k]:
    print(ansi)

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

import bisect

ans = []
for aa in a:
    for bb in b:
        for cc in c:
            bisect.insort_right(ans,aa+bb+cc)

for ansi in ans[::-1][:k]:
    print(ansi)

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=1)
B = sorted(list(map(int,input().split())),reverse=1)
C = sorted(list(map(int,input().split())),reverse=1)
Q = [(-A[0]-B[0]-C[0],0,0,0)]
heapq.heapify(Q)
abc = [[[1 for i in range(Z)] for j in range(Y)] for k in range(X)]
for i in range(K):
    n,a,b,c = heapq.heappop(Q)
    if a < X-1 and abc[a+1][b][c]:
        abc[a+1][b][c] = 0
        heapq.heappush(Q,(n+A[a]-A[a+1],a+1,b,c))
    if b < Y-1 and abc[a][b+1][c]:
        abc[a][b+1][c] = 0
        heapq.heappush(Q,(n+B[b]-B[b+1],a,b+1,c))
    if c < Z-1 and abc[a][b][c+1]:
        abc[a][b][c+1] = 0
        heapq.heappush(Q,(n+C[c]-C[c+1],a,b,c+1))
    print(-n)

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()))
AB = sorted([a+b for a,b in product(A,B)],reverse=True)[:K]
ans = sorted([a+b for a,b in product(AB,C)],reverse=True)[:K]
for i in ans:
    print(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()))

l1=sorted([i+j for i in a for j in b],reverse=True)[:k]
l2=sorted([i+j for i in l1 for j in c],reverse=True)[:k]
for i in l2:
  print(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()))

l1=sorted([i+j for i in a for j in b],reverse=True)[:k]
l2=[i+j for i in l1 for j in c]
l2.sort(reverse=True)
for i in l2[:k]:
  print(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()))
l1=[]
#print(a,end=" a\n")
#print(b,end=" b\n")

l1=list([i+j for i in a for j in b])
l1.sort(reverse=True)
#for i in a:
#  for j in b:
#    # print(i,j)
#    l1.append(i+j)
#l1.sort(reverse=True)
#print(l1)
l1=l1[:min(len(l1),k)]
l2=list([i+j for i in l1 for j in c])
#l2=[]
#for i in l1:
#  for j in c:
#    l2.append(i+j)
l2.sort(reverse=True)
#print(l2)
#print("\n".join(map(str,l2[:k])))
for i in l2[:k]:
  print(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()))
l1=[]
#print(a,end=" a\n")
#print(b,end=" b\n")

l1=list([i+j for i in a for j in b])
l1.sort(reverse=True)
#for i in a:
#  for j in b:
#    # print(i,j)
#    l1.append(i+j)
#l1.sort(reverse=True)
#print(l1)
l1=l1[:min(len(l1),k)]
l2=list([i+j for i in l1 for j in c])
#l2=[]
#for i in l1:
#  for j in c:
#    l2.append(i+j)
l2.sort(reverse=True)
#print(l2)
print("\n".join(map(str,l2[:k])))

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()))
l1=[]
#print(a,end=" a\n")
#print(b,end=" b\n")
for i in a:
  for j in b:
    # print(i,j)
    l1.append(i+j)
l1.sort(reverse=True)
#print(l1)
l1=l1[:min(len(l1),k)]
l2=[]
for i in l1:
  for j in c:
    l2.append(i+j)
l2.sort(reverse=True)
#print(l2)
print("\n".join(map(str,l2[: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()))
l1=[]
#print(a,end=" a\n")
#print(b,end=" b\n")
for i in a:
  for j in b:
    # print(i,j)
    l1.append(i+j)
l1.sort(reverse=True)
#print(l1)
l1=l1[:min(len(l1),k)]
l2=[]
for i in l1:
  for j in c:
    l2.append(i+j)
l2.sort(reverse=True)
#print(l2)
print("\n".join(map(str,l2[: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()))
l1=[]
#print(a,end=" a\n")
#print(b,end=" b\n")
for i in a:
  for j in b:
    # print(i,j)
    l1.append(i+j)
l1.sort(reverse=True)
#print(l1)
l1=l1[:min(len(l1),3000)]
l2=[]
for i in l1:
  for j in c:
    l2.append(i+j)
l2.sort(reverse=True)
#print(l2)
print("\n".join(map(str,l2[:k])))

Submission status: Time Limit Exceeded

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

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

abc.sort(reverse=True)
print("\n".join(map(str, 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 = []
for i in a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse=True)
ab = ab[:k]

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

Submission status: Time Limit Exceeded

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

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

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

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

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

Submission status: Time Limit Exceeded

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

a.sort()
b.sort()
c.sort()

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

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

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