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:
#coding:utf-8
import Queue

X,Y,Z,K=map(int, raw_input().split())
A=map(int, raw_input().split())
B=map(int, raw_input().split())
C=map(int, raw_input().split())

pq=Queue.PriorityQueue()

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
S=[]
vis=set()
pq.put((-(A[0]+B[0]+C[0]),0,0,0))
vis.add((0,0,0))
while len(S)!=K:
    cost,ap,bp,cp=pq.get()
    S.append(-cost)
    for i in range(1<<3):
        da=i&0b001
        db=(i&0b010) >> 1
        dc=(i&0b100) >> 2
        nap=ap+da;nbp=bp+db;ncp=cp+dc
        if nap>=len(A) or nbp>=len(B) or ncp>=len(C):continue
        if (nap,nbp,ncp) not in vis:
            vis.add((nap,nbp,ncp))
            newcost=A[nap] + B[nbp] + C[ncp]
            pq.put((-newcost, nap,nbp,ncp))

for s in S:
    print(s)

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

li=[]
for i in a:
  for j in b:
    li.append(i+j)
li.sort(reverse=True)
 
ansli=[]
for i in c:
  for j in range(min(k,len(li))):
    ansli.append(i+li[j])
ansli.sort(reverse=True)
 
for i in range(k):
  print(ansli[i])

Submission status: Accepted

Solution code:
import itertools
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)
del a[min(k,x):]
del b[min(k,y):]
del c[min(k,z):]
 
ab = [i + j for (i, j) in list(itertools.product(a, b))]
 
ab.sort(reverse=True)
del ab[min(k,x*y):]
 
abc = [ij + h for (ij, h) in list(itertools.product(ab, c))]
 
abc.sort(reverse=True)
 
for g in range(k):
  print(abc[g])

Submission status: Accepted

Solution code:
X, Y, Z, K = map(int,input().split())
A_list = sorted(list(map(int,input().split())), reverse=True)
B_list = sorted(list(map(int,input().split())), reverse=True)
C_list = sorted(list(map(int,input().split())), reverse=True)

E_list = sorted([ai+bi for ai in A_list for bi in B_list], reverse=True)[:K]
sum_list = sorted([ei+ci for ei in E_list for ci in C_list], reverse=True)[:K]

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

Submission status: Accepted

Solution code:
import sys
import heapq
input = sys.stdin.readline
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()))
DD = [i+j for i in A for j in B]
DD.sort()
heap = []
for i in DD[max(len(DD)-3001,0):]:
     for j in C:
         heapq.heappush(heap,-(i+j))
for i in range(K):
    print(-heapq.heappop(heap))

Submission status: Accepted

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

Anum = 0
Bnum = 0
Cnum = 0

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(X*Y,K)]

ABC = []
for i in range(len(AB)):
    for j in range(Z):
        ABC.append(AB[i]+C[j])

ABC = sorted(ABC, reverse=True) 

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

Submission status: Accepted

Solution code:
x,y,z,n = 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,i1 in enumerate(a):
  for j,j1 in enumerate(b):
    for k,k1 in enumerate(c):
      if (i+1) * (j+1) * (k+1) <= n:
        d.append(i1 + j1 + k1)
      else:
        break
d.sort(reverse = True)
for i in d[:n]:
  print(i)

Submission status: Accepted

Solution code:
x,y,z,n = 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,i1 in enumerate(a):
  for j,j1 in enumerate(b):
    if (i+1)*(j+1) <= n:
      for k,k1 in enumerate(c):
        if (i+1) * (j+1) * (k+1) <= n:
          d.append(i1 + j1 + k1)
        else:
          break
    else:
      break
d.sort(reverse = True)
for i in d[:n]:
  print(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()))
asort,bsort,csort = sorted(A,reverse=True),sorted(B,reverse=True),sorted(C,reverse=True)
already = set((0,0,0))
print(asort[0] + bsort[0] + csort[0])
x,y,z = 0,0,0
check = []
for k in range(K - 1):
    if (x + 1,y,z) not in already and x + 1 < X:
        already.add((x + 1,y,z))
        heapq.heappush(check,(-(asort[x + 1] + bsort[y] + csort[z]),x + 1,y,z))
    if (x,y + 1,z) not in already and y + 1 < Y:
        already.add((x,y + 1,z))
        heapq.heappush(check,(-(asort[x] + bsort[y + 1] + csort[z]),x,y + 1,z))
    if (x,y,z + 1) not in already and z + 1 < Z:
        already.add((x,y,z + 1))
        heapq.heappush(check,(-(asort[x] + bsort[y] + csort[z + 1]),x,y,z + 1))
    maxsum = heapq.heappop(check)
    print(-maxsum[0])
    x,y,z = maxsum[1],maxsum[2],maxsum[3]

