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 heapq


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

    used = set()
    candidate = []
    heapq.heappush(candidate, (-A[0]-B[0]-C[0], 0, 0, 0))

    for _ in range(K):
        value, x, y, z = heapq.heappop(candidate)
        print(-value)

        if x+1 < X and (x+1, y, z) not in used:
            heapq.heappush(candidate, (-A[x+1]-B[y]-C[z], x+1, y, z))
            used.add((x+1, y, z))

        if y+1 < Y and (x, y+1, z) not in used:
            heapq.heappush(candidate, (-A[x]-B[y+1]-C[z], x, y+1, z))
            used.add((x, y+1, z))

        if z+1 < Z and (x, y, z+1) not in used:
            heapq.heappush(candidate, (-A[x]-B[y]-C[z+1], x , y, z+1))
            used.add((x, y, z+1))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import heapq

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

used = set()
candidate = []
heapq.heappush(candidate, (-A[0]-B[0]-C[0], 0, 0, 0))

for _ in range(K):
    value, x, y, z = heapq.heappop(candidate)
    print(-value)

    if x+1 < X and (x+1, y, z) not in used:
        heapq.heappush(candidate, (-A[x+1]-B[y]-C[z], x+1, y, z))
        used.add((x+1, y, z))

    if y+1 < Y and (x, y+1, z) not in used:
        heapq.heappush(candidate, (-A[x]-B[y+1]-C[z], x, y+1, z))
        used.add((x, y+1, z))

    if z+1 < Z and (x, y, z+1) not in used:
        heapq.heappush(candidate, (-A[x]-B[y]-C[z+1], x , y, z+1))
        used.add((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)

ans = []
for x in range(X):
    if x+1 > K:
        break
    for y in range(Y):
        if (x+1) * (y+1) > K:
            break
        for z in range(Z):
            if (x+1) * (y+1) * (z+1) > K:
                break

            value = A[x] + B[y] + C[z]
            ans.append(value)

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

Submission status: Accepted

Solution code:
import heapq
def main():
    X, Y, Z, K = map(int, input().split())
    A = [int(a) for a in input().split()]
    B = [int(b) for b in input().split()]
    C = [int(c) for c in input().split()]
    A = sorted(A, reverse = True)
    B = sorted(B, reverse = True)
    C = sorted(C, reverse = True)

    ans = []
    h = [[-(A[0]+B[0]+C[0]), 0, 0, 0]]
    added = set([(0, 0, 0)])
    for _ in range(K):
        v, i, j, k = heapq.heappop(h)
        # ans.append(-v)
        print(-v)
        if i+1 < len(A) and (i+1, j, k) not in added:
            heapq.heappush(h, [v+A[i]-A[i+1], i+1, j, k])
            added.add((i+1, j, k))
        if j+1 < len(B) and (i, j+1, k) not in added:
            heapq.heappush(h, [v+B[j]-B[j+1], i, j+1, k])
            added.add((i, j+1, k))
        if k+1 < len(C) and (i, j, k+1) not in added:
            heapq.heappush(h, [v+C[k]-C[k+1], i, j, k+1])
            added.add((i, j, k+1))
    # return ans

if __name__ == '__main__':
    main()

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


def solve1():
    ab = sorted([x + y for x in A[:K] for y in B[:K]], reverse=True)
    f = sorted([c + e for c in C for e in ab[:K]], reverse=True)
    return f[:K]


for top in solve1():
    print(top)

Submission status: Accepted

Solution code:
import heapq
x, y, z, K = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse = True)
b = sorted(list(map(int, input().split())), reverse = True)
c = sorted(list(map(int, input().split())), reverse = True)
heap = []
for i in range(min(x, K)):
    for j in range(min(y, K//(i+1) + 1)):
        for k in range(min(z, K//((i+1)*(j+1)) + 1)):
            heapq.heappush(heap, - a[i] - b[j] - c[k])
for a in heapq.nsmallest(K, heap):
    print(-a)

Submission status: Accepted

Solution code:
from heapq import heappop, heappush
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)

count = 0
memo = []
H = []
H.append([-(A[0]+B[0]+C[0]),0,0,0])
memo.append((0,0,0))

while count < K:
  p,i,j,k = heappop(H)
  print(-p)
  count += 1
  if (i+1,j,k) not in memo and i <= X-2:
    memo.append((i+1,j,k))
    heappush(H,[-(A[i+1]+B[j]+C[k]),i+1,j,k])
  if (i,j+1,k) not in memo and j <= Y-2:
    memo.append((i,j+1,k))
    heappush(H,[-(A[i]+B[j+1]+C[k]),i,j+1,k])
  if (i,j,k+1) not in memo and k <= Z-2:
    memo.append((i,j,k+1))
    heappush(H,[-(A[i]+B[j]+C[k+1]),i,j,k+1])

Submission status: Accepted

Solution code:
def main():
    import sys
    import heapq
    heap = []
    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 = sorted(A)[::-1]
    B = sorted(B)[::-1]
    C = sorted(C)[::-1]
    S = A[0]+B[0]+C[0]
    mA = A[0]
    mB = B[0]
    mC = C[0]
    for i in range(X):
        A[i] -= mA
    for i in range(Y):
        B[i] -= mB
    for i in range(Z):
        C[i] -= mC
    
    for i in range(Z):
        heapq.heappush(heap, [-C[i],-C[i],0])
    
    answer = []
    
    AB = []
    for i in range(X):
        for j in range(Y):
            AB.append(A[i]+B[j])
    AB = sorted(AB)[::-1]
    for i in range(K):
        c = heapq.heappop(heap)
        answer.append(S-c[0])
        if c[2]+1 < len(AB):
	        heapq.heappush(heap,[c[1]-AB[c[2]+1],c[1],c[2]+1])
        
    print(*answer,sep="\n")
        
            
    
    

main()

Submission status: Accepted

Solution code:


from collections import deque, defaultdict
import copy
import bisect
#sys.setrecursionlimit(10 ** 9)
import math
import heapq


import sys
def input():
	return sys.stdin.readline().strip()


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)

kouho = []
all_set = set()
heapq.heappush(kouho, [-(A[0] + B[0] + C[0]), 0, 0, 0])
all_set.add(0)
for i in range(K):
	comb = heapq.heappop(kouho)
	print(-comb[0])
	if comb[1] < X - 1 and not (comb[1] + 1)*Y*Z + comb[2]*Z + comb[3] in all_set:
		heapq.heappush(kouho,  [-(A[comb[1] + 1] + B[comb[2]] + C[comb[3]]), comb[1] + 1, comb[2], comb[3]])
		all_set.add((comb[1] + 1)*Y*Z + comb[2]*Z + comb[3])
	if comb[2] < Y - 1 and not comb[1]*Y*Z + (comb[2] + 1)*Z + comb[3] in all_set:
		heapq.heappush(kouho,  [-(A[comb[1]] + B[comb[2] + 1] + C[comb[3]]), comb[1], comb[2] + 1, comb[3]])
		all_set.add(comb[1]*Y*Z + (comb[2] + 1)*Z + comb[3])
	if comb[3] < Z - 1 and not comb[1]*Y*Z + comb[2]*Z + comb[3] + 1 in all_set:
		heapq.heappush(kouho,  [-(A[comb[1]] + B[comb[2]] + C[comb[3] + 1]), comb[1], comb[2], comb[3] + 1])
		all_set.add(comb[1]*Y*Z + comb[2]*Z + comb[3] + 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)
def conbination(l1, l2):
  con_l = []
  for i in l1:
    for j in l2:
      con_l.append(i+j)
  con_l.sort(reverse=True)
  con_l = con_l[:k]
  return con_l
ans = []
ans += (conbination(conbination(a, b),c))

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

Submission status: Accepted

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

X,Y,Z,K = II()
A = III()
B = III()
C = III()

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

a = []
if X*Y*Z <= 10**6:
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                a.append(A[i]+B[j]+C[k])
    a.sort(reverse=True)
    for i in range(K):
        print(a[i])
else:
    for i in range(X):
        if i==0: jmax = Y
        else: jmax = min(Y,math.ceil(5000/i))
        for j in range(jmax):
            if i*j==0: kmax = Z
            else: kmax = min(Z,math.ceil(5000/i/j))
            for k in range(kmax):
                a.append(A[i]+B[j]+C[k])
    a.sort(reverse=True)   
    for i in range(K):
        print(a[i])

Submission status: Accepted

Solution code:
x,y,z,K = map(int,input().split())
ans = 0
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

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

ans = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1)*(j+1)*(k+1) <= K: ans.append(A[i]+B[j]+C[k])
            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)
