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:
def d_cake_123_binary_search():
    """editorialの解法4 O(K^2log(max(P))"""
    # 参考: https://atcoder.jp/contests/abc123/submissions/4871511
    import sys
    import bisect
    input = sys.stdin.readline
    X, Y, Z, K = [int(i) for i in input().split()]
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))
    C = tuple(map(int, input().split()))
    ab = sorted([e1 + e2 for e2 in B for e1 in A])  # A, Bの要素は全部調べる

    # rejected 以上の値は，美味しさの合計がそれ以上である個数が K 個未満である
    accepted, rejected = -1, 10**11
    while abs(accepted - rejected) > 1:
        mid = (accepted + rejected) // 2
        count = sum([len(ab) - bisect.bisect_left(ab, mid - e) for e in C])
        if count >= K:
            accepted = mid
        else:
            rejected = mid

    ans = []
    for e in C:
        idx = bisect.bisect_left(ab, accepted - e)
        ans.extend(e + _ab for _ab in ab[idx:])
    ans.sort(reverse=True)
    return '\n'.join(map(str, ans[:K]))

print(d_cake_123_binary_search())

Submission status: Accepted

Solution code:
def d_cake_123_sort_modified():
    """editorial の解法 1."""
    import sys
    import numpy as np
    input = sys.stdin.readline
    X, Y, Z, K = [int(i) for i in input().split()]
    A = np.array([int(i) for i in input().split()])
    B = np.array([int(i) for i in input().split()])
    C = np.array([int(i) for i in input().split()])

    _a, _b = np.meshgrid(A, B)
    ab = np.sort((_a + _b).flatten())[::-1][:K]
    _ab, _c = np.meshgrid(ab, C)
    ans = np.sort((_ab + _c).flatten())[::-1][:K]
    return '\n'.join(map(str, ans))

print(d_cake_123_sort_modified())

Submission status: Accepted

Solution code:
x, y, z, k = map(int, input().split())
alist = list(map(int, input().split()))
blist = list(map(int, input().split()))
clist = list(map(int, input().split()))

bclist=[]

for i in range(y):
    now=blist[i]
    for j in range(z):
        bclist.append(now+clist[j])

bclist=sorted(bclist)
bclist2=bclist[-k:]
#print(bclist2)

ng=len(bclist2)
abclist=[]

for i in range(ng):
    now=bclist2[i]
    for j in range(x):
        abclist.append(now+alist[j])

abclist=sorted(abclist)
for i in range(k):
    print(abclist[-1-i])

Submission status: Accepted