Submission status: Accepted

Solution code:
import heapq
 
_x, _y, _z, k = map(int, input().split())
aa = list(reversed(sorted(map(int, input().split()))))
bb = list(reversed(sorted(map(int, input().split()))))
cc = list(reversed(sorted(map(int, input().split()))))
 
ar = []
history = set()
 
 
def push(x, y, z):
    if (x, y, z) in history:
        pass
    else:
        v = aa[x] + bb[y] + cc[z]
        heapq.heappush(ar, (-v, x, y, z))
        history.add((x, y, z))
 
 
def pop():
    (v, x, y, z) = heapq.heappop(ar)
    return (-v, x, y, z)
 
 
push(0, 0, 0)
for _ in range(k):
    (total, x, y, z) = pop()
    print(total)
    if x + 1 < len(aa):
        push(x + 1, y, z)
    if y + 1 < len(bb):
        push(x, y + 1, z)
    if z + 1 < len(cc):
        push(x, y, z + 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)
def search(a, b):
  ret = []
  al = len(a)
  bl = len(b)
  for i in range(min(al, k)):
    for j in range(min(bl, k-i)):
      ret.append(a[i] + b[j])
  ret.sort(reverse = True)
  return ret
d = search(a, b)      
e = search(c, d)    

for i in range(k):
  print(e[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()))
asort,bsort,csort = sorted(A,reverse=True),sorted(B,reverse=True),sorted(C,reverse=True)
already = [[0,0,0]]
print(asort[0] + bsort[0] + csort[0])
x,y,z = 0,0,0
check = []
for k in range(K - 1):
    if [x + 1,y,z] not in already and x + 1 < X:
        already.append([x + 1,y,z])
        heapq.heappush(check,(-(asort[x + 1] + bsort[y] + csort[z]),x + 1,y,z))
    if [x,y + 1,z] not in already and y + 1 < Y:
        already.append([x,y + 1,z])
        heapq.heappush(check,(-(asort[x] + bsort[y + 1] + csort[z]),x,y + 1,z))
    if [x,y,z + 1] not in already and z + 1 < Z:
        already.append([x,y,z + 1])
        heapq.heappush(check,(-(asort[x] + bsort[y] + csort[z + 1]),x,y,z + 1))
    maxsum = heapq.heappop(check)
    print(-maxsum[0])
    x,y,z = maxsum[1],maxsum[2],maxsum[3]

Submission status: Accepted

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

A = sorted(list(map(int, input().split()))[:K])
B = sorted(list(map(int, input().split()))[:K])
C = sorted(list(map(int, input().split()))[:K])

ab = sorted(list(a+b for a in A for b in B), reverse = True)[:K]
abc = sorted(list(ab+c for ab in ab for c in C), reverse = True)[:K]

for i in abc : 
    print(i)

Submission status: Accepted

Solution code:
from heapq import *

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)

q=[]
li=[]
heappush(q,((a[0]+b[0]+c[0]) * (-1),0,0,0))

for i in range(k):
  node=heappop(q)
  
  print(node[0]*-1)
  if node[1]+1 != x:
    node_a = ((a[node[1]+1]+b[node[2]]+c[node[3]])*(-1), node[1]+1, node[2], node[3])
    if node_a not in q:
      heappush(q,node_a)
  
  if node[2]+1 != y:
    node_b = ((a[node[1]]+b[node[2]+1]+c[node[3]])*(-1), node[1], node[2]+1, node[3])
    if node_b not in q:
      heappush(q,node_b)
  
  if node[3]+1 != z:
    node_c = ((a[node[1]]+b[node[2]]+c[node[3]+1])*(-1), node[1], node[2], node[3]+1)
    if node_c not in q:
      heappush(q,node_c)

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 = []
heapq.heapify(q)
heapq.heappush(q,(-(A[0] + B[0] + C[0]),0,0,0))
for _ in range(K):
    m, i, j, k = heapq.heappop(q) #mに和、ijkにインデックス
    print(-m) #親から取り出すが、親は子よりも小さい、大きい順にするために-を入れている
    if i + 1 < X: #最初にiには０が挿入されている,pythonではインデックスが０から始まるから<xでいい
        s = (-(A[i+1] + B[j] + C[k]), i+1, j, k)
        if s not in q:
            heapq.heappush(q,s)
    if j + 1 < Y:
        s = (-(A[i] + B[j+1] + C[k]), i, j+1, k)
        if s not in q:
            heapq.heappush(q, s)
    if k + 1 < Z:
        s = (-(A[i] + B[j] + C[k+1]), i, j, k+1)
        if s not in q:
            heapq.heappush(q,s)