p = []
for h in range(min(x,k)):
    for i in range(y):
        if h*i > k:
            break
        for j in range(z):
            if h*i*j > k:
                break
            p.append(a[h]+b[i]+c[j])

p.sort(reverse=True)
for i in range(k):
    print(p[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)
p = []
for i in a:
    for j in b:
        p.append(i+j)
p.sort(reverse=True)
p = p[:min(3001, x*y*z+1)]
q = []
for j in c:
    for i in p:
        q.append(i+j)
q.sort(reverse=True)
for i in range(k):
    print(q[i])

Submission status: Accepted

Solution code:
import heapq
x, y, z , k = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse=True)
b = sorted(list(map(int, input().split())), reverse=True)
c = sorted(list(map(int, input().split())), reverse=True)
q = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
qh = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
heapq.heapify(q)
ans = []
heapq.heapify(ans)
for i in range(k):
    t, aq, bq, cq = heapq.heappop(q)
    heapq.heappush(ans, t)
    if aq + 1 < len(a):
        if [-(a[aq + 1] + b[bq] + c[cq]), aq + 1, bq, cq] not in qh:
            heapq.heappush(q, [-(a[aq + 1] + b[bq] + c[cq]), aq + 1, bq, cq])
            heapq.heappush(qh, [-(a[aq + 1] + b[bq] + c[cq]), aq + 1, bq, cq])
    if bq + 1 < len(a):
        if [-(a[aq] + b[bq + 1] + c[cq]), aq, bq + 1, cq] not in qh:
            heapq.heappush(q, [-(a[aq] + b[bq + 1] + c[cq]), aq, bq + 1, cq])
            heapq.heappush(qh, [-(a[aq] + b[bq + 1] + c[cq]), aq, bq + 1, cq])
    if cq + 1 < len(a):
        if [-(a[aq] + b[bq] + c[cq + 1]), aq, bq, cq + 1] not in qh:
            heapq.heappush(q, [-(a[aq] + b[bq] + c[cq + 1]), aq, bq, cq + 1])
            heapq.heappush(qh, [-(a[aq] + b[bq] + c[cq + 1]), aq, bq, cq + 1])