Solution code:
import sys
import heapq as hq
input = sys.stdin.readline
x,y,z,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ans = []
for p in range(1,x+1):
  for q in range(1,y+1):
    for r in range(min(k//(p*q)+1,z)):
      ans.append(-(a[p-1]+b[q-1]+c[r]))
hq.heapify(ans)
for i in range(k):
  s = hq.heappop(ans)
  print(-s)

Submission status: Accepted

Solution code:
import heapq as hq
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 p in range(1,x+1):
  for q in range(1,y+1):
    for r in range(min(k//(p*q)+1,z)):
      ans.append(-(a[p-1]+b[q-1]+c[r]))
hq.heapify(ans)
for i in range(k):
  s = hq.heappop(ans)
  print(-s)

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)
bc = [x + y for y in c for x in b]
bc.sort(reverse=True)
ans = [x + y for y in a for x in bc[:k]]
ans.sort(reverse=True)
print(*ans[:k], sep="\n")

Submission status: Accepted

Solution code:
X, Y, Z, K = map(int, input().split())
A = [int(aaa) for aaa in input().split()]
B = [int(bbb) for bbb in input().split()]
C = [int(ccc) for ccc in input().split()]
kari = []
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
for i in range(X):
    for j in range(Y):
        kari.append(A[i] + B[j])
karisize = len(kari)
kari = sorted(kari, reverse=True)
kouho_list = [0] * Z
index_list = [0] * Z
for k in range(Z):
    kouho_list[k] = kari[0] + C[k]
print(kouho_list[0])
if karisize == 1:
    kouho_list[0] = -10**9
else:
    index_list[0] += 1
    kouho_list[0] = kari[1] + C[0]
for p in range(K - 1):
    ans = max(kouho_list)
    max_index = kouho_list.index(ans)
    print(ans)
    if index_list[max_index] == karisize - 1:
        kouho_list[max_index] = -10 ** 9
    else:
        index_list[max_index] += 1
        kouho_list[max_index] = C[max_index] + kari[index_list[max_index]]
    # print(kouho_list)
# print(kari)

Submission status: Accepted

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)

result = []

for x in range(X):
  for y in range(Y):
    if x * y > K:
      break
    for z in range(Z):
      if x * y * z > K:
        break
      result.append(A[x] + B[y] + C[z])

for k in list(sorted(result, reverse=True))[:K]:
  print(k)

Submission status: Accepted

Solution code:
import sys
import heapq
def input(): return sys.stdin.readline().rstrip()

def main():
    X, Y, Z, K = map(int, input().split())
    A, B, C = [sorted(map(int, input().split()), reverse=True) for _ in range(3)]

    hp = []
    heapq.heappush(hp, (- A[0] - B[0] - C[0], 0, 0, 0))
    exists = set([(0, 0, 0)])
    for _ in range(K):
        v, x, y, z = heapq.heappop(hp)
        print(-v)
        if x + 1 < X and (x + 1, y, z) not in exists:
            heapq.heappush(hp, (v + A[x] - A[x+1], x + 1, y, z))
            exists.add((x + 1, y, z))
        if y + 1 < Y and (x, y + 1, z) not in exists:
            heapq.heappush(hp, (v + B[y] - B[y+1], x, y + 1, z))
            exists.add((x, y + 1, z))
        if z + 1 < Z and (x, y, z + 1) not in exists:
            heapq.heappush(hp, (v + C[z] - C[z+1], x, y, z + 1))
            exists.add((x, y, z + 1))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from itertools import product
def input(): return sys.stdin.readline().rstrip()

def main():
    X, Y, Z, K = map(int, input().split())
    A, B, C = [sorted(map(int, input().split())) for _ in range(3)]

    AB = [a + b for a, b in product(A, B)]
    AB = sorted(AB, reverse=True)[:K]

    ABC = [ab + c for ab, c in product(AB, C)]
    ABC = sorted(ABC, reverse=True)[:K]
    for i in ABC:
        print(i)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    X, Y, Z, K = map(int, input().split())
    A, B, C = [sorted(map(int, input().split())) for _ in range(3)]

    AB = [a + b for a in A for b in B]
    AB = sorted(AB, reverse=True)[:K]

    ABC = [ab + c for ab in AB for c in C]
    ABC = sorted(ABC, reverse=True)[:K]
    for i in ABC:
        print(i)

if __name__ == '__main__':
    main()

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()))
result = []

def func1():
    E = [a+b for a in A for b in B]
    E.sort()
    if len(E) > k: E = E[-k:]
    F = [c+e for c in C for e in E]
    F.sort()
    return F[-k:][::-1]

[print(val) for val in func1()]

Submission status: Accepted

Solution code:
# input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *

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)

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

_lst=[]
for a in lst:
    for c in C:
        _lst.append(a+c)

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

Submission status: Accepted

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=[]
for i in range(1,X+1):
    for j in range(1,Y+1):
        if i*j>3000:
            break
        for k in range(1,Z+1):
            if i*j*k<=K:
                D.append((i-1,j-1,k-1))
            else:
                break

E=[]
for i in D:
    E.append(A[i[0]]+B[i[1]]+C[i[2]])

E.sort(reverse=True)
E=E[:K]
for i in E:
    print(i)

Submission status: Accepted

Solution code:
# 想定解法3

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 =[]
heapq.heappush(Q, [-(A[0]+B[0]+C[0]), 0, 0, 0])
added_idx = set((0, 0, 0))

