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 numpy as np
import pprint

#入力を受け付ける
X,Y,Z,K = list(map(int, input().split()))
A_list =[int(i) for i in input().split()]
B_list =[int(i) for i in input().split()]
C_list =[int(i) for i in input().split()]

#並び替え
A_list.sort(reverse=True)
B_list.sort(reverse=True)
C_list.sort(reverse=True)

#処理準備
calclated_flag = np.zeros((X,Y,Z), dtype=np.bool) #計算済みか
#calclated_result = np.zeros((X,Y,Z), dtype=np.int64) #計算結果 　…stockを使うので不要
ans = [] #解答

#木の頭
x=0
y=0
z=0
stock=[[x,y,z,A_list[x] + B_list[y] + C_list[z]]]
calclated_flag[x,y,z]=True

#ここから解答が条件の手前になるまでループ
for i in range(K):
  #stockの中から最大を調べる
  stock.sort(key=lambda x: x[3]) #ソート
  ans.append(stock[len(stock)-1][3]) #最小は一番最後。ansに追加

  temp = stock.pop() #stockから削除。
  x=temp[0]
  y=temp[1]
  z=temp[2]

  #子に分岐 ここで範囲外にならないよう判断が必要
  if x+1 <= X-1:
    if calclated_flag[x+1,y,z]!=True:
      stock.append([x+1,y,z,A_list[x+1] + B_list[y] + C_list[z]])
      calclated_flag[x+1,y,z]=True
      
  if y+1 <= Y-1:
    if calclated_flag[x,y+1,z]!=True: 
      stock.append([x,y+1,z,A_list[x] + B_list[y+1] + C_list[z]])
      calclated_flag[x,y+1,z]=True
      
  if z+1 <= Z-1:
    if calclated_flag[x,y,z+1]!=True:
      stock.append([x,y,z+1,A_list[x] + B_list[y] + C_list[z+1]])
      calclated_flag[x,y,z+1]=True
  
#回答を出力
for i in range(K):
  print(ans[i])

Submission status: Accepted

Solution code:
x, y, z, k = list(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)
ab = sorted([e+f for e in a[:k] for f in b[:k]], reverse=True)
abc = sorted([g+h for g in ab[:k] for h in c[:k]], reverse=True)
for i, ele in enumerate(abc):
  if i>=k:
    break
  print(ele)

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

Submission status: Accepted

Solution code:
X, Y, Z, K = map(int, input().split())
xyz = []
xyz.append(list(map(int, input().split())))
xyz.append(list(map(int, input().split())))
xyz.append(list(map(int, input().split())))
for v in xyz:
    v.sort()
    v.reverse()

ans = []
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
            ans.append(xyz[0][x]+xyz[1][y]+xyz[2][z])

ans.sort()
ans.reverse()
print("\n".join([str(v) for v in ans[:K]]))

Submission status: Accepted

Solution code:
x, y, z, k = list(map(int, input().split()))
A, B, C = [list(map(int, input().split())) for i in range(3)]

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

li = []

for p in range(len(A)):
    for q in range(len(B)):
        value = min(- int(- k / ((p + 1) * (q + 1))), len(C))
        for r in range(value):
            tasty = A[p] + B[q] + C[r]
            li.append(tasty)

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


# Priority QueueとしてHeapを使う
# Pythonのheapqは小さい順に取り出すので、-1を掛けて負数としてpushしていく
#   (-Ai-Bj-Ck, i, j, k)
# 取り出したi, j, kについて、(i+1, j, k), (i, j+1, k), (i, j, k+1)に対応するタプルをpushする
# ただし、すでにpushした値は入れないようにする

import heapq

def make(i, j, k):
    return (-A[i]-B[j]-C[k], i, j, k)


used = set()
l = []

heapq.heappush(l, make(0, 0, 0))
used.add((0, 0, 0))

for cnt in range(K):
    V, i, j, k = heapq.heappop(l)
    
    if i < X - 1 and (i+1, j, k) not in used:
        heapq.heappush(l, make(i+1, j, k))
        used.add((i+1, j, k))
    
    if j < Y - 1 and (i, j+1, k) not in used:
        heapq.heappush(l, make(i, j+1, k))
        used.add((i, j+1, k))
    
    if k < Z - 1 and (i, j, k+1) not in used:
        heapq.heappush(l, make(i, j, k+1))
        used.add((i, j, k+1))
    
    print(-V)

Submission status: Accepted

