Problem:


Score: 400 points

Problem Statement

The Patisserie AtCoder sells cakes with number-shaped candles.
There are X, Y and Z kinds of cakes with 1-shaped, 2-shaped and 3-shaped candles, respectively.
Each cake has an integer value called deliciousness, as follows:

The deliciousness of the cakes with 1-shaped candles are A_1, A_2, ..., A_X.

The deliciousness of the cakes with 2-shaped candles are B_1, B_2, ..., B_Y.

The deliciousness of the cakes with 3-shaped candles are C_1, C_2, ..., C_Z.

Takahashi decides to buy three cakes, one for each of the three shapes of the candles, to celebrate ABC 123.

There are X \times Y \times Z such ways to choose three cakes.

We will arrange these X \times Y \times Z ways in descending order of the sum of the deliciousness of the cakes.

Print the sums of the deliciousness of the cakes for the first, second, ..., K-th ways in this list.

Constraints

1 \leq X \leq 1 \ 000

1 \leq Y \leq 1 \ 000

1 \leq Z \leq 1 \ 000

1 \leq K \leq \min(3 \ 000, X \times Y \times Z)

1 \leq A_i \leq 10 \ 000 \ 000 \ 000

1 \leq B_i \leq 10 \ 000 \ 000 \ 000

1 \leq C_i \leq 10 \ 000 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z K
A_1 \ A_2 \ A_3 \ ... \ A_X
B_1 \ B_2 \ B_3 \ ... \ B_Y
C_1 \ C_2 \ C_3 \ ... \ C_Z

Output

Print K lines. The i-th line should contain the i-th value stated in the problem statement.

Sample Input 1

2 2 2 8
4 6
1 5
3 8

Sample Output 1

19
17
15
14
13
12
10
8

There are 2 \times 2 \times 2 = 8 ways to choose three cakes, as shown below in descending order of the sum of the deliciousness of the cakes:

(A_2, B_2, C_2): 6 + 5 + 8 = 19

(A_1, B_2, C_2): 4 + 5 + 8 = 17

(A_2, B_1, C_2): 6 + 1 + 8 = 15

(A_2, B_2, C_1): 6 + 5 + 3 = 14

(A_1, B_1, C_2): 4 + 1 + 8 = 13

(A_1, B_2, C_1): 4 + 5 + 3 = 12

(A_2, B_1, C_1): 6 + 1 + 3 = 10

(A_1, B_1, C_1): 4 + 1 + 3 = 8

Sample Input 2

3 3 3 5
1 10 100
2 20 200
1 10 100

Sample Output 2

400
310
310
301
301

There may be multiple combinations of cakes with the same sum of the deliciousness. For example, in this test case, the sum of A_1, B_3, C_3 and the sum of A_3, B_3, C_1 are both 301.
However, they are different ways of choosing cakes, so 301 occurs twice in the output.

Sample Input 3

10 10 10 20
7467038376 5724769290 292794712 2843504496 3381970101 8402252870 249131806 6310293640 6690322794 6082257488
1873977926 2576529623 1144842195 1379118507 6003234687 4925540914 3902539811 3326692703 484657758 2877436338
4975681328 8974383988 2882263257 7690203955 514305523 6679823484 4263279310 585966808 3752282379 620585736

Sample Output 3

23379871545
22444657051
22302177772
22095691512
21667941469
21366963278
21287912315
21279176669
21160477018
21085311041
21059876163
21017997739
20703329561
20702387965
20590247696
20383761436
20343962175
20254073196
20210218542
20150096547

Note that the input or output may not fit into a 32-bit integer type.



Sample input:
2 2 2 8
4 6
1 5
3 8


Sample output:
19
17
15
14
13
12
10
8

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    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()))
    S=[]
    T=[]
    from itertools import product
    for a,b in product(A,B):
        S.append(a+b)
    S.sort(reverse=1)
    S=S[:k]
    for s,c in product(S,C):
        T.append(s+c)
    T.sort(reverse=1)
    for i in range(k):
        print(T[i])
resolve()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines
#rstrip().decode('utf-8')

