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: Accepted

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)

cakes = []

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

visited = set((A[0], B[0], C[0]))

for i in range(K):
    v, a, b, c = heappop(cakes)
    print(-v)

    if a+1 <= len(A) - 1 and (a+1, b, c) not in visited:
        v = A[a+1] + B[b] + C[c]
        heappush(cakes, (-v, a+1, b, c))
        visited.add((a+1, b, c))
    if b+1 <= len(B) - 1 and (a, b+1, c) not in visited:
        v = A[a] + B[b+1] + C[c]
        heappush(cakes, (-v, a, b+1, c))
        visited.add((a, b+1, c))
    if c+1 <= len(C) - 1 and (a, b, c+1) not in visited:
        v = A[a] + B[b] + C[c+1]
        heappush(cakes, (-v, a, b, c+1))
        visited.add((a, b, c+1))

Submission status: Accepted

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

import heapq as hq

x,y,z,k=map(int,input().split())
A=list(reversed(sorted(map(int,input().split()))))
B=list(reversed(sorted(map(int,input().split()))))
C=list(reversed(sorted(map(int,input().split()))))
P=[]
hq.heapify(P)
for a in A:
    for b in B:
        hq.heappush(P,-(a+b))
Q=[]
hq.heapify(Q)
for i in range(min(x*y,3000)):
    p=-hq.heappop(P)
    for c in C:
        hq.heappush(Q,-(p+c))
for i in range(k):
    print(-hq.heappop(Q))

Submission status: Accepted

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

from heapq import heappop, heappush, heapify
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))

for _ in range(K):
    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: Accepted

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


from heapq import heappop, heappush, heapify
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))

for _ in range(K):
    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: Accepted

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

def main():

    from heapq import heappop, heappush, heapify
    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))

    for _ in range(K):
        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__":
    main()

Submission status: Accepted

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
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)
      else:
        break
ans = list(map(lambda x: x*(-1), ans)) 
heapq.heapify(ans)
for i in range(K):
  print(heapq.heappop(ans)*(-1))

Submission status: Accepted

Solution code:
import heapq
import math

NN = 2020202
MOD = 10**9+7
INF = float("inf")

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 = [(-(A[0]+B[0]+C[0]), 0, 0, 0)]
seen = set([(0, 0, 0)])

for i in range(k):
    v, ai, bi, ci = heapq.heappop(que)
    print(-v)
    if ai < len(A)-1 and (ai+1, bi, ci) not in seen:
        seen.add((ai+1, bi, ci))
        heapq.heappush(que, (-(A[ai+1]+B[bi]+C[ci]), ai+1, bi, ci))
    if bi < len(B)-1 and (ai, bi+1, ci) not in seen:
        seen.add((ai, bi+1, ci))
        heapq.heappush(que, (-(A[ai]+B[bi+1]+C[ci]), ai, bi+1, ci))
    if ci < len(C)-1 and (ai, bi, ci+1) not in seen:
        seen.add((ai, bi, ci+1))
        heapq.heappush(que, (-(A[ai]+B[bi]+C[ci+1]), ai, bi, ci+1))

Submission status: Accepted

Solution code:
from heapq import nlargest
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=nlargest(k,(x+y for x in a for y in b))
abc=nlargest(k,(x+y for x in ab for y in c))
for i in abc:
  print(i)