Solution code:
X, Y, Z, K = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]
C = [int(_) for _ in input().split()]

A = sorted(A)
B = sorted(B)
C = sorted(C)

maxn = A[0] * B[0] * C[0]

from heapq import *

h = []


heappush(h, (-A[-1] - B[-1] - C[-1], len(A) - 1, len(B) - 1, len(C) - 1))

used = {}

for i in range(min(K, X * Y * Z)):
    v, ix, iy, iz = heappop(h)
    print(-v)
    def push(x, y, z):
        if x >= 0 and y >= 0 and z >= 0 and (x, y, z) not in used:
            heappush(h, (-A[x] - B[y] - C[z], x, y, z))
            used[x, y, z] = 1
    push(ix - 1, iy, iz)
    push(ix, iy - 1, iz)
    push(ix, iy, iz - 1)

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)

r = []
for i in range(x):
  for j in range(y):
    for h in range(z):
      if (i+1)*(j+1)*(h+1) > k:
                break
      r.append(a[i]+b[j]+c[h])
list.sort(r,reverse=True)
for i in range(k):
  print(r[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(' ')))

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

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

for a in sorted(sums, reverse=True)[:K]:
    print(a)

Submission status: Accepted

Solution code:
# input
x, y, z, 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=True)
b.sort(reverse=True)
c.sort(reverse=True)

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

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

print('\n'.join(str(i) for i in abc[:k]))

Submission status: Accepted

Solution code:
# input
x, y, z, 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=True)
b.sort(reverse=True)
c.sort(reverse=True)

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

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

print('\n'.join(str(i) for i in abc[:k]))

Submission status: Accepted

Solution code:
import heapq
X, Y, Z, K = map(int, input().split())
A_array = list(map(int, input().split()))
B_array = list(map(int, input().split()))
C_array = list(map(int, input().split()))

A_array.sort()
B_array.sort()
C_array.sort()

use_array = []
sum_array = []

candidate = []
heapq.heapify(candidate)

max_sum = A_array[-1] + B_array[-1] + C_array[-1]

heapq.heappush(candidate, (0, X - 1, Y - 1, Z - 1))
use_array.append((X - 1, Y - 1, Z - 1))

for i in range(K):
    s, x, y, z = heapq.heappop(candidate)

    sum_array.append(s)
    if x != 0 and (x - 1, y, z) not in use_array:
        use_array.append((x - 1, y, z))
        diff = A_array[x] - A_array[x - 1]
        heapq.heappush(candidate, (s + diff, x - 1, y, z))

    if y != 0 and (x, y - 1, z) not in use_array:
        use_array.append((x, y - 1, z))
        diff = B_array[y] - B_array[y - 1]
        heapq.heappush(candidate, (s + diff, x, y - 1, z))

    if z != 0 and (x, y, z - 1) not in use_array:
        use_array.append((x, y, z - 1))
        diff = C_array[z] - C_array[z - 1]
        heapq.heappush(candidate, (s + diff, x, y, z - 1))

for ans in sum_array:
    print(max_sum - ans)

Submission status: Accepted

Solution code:

import heapq
X,Y,Z,K = map(int,input().split())
As = sorted(list(map(int,input().split())),reverse=True)
Bs = sorted(list(map(int,input().split())),reverse=True)
Cs = sorted(list(map(int,input().split())),reverse=True)
q = []
heapq.heapify(q)
heapq.heappush(q,(-(As[0]+Bs[0]+Cs[0]),0,0,0))
list_ijk = [(0,0,0)]
for i in range(K):
    # valueのマイナス値の小さい順にpop(優先度付きqueue)
    v,i,j,k = heapq.heappop(q)
    if i < len(As)-1 and (i+1,j,k) not in list_ijk:
        heapq.heappush(q,(-(As[i+1]+Bs[j]+Cs[k]),i+1,j,k))
        list_ijk.append((i+1,j,k))
    if j < len(Bs)-1 and (i,j+1,k) not in list_ijk:
        heapq.heappush(q,(-(As[i]+Bs[j+1]+Cs[k]),i,j+1,k))
        list_ijk.append((i,j+1,k))
    if k < len(Cs)-1 and (i,j,k+1) not in list_ijk:
        heapq.heappush(q,(-(As[i]+Bs[j]+Cs[k+1]),i,j,k+1))
        list_ijk.append((i,j,k+1))
    print(-v)

Submission status: Accepted