#import math
import numpy as np
#import operator
#import bisect
#from heapq import heapify,heappop,heappush
#from math import gcd
#from fractions import gcd
#from collections import deque
#from collections import defaultdict
#from collections import Counter
#from itertools import accumulate
#from itertools import groupby
#from itertools import permutations
#from itertools import combinations
#from scipy.sparse import csr_matrix
#from scipy.sparse.csgraph import floyd_warshall
#from scipy.sparse.csgraph import csgraph_from_dense
#from scipy.sparse.csgraph import dijkstra
#sys.setrecursionlimit(10**7)

#map(int,input().split())

def main():
	x,y,z,k=map(int,input().split())
	ABC=np.array(read().split(),np.int64)
	A=ABC[:x]
	B=ABC[x:-z]
	C=ABC[-z:]
	AB=np.add.outer(A,B)
	AB=AB.ravel()
	AB.sort()
	AB=AB[-k:]
	
	ans=np.add.outer(AB,C)
	ans=ans.ravel()
	ans.sort()
	
	for i in range(k):
		print(ans[-i-1])
	
if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines
#rstrip().decode('utf-8')

#import math
import numpy as np
#import operator
#import bisect
#from heapq import heapify,heappop,heappush
#from math import gcd
#from fractions import gcd
#from collections import deque
#from collections import defaultdict
#from collections import Counter
#from itertools import accumulate
#from itertools import groupby
#from itertools import permutations
#from itertools import combinations
#from scipy.sparse import csr_matrix
#from scipy.sparse.csgraph import floyd_warshall
#from scipy.sparse.csgraph import csgraph_from_dense
#from scipy.sparse.csgraph import dijkstra
#sys.setrecursionlimit(10**7)

#map(int,input().split())

def main():
	x,y,z,k=map(int,input().split())
	ABC=np.array(read().split(),np.int64)
	A=ABC[:x]
	B=ABC[x:-z]
	C=ABC[-z:]
	AB=np.add.outer(A,B)
	AB=AB.ravel()
	AB.sort()
	AB=AB[-3000:]
	
	ans=np.add.outer(AB,C)
	ans=ans.ravel()
	ans.sort()
	
	for i in range(k):
		print(ans[-i-1])
	
if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
from heapq import nlargest
x,y,z,k=map(int,input().split())
A,B,C=[list(map(int,input().split()))for _ in[0]*3]
D=nlargest(k,(a+b for a in A for b in B))
E=nlargest(k,(d+c for d in D for c in C))
for i in E:
    print(i)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

x,y,z,k = inpl()
a = inpl()
b = inpl()
c = inpl()
a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
q = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
heapify(q)
old = set((0,0,0))
for i in range(k):
    k,l,m,n = heappop(q)
    print(-k)
    if l + 1 < x and not (l+1,m,n) in old:
        heappush(q,[-(a[l+1] + b[m] + c[n]), l+1, m, n])
        old.add((l+1,m,n))
    if m + 1 < y and not(l,m+1,n) in old:
        heappush(q,[-(a[l] + b[m+1] + c[n]), l, m+1, n])
        old.add((l,m+1,n))
    if n + 1 < z and not (l,m,n+1)  in old:
        heappush(q,[-(a[l] + b[m] + c[n+1]), l, m, n+1])
        old.add((l,m,n+1))
    # print(q)

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)
queue = []
heapq.heappush(queue, (-(a[0] + b[0] + c[0]), 0, 0, 0))
already = set([0, 0, 0])
ans = []
for _ in range(k):
    val, p, q, r = heapq.heappop(queue)
    ans.append(-val)
    if ((p + 1, q, r) not in already) and p + 1 < x:
        heapq.heappush(queue, (-(a[p+1] + b[q] + c[r]), p+1, q, r))
        already.add((p+1, q, r))
    if ((p, q+1, r) not in already) and q + 1 < y:
        heapq.heappush(queue, (-(a[p] + b[q+1] + c[r]), p, q+1, r))
        already.add((p, q+1, r))
    if ((p, q, r+1) not in already) and r + 1 < z:
        heapq.heappush(queue, (-(a[p] + b[q] + c[r+1]), p, q, r+1))
        already.add((p, q, r+1))
for i in ans:
    print(i)

Submission status: Accepted

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

list_ab = []
list_ab_c = []

for i in list1[0]:
	for j in list1[1]:
		list_ab.append(i+j)
list_ab.sort(reverse=True)
for i in list_ab[:3000]:
	for j in list1[2]:
		list_ab_c.append(i+j)