Submission status: Accepted

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 = []
heapq.heapify(q)
for i in range(x):
    if i == 0:
        jlim = y
    else:
        jlim = min(-(-k//i),y)
    for j in range(jlim):
        if i*j == 0:
            llim = z
        else:
            llim = min(-(-k//(i*j)),z)
        for l in range(llim):
            heapq.heappush(q,-(a[i]+b[j]+c[l]))
for i in range(k):
    x = heapq.heappop(q)
    print(-x)

Submission status: Accepted

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
    #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,n = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = list(map(int, input().split()))
    a.sort(reverse=1)
    b.sort(reverse=1)
    c.sort(reverse=1)
    
    def check(p):
        cnt = 0
        for i in range(x):
            for j in range(y):
                for k in range(z):
                    if a[i]+b[j]+c[k] < p:
                        break
                    cnt += 1
                    if cnt==n:
                        return True
        return False

    left = 0
    right = 10**15
    while right-left>1:
        mid = (right+left)//2
        if check(mid):
            left = mid
        else:
            right = mid
    
    res = []
    for i in range(x):
        for j in range(y):
            for k in range(z):
                if a[i]+b[j]+c[k] < left+1:
                    break
                res.append(a[i]+b[j]+c[k])
    res.sort(reverse=1)
    if len(res)<n:
        for i in res:
            print(i)
        for _ in range(n-len(res)):
            print(left)
    else:
        for i in res:
            print(i)

if __name__ == '__main__':
    main()

Submission status: Accepted

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
    #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,n = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = list(map(int, input().split()))
    a.sort(reverse=1)
    b.sort(reverse=1)
    c.sort(reverse=1)
    res = []
    hq = [[-(a[0]+b[0]+c[0]), 0, 0, 0]]
    heapq.heapify(hq)
    check = set([(0, 0, 0)])
    for _ in range(n):
        cost, i, j, k = heapq.heappop(hq)
        res.append(-cost)
        if i+1<x and not (i+1, j, k) in check:
            heapq.heappush(hq, [-(a[i+1]+b[j]+c[k]), i+1, j, k])
            check.add((i+1, j, k))
        if j+1<y and not (i, j+1, k) in check:
            heapq.heappush(hq, [-(a[i]+b[j+1]+c[k]), i, j+1, k])
            check.add((i, j+1, k))
        if k+1<z and not (i, j, k+1) in check:
            heapq.heappush(hq, [-(a[i]+b[j]+c[k+1]), i, j, k+1])
            check.add((i, j, k+1))
    for i in res:
        print(i)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#xyzの全通り調べると間に合わない
#まず問題を分けよう
#先にxyだけ考えてみる
#xyの和をとってからそれとzの和をとるとき
#xyの和においてk番目以降の値を使うことは無い
#

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()))
    a.sort(reverse=1)
    b.sort(reverse=1)
    c.sort(reverse=1)
    abc = []
    for i in range(x):
        for j in range(y):
            if i*j>k:
                continue
            for t in range(z):
                if i*j*t>k:
                    continue
                abc.append(a[i]+b[j]+c[t])
    abc.sort(reverse=1)
    for i in range(k):
        print(abc[i])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

import bisect

# from collections import Counter, deque, defaultdict
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd

import itertools

# from operator import attrgetter, itemgetter
# import math

# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    x, y, z, k = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    b = list(map(int, readline().split()))
    c = list(map(int, readline().split()))

    ab = [0] * x * y

    ab = [i + j for i, j in itertools.product(a, b)]
    ab.sort(reverse=True)
    c.sort(reverse=True)

    ab = ab[:k]

    abc = [i + j for i, j in itertools.product(ab, c)]
    abc.sort(reverse=True)

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

ans=[]

for i in range(len(a)):
  for j in range(len(b)):
    if (i+1)*(j+1)>K:
      break
    for k in range(len(c)):
      if (i+1)*(j+1)*(k+1)>K:
        break
      ans.append(a[i]+b[j]+c[k])
    
ans.sort()
ans.reverse()

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

Submission status: Accepted

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:]

c.sort()
c.reverse()

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: Accepted

Solution code:
x, y, z, K = map(int, input().split())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
Z = list(map(int, input().split()))
XY = []
for i in range(x):
    for j in range(y):
        XY.append(X[i]+Y[j])
XY.sort()
Z.sort()
XYZ = []
for x in XY[::-1][:K]:
    for z in Z:
        XYZ.append(x+z)
XYZ.sort()
print(*XYZ[::-1][:K])

Submission status: Accepted

Solution code:
#123_D
import heapq
ans = []
heapq.heapify(ans)
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)

for i in range(x):
    p = k // (i+1)
    for j in range(min(p + 1, y)):
        q = p // (j+1)
        for l in range(min(q + 1, z)):
            heapq.heappush(ans, -(a[i] + b[j] + c[l]))
            
for _ in range(k):
    print(-heapq.heappop(ans))

Submission status: Accepted

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)
ans=[]
for i in range(min(x,k+1)):
	for j in range(y):
		if (i+1)*(j+1)>k:
			break
		for l in range(z):
			if (i+1)*(j+1)*(l+1)<=k:
				ans.append(a[i]+b[j]+c[l])
			else:
				break
ans.sort()
for i in range(k):
	print(ans[-i-1])

Submission status: Accepted