Solution code:
import heapq
X,Y,Z,K = map(int,input().split())
As = sorted(list(map(int,input().split())),reverse=True)
Bs = sorted(list(map(int,input().split())),reverse=True)
Cs = sorted(list(map(int,input().split())),reverse=True)
q = []
heapq.heapify(q)
heapq.heappush(q,(-(As[0]+Bs[0]+Cs[0]),0,0,0))
list_ijk = [(0,0,0)]
for i in range(K):
    # valueのマイナス値の小さい順にpop(優先度付きqueue)
    v,i,j,k = heapq.heappop(q)
    if i < len(As)-1 and (i+1,j,k) not in list_ijk:
        heapq.heappush(q,(-(As[i+1]+Bs[j]+Cs[k]),i+1,j,k))
        list_ijk.append((i+1,j,k))
    if j < len(Bs)-1 and (i,j+1,k) not in list_ijk:
        heapq.heappush(q,(-(As[i]+Bs[j+1]+Cs[k]),i,j+1,k))
        list_ijk.append((i,j+1,k))
    if k < len(Cs)-1 and (i,j,k+1) not in list_ijk:
        heapq.heappush(q,(-(As[i]+Bs[j]+Cs[k+1]),i,j,k+1))
        list_ijk.append((i,j,k+1))
    print(-v)

Submission status: Accepted

Solution code:
# どうやって自明な条件で計算量を落としていくか
# 不等式に注目。pythonが耐えるのは10^6程度
X,Y,Z,K = map(int,input().split())
As = sorted(list(map(int,input().split())),reverse=True)
Bs = sorted(list(map(int,input().split())),reverse=True)
Cs = sorted(list(map(int,input().split())),reverse=True)

# ABでソート
AB = []
for A in As:
    for B in Bs:
        AB.append(A+B)
ABs = sorted(AB,reverse=True)

# その中からK番目まで
ABC = []
for i in range(min(len(ABs),K)):
    for C in Cs:
        ABC.append(ABs[i]+C)

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

Submission status: Accepted