list_ab_c.sort(reverse=True)

for i in range(k):
	print(list_ab_c[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)
a = []; b = []; c = []

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


answer = []
for i, j, k in candidate:
    answer.append(A[i - 1] + B[j - 1] + C[k - 1])

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

Submission status: Accepted

Solution code:
# D - Cake 123

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)

d = []
heapq.heappush(d, (-(A[0]+B[0]+C[0]), 0, 0, 0)) # heapqにタプルを与えると最初の要素でソートする
ijk_set = set((0,0,0))

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

Submission status: Accepted

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

def main():
    X, Y, Z, K = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]
    B = [int(x) for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = []
    for a in A:
        for b in B:
            ans.append(a + b)

    ans_s = sorted(ans, reverse=True)

    ans2 = []
    for a in ans_s[:3000]:
        for c in C:
            ans2.append(a + c)

    ans2_s = sorted(ans2, reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import collections
import heapq


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    x, y, z, k = list(map(int, input().rstrip('\n').split()))
    a = list(map(int, input().rstrip('\n').split()))
    b = list(map(int, input().rstrip('\n').split()))
    c = list(map(int, input().rstrip('\n').split()))
    a.sort(reverse=True)
    b.sort(reverse=True)
    c.sort(reverse=True)
    ql = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
    heapq.heapify(ql)
    fq = collections.defaultdict(int)
    fq[0, 0, 0]
    for i in range(k):
        s, ap, bp, cp = heapq.heappop(ql)
        for app, bpp, cpp in ([[ap+1, bp, cp], [ap, bp+1, cp], [ap, bp, cp+1]]):
            if app < x and bpp < y and cpp < z:
                if (app, bpp, cpp) not in fq:
                    heapq.heappush(ql, [-(a[app] + b[bpp] + c[cpp]), app, bpp, cpp])
                    fq[app, bpp, cpp]
        print(-s)


if __name__ == '__main__':
    solve()

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)

used = set()
h = []
heappush(h, (-A[0]-B[0]-C[0], 0, 0, 0))
result = []
used.add((0, 0, 0))

for _ in range(K):
    sum, i, j, k = heappop(h)
    result.append(-sum)
    if i < X-1 and (i+1, j, k) not in used:
        heappush(h, (-A[i+1]-B[j]-C[k], i+1, j, k))
        used.add((i+1, j, k))
    if j < Y-1 and (i, j+1, k) not in used:
        heappush(h, (-A[i]-B[j+1]-C[k], i, j+1, k))
        used.add((i, j+1, k))
    if k < Z-1 and (i, j, k+1) not in used:
        heappush(h, (-A[i]-B[j]-C[k+1], i, j, k+1))
        used.add((i, j, k+1))

for v in result:
    print(v)

Submission status: Accepted

Solution code:
x,y,z,k=map(int,input().split())
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
c=[int(x) for x in input().split()]

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

s=list()

for l in range(x):
  if l>k:
    break 
  for m in range(y):
    if l*m>k:
      break
    for n in range(z):
      if l*m*n>k:
        break
      s.append(a[l]+b[m]+c[n])
 
s.sort(reverse=True)
for i in range(k):
  print(s[i])

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

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

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

que = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
V.add((0, 0, 0))
ans = []
for _ in range(K):
    val, a, b, c = heappop(que)
    ans.append(-val)

    for x, y, z in [(0, 0, 1), (0, 1, 0), (1, 0, 0)]:
        x = min(a + x, X - 1)
        y = min(b + y, Y - 1)
        z = min(c + z, Z - 1)
        if not (x, y, z) in V:
            heappush(que, (-(A[x] + B[y] + C[z]), x, y, z))
            V.add((x, y, z))

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

Submission status: Accepted

Solution code:
X,Y,Z,K = map(int,input().split())
l_a = list(map(int,input().split()))
l_b = list(map(int,input().split()))
l_c = list(map(int,input().split()))
ll = []
li = []
for i in l_a:
    for m in l_b:
        ll.append(i+m)
ll.sort(reverse = True)

del ll[K+1:]
for c in l_c:
    for i in ll:
        li.append(c+i)
li.sort(reverse = True)
for i in range(K):
    print(li[i])

Submission status: Accepted

Solution code:
X, Y, Z, K = [int(x) for x in input().split()]
A = sorted([int(x) for x in input().split()], reverse=True)
B = sorted([int(x) for x in input().split()], reverse=True)
C = sorted([int(x) for x in input().split()], reverse=True)
lst = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if ((i+1)*(j+1)*(k+1)<=K):
                lst.append(A[i]+B[j]+C[k])
            else:
                break
for l in sorted(lst, reverse=True)[:K]:
    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.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
K = []
flag = False

for i in range(x):
    for j in range(y):
        for l in range(z):
            heapq.heappush(K,a[i]+b[j]+c[l])
            if len(K)>k:
                mn = heapq.heappop(K)
                if a[i]+b[j]+c[l] == mn:
                    break
        if l == 0:
            break
    if j ==0:
        break
K.sort(reverse= True)
for i in range(k):
    print(K[i])

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)