for i in range(K):
  nega_ans, x, y, z = heapq.heappop(Q)
  print(-nega_ans)
  if x+1 < X and (x+1, y, z) not in added_idx:
    heapq.heappush(Q, [-(A[x+1]+B[y]+C[z]), x+1, y, z])
    added_idx.add((x+1, y, z))
  if y+1 < Y and (x, y+1, z) not in added_idx:
    heapq.heappush(Q, [-(A[x]+B[y+1]+C[z]), x, y+1, z])
    added_idx.add((x, y+1, z))
  if z+1 < Z and (x, y, z+1) not in added_idx:
    heapq.heappush(Q, [-(A[x]+B[y]+C[z+1]), x, y, z+1])
    added_idx.add((x, y, z+1))

Submission status: Accepted

Solution code:
# 想定解法2

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)

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

Ans.sort(reverse=True)
Ans = Ans[:K]
for ans in Ans:
  print(ans)

Submission status: Accepted

Solution code:
# 想定解法1

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 a in A:
  for b in B:
    AB.append(a+b)

AB.sort(reverse=True)
AB = AB[:K]

ABC = []
for ab in AB:
  for c in C:
    ABC.append(ab+c)

ABC.sort(reverse=True)
ABC = ABC[:K]

for ans in ABC:
  print(ans)

Submission status: Accepted

Solution code:
import bisect

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)

YZ = Y*Z
BC = []
for b in B:
  for c in C:
    BC.append(b+c)

BC.sort()
def check(S):
  count = 0
  left = X*Y*Z
  for a in A:
    count += YZ - bisect.bisect_left(BC, S-a)
    left -= YZ
    if count >= K or count+left < K:
      break
  
  return count >= K



Sl = 0
Sr = A[0]+B[0]+C[0]+1

while Sr - Sl > 1:
  mid = (Sl + Sr) // 2
  if check(mid):
    Sl = mid
  else:
    Sr = mid

Ans = []
for a in A:
  for b in B:
    if a + b + C[0] < Sr:
      break
    for c in C:
      if a + b + c >= Sr:
        Ans.append(a+b+c)
      else:
        break

Ans.sort(reverse=True)
Ans = Ans + [Sl]*(K-len(Ans))
for ans in Ans:
  print(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)

import heapq
nums=[[-(A[0]+B[0]+C[0]), 0, 0, 0]]
heapq.heapify(nums)

sumi=set()
sumi.add((0, 0, 0))

for _ in range(K):
  out, a, b, c=heapq.heappop(nums)
  print(-out)
  if a<X-1 and not((a+1, b, c) in sumi):
    heapq.heappush(nums, [-(A[a+1]+B[b]+C[c]), a+1, b, c])
    sumi.add((a+1, b, c))
  if b<Y-1 and not((a, b+1, c) in sumi):
    heapq.heappush(nums, [-(A[a]+B[b+1]+C[c]), a, b+1, c])
    sumi.add((a, b+1, c))
  if c<Z-1 and not((a, b, c+1) in sumi):
    heapq.heappush(nums, [-(A[a]+B[b]+C[c+1]), a, b, c+1])
    sumi.add((a, b, c+1))

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)

import heapq
nums=[[-(A[0]+B[0]+C[0]), 0, 0, 0]]
heapq.heapify(nums)

sumi=[[0, 0, 0]]
for _ in range(K):
  out, a, b, c=heapq.heappop(nums)
  print(-out)
  if a<X-1 and not([a+1, b, c] in sumi):
    heapq.heappush(nums, [-(A[a+1]+B[b]+C[c]), a+1, b, c])
    sumi.append([a+1, b, c])
  if b<Y-1 and not([a, b+1, c] in sumi):
    heapq.heappush(nums, [-(A[a]+B[b+1]+C[c]), a, b+1, c])
    sumi.append([a, b+1, c])
  if c<Z-1 and not([a, b, c+1] in sumi):
    heapq.heappush(nums, [-(A[a]+B[b]+C[c+1]), a, b, c+1])
    sumi.append([a, b, c+1])

Submission status: Accepted

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

A = sorted(map(int, input().split()), reverse=True)
B = sorted(map(int, input().split()), reverse=True)
C = sorted(map(int, input().split()), reverse=True)

ABC = []
for a in range(X):
    for b in range(Y):
        if (a + 1) * (b + 1) > K:
            break
        for c in range(Z):
            if (a + 1) * (b + 1) * (c + 1) > K:
                break
            ABC.append(A[a] + B[b] + C[c])

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

Submission status: Accepted