Submission status: Accepted

Solution code:
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 = sorted(a, reverse=True)[:k[3]]
B = sorted(b, reverse=True)[:k[3]]
C = sorted(c, reverse=True)[:k[3]]

ab = sorted(list(ax+bx for ax in A for bx in B), reverse=True)[:k[3]]
abc = sorted(list(aby+cy for aby in ab for cy in C), reverse=True)[:k[3]]
for i in abc: print(i)

Submission status: Accepted

Solution code:
ns = list(map(int, input().split()))
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))
zs = list(map(int, input().split()))
xys = []
ans = []
for x in xs:
	for y in ys:
		xys.append(x+y)
xys.sort()
xys.reverse()
for i in range(min(ns[0]*ns[1], ns[3])):
	for j in range(min(ns[2], ns[3])):
		ans.append(xys[i]+zs[j])
ans.sort()
ans.reverse()
for i in range(ns[3]):
	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()))
anss = []

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

for i in range(x):
    for j in range(y):
        for l in range(z):
            if (i + 1) * (j + 1) * (l + 1) <= k:
                anss.append(a[i] + b[j] + c[l])
            else:
                break
anss.sort(reverse = True)

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

Submission status: Accepted

Solution code:
import heapq
 
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)
 
q = []
heapq.heapify(q)
heapq.heappush(q, (-(A[0] + B[0] + C[0]), 0, 0, 0))  # heapq では昇順ソートするためマイナスにする
 
for _ in range(K):
    m, i, j, k = heapq.heappop(q)
    print(-m)
    if i + 1 < X:
        s = (-(A[i+1] + B[j] + C[k]), i+1, j, k) 
        if s not in q:
            heapq.heappush(q, s)
    if j + 1 < Y:
        s = (-(A[i] + B[j+1] + C[k]), i, j+1, k) 
        if s not in q:
            heapq.heappush(q, s)
    if k + 1 < Z:
        s = (-(A[i] + B[j] + C[k+1]), i, j, k+1) 
        if s not in q:
            heapq.heappush(q, s)

Submission status: Accepted

Solution code:
import heapq

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)

q = []
heapq.heapify(q)
heapq.heappush(q, (-(A[0] + B[0] + C[0]), 0, 0, 0))  # heapq では昇順ソートするためマイナスにする

for _ in range(K):
    m, i, j, k = heapq.heappop(q)
    print(-m)
    if i + 1 < X:
        s = (-(A[i+1] + B[j] + C[k]), i+1, j, k) 
        if s not in q:
            heapq.heappush(q, s)
    if j + 1 < Y:
        s = (-(A[i] + B[j+1] + C[k]), i, j+1, k) 
        if s not in q:
            heapq.heappush(q, s)
    if k + 1 < Z:
        s = (-(A[i] + B[j] + C[k+1]), i, j, k+1) 
        if s not in q:
            heapq.heappush(q, s)

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)
l = []
for i in range(len(a)):
    for j in range(len(b)):
        for kk in range(len(c)):
            if (i + 1) * (j + 1) * (kk + 1) <= k:
                l.append(a[i] + b[j] + c[kk])
            else:
                break
l.sort(reverse = True)
for i in range(k):
    print(l[i])

Submission status: Accepted

Solution code:
X,Y,Z,K = map(int, input().split())
cakes = [list(map(int, input().split())) for _ in range(3)]
 
cakes = list(map(lambda x : sorted(x, reverse=True), cakes))
ans = list()
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1)*(j+1)*(k+1)>K:
                break
            ans.append(cakes[0][i] + cakes[1][j] + cakes[2][k])
ans.sort(reverse=True)
print("\n".join(map(str, ans[:K])))

Submission status: Accepted

Solution code:
import itertools
import sys

input = sys.stdin.readline

maxn = 10000000000

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)

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