h = []
heappush(h, (-A[0]-B[0]-C[0], 0, 0, 0))
used = set()
result = []

for _ in range(K):
    sum, i, j, k = heappop(h)
    result.append(-sum)
    if i < X-1 and (i+1, j, k) not in used:
        heappush(h, (-A[i+1]-B[j]-C[k], i+1, j, k))
        used.add((i+1, j, k))
    if j < Y-1 and (i, j+1, k) not in used:
        heappush(h, (-A[i]-B[j+1]-C[k], i, j+1, k))
        used.add((i, j+1, k))
    if k < Z-1 and (i, j, k+1) not in used:
        heappush(h, (-A[i]-B[j]-C[k+1], i, j, k+1))
        used.add((i, j, k+1))

for v in result:
    print(v)

Submission status: Accepted

Solution code:
from heapq import heappop, heappush

X, Y, Z, K = map(int, input().split())
A = [int(x) for x in  input().split()]
B = [int(x) for x in  input().split()]
C = [int(x) for x in  input().split()]

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

h = []
heappush(h, (-A[0]-B[0]-C[0], 0, 0, 0))
used = set()
result = []

for _ in range(K):
    sum, i, j, k = heappop(h)
    result.append(-sum)
    if i < X-1 and (i+1, j, k) not in used:
        heappush(h, (-A[i+1]-B[j]-C[k], i+1, j, k))
        used.add((i+1, j, k))
    if j < Y-1 and (i, j+1, k) not in used:
        heappush(h, (-A[i]-B[j+1]-C[k], i, j+1, k))
        used.add((i, j+1, k))
    if k < Z-1 and (i, j, k+1) not in used:
        heappush(h, (-A[i]-B[j]-C[k+1], i, j, k+1))
        used.add((i, j, k+1))

for v in result:
    print(v)

Submission status: Accepted

Solution code:
import numpy as np
from heapq import *

X, Y, Z, K = map(int, input().split())
A = [int(x) for x in  input().split()]
B = [int(x) for x in  input().split()]
C = [int(x) for x in  input().split()]

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

h = []
heappush(h, (-A[0]-B[0]-C[0], 0, 0, 0))
used = set()
result = []

for _ in range(K):
    sum, i, j, k = heappop(h)
    result.append(-sum)
    if i < X-1 and (i+1, j, k) not in used:
        heappush(h, (-A[i+1]-B[j]-C[k], i+1, j, k))
        used.add((i+1, j, k))
    if j < Y-1 and (i, j+1, k) not in used:
        heappush(h, (-A[i]-B[j+1]-C[k], i, j+1, k))
        used.add((i, j+1, k))
    if k < Z-1 and (i, j, k+1) not in used:
        heappush(h, (-A[i]-B[j]-C[k+1], i, j, k+1))
        used.add((i, j, k+1))

for v in result:
    print(v)

Submission status: Accepted

Solution code:
import numpy as np
from heapq 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)

h = []
heappush(h, (-A[0]-B[0]-C[0], 0, 0, 0))
used = set()
result = []

for _ in range(K):
    sum, i, j, k = heappop(h)
    result.append(-sum)
    if i < X-1 and (i+1, j, k) not in used:
        heappush(h, (-A[i+1]-B[j]-C[k], i+1, j, k))
        used.add((i+1, j, k))
    if j < Y-1 and (i, j+1, k) not in used:
        heappush(h, (-A[i]-B[j+1]-C[k], i, j+1, k))
        used.add((i, j+1, k))
    if k < Z-1 and (i, j, k+1) not in used:
        heappush(h, (-A[i]-B[j]-C[k+1], i, j, k+1))
        used.add((i, j, k+1))