Solution code:
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()))
	ab=[]
	for x in a:
		for y in b:
			ab.append(x+y)
	ab.sort(reverse=True)
	if len(ab)>k:
		ab=ab[:k+1]
	c.sort()
	l=0
	r=max(ab)+max(c)+1
	import bisect
	while r-l>1:
		cnt=0
		mid=(l+r)//2
		for x in ab:
			cnt+=Z-bisect.bisect_left(c,mid-x)
		if cnt>=k:
			l=mid
		else:
			r=mid
	ans=[]
	for i in range(len(ab)):
		for j in range(bisect.bisect_left(c,l-ab[i]),Z):
			ans.append(ab[i]+c[j])
	ans.sort(reverse=True)
	for i in range(min(k,len(ans))):
		print(ans[i])
if __name__=="__main__":
	main()

Submission status: Accepted

Solution code:
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()))
	ab=[]
	for x in a:
		for y in b:
			ab.append(x+y)
	ab.sort(reverse=True)
	if len(ab)>k:
		ab=ab[:k+1]
	c.sort()
	l=0
	r=max(ab)+max(c)+1
	import bisect
	while r-l>1:
		cnt=0
		mid=(l+r)//2
		for x in ab:
			cnt+=Z-bisect.bisect_left(c,mid-x)
		if cnt>=k:
			l=mid
		else:
			r=mid
	ans=[]
	for i in range(len(ab)):
		for j in range(bisect.bisect_left(c,l-ab[i]),Z):
			ans.append(ab[i]+c[j])
	ans.sort(reverse=True)
	for i in range(min(k,len(ans))):
		print(ans[i])
main()

Submission status: Accepted

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
            if ii < 0:
                return False
        return t >= K
    l = binsearch(D[-1] + A[-1], D[0] + A[0]+1, calc)
    ii = len(D) - 1
    rl = []
    for i in A:
        while ii >= 0 and D[ii] + i < l + 1:
            ii -= 1
        if ii < 0:
            break
        rl.extend(i + j for j in D[:ii+1])
    rl.sort(reverse=True)
    rl += [l] * (K - len(rl))
    print('\n'.join(str(i) for i in rl))
main()

Submission status: Accepted

Solution code:
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
from fractions import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

def main():
    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)
    h = []
    for i in A:
        for j in B:
            if len(h) < K:
                heappush(h, i + j)
            elif h[0] < i + j:
                heappush(h, i + j)
                heappop(h)
    AB = h[:]
    AB.sort(reverse=True)
    h = []
    for i in AB:
        for j in C:
            if len(h) < K:
                heappush(h, i + j)
            elif h[0] < i + j:
                heappush(h,i + j)
                heappop(h)
    ABC = h[:]
    ABC.sort(reverse=True)
    for i in range(K):
        print(ABC[i])

main()

Submission status: Accepted

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

x, y, z, k = [int(x) for x in 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()]
a.sort()
b.sort()
c.sort()

x_y_list = []

for i in a:
    for j in b:
        x_y_list.append(i + j)

x_y_list.sort()

import bisect
left = 0
right = 10 ** 18

import numpy as np

x_y_list_np = np.array(x_y_list, np.int64)
c_np = np.array(c, np.int64)

while right - left > 1:
    mid = (left + right) // 2
    cnt = np.searchsorted(x_y_list_np, mid - c_np, side="right").sum()
    if cnt >= x*y*z - k + 1:
        right = mid
    else:
        left = mid

up_k = right # 上からk番目

ans = []
for i in c:
    idx = bisect.bisect_right(x_y_list, up_k - i)
    for j in range(idx, len(x_y_list)):
        ans.append(i + x_y_list[j])

for i in range(k - len(ans)):
    ans.append(up_k)

ans.sort()

for i in ans[::-1]:
    print(i)

Submission status: Accepted

Solution code:
xx, yy, zz, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
a.sort(reverse=1)
b.sort(reverse=1)
c.sort(reverse=1)
ans = []