for i in range(len(ans)):
    print(-heapq.heappop(ans))

Submission status: Accepted

Solution code:
# データ構造：ヒープ
import heapq
class heapque:
    def __init__(self, *args):
        self.que = []
        for arg in args:
            self.push(arg)
    def push(self, v):
        heapq.heappush(self.que, v)
    def pop(self):
        return heapq.heappop(self.que)

# 定数
INF = float("inf")
MOD = int(1e9 + 7)

# エントリーポイント
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 = sorted(A, reverse=True)
    b = sorted(B, reverse=True)
    c = sorted(C, reverse=True)

    asum = lambda x: a[x[0]] + b[x[1]] + c[x[2]]

    z = (0, 0, 0)
    heap = heapque((-asum(z), z))
    used = {z}
    for _ in range(K):
        cur = heap.pop()
        print(-cur[0])

        ai, bi, ci = cur[1]
        ns = ((min(ai + 1, X - 1), bi, ci)
            , (ai, min(bi + 1, Y - 1), ci)
            , (ai, bi, min(ci + 1, Z - 1)))
        for n in ns:
            if n in used: continue
            heap.push((-asum(n), n))
            used |= {n}

main()

Submission status: Accepted

Solution code:
# データ構造：ヒープ
import heapq
class heapque:
    def __init__(self, *args):
        self.que = []
        for arg in args:
            self.push(arg)
    def push(self, v):
        heapq.heappush(self.que, v)
    def pop(self):
        return heapq.heappop(self.que)

# 定数
INF = float("inf")
MOD = int(1e9 + 7)

# エントリーポイント
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 = sorted(A, reverse=True)
    b = sorted(B, reverse=True)
    c = sorted(C, reverse=True)
    heap = heapque((-(a[0] + b[0] + c[0]), (0, 0, 0)))
    used = {(0, 0, 0)}
    for _ in range(K):
        cur = heap.pop()
        print(-cur[0])

        ai, bi, ci = cur[1]
        ns = ((min(ai + 1, X - 1), bi, ci), (ai, min(bi + 1, Y - 1), ci), (ai, bi, min(ci + 1, Z - 1)))
        for n in ns:
            if n in used: continue
            heap.push((-(a[n[0]] + b[n[1]] + c[n[2]]), n))
            used |= {n}

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=sorted(list(map(int,input().split())),reverse=True)
ab=[]
for i in a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse=True)
ans=[]
for i in range(min(k,len(ab))):
    for j in range(min(k,len(c))):
        ans.append(ab[i]+c[j])
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 = sorted(A, reverse = True)
B = sorted(B, reverse = True)
C = sorted(C, reverse = True)