for v in result:
    print(v)

Submission status: Accepted

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

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

import heapq

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

Submission status: Accepted

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

A=(sorted(A,reverse=True))[:K]
B=(sorted(B,reverse=True))[:K]

AB=[]
for Ai in A:
    for Bj in B:
        AB.append(Ai+Bj)
AB=(sorted(AB,reverse=True))[:K]

C=(sorted(C,reverse=True))[:K]

ABC=[]
for ABi in AB:
    for Cj in C:
        ABC.append(ABi+Cj)
ABC=(sorted(ABC,reverse=True))[:K]

for ABCi in ABC:
    print(ABCi)

Submission status: Accepted

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

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

R  = [sum([A[0], B[0], C[0]])]
Q  = {(0, 0, 1), (0, 1, 0), (1, 0, 0)}
D  = {(0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0)}
iS = [((a, b, c), sum([A[a], B[b], C[c]])) for a, b, c in Q if (a < X) and (b < Y) and (c < Z)]
iS = sorted(iS, key=lambda x: [x[1], x[0]], reverse=True)

while len(R) < K:
    R.append(iS[0][1])
    abc     = iS[0][0]
    a, b, c = abc[0], abc[1], abc[2]
    iS      = iS[1:]
    if a + 1 < X:
        if not (a + 1, b, c) in D:
            s = sum([A[a + 1], B[b], C[c]])
            is_done = False
            for i in range(len(iS)):
                if iS[i][1] < s:
                    iS = iS[:i] + [((a + 1, b, c), s)] + iS[i:]
                    is_done = True
                    break
            if is_done == False:
                iS = iS + [((a + 1, b, c), s)]
            D.add((a + 1, b, c))
    if b + 1 < Y:
        if not (a, b + 1, c) in D:
            s = sum([A[a], B[b + 1], C[c]])
            is_done = False
            for i in range(len(iS)):
                if iS[i][1] < s:
                    iS = iS[:i] + [((a, b + 1, c), s)] + iS[i:]
                    is_done = True
                    break
            if is_done == False:
                iS = iS + [((a, b + 1, c), s)]
            D.add((a, b + 1, c))
    if c + 1 < Z:
        if not (a, b, c + 1) in D:
            s = sum([A[a], B[b], C[c + 1]])
            is_done = False
            for i in range(len(iS)):
                if iS[i][1] < s:
                    iS = iS[:i] + [((a, b, c + 1), s)] + iS[i:]
                    is_done = True
                    break
            if is_done == False:
                iS = iS + [((a, b, c + 1), s)]
            D.add((a, b, c + 1))

for r in R:
    print(r)

Submission status: Accepted

Solution code:
import heapq as hq

x,y,z,k = map(int,input().split())
a = list(map(lambda x:int(x) * (-1),input().split()))
b =list(map(lambda x:int(x) * (-1),input().split()))
c = list(map(lambda x:int(x) * (-1),input().split()))
a.sort()
b.sort()
c.sort()
possibles = [(a[0]+b[0]+c[0],0,0,0)]

done = set()
done |= {(0,0,0)}
def neibor(ai,bi,ci,possibles,done):
    for aj,bj,cj in [(ai+1,bi,ci),(ai,bi+1,ci),(ai,bi,ci+1)]:
        if (aj<=x-1) and (bj<=y-1) and (cj<=z-1) and (not (aj,bj,cj) in done):
            hq.heappush(possibles,(a[aj]+b[bj]+c[cj],aj,bj,cj))
            done  |= {(aj,bj,cj)}
    

def conf(possibles,done):
    yam,ai,bi,ci = hq.heappop(possibles)
    print(-1*yam)
    neibor(ai,bi,ci,possibles,done)

for i in range(k):
    yam,ai,bi,ci = hq.heappop(possibles)
    print(-1*yam)
    for aj,bj,cj in [(ai+1,bi,ci),(ai,bi+1,ci),(ai,bi,ci+1)]:
        if (aj<=x-1) and (bj<=y-1) and (cj<=z-1) and (not (aj,bj,cj) in done):
            hq.heappush(possibles,(a[aj]+b[bj]+c[cj],aj,bj,cj))
            done  |= {(aj,bj,cj)}

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()))
ab_value = []
value = []
flag = False