Solution code:
K = int(input().split()[3])
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(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(reverse=True)
for _ in range(K):
    print(ans[_])

Submission status: Accepted

Solution code:
import heapq
X,Y,Z,K=[int(i) for i in input().split()]
A=sorted([int(i) for i in input().split()],reverse=True)
B=sorted([int(i) for i in input().split()],reverse=True)
C=sorted([int(i) for i in input().split()],reverse=True)
#print(A)
#print(B)
#print(C)
Q=[(-A[0]-B[0]-C[0],0,0,0)]
heapq.heapify(Q)
ans=[]
S=set([(0,0,0)])
for _ in range(K):
  q=heapq.heappop(Q)
  ans+=[-q[0]]
  a=q[1]
  b=q[2]
  c=q[3]
  if a!=X-1 and not ((a+1,b,c) in S):
    heapq.heappush(Q,(-A[a+1]-B[b]-C[c],a+1,b,c))
    S.add((a+1,b,c))
  if b!=Y-1 and not ((a,b+1,c) in S):
    heapq.heappush(Q,(-A[a]-B[b+1]-C[c],a,b+1,c))
    S.add((a,b+1,c))
  if c!=Z-1 and not ((a,b,c+1) in S):
    heapq.heappush(Q,(-A[a]-B[b]-C[c+1],a,b,c+1))
    S.add((a,b,c+1))
for i in ans:
  print(i)

Submission status: Accepted

Solution code:
x, y, z, o = [int(_) for _ in input().split()]
a = sorted([int(_) for _ in input().split()])[::-1]
b = sorted([int(_) for _ in input().split()])[::-1]
c = sorted([int(_) for _ in input().split()])[::-1]

def isEnough(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 >= o: return True
    return False

ok, ng = 0, a[0]+b[0]+c[0]
while abs(ok-ng)>1:
    m = (ok+ng)//2
    if isEnough(m):
        ok = m
    else:
        ng = m

l = []
for i in range(x):
    for j in range(y):
        for k in range(z):
            if a[i]+b[j]+c[k] <= ok : break
            l.append(a[i]+b[j]+c[k])
l.sort(reverse=True)
for i in range(o):
    if len(l) > i:
        print(l[i])
    else:
        print(ok)

Submission status: Accepted

Solution code:
#解答を見た後


x,y,z,N =(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()]

sa=sorted(a,reverse=True)
sb=sorted(b,reverse=True)
sc=sorted(c,reverse=True)
answer=[]

for i in range(len(sa)):
  for j in range(len(sb)):
    if (i+1)*(j+1)>N:break
    for k in range(len(sb)):
      if (i+1)*(j+1)*(k+1)>N:break 
      answer.append(sa[i]+sb[j]+sc[k])
answer=sorted(answer,reverse=True)
for i in range(0,N):
  print(answer[i])

Submission status: Accepted

Solution code:
import heapq

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

queue = [(A[0]+B[0]+C[0], 0, 0, 0)]
flg = {}
for i in range(K):
    cake, a, b, c = heapq.heappop(queue)
    print(-cake)
    for j in range(3):
        aa = a + [1, 0, 0][j]
        bb = b + [0, 1, 0][j]
        cc = c + [0, 0, 1][j]
        if aa < X and bb < Y and cc < Z:
            if flg.get((aa, bb, cc)) is None:
                flg[aa, bb, cc] = 1
                heapq.heappush(queue, (A[aa]+B[bb]+C[cc], aa, bb, cc))

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

queue = [(A[0]+B[0]+C[0], 0, 0, 0)]
ans = [(A[0]+B[0]+C[0], 0, 0, 0)]

for i in range(K):
    cake = max(queue, key=lambda x: x[0])
    queue.remove(cake)
    a, b, c = cake[1], cake[2], cake[3]
    print(cake[0])
    for j in range(3):
        aa = a + [1, 0, 0][j]
        bb = b + [0, 1, 0][j]
        cc = c + [0, 0, 1][j]
        if aa < X and bb < Y and cc < Z:
            tmp = A[aa]+B[bb]+C[cc]
            if (tmp, aa, bb, cc) not in ans:
                ans.append((tmp, aa, bb, cc))
                queue.append((tmp, aa, bb, cc))

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

queue = [(A[0]+B[0]+C[0], 0, 0, 0)]
ans = [(A[0]+B[0]+C[0], 0, 0, 0)]

for i in range(K):
    cake = max(queue, key=lambda x: x[0])
    queue.remove(cake)
    a, b, c = cake[1], cake[2], cake[3]
    for j in range(3):
        aa = a + [1, 0, 0][j]
        bb = b + [0, 1, 0][j]
        cc = c + [0, 0, 1][j]
        if aa < X and bb < Y and cc < Z:
            tmp = A[aa]+B[bb]+C[cc]
            if (tmp, aa, bb, cc) not in ans:
                ans.append((tmp, aa, bb, cc))
                queue.append((tmp, aa, bb, cc))

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

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)

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

L = sorted(L,reverse=True)[:K]
for l in L:
    print(l)

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)
heap = []
already = [[[] for _ in range(Y)] for _ in range(X)]
maxv = A[0]+B[0]+C[0]
heapq.heapify(heap)
heapq.heappush(heap, (0, 0, 0, 0))


def ans(v):
    return maxv - v


for i in range(K):
    res, x, y, z = heapq.heappop(heap)
    print(ans(res))
    if x + 1 < X and z not in already[x+1][y]:
        heapq.heappush(heap, (ans(A[x+1]+B[y]+C[z]), x+1, y, z))
        already[x+1][y].append(z)
    if y + 1 < Y and z not in already[x][y+1]:
        heapq.heappush(heap, (ans(A[x]+B[y+1]+C[z]), x, y+1, z))
        already[x][y+1].append(z)
    if z + 1 < Z and z+1 not in already[x][y]:
        heapq.heappush(heap, (ans(A[x]+B[y]+C[z+1]), x, y, z+1))
        already[x][y].append(z+1)

Submission status: Accepted

Solution code:
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    X, Y, Z, 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()] 
  else:
    A = [1,10,100]
    B = [2,20,200]
    C = [1,10,100]
    #A = [4,6]
    #B = [1,5]
    #C = [3,8]
    X = len(A)
    Y = len(B)
    Z = len(C)
    K = 5
    #K = 8

  # Processing
  AB = []
  A.sort(reverse=True)
  B.sort(reverse=True)
  for i in A[0:K]:
    for j in B[0:K]:
      AB.append(i+j)
  
  AB.sort(reverse=True)
  
  ABC = []
  C.sort(reverse=True)
  
  for i in AB[0:K]:
    for j in C[0:K]:
      ABC.append(i+j)  
  
  ABC.sort(reverse=True)

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

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

anslist = []

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