ans = []

for i in range(min(K,X)):
  for j in range(min(K//(i+1),Y)):
    for k in range(min(K//(i+1)//(j+1),Z)):
      ans.append(A[i]+B[j]+C[k])
    
ans = sorted(ans, reverse=True)

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

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()))
AList.sort(reverse=True)
BList.sort(reverse=True)
CList.sort(reverse=True)
ans = []
for x in range(X):
    for y in range(Y):
        if (x+1)*(y+1) > min(K,X*Y):
            break
        for z in range(Z):
            if (x+1)*(y+1)*(z+1) > min(K,X*Y*Z):
                break
            ans.append( AList[x] + BList[y] + CList[z])

ans.sort(reverse=True)
for i in range(K):
    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 = []
b=0
c=0
for a in range(len(A)):
	for b in range(len(B)):
		for c in range(len(C)):
			if (a+1)*(b+1)*(c+1)<=K:
				ans.append(A[a]+B[b]+C[c])
			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)
ans = []
for i in range(X):
	for j in range(min(Y,K//(i+1)+1)):
		for k in range(min(Z,K//((i+1)*(j+1))+1)):
			ans.append(A[i]+B[j]+C[k])

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

Submission status: Accepted

Solution code:

import heapq
x, y, z, k = [ int(v) for v in input().split() ]
x_list = sorted([ int(v) for v in input().split() ], reverse = True)
y_list = sorted([ int(v) for v in input().split() ], reverse = True)
z_list = sorted([ int(v) for v in input().split() ], reverse = True)

cake_list = []
num_set = set([0])

def hash2(a,b,c):
    return 10**8*a+10**4*b+c


def maling_cake(a,b,c):
    d = x_list[a]+y_list[b]+z_list[c]
    return (-d,a,b,c)

for i in range(k):
    if i == 0:
        print(-maling_cake(0,0,0)[0])
        if x > 1: 
            heapq.heappush(cake_list, maling_cake(1,0,0))
            num_set.add(hash2(1,0,0))
        if y > 1: 
            heapq.heappush(cake_list, maling_cake(0,1,0))
            num_set.add(hash2(0,1,0))
        if z > 1: 
            heapq.heappush(cake_list, maling_cake(0,0,1))
            num_set.add(hash2(0,0,1))
        
    else:
        di, ai, bi, ci = heapq.heappop(cake_list)
        if ai < x - 1:
            if hash2(ai+1,bi,ci) not in num_set:
                heapq.heappush(cake_list, maling_cake(ai+1,bi,ci))
                num_set.add(hash2(ai+1,bi,ci))
        if bi < y - 1:
            if hash2(ai,bi+1,ci) not in num_set:
                heapq.heappush(cake_list, maling_cake(ai,bi+1,ci))
                num_set.add(hash2(ai,bi+1,ci))
        if ci < z - 1:
            if hash2(ai,bi,ci+1) not in num_set:
                heapq.heappush(cake_list, maling_cake(ai,bi,ci+1))
                num_set.add(hash2(ai,bi,ci+1))
        print(-di)

Submission status: Accepted

Solution code:
import heapq

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

q=[(-(a[0]+b[0]+c[0]),0,0,0)]
trash=[]

for _ in range(k):
  v,i,j,k=heapq.heappop(q)
  print(-v)
  if (i+1,j,k) not in trash and i+1<x:
    heapq.heappush(q,(-(a[i+1]+b[j]+c[k]),i+1,j,k))
    trash.append((i+1,j,k))
  if (i,j+1,k) not in trash and j+1<y:
    heapq.heappush(q,(-(a[i]+b[j+1]+c[k]),i,j+1,k))
    trash.append((i,j+1,k))
  if (i,j,k+1) not in trash and k+1<z:
    heapq.heappush(q,(-(a[i]+b[j]+c[k+1]),i,j,k+1))
    trash.append((i,j,k+1))

Submission status: Accepted

Solution code:

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

Q = []
topK = []
dic = {}

A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

A.sort()
B.sort()
C.sort()

Q.append([X-1,Y-1,Z-1])
dic[(X-1,Y-1,Z-1)] = 1

while len(Q) > 0:

    now = Q.pop(0)

    if len(topK) < K:
        
        #if ( now[0] , now[1] , now[2]) not in dic:
        #dic[ ( now[0] , now[1] , now[2]) ] = 1
        topK.append( A[now[0]] + B[now[1]] + C[now[2]] )
        topK.sort()
            
        if now[0] != 0 and (now[0]-1 , now[1] , now[2]) not in dic:
            Q.append([ now[0]-1 , now[1] , now[2] ])
            dic[( now[0]-1 , now[1] , now[2] )] = 1
        if now[1] != 0 and (now[0] , now[1]-1 , now[2]) not in dic:
            Q.append([ now[0] , now[1]-1 , now[2] ])
            dic[( now[0] , now[1]-1 , now[2] )] = 1
        if now[2] != 0 and (now[0] , now[1] , now[2]-1) not in dic:
            Q.append([ now[0] , now[1] , now[2]-1 ])
            dic[( now[0] , now[1] , now[2]-1 )] = 1


    elif topK[0] < A[now[0]] + B[now[1]] + C[now[2]]:

        #if ( now[0] , now[1] , now[2] ) not in dic:

        #dic[ (now[0] , now[1] , now[2])] = 1
            #topK.append( A[now[0]] + B[now[1]] + C[now[2]] )
        del topK[0]
        topK.append(A[now[0]] + B[now[1]] + C[now[2]])
        topK.sort()

        if now[0] != 0 and (now[0]-1 , now[1] , now[2]) not in dic:
            Q.append([ now[0]-1 , now[1] , now[2] ])
            dic[( now[0]-1 , now[1] , now[2] )] = 1
        if now[1] != 0 and (now[0] , now[1]-1 , now[2]) not in dic:
            Q.append([ now[0] , now[1]-1 , now[2] ])
            dic[( now[0] , now[1]-1 , now[2] )] = 1
        if now[2] != 0 and (now[0] , now[1] , now[2]-1) not in dic:
            Q.append([ now[0] , now[1] , now[2]-1 ])
            dic[( now[0] , now[1] , now[2]-1 )] = 1
     

topK.sort()
#print (dic)

for i in range(K):

    print (topK[-1-i])

Submission status: Accepted

Solution code:
import heapq


x, y, z, k = map(int, input().split())
x_cake = [-float('inf')] + sorted(map(int, input().split()))
y_cake = [-float('inf')] + sorted(map(int, input().split()))
z_cake = [-float('inf')] + sorted(map(int, input().split()))


index_set = set()
x_index = y_index = z_index = -1
que = []
heapq.heappush(que, (-(x_cake[-1] + y_cake[-1] + z_cake[-1]), (-1, -1, -1)))
index_set.add((-1, -1, -1))
for _ in range(k):
    popped_tuple = heapq.heappop(que)
    print(-popped_tuple[0])
    x_index, y_index, z_index = popped_tuple[1]
    if (x_index-1, y_index, z_index) not in index_set:
        heapq.heappush(que, (-(x_cake[x_index-1] + y_cake[y_index] + z_cake[z_index]), (x_index-1, y_index, z_index)))
        index_set.add((x_index-1, y_index, z_index))
    if (x_index, y_index-1, z_index) not in index_set:
        heapq.heappush(que, (-(x_cake[x_index] + y_cake[y_index-1] + z_cake[z_index]), (x_index, y_index-1, z_index)))
        index_set.add((x_index, y_index-1, z_index))
    if (x_index, y_index, z_index-1) not in index_set:
        heapq.heappush(que, (-(x_cake[x_index] + y_cake[y_index] + z_cake[z_index-1]), (x_index, y_index, z_index-1)))
        index_set.add((x_index, y_index, z_index-1))

Submission status: Accepted

Solution code:
X, Y, Z, K = map(int, input().split())
a_cakes = list(map(int, input().split()))
b_cakes = list(map(int, input().split()))
c_cakes = list(map(int, input().split()))
a_b_cakes = []
for a in a_cakes:
  for b in b_cakes:
    a_b_cakes.append(a+b)
a_b_cakes.sort(reverse=True)
a_b_c_cakes = []
for a_b in a_b_cakes[:K]:
  for c in c_cakes:
    a_b_c_cakes.append(a_b+c)
a_b_c_cakes.sort(reverse=True)
for ans in a_b_c_cakes[:K]:
  print(ans)

Submission status: Accepted

Solution code:
class MaxHeap:
    def __init__(self):
        self.__pq = []
        self.__length = 0

    def __str__(self):
        return self.__pq.__str__()
    
    def length(self):
        return self.__length
    
    def push(self,item):
        self.__pq.append(item)
        self.__length += 1
        i = self.__length - 1
        while i > 0:
            p = (i-1)//2
            tmp = self.__pq[p]
            if tmp >= item:
                break
            self.__pq[p] = item
            self.__pq[i] = tmp
            i = p
    
    def popMax(self):
        if self.__length == 0:
            return None
        ret = self.__pq[0]
        if self.__length == 1:
            self.__length -= 1
            return self.__pq.pop(0)
        self.__pq[0] = self.__pq.pop(self.__length-1)
        self.__length -= 1
        i = 0
        while 2*i+1 < self.__length:
            larger = 2*i+1
            if 2*i+2 < self.__length:
                if self.__pq[2*i+2] > self.__pq[larger]:
                    larger = 2*i+2
            tmp = self.__pq[larger]
            if tmp <= self.__pq[i]:
                break
            self.__pq[larger] = self.__pq[i]
            self.__pq[i] = tmp
            i = larger
        return ret
    
    def isempty(self):
        return self.__length == 0
from collections import defaultdict
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)

PQ = MaxHeap()
used = defaultdict(bool)
PQ.push((A[0]+B[0]+C[0],0,0,0))
cnt = 0
arr = []
while cnt < K:
    s,i,j,k = PQ.popMax()
    if not used[(i,j,k)]:
        arr.append(s)
        used[(i,j,k)] = True
        if i+1 < X and not used[(i+1,j,k)]:
            PQ.push((A[i+1]+B[j]+C[k], i+1,j,k))
        if j+1 < Y and not used[(i,j+1,k)]:
            PQ.push((A[i]+B[j+1]+C[k], i,j+1,k))
        if k+1 < Z and not used[(i,j,k+1)]:
            PQ.push((A[i]+B[j]+C[k+1], i,j,k+1))
        cnt += 1
for i in range(K):
    print(arr[i])

Submission status: Accepted

Solution code:
from collections import deque

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

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)

ABCdeque = deque(ABC)

for _ in range(K):
    print(ABCdeque.popleft())

Submission status: Accepted

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
Mod = 1000000007




def main(): #startline--------------------------------------------
    X,Y,Z,K = map(int,input().split())
    A = list(map(int,input().split()))
    A.sort(reverse = True)
    A = A[:min(K,X)]
    B = list(map(int,input().split()))
    B.sort(reverse = True)
    B = B[:min(K,X)]
    C = list(map(int,input().split()))
    C.sort(reverse = True)
    C = C[:min(K,X)]
    Pre = []
    for i in range(min(X,K)):
        for j in range(min(Y,K)):
            Pre.append(A[i]+B[j])
    Pre.sort(reverse = True)
    Pre = Pre[:K]
    ans = []
    for i in range(min(Z,K)):
        for j in Pre:
            ans.append(j+C[i])
    ans.sort(reverse = True)
    for i in range(K):
        print(ans[i])



if __name__ == "__main__":
    main() #endline===============================================





def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

Submission status: Accepted

Solution code:
from heapq import heappop,heappush
X,Y,Z,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A.sort(reverse = True)
B.sort(reverse = True)
C.sort(reverse = True)
Heap = []
memo = [[0,0,0]]
heappush(Heap,[-1 * (A[0] + B[0] + C[0]),0,0,0])
for _ in range(K):
    S,k,l,m = heappop(Heap)
    print(-1 * S)
    if k < X - 1 and not [k + 1,l,m] in memo:
        heappush(Heap,[-1 * (A[k + 1] + B[l] + C[m]),k + 1,l,m])
        memo.append([k + 1,l,m])
    if l < X - 1 and not [k,l + 1,m] in memo:
        heappush(Heap,[-1 * (A[k] + B[l + 1] + C[m]),k,l + 1,m])
        memo.append([k,l + 1,m])
    if m < X - 1 and not [k,l,m + 1] in memo:
        heappush(Heap,[-1 * (A[k] + B[l] + C[m + 1]),k,l,m + 1])
        memo.append([k,l,m + 1])