for a in A:
    for b in B:
        ab_value.append(a + b)

ab_value.sort(reverse = True)
ab_value = ab_value[:K + 1]
C.sort(reverse = True)
i = 0
for k in ab_value:
    for c in C:
        value.append(c + k)
value.sort(reverse= True)
for i in range(K):
    print(value[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()))

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)

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

Submission status: Accepted

Solution code:
from sys import stdin
import heapq

X, Y, Z, K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]
C = [int(x) for x in stdin.readline().rstrip().split()]

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

heap = []
heapq.heappush(heap, (-(A[0] + B[0] + C[0]), 0, 0, 0))
s = {(0, 0, 0)}
for _ in range(K):
    v, i, j, k = heapq.heappop(heap)
    if i + 1 < X and (i + 1, j, k) not in s:
        s.add((i + 1, j, k))
        heapq.heappush(heap, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
    if j + 1 < Y and (i, j + 1, k) not in s:
        s.add((i, j + 1, k))
        heapq.heappush(heap, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
    if k + 1 < Z and (i, j, k + 1) not in s:
        s.add((i, j, k + 1))
        heapq.heappush(heap, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
    print(-v)

Submission status: Accepted

Solution code:
import heapq
X,Y,Z,K=map(int,input().split())
lst1=sorted(list(map(int,input().split())))
lst2=sorted(list(map(int,input().split())))
lst3=sorted(list(map(int,input().split())))
ans=[]
heapq.heappush(ans,[-(lst1[-1]+lst2[-1]+lst3[-1]),1,1,1])
tmp=0
while tmp!=K:
  H=heapq.heappop(ans)
  V=-1*H[0]
  tmp+=1
  a,b,c=-H[1],-H[2],-H[3]
  print(V)
  if a-1>=-X and [-(lst1[a-1]+lst2[b]+lst3[c]),-a+1,-b,-c] not in ans:
    heapq.heappush(ans,[-(lst1[a-1]+lst2[b]+lst3[c]),-a+1,-b,-c])
  if b-1>=-Y and [-(lst1[a]+lst2[b-1]+lst3[c]),-a,-b+1,-c] not in ans :
    heapq.heappush(ans,[-(lst1[a]+lst2[b-1]+lst3[c]),-a,-b+1,-c])
  if c-1>=-Z and [-(lst1[a]+lst2[b]+lst3[c-1]),-a,-b,-c+1] not in ans:
    heapq.heappush(ans,[-(lst1[a]+lst2[b]+lst3[c-1]),-a,-b,-c+1])

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)
inf = float("INF")
A.append(-inf)
B.append(-inf)
C.append(-inf)
s = []
Q = []
heappush(Q,[-(A[0]+B[0]+C[0]),0,0,0])
for _ in range(K):
    tmp = heappop(Q)
    print(-tmp[0])
    i = tmp[1]
    j = tmp[2]
    k = tmp[3]
    if [i+1,j,k] not in s:
        heappush(Q,[-(A[i+1]+B[j]+C[k]),i+1,j,k])
        s.append([i+1,j,k])
    if [i,j+1,k] not in s:
        heappush(Q,[-(A[i]+B[j+1]+C[k]),i,j+1,k])
        s.append([i,j+1,k])
    if [i,j,k+1] not in s:
        heappush(Q,[-(A[i]+B[j]+C[k+1]),i,j,k+1])
        s.append([i,j,k+1])

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)

seen=[(0,0,0)]
Q=[]
heapq.heappush(Q,[-(A[0]+B[0]+C[0]),0,0,0])
for _ in range(K):
  h=heapq.heappop(Q)
  i,j,k=h[1],h[2],h[3]
  print(-h[0])
  if i+1<X:
    if (i+1,j,k) not in seen:
      heapq.heappush(Q,[-(A[i+1]+B[j]+C[k]),i+1,j,k])
      seen.append((i+1,j,k))
  if j+1<Y:
    if (i,j+1,k) not in seen:
      heapq.heappush(Q,[-(A[i]+B[j+1]+C[k]),i,j+1,k])
      seen.append((i,j+1,k))
  if k+1<Z:
    if (i,j,k+1) not in seen:
      heapq.heappush(Q,[-(A[i]+B[j]+C[k+1]),i,j,k+1])
      seen.append((i,j,k+1))

Submission status: Accepted

Solution code:
import collections
import heapq
 
x, y, z, k = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(3)]
for i in range(3):
    ls[i].sort(reverse=True)