Solution code:
# coding: utf-8
#解法2 ヒープを使う
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)

used=[]

heap=[]

heapq.heappush(heap,(-(A[0]+B[0]+C[0]),0,0,0))
used.append((0,0,0))

for i in range(K):
    u=heapq.heappop(heap)
    if u[1]+1<=X-1:
        if not((u[1]+1,u[2],u[3]) in used):
            used.append((u[1]+1,u[2],u[3]))
            heapq.heappush(heap,(-(A[u[1]+1]+B[u[2]]+C[u[3]]),u[1]+1,u[2],u[3]))
    
    if u[2]+1<=Y-1:
        if not((u[1],u[2]+1,u[3]) in used):
            used.append((u[1],u[2]+1,u[3]))
            heapq.heappush(heap,(-(A[u[1]]+B[u[2]+1]+C[u[3]]),u[1],u[2]+1,u[3]))
    
    if u[3]+1<=Z-1:
        if not((u[1],u[2],u[3]+1) in used):
            used.append((u[1],u[2],u[3]+1))
            heapq.heappush(heap,(-(A[u[1]]+B[u[2]]+C[u[3]+1]),u[1],u[2],u[3]+1))
    print(-u[0])

Submission status: Accepted

Solution code:
# coding: utf-8
#解法2 abc<=Kのみのリスト
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)

ABC=[]

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

ABC.sort(reverse=True)

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

Submission status: Accepted

Solution code:
# coding: utf-8
#解法1 ABのK番まで　＋　CのK番まで
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)

ABC=[]

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

ABC.sort(reverse=True)

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

Submission status: Accepted

Solution code:
import heapq
import copy
from collections import defaultdict

X=[0]*3
X[0],X[1],X[2],K=map(int,input().split())
m=3

A=[[] for _ in range(m)]
for i in range(m):
    A[i]=list(map(int,input().split()))
    A[i].sort(reverse=True)
    A[i].append(-2*(10**10))



def calc_val(index):
    res=0
    for j in range(m):
        res-=A[j][index[j]]
    return res


def hash(index):
    a=[1,1001,1001**2]
    res=0
    for j in range(m):
        res+=a[j]*index[j]
    return res
    
    

q=[]
index=[0]*m
heapq.heappush(q,(calc_val(index),index))

used = defaultdict(int)

for i in range(K):
    res,index=heapq.heappop(q)
    print(-1*res)
    for j in range(m):
        new_index=copy.deepcopy(index)
        new_index[j]+=1
        if used[hash(new_index)]==0:
            heapq.heappush(q,(calc_val(new_index),new_index))
            used[hash(new_index)]=1

Submission status: Accepted

Solution code:
import sys