anslist.sort()
anslist.reverse()
for hoge in range(K):
	print (anslist[hoge])

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)
# print(a,b,c,x,y,z,k)
ans = []
for xi in range(x): # search a at first
    if xi+1 > k: # k より大きい数は探索不要
        break
    for yi in range(y):
        if (xi+1)*(yi+1) > k:
            break
        for zi in range(z):
            if (xi+1)*(yi+1)*(zi+1) > k:
                break
            ans.append(a[xi]+b[yi]+c[zi])
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Accepted

Solution code:
X,Y,Z,K = list(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 j in range(X):
    if j+1 > K:
        break
    for k in range(Y):
        if j*k+1 > K:
            break
        for l in range(Z):
            if j*k*l+1 <= K:
                ans.append(A[j]+B[k]+C[l])
            else: 
                break
                
ans.sort(reverse=True)
for i in range(K):
    print(ans[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)

def judge(p):
    cnt = 0
    for i in range(x):
        for j in range(y):
            for kk in range(z):
                if a[i]+b[j]+c[kk] < p : break
                cnt += 1
                if (cnt >= k):return True
    return False

ll,rr = 0,a[0]+b[0]+c[0]
while(rr- ll > 1):
    md = (ll+rr) // 2
    if(judge(md)) : ll = md
    else : rr = md

ans = []
cnt=0
for i in range(x):
    if(cnt >= k):break
    for j in range(y):
        if(cnt >= k):break
        for kk in range(z):
            if(cnt >= k):break
            if a[i]+b[j]+c[kk] < rr : break
            cnt += 1
            ans.append(a[i]+b[j]+c[kk])

if len(ans) <k:
    for i in range(k-len(ans)):
        ans.append(ll)

ans.sort(reverse=True)

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
import sys

import numpy as np

sys.setrecursionlimit(10000)
INF = float('inf')

X, Y, Z, K = list(map(int, input().split()))
A = np.array(list(map(int, input().split())))
B = np.array(list(map(int, input().split())))
C = np.array(list(map(int, input().split())))

AB = np.add.outer(A, B).flatten()
AB.sort()
C.sort()
ABC = np.add.outer(AB[-K:], C).flatten()
ABC.sort()

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

Submission status: Accepted

Solution code:
import heapq
import sys

sys.setrecursionlimit(10000)
INF = float('inf')

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

heap = []
heapq.heappush(heap, (-(A[0] + B[0] + C[0]), 0, 0, 0))
history = {(0, 0, 0)}
for _ in range(K):
    d, a, b, c = heapq.heappop(heap)
    if a + 1 < X and (a + 1, b, c) not in history:
        history.add((a + 1, b, c))
        heapq.heappush(heap, (-(A[a + 1] + B[b] + C[c]), a + 1, b, c))
    if b + 1 < Y and (a, b + 1, c) not in history:
        history.add((a, b + 1, c))
        heapq.heappush(heap, (-(A[a] + B[b + 1] + C[c]), a, b + 1, c))
    if c + 1 < Z and (a, b, c + 1) not in history:
        history.add((a, b, c + 1))
        heapq.heappush(heap, (-(A[a] + B[b] + C[c + 1]), a, b, c + 1))
    print(-d)

Submission status: Accepted

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

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

q = []
heapq.heappush(q, (-(a[0] + b[0] + c[0]), 0, 0, 0))
kk = k
ans = []
chk = defaultdict(lambda:defaultdict(lambda:defaultdict(bool)))

while(k):
    k -= 1
    v, ai, bi, ci = heapq.heappop(q)
    ans.append(v)
    if ai + 1 < x:
        if chk[ai + 1][bi][ci] == False:
            heapq.heappush(q, (-(a[ai + 1] + b[bi] + c[ci]), ai + 1, bi, ci))
            chk[ai + 1][bi][ci] = True
    if bi + 1 < y:
        if chk[ai][bi + 1][ci] == False:
            heapq.heappush(q, (-(a[ai] + b[bi + 1] + c[ci]), ai, bi + 1, ci))
            chk[ai][bi + 1][ci] = True
    if ci + 1 < z:
        if chk[ai][bi][ci + 1] == False:
            heapq.heappush(q, (-(a[ai] + b[bi] + c[ci + 1]), ai, bi, ci + 1))
            chk[ai][bi][ci + 1] = True
for i in range(kk):
    print(-ans[i])

Submission status: Accepted

Solution code:


x,y,z,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=True)
b.sort(reverse=True)
c.sort(reverse=True)

ans=[]
for i in range(x):
    if i > k:
        break
    for j in range(y):
        if i*j > k:
            break
        for l in range(z):
            if i*j*l > k:
                break
            ans.append(a[i]+b[j]+c[l])

ans.sort(reverse=True)

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

Submission status: Accepted

Solution code:
x, y, z, o = [int(_) for _ in input().split()]
a = sorted([int(_) for _ in input().split()])[::-1]
b = sorted([int(_) for _ in input().split()])[::-1]
c = sorted([int(_) for _ in input().split()])[::-1]
s = []
for i in range(x):
    for j in range(y):
        for k in range(z):
            if (i+1)*(j+1)*(k+1) <= o:
                s.append(a[i]+b[j]+c[k])
            else:
                break
for ans in sorted(s)[::-1][:o]:
    print(ans)

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 123
# https://atcoder.jp/contests/abc123
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    tk = lambda i, j, k: '%d %d %d' % (i, j, k)
    X, Y, Z, K = i2nn()
    A = i2nn()
    B = i2nn()
    C = i2nn()
    A.sort()
    A.reverse()
    B.sort()
    B.reverse()
    C.sort()
    C.reverse()
    dic = {}
    fin = set()
    def put(i, j, k):
        if i >= X or j >= Y or k >= Z:
            return
        key = tk(i, j, k)
        if key in fin:
            return
        if key not in dic:
            dic[key] = (A[i] + B[j] + C[k], key, i, j, k)
    put(0, 0, 0)
    for _ in range(K):
        v = max(dic.values(), key=lambda v: v[0])
        dic.pop(v[1])
        fin.add(v[1])
        put(v[2]+1, v[3],   v[4]  )
        put(v[2],   v[3]+1, v[4]  )
        put(v[2],   v[3],   v[4]+1)
        print(v[0])

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()))
sam = []
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ba,bb,bc = 0,0,0
print(a[0] + b[0] + c[0])
used = set()
for K in range(k - 1):
    if ba < x - 1 and (ba + 1,bb,bc) not in used:
        sam.append([a[ba + 1] + b[bb] + c[bc],ba + 1,bb,bc])
        used.add((ba + 1,bb,bc))
    if bb < y - 1 and (ba,bb + 1,bc) not in used:
        sam.append([a[ba] + b[bb + 1] + c[bc],ba,bb + 1,bc])
        used.add((ba,bb + 1,bc))
    if bc < z - 1 and (ba,bb,bc + 1) not in used:
        sam.append([a[ba] + b[bb] + c[bc + 1],ba,bb,bc + 1])
        used.add((ba,bb,bc + 1))
    big = max(sam)
    sam.remove(big)
    print(big[0])
    ba,bb,bc = big[1],big[2],big[3]

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)