cand.sort(reverse=True)
for k in range(K):
    print(cand[k])

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()))
asort,bsort,csort = sorted(A,reverse=True),sorted(B,reverse=True),sorted(C,reverse=True)
print(asort[0] + bsort[0] + csort[0])
already = [[0,0,0]]
x,y,z = 0,0,0
place_sum = []
check = []
for k in range(K - 1):
    if [x + 1,y,z] not in already and x + 1 < X:
        already.append([x + 1,y,z])
        place_sum.append([x + 1,y,z])
        check.append(asort[x + 1] + bsort[y] + csort[z])
    if [x,y + 1,z] not in already and y + 1 < Y:
        already.append([x,y + 1,z])
        place_sum.append([x,y + 1,z])
        check.append(asort[x] + bsort[y + 1] + csort[z])
    if [x,y,z + 1] not in already and z + 1 < Z:
        already.append([x,y,z + 1])
        place_sum.append([x,y,z + 1])
        check.append(asort[x] + bsort[y] + csort[z + 1])
    print(max(check))
    x,y,z = place_sum[check.index(max(check))][0],place_sum[check.index(max(check))][1],place_sum[check.index(max(check))][2]
    place_sum.pop(check.index(max(check)))
    check.remove(max(check))

Submission status: Accepted

Solution code:
from heapq import *

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 = sorted([int(x) for x in A], reverse=True)
B = sorted([int(x) for x in B], reverse=True)
C = sorted([int(x) for x in C], reverse=True)

res = []
memo = []
ans = []
heappush(res, (-A[0]-B[0]-C[0], 0, 0, 0))
while len(ans) < K:
  e, i, j, k = heappop(res)
  ans.append(-e)
  if i+1 < X and [i+1, j, k] not in memo:
    heappush(res, (-A[i+1]-B[j]-C[k], i+1, j, k))
    memo.append([i+1, j, k])
  if j+1 < Y and [i, j+1, k] not in memo:
    heappush(res, (-A[i]-B[j+1]-C[k], i, j+1, k))
    memo.append([i, j+1, k])
  if k+1 < Z and [i, j, k+1] not in memo:
    heappush(res, (-A[i]-B[j]-C[k+1], i, j, k+1))
    memo.append([i, j, k+1])

for i in ans:
  print(i)

Submission status: Accepted

Solution code:
import itertools
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)
A,B,C =A[:k],B[:k],C[:k]
AB = sorted(list(i+j for i in A for j in B), reverse=True)[:k]

ABC = sorted(list(i+j for i in AB for j in C),reverse = True)[:k]

for i in ABC:
    print(i)

Submission status: Accepted

Solution code:
from sys import exit

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 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:
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([-aa for aa in a])
b = sorted([-bb for bb in b])
c = sorted([-cc for cc in c])
a_diff = [a[i+1]-a[i] for i in range(x-1)]
b_diff = [b[i+1]-b[i] for i in range(y-1)]
c_diff = [c[i+1]-c[i] for i in range(z-1)]

que = [(a[0]+b[0]+c[0], (0, 0, 0))]
heapq.heapify(que)
s = set([(0, 0, 0)])
while k > 0:
    ans, (ia, ib, ic) = heapq.heappop(que)
    print(-ans)
    # print(-ans, (ia, ib, ic))
    k -= 1

    if ia < x-1 and (ia+1, ib, ic) not in s:
        heapq.heappush(que, (ans+a_diff[ia], (ia+1, ib, ic)))
        s.add((ia+1, ib, ic))
    if ib < y-1 and (ia, ib+1, ic) not in s:
        heapq.heappush(que, (ans+b_diff[ib], (ia, ib+1, ic)))
        s.add((ia, ib+1, ic))
    if ic < z-1 and (ia, ib, ic+1) not in s:
        heapq.heappush(que, (ans+c_diff[ic], (ia, ib, ic+1)))
        s.add((ia, ib, ic+1))

Submission status: Accepted

Solution code:
import heapq

_x, _y, _z, k = map(int, input().split())
aa = list(reversed(sorted(map(int, input().split()))))
bb = list(reversed(sorted(map(int, input().split()))))
cc = list(reversed(sorted(map(int, input().split()))))

ar = []
history = set()


def push(x, y, z):
    if (x, y, z) in history:
        pass
    else:
        v = aa[x] + bb[y] + cc[z]
        heapq.heappush(ar, (-v, x, y, z))
        history.add((x, y, z))


def pop():
    (v, x, y, z) = heapq.heappop(ar)
    return (-v, x, y, z)