x, y, z, K = map(int, sys.stdin.readline().split())
a, b, c = (sorted(map(int, sys.stdin.readline().split()), reverse=True) for _ in range(3))
def main():
    res = []
    for i in range(1, min(K, x) + 1):
        for j in range(1, min(K // i, y) + 1):
            for k in range(1, min(K // (i * j), z) + 1):
                res.append(a[i-1] + b[j-1] + c[k-1])

    return sorted(res, reverse=True)[:K]

if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Submission status: Accepted

Solution code:
import sys
import numpy as np

x, y, z, k = map(int, sys.stdin.readline().split())
a, b, c = (np.array(sys.stdin.readline().split(), dtype=np.int64) for _ in range(3))

def main():
    res = np.sort(np.ravel(a[:, None] + b))[::-1]
    res = np.sort(np.ravel(c[:, None] + res[:min(k, x*y)]))[::-1]
    return res[:k]

if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Submission status: Accepted

Solution code:
import sys
from heapq import heappush, heappop

x, y, z, k = map(int ,sys.stdin.readline().split())
a, b, c = (sorted(map(int, sys.stdin.readline().split())) for _ in range(3))

def main():
    res = []
    for i in range(x):
        for j in range(y):
            heappush(res, -(a[i] + b[j]))
    
    res2 = []
    for i in range(min(k, x * y)):
        ab = -heappop(res)
        for j in range(z):
            heappush(res2, -(ab + c[j]))
    
    for i in range(k):
        yield -heappop(res2)

if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

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 = sorted(list(map(int,input().split())),reverse=True)
D = []
for i in range(X):
    for j in range(Y):
        D.append(A[i]+B[j])
D = sorted(D,reverse=True)
E = []
for i in range(min(K,X*Y)):
    for j in range(Z):
        E.append(D[i]+C[j])
E = sorted(E,reverse=True)
for i in range(K):
    print(E[i])

Submission status: Accepted

Solution code:
import sys
from heapq import heappush, heappop

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

def main():
    cand = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
    added = set([(0, 0, 0)])
    for _ in range(K):
        s, i, j, k = heappop(cand)
        yield -s
        if i + 1 < x and not (i+1, j, k) in added:
            heappush(cand, (s + a[i] - a[i+1], i+1, j, k))
            added.add((i+1, j, k))
        if j + 1 < y and not (i, j+1, k) in added:
            heappush(cand, (s + b[j] - b[j+1], i, j+1, k))
            added.add((i, j+1, k))
        if k + 1 < z and not (i, j, k+1) in added:
            heappush(cand, (s + c[k] - c[k+1], i, j, k+1))
            added.add((i, j, k+1))

if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Submission status: Accepted

Solution code:
#AC済み
x,y,z,k=map(int,input().split())
a=sorted([int(i) for i in input().split()])
b=sorted([int(i) for i in input().split()])
c=sorted([int(i) for i in input().split()])
ab=sorted([a[i]+b[j] for j in range(y) for i in range(x)],reverse=True)
ab=ab[:k]
l=len(ab)
abc=sorted([ab[i]+c[j] for j in range(z) for i in range(l)],reverse=True)
for i in range(k):
    print(abc[i])

Submission status: Accepted

Solution code:
#AC済み
x,y,z,k=map(int,input().split())
a=sorted([int(i) for i in input().split()])
b=sorted([int(i) for i in input().split()])
c=sorted([int(i) for i in input().split()])
ab=[a[i]+b[j] for j in range(y) for i in range(x)]
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[ab[i]+c[j] for j in range(z) for i in range(l)]
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])

Submission status: Accepted

Solution code:
#AC済み
x,y,z,k=map(int,input().split())
a=sorted([int(i) for i in input().split()])
b=sorted([int(i) for i in input().split()])
c=sorted([int(i) for i in input().split()])
a.sort()
b.sort()
c.sort()
ab=[a[i]+b[j] for j in range(y) for i in range(x)]
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[ab[i]+c[j] for j in range(z) for i in range(l)]
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])

Submission status: Accepted

Solution code:
#AC済み
x,y,z,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
c=[int(i) for i in input().split()]
a.sort()
b.sort()
c.sort()
ab=[a[i]+b[j] for j in range(y) for i in range(x)]
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[ab[i]+c[j] for j in range(z) for i in range(l)]
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])

Submission status: Accepted

Solution code:
#AC済み
import heapq
x,y,z,k=map(int,input().split())
#heapqは小さい順にしか並べられないので、-をつければ大きい順になる
a=sorted([-int(i) for i in input().split()])
b=sorted([-int(i) for i in input().split()])
c=sorted([-int(i) for i in input().split()])
check=set()
ans=[]
heapq.heappush(ans,(a[0]+b[0]+c[0],0,0,0))
check.add((0,0,0))
for i in range(k):
    h=heapq.heappop(ans)
    print(-h[0])
    if h[1]+1<x:
        if (h[1]+1,h[2],h[3]) not in check:
            heapq.heappush(ans,(a[h[1]+1]+b[h[2]]+c[h[3]],h[1]+1,h[2],h[3]))
            check.add((h[1]+1,h[2],h[3]))
    if h[2]+1<y:
        if (h[1],h[2]+1,h[3]) not in check:
            heapq.heappush(ans,(a[h[1]]+b[h[2]+1]+c[h[3]],h[1],h[2]+1,h[3]))
            check.add((h[1],h[2]+1,h[3]))
    if h[3]+1<z:
        if (h[1],h[2],h[3]+1) not in check:
            heapq.heappush(ans,(a[h[1]]+b[h[2]]+c[h[3]+1],h[1],h[2],h[3]+1))
            check.add((h[1],h[2],h[3]+1))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