arr = []

for i, a in enumerate(A):
    for j, b in enumerate(B):
        if (i+1) * (j+1) > K:
            break
        for k, c in enumerate(C):
            if (i+1) * (j+1) * (k+1) <= K:
                arr.append(a + b + c)
            else:
                break

arr.sort(reverse=True)

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

Submission status: Accepted

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

import heapq

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(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

yam = []
for l in range(x):
    for m in range(y):
        for n in range(z):
            if k < len(yam) and a[l]+b[m]+c[n] <= yam[0]:
                break
            elif k < len(yam):
                heapq.heappushpop(yam, a[l]+b[m]+c[n])
            else:
                heapq.heappush(yam, a[l]+b[m]+c[n])
yam.sort(reverse=True)
#print(yam)

for l in range(k):
        print(yam[l])

Submission status: Accepted

Solution code:
from heapq import heappush,heappop,heapify
from collections import defaultdict
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()
heap=[(-(A[X-1]+B[Y-1]+C[Z-1]),X-1,Y-1,Z-1)]
heapify(heap)
D=defaultdict(bool)
for k in range(K):
  s,x,y,z=heappop(heap)
  while D[(x,y,z)]:
    s,x,y,z=heappop(heap)
  s*=-1
  D[(x,y,z)]=True
  print(s)
  if x>=1:
    heappush(heap,(-A[x-1]-B[y]-C[z],x-1,y,z))
  if y>=1:
    heappush(heap,(-A[x]-B[y-1]-C[z],x,y-1,z))
  if z>=1:
    heappush(heap,(-A[x]-B[y]-C[z-1],x,y,z-1))