q = [(-sum([ls[i][0] for i in range(3)]), 0, 0, 0)]
res = []
used = collections.defaultdict(lambda: False)
while len(res) < k:
    s, i, j, l = heapq.heappop(q)
    res.append(-s)
    if i + 1 < x and not used[(i + 1, j, l)]:
        heapq.heappush(q, (-(ls[0][i + 1] + ls[1][j] + ls[2][l]), i + 1, j, l))
        used[(i + 1, j, l)] = True
    if j + 1 < y and not used[(i, j + 1, l)]:
        heapq.heappush(q, (-(ls[0][i] + ls[1][j + 1] + ls[2][l]), i, j + 1, l))
        used[(i, j + 1, l)] = True
    if l + 1 < z and not used[(i, j, l + 1)]:
        heapq.heappush(q, (-(ls[0][i] + ls[1][j] + ls[2][l + 1]), i, j, l + 1))
        used[(i, j, l + 1)] = True
print(*res, sep='\n')

Submission status: Accepted

Solution code:
import heapq

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

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

Submission status: Accepted

Solution code:
x,y,z,k = map(int,input().split())
x_list = list(map(int,input().split()))
y_list = list(map(int,input().split()))
z_list = list(map(int,input().split()))

ans_list = []

x_list.append(-99999999999)
y_list.append(-99999999999)
z_list.append(-99999999999)

l,m,n = 0,0,0

x_list.sort(reverse=True)
y_list.sort(reverse=True)
z_list.sort(reverse=True)

x1 = x_list[0]
y1 = y_list[0]
z1 = z_list[0]

while (l+1)*(m+1)*(n+1) < 100000 and (l<x and m<y and n<z):
    if x1-x_list[l+1] <= y1-y_list[m+1] and x1-x_list[l+1] <= z1-z_list[n+1]:
        l += 1
    elif y1-y_list[m+1] <= x1-x_list[l+1] and y1-y_list[m+1] <= z1-z_list[n+1]:
        m += 1
    elif z1-z_list[n+1] <= x1-x_list[l+1] and z1-z_list[n+1] <= y1-y_list[m+1]:
        n += 1
    else:
        print(ans_list[9999999])
        break
#print(l,m,n)
for p in range(l+1):
    for q in range(m+1):
        for r in range(n+1):
            ans_list.append(x_list[p]+y_list[q]+z_list[r])

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

Submission status: Accepted

Solution code:
x, y, z, k = map(int, input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))
A_B_list = []
for i in range(x):
    for j in range(y):
        A_B = A_list[i] + B_list[j]
        A_B_list.append(A_B)
A_B_ranking = sorted(A_B_list, reverse=True)

A_B_C_list = []
for i in range(min(k, x*y)):
    for j in range(z):
        A_B_C = A_B_ranking[i] + C_list[j]
        A_B_C_list.append(A_B_C)

A_B_C_ranking = sorted(A_B_C_list, reverse=True)
for i in range(k):
    print(A_B_C_ranking[i])

Submission status: Accepted

Solution code:
import collections
import heapq

x, y, z, k = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(3)]
for i in range(3):
    ls[i].sort(reverse=True)
q = [(-sum([ls[i][0] for i in range(3)]), 0, 0, 0)]
res = []
used = collections.defaultdict(lambda: False)
while len(res) < k:
    s, i, j, l = heapq.heappop(q)
    res.append(-s)
    index_list = [
        (i + 1, j, l),
        (i, j + 1, l),
        (i, j, l + 1),
    ]
    for i, (indices, limit) in enumerate(zip(index_list, [x, y, z])):
        if indices[i] < limit and not used[indices]:
            ss = sum([ls[j][l] for j, l in zip(range(3), indices)])
            heapq.heappush(q, (-ss, indices[0], indices[1], indices[2]))
            used[indices] = True
print(*res, sep='\n')