push(0, 0, 0)
for _ in range(k):
    (total, x, y, z) = pop()
    print(total)
    if x + 1 < len(aa):
        push(x + 1, y, z)
    if y + 1 < len(bb):
        push(x, y + 1, z)
    if z + 1 < len(cc):
        push(x, y, z + 1)

Submission status: Accepted

Solution code:
x,y,z,w = map(int, input().split())
a = [list(map(int, input().split())) for i in range(3)]
for i in range(3) : a[i].sort(reverse=True)
li = []
for i in range(1, x+1):
  for j in range(1, y+1):
    if i*j > w : break
    for k in range(1, z+1):
      if i*j*k > w : break
      li.append(a[0][i-1]+a[1][j-1]+a[2][k-1])
li.sort(reverse=True)
print("\n".join(map(str,li[:w])))

Submission status: Accepted

Solution code:
import heapq
from collections import defaultdict as dd

X, Y, Z, K = [int(i) for i in input().split()]
ABCList = [sorted([int(i) for i in input().split()], reverse=True) for _ in range(3)]

pQ = []
heapq.heappush(pQ, [sum(ABC[0] for ABC in ABCList) * (-1), [0, 0, 0]])
QDict = dd(int)
QDict[(0, 0, 0)] = 1
for _ in range(K):
    item = heapq.heappop(pQ)
    print(item[0] * (-1))
    abc = item.copy()[1]
    for i, element in enumerate(abc):
        abcTemp = abc.copy()
        abcTemp[i] += 1
        if abcTemp[i] < len(ABCList[i]) and QDict[tuple(abcTemp)] == 0:
            newItem = [sum([ABCList[i][abcTemp[i]] for i in range(3)]) * (-1), abcTemp]
            heapq.heappush(pQ, newItem)
            QDict[(tuple(abcTemp))] = 1

Submission status: Accepted

Solution code:
x,y,z,K = (int(i) for i in input().split(" "))

a_ = sorted([int(i) for i in input().split(" ")])[::-1]
b_ = sorted([int(i) for i in input().split(" ")])[::-1]
c_ = sorted([int(i) for i in input().split(" ")])[::-1]

res = []
for i in range(x):
    for j in range(y):
        for k in range(z):
            if (i+1)*(j+1)*(k+1) <= K:
                res.append(a_[i]+b_[j]+c_[k])
            else:
                break

res = sorted(res)[::-1]

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

Submission status: Accepted

Solution code:
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True # and False
def ddprint(x):
  if DBG:
    print(x)

x,y,z,k = inm()
a = inl()
b = inl()
c = inl()
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
if x < 60 or y < 60:
    loa = x
    lob = y
elif a[0]-a[60] > b[0]-b[60]:
    loa = 60
    lob = y
else:
    loa = x
    lob = 60

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

u = len(ab)
if z < 60 or u < 60:
    loc = z
    loab = u
elif c[0]-c[60] > ab[0]-ab[60]:
    loc = 60
    loab = u
else:
    loc = z
    loab = 60

abc = []
for i in range(loc):
    for j in range(loab):
        abc.append(c[i]+ab[j])
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())
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)

heap = []
item = (0,(0,0,0))
max_score = A[0] + B[0] + C[0]
xyzset = set(item[1])
answer = []

heapq.heappush(heap,item)

while len(answer) < K:
    score,(x,y,z) = heapq.heappop(heap)
    answer.append(max_score - score)

    if x != X-1 and (x+1,y,z) not in xyzset:
        heapq.heappush(heap,(max_score - A[x+1] - B[y] - C[z],(x+1,y,z)))
        xyzset.add((x+1,y,z))

    if y != Y-1 and (x,y+1,z) not in xyzset:
        heapq.heappush(heap,(max_score - A[x] - B[y+1] - C[z],(x,y+1,z)))
        xyzset.add((x,y+1,z))

    if z != Z-1 and (x,y,z+1) not in xyzset:
        heapq.heappush(heap,(max_score - A[x] - B[y] - C[z+1],(x,y,z+1)))
        xyzset.add((x,y,z+1))
    
for x in answer:
    print(x)

Submission status: Accepted

Solution code:
import heapq

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)

anslist = []
flag = {}

heapq.heappush(anslist, (-(A[0]+B[0]+C[0]), (0, 0, 0)))