for x in range(min(xx, k)):
    for y in range(min(yy, k // (x + 1))):
        for z in range(min(zz, k // ((x + 1) * (y + 1)))):
            ans.append(a[x] + b[y] + c[z])
ans.sort(reverse=1)

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

Submission status: Accepted

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])
            else:
                break

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

Submission status: Accepted

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: Accepted

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)

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: Accepted

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

def condition(num):
    count=0
    for i in range(0,Z):
        subcount=0
        a=num-C[i]
        s=0
        t=Y-1
        while X-1>=s and t>=0:
            if A[s]+B[t]>=a:
                t-=1
            else:
                subcount+=t+1
                s+=1
        count+=X*Y-subcount
    return count>=K

start=0
end=10**12
while end-start>1:
    test=(end+start)//2
    if condition(test):
        start=test
    else:
        end=test

Kth=0
if condition(end):
    Kth=end
else:
    Kth=start

ans=[]
for i in range(0,Z):
    a=Kth+1-C[i]
    s=0
    t=Y-1
    while X-1>=s and t>=0:
        if A[s]+B[t]>=a:
            for j in range(s,X):
                ans.append(A[j]+B[t]+C[i])
            t-=1
        else:
            s+=1

for i in range(0,K-len(ans)):
    ans.append(Kth)
ans.sort()
for i in range(0,K):
    print(ans[-i-1])

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

X, Y, Z, K = MAP()
A = LIST()
B = LIST()
C = LIST()
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

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

V.sort(reverse=True)

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

Submission status: Accepted

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)

ans = []
for x in range(X):
    for y in range(Y):
        for z in range(Z):
            if (x + 1) * (y + 1) * (z + 1) <= K:
                ans.append(A[x] + B[y] + C[z])
            else:
                break

ans.sort(reverse=True)
print(*ans[:K], sep='\n')

Submission status: Accepted

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

q = []
x = y = z = 0
heappush(q, (-(A[x] + B[y] + C[z]), (x, y, z)))
done = set((0, 0, 0))
ans = []
for _ in range(K):
    val, (x, y, z) = heappop(q)
    ans.append(-val)
    for i, j, k in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
        nx = x + i; ny = y + j; nz = z + k
        if nx < X and ny < Y and nz < Z and (nx, ny, nz) not in done:
            heappush(q, (-(A[nx] + B[ny] + C[nz]), (nx, ny, nz)))
            done.add((nx, ny, nz))

print(*ans, sep='\n')

Submission status: Accepted

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)

q=[(-1*(A[0]+B[0]+C[0]),0,0,0)]

heapq.heapify(q)

used=[[0,0,0]]

a=0
b=0
c=0
cnt=0
while cnt<k:
    p,a,b,c=heapq.heappop(q)
    cnt+=1
    print(p*-1)
    a+=1
    if (not [a,b,c] in used) and a<x:
        heapq.heappush(q,(-A[a]-B[b]-C[c],a,b,c))
        used.append([a,b,c])
    a-=1
    b+=1
    if (not [a,b,c] in used) and b<y:
        heapq.heappush(q,(-A[a]-B[b]-C[c],a,b,c))
        used.append([a,b,c])
    b-=1
    c+=1
    if (not [a,b,c] in used) and c<z:
        heapq.heappush(q,(-A[a]-B[b]-C[c],a,b,c))
        used.append([a,b,c])

Submission status: Accepted

Solution code:
from datetime import datetime as dt
import heapq
X,Y,Z,K = list(map(int,input().split(" ")))
As = list(map(int,input().split(" ")))
As.sort(reverse=True)
Bs = list(map(int,input().split(" ")))
Bs.sort(reverse=True)
Cs = list(map(int,input().split(" ")))
Cs.sort(reverse=True)

a = 0
b = 0
c = 0
se = set()
nums = []
num = As[a] + Bs[b] + Cs[c]
heapq.heappush(nums,(-num,(a ,b,c)))
# se.add((a ,b,c))
for _ in range(K):
    while True:
        temp = heapq.heappop(nums)
        num = temp[0]
        a,b,c = temp[1]
        if temp[1] in se:
            pass
        else:
            se.add(temp[1])
            break
    print(-num)
    #次のステップ
    if a + 1 < X:
        num = As[a + 1] + Bs[b] + Cs[c]
        heapq.heappush(nums,(-num,(a + 1,b,c)))
    if b + 1 < Y:
        num = As[a ] + Bs[b + 1] + Cs[c]
        heapq.heappush(nums,(-num,(a ,b + 1,c)))
    if c + 1 < Z:
        num = As[a] + Bs[b ] + Cs[c + 1]
        heapq.heappush(nums,(-num,(a ,b,c + 1)))

Submission status: Accepted

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]

AB = nlargest(k,[a+b for a in A for b in B])
ABC = nlargest(k,[ab+c for ab in AB for c in C])
print("\n".join(map(str,ABC)))