abc = [ab[i] + c[j] for j in range(z) for i in range(len(ab))]

abc.sort(reverse=True)

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
x,y,z,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
c=[int(i) for i in input().split()]
a.sort()
b.sort()
c.sort()
ab=[a[i]+b[j] for j in range(y) for i in range(x)]
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[ab[i]+c[j] for j in range(z) for i in range(l)]
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])

Submission status: Accepted

Solution code:
x,y,z,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
c=[int(i) for i in input().split()]
a.sort()
b.sort()
c.sort()
ab=[a[i]+b[j] for j in range(y) for i in range(x)]
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[ab[i]+c[j] for j in range(z) for i in range(l)]
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])

Submission status: Accepted

Solution code:
import heapq
x,y,z,k=map(int,input().split())
#heapqは小さい順にしか並べられないので、-をつければ大きい順になる
a=sorted([-int(i) for i in input().split()])
b=sorted([-int(i) for i in input().split()])
c=sorted([-int(i) for i in input().split()])
check=set()
ans=[]
heapq.heappush(ans,(a[0]+b[0]+c[0],0,0,0))
check.add((0,0,0))
for i in range(k):
    h=heapq.heappop(ans)
    print(-h[0])
    if h[1]+1<x:
        if (h[1]+1,h[2],h[3]) not in check:
            heapq.heappush(ans,(a[h[1]+1]+b[h[2]]+c[h[3]],h[1]+1,h[2],h[3]))
            check.add((h[1]+1,h[2],h[3]))
    if h[2]+1<y:
        if (h[1],h[2]+1,h[3]) not in check:
            heapq.heappush(ans,(a[h[1]]+b[h[2]+1]+c[h[3]],h[1],h[2]+1,h[3]))
            check.add((h[1],h[2]+1,h[3]))
    if h[3]+1<z:
        if (h[1],h[2],h[3]+1) not in check:
            heapq.heappush(ans,(a[h[1]]+b[h[2]]+c[h[3]+1],h[1],h[2],h[3]+1))
            check.add((h[1],h[2],h[3]+1))
'''
while len(ans)!=0:
    print(heapq.heappop(ans)[0])
'''

Submission status: Accepted

Solution code:
import sys
from heapq import heappush,heappop
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

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()))
    A.sort(reverse=True); B.sort(reverse=True); C.sort(reverse=True)
    h=[]
    s=set()
    heappush(h,(-(A[0]+B[0]+C[0]),0,0,0))
    s.add((0,0,0))
    for _ in range(K):
        c=heappop(h)
        print(-c[0])
        for k in range(3):
            if (c[1]+(k==0)<X and c[2]+(k==1)<Y and c[3]+(k==2)<Z) and not (c[1]+(k==0),c[2]+(k==1),c[3]+(k==2)) in s:
                heappush(h,(-(A[c[1]+(k==0)]+B[c[2]+(k==1)]+C[c[3]+(k==2)]),c[1]+(k==0),c[2]+(k==1),c[3]+(k==2)))
                s.add((c[1]+(k==0),c[2]+(k==1),c[3]+(k==2)))
                

if __name__ == '__main__':
    main()

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)

lst = []

for a in A:
	for b in B:
		lst.append(a + b)
		
lst.sort(reverse = True)

lst = lst[0 : K] 
# もしCのc番目の値しか使わないケースを考えたとき、大小関係は保たれるから、
# K番目以降の値は使うことがない

ans = []

for c in C:
	for i in lst:
		ans.append(c + i)
		
ans.sort(reverse = True)

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

Submission status: Accepted

Solution code:
n1, n2, n3, kth = map(int, input().split())
a1 = sorted(map(int, input().split()), reverse=True)
a2 = sorted(map(int, input().split()), reverse=True)
a3 = sorted(map(int, input().split()), reverse=True)

ret = []
for i in range(n1):
    for j in range(n2):
        for k in range(n3):
            if (i + 1) * (j + 1) * (k + 1) > kth:
                break
            else:
                ret.append(a1[i] + a2[j] + a3[k])
ret.sort(reverse=True)
print('\n'.join(map(str, ret[:kth])))