for _ in range(K):
    p = heapq.heappop(anslist)
    while p[1] in flag:
        p = heapq.heappop(anslist)
    print(p[0]*(-1))
    a, b, c = p[1]
    flag[a, b, c] = True
    # push
    for i, j, k in [[1, 0, 0], [0, 1, 0], [0, 0, 1]]:
        if a+i >= X or b+j >= Y or c+k >= Z:
            continue
        tmp = (a+i, b+j, c+k)
        heapq.heappush(anslist, (-(A[a+i]+B[b+j]+C[c+k]), tmp))

Submission status: Accepted

Solution code:
# -*- coding utf-8 -*-
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)

hq = []
visited = set()
c = 0

def visit(i, j, k):
    if (i < X and j < Y and k < Z) and (visited.isdisjoint({(i, j, k)})):
        heapq.heappush(hq, ( -(A[i] + B[j] + C[k]), (i, j, k)))
        visited.add((i, j, k))

visit(0, 0, 0)

while len(hq) != 0 and c < K:
    taberu = heapq.heappop(hq)
    print(-taberu[0])

    visit(taberu[1][0] + 1, taberu[1][1], taberu[1][2])
    visit(taberu[1][0], taberu[1][1] + 1, taberu[1][2])
    visit(taberu[1][0], taberu[1][1], taberu[1][2] + 1)

    c += 1

Submission status: Accepted

Solution code:
import heapq


class Delicious (object):
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

    def total(self):
        return(A[self.x] + B[self.y] + C[self.z])

    def __lt__(self, other):
        return self.total() > other.total()


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)

ds = []
heapq.heappush(ds, Delicious(0, 0, 0))

check = {}

for _ in range(K):
    d = heapq.heappop(ds)
    while (d.x, d.y, d.z) in check:
        d = heapq.heappop(ds)
    print(d.total())
    check[d.x, d.y, d.z] = True

    if d.x < X-1:
        heapq.heappush(ds, Delicious(d.x+1, d.y, d.z))
    if d.y < Y-1:
        heapq.heappush(ds, Delicious(d.x, d.y+1, d.z))
    if d.z < Z-1:
        heapq.heappush(ds, Delicious(d.x, d.y, d.z+1))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import heapq

def pResult(a,b,c):
    print(a[1]+b[1]+c[1])


def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    A.sort(reverse = True)
    B.sort(reverse = True)
    C.sort(reverse = True)
    Q = []
    D = set([])
    I = A[0] + B[0] + C[0]
    heapq.heappush(Q,(-I,I,0,0,0))
    k = 0
    while(True):
        if k == K:
            break
        prev = heapq.heappop(Q)
        ai = prev[2]
        bi = prev[3]
        ci = prev[4]
        if (ai,bi,ci) in D:
            continue
        print(prev[1])
        D.add((ai,bi,ci))
        k+=1
        if ai+1 < len(A):
            i = A[ai + 1] + B[bi] + C[ci]
            heapq.heappush(Q,(-i,i,ai+1,bi,ci))
        if bi+1 < len(B):
            i = A[ai] + B[bi + 1] + C[ci]
            heapq.heappush(Q,(-i,i,ai,bi + 1,ci))
        if ci+1 < len(C):
            i = A[ai] + B[bi] + C[ci + 1]
            heapq.heappush(Q,(-i,i,ai,bi,ci + 1))



# Generated by 1.1.3 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    Y = int(next(tokens))  # type: int
    Z = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(X) ]  # type: "List[int]"
    B = [ int(next(tokens)) for _ in range(Y) ]  # type: "List[int]"
    C = [ int(next(tokens)) for _ in range(Z) ]  # type: "List[int]"
    solve(X, Y, Z, K, A, B, C)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
x,y,z,k = map(int,input().split())
lia = sorted(list(map(int,input().split())),reverse = True)
lib = sorted(list(map(int,input().split())),reverse = True)
lic = sorted(list(map(int,input().split())),reverse = True)
li = []

for s in range(x):
    for t in range(y):
        if s*t > k: break
        for u in range(z):
            if s*t*u <= k:
                li.append(lia[s]+lib[t]+lic[u])
            else: break
ans = sorted(li,reverse = True)[:k]

print('\n'.join(map(str,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()))
ABlist = []
ABClist = []

for x in A:
    for y in B:
        sumple = x + y
        ABlist.append(sumple)
    ABlist.sort(reverse=True)
    ABlist = ABlist[:K]

for x in ABlist:
    for z in C:
        test = x + z
        ABClist.append(test)
    ABClist.sort(reverse=True)
    ABClist = ABClist[:K]

for x in ABClist:
    print(x)