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 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())
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 = [(0, 0, 0)]
while len(res) < k:
    s, i, j, l = heapq.heappop(q)
    res.append(-s)
    if i + 1 < x and (i + 1, j, l) not in used:
        heapq.heappush(q, (-(ls[0][i + 1] + ls[1][j] + ls[2][l]), i + 1, j, l))
        used.append((i + 1, j, l))
    if j + 1 < y and (i, j + 1, l) not in used:
        heapq.heappush(q, (-(ls[0][i] + ls[1][j + 1] + ls[2][l]), i, j + 1, l))
        used.append((i, j + 1, l))
    if l + 1 < z and (i, j, l + 1) not in used:
        heapq.heappush(q, (-(ls[0][i] + ls[1][j] + ls[2][l + 1]), i, j, l + 1))
        used.append((i, j, l + 1))
print(*res, sep='\n')

Submission status: Accepted

Solution code:
import heapq


def select(ls0, ls1, k):
    ls = []
    for a in ls0:
        for b in ls1:
            heapq.heappush(ls, -(a + b))
    return [-heapq.heappop(ls) for _ in range(min(k, len(ls0) * len(ls1)))]


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)
ps = select(ls[0], ls[1], k)
res = select(ps, ls[2], k)
print(*res, sep='\n')

Submission status: Accepted

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

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

    D.sort(reverse=True)
    E = []
    for i in range(min(len(D),K)):
        for c in C:
            E.append(D[i] + c)

    E.sort(reverse=True)

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

main()

Submission status: Accepted

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

    m = set((0, 0, 0))
    q = [[-1*(a[0]+b[0]+c[0]), 0, 0, 0]]
    heapq.heapify(q)
    for _ in range(k):
        ans, ai, bi, ci = heapq.heappop(q)
        if ai+1<x and (ai+1, bi, ci) not in m:
            m.add((ai+1, bi, ci))
            heapq.heappush(q, [-1*(a[ai+1]+b[bi]+c[ci]), ai+1, bi, ci])
        if bi+1<y and (ai, bi+1, ci) not in m:
            m.add((ai, bi+1, ci))
            heapq.heappush(q, [-1*(a[ai]+b[bi+1]+c[ci]), ai, bi+1, ci])
        if ci+1<z and (ai, bi, ci+1) not in m:
            m.add((ai, bi, ci+1))
            heapq.heappush(q, [-1*(a[ai]+b[bi]+c[ci+1]), ai, bi, ci+1])
        print(-1*ans)

if __name__ == "__main__":
    f_d()

Submission status: Accepted

Solution code:
import heapq


def solve():
    X, Y, Z, K = map(int, input().split())
    a_ls = sorted(map(lambda x: -int(x), input().split()))
    b_ls = sorted(map(lambda x: -int(x), input().split()))
    c_ls = sorted(map(lambda x: -int(x), input().split()))
    hq = []
    heapq.heappush(hq, (a_ls[0]+b_ls[0]+c_ls[0], 0, 0, 0))
    hq_hash = {(0, 0, 0)}
    
    for _ in range(K):
        ans, i, j, k = heapq.heappop(hq)
        
        if i+1 < X and not((i+1, j, k) in hq_hash):
            hq_hash.add((i+1, j, k))
            heapq.heappush(hq, (a_ls[i+1]+b_ls[j]+c_ls[k], i+1, j, k))
        if j+1 < Y and not((i, j+1, k) in hq_hash):
            hq_hash.add((i, j+1, k))
            heapq.heappush(hq, (a_ls[i]+b_ls[j+1]+c_ls[k], i, j+1, k))
        if k+1 < Z and not((i, j, k+1) in hq_hash):
            hq_hash.add((i, j, k+1))
            heapq.heappush(hq, (a_ls[i]+b_ls[j]+c_ls[k+1], i, j, k+1))
        
        print(-ans)


if __name__ == '__main__':
    solve()

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

for l in [A, B, C]:
    l.sort()

exists = set()
q = [(-(A[X - 1] + B[Y - 1] + C[Z - 1]), (X - 1, Y - 1, Z - 1))]
for _ in range(K):
    v, n = heappop(q)
    print(-v)
    for i in range(3):
        if n[i] > 0:
            t = [n[j] for j in range(3)]
            t[i] -= 1
            nn = tuple(t)
            if nn not in exists:
                exists.add(nn)
                nv = A[nn[0]] + B[nn[1]] + C[nn[2]]
                heappush(q, (-nv, nn))

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)

exists = set()
q = [(-(A[0] + B[0] + C[0]), (0, 0, 0))]
for _ in range(K):
    v, i = heappop(q)
    print(-v)
    if i[0] < X - 1:
        ni = (i[0] + 1, i[1], i[2])
        if ni not in exists:
            exists.add(ni)
            nv = A[i[0] + 1] + B[i[1]] + C[i[2]]
            heappush(q, (-nv, ni))
    if i[1] < Y - 1:
        ni = (i[0], i[1] + 1, i[2])
        if ni not in exists:
            exists.add(ni)
            nv = A[i[0]] + B[i[1] + 1] + C[i[2]]
            heappush(q, (-nv, ni))
    if i[2] < Z - 1:
        ni = (i[0], i[1], i[2] + 1)
        if ni not in exists:
            exists.add(ni)
            nv = A[i[0]] + B[i[1]] + C[i[2] + 1]
            heappush(q, (-nv, ni))

Submission status: Accepted

Solution code:
#D
from heapq import heappush,heappop,heapify
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)
d=[]
for i in range(x):
    for j in range(y):
        if i*j>k:
            break
        for l in range(z):
            if i*j*l>k:
                break
            heappush(d,-a[i]-b[j]-c[l])
for i in range(k):
    print(-heappop(d))

Submission status: Accepted

Solution code:
#D
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()))
d=[]
for i in range(x):
    for j in range(y):
        heappush(d,-a[i]-b[j])
e=[]
for i in range(min(3000,x*y)):
    e.append(-heappop(d))
f=[]
for i in range(len(e)):
    for j in range(z):
        heappush(f,-e[i]-c[j])
for i in range(k):
    print(-heappop(f))

Submission status: Accepted

Solution code:
import heapq

def main():
    X, Y, Z, K = map(int,input().split())
    A = [int(o) for o in input().split()]
    B = [int(o) for o in input().split()]
    C = [int(o) for o in input().split()]
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    A_size = len(A)
    B_size = len(B)
    C_size = len(C)
    heap = []
    heapq.heappush(heap,(-(A[0]+B[0]+C[0]),0,0,0))
    for i in range(K):
        val,a,b,c = heapq.heappop(heap)
        if a < (A_size-1) and (-(A[a+1]+B[b]+C[c]),a+1,b,c) not in heap:
            heapq.heappush(heap,(-(A[a+1]+B[b]+C[c]),a+1,b,c))
        if b < (B_size-1) and (-(A[a]+B[b+1]+C[c]),a,b+1,c) not in heap:
            heapq.heappush(heap,(-(A[a]+B[b+1]+C[c]),a,b+1,c))
        if c < (C_size-1) and (-(A[a]+B[b]+C[c+1]),a,b,c+1) not in heap:
            heapq.heappush(heap,(-(A[a]+B[b]+C[c+1]),a,b,c+1))
        print(-val)
if __name__ == "__main__" :
    main()

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

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

hq = []
heappush(hq, (-(A[0] + B[0] + C[0]), 0, 0, 0))
for _ in range(K):
    ans, i, j, k = heappop(hq)
    print(-ans)
 
    if i+1 < X:
        t = (-(A[i+1] + B[j] + C[k]), i+1, j, k)
        if t not in hq:
            heappush(hq, t)
 
    if j+1 < Y:
        t = (-(A[i] + B[j+1] + C[k]), i, j+1, k)
        if t not in hq:
            heappush(hq, t)
    
    if k+1 < Z:
        t = (-(A[i] + B[j] + C[k+1]), i, j, k+1)
        if t not in hq:
            heappush(hq, t)

Submission status: Accepted

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

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

li=[]
heapq.heappush(li,(-a[0]-b[0]-c[0],0,0,0))

ans=[]
used=set()

for _ in range(k):
    value,i,j,l=heapq.heappop(li)
    ans.append(-value)
    
    if i<x-1 and (i+1,j,l) not in used:
        heapq.heappush(li,(-a[i+1]-b[j]-c[l],i+1,j,l))
        used.add((i+1,j,l))
    if j<y-1 and (i,j+1,l) not in used:
        heapq.heappush(li,(-a[i]-b[j+1]-c[l],i,j+1,l))
        used.add((i,j+1,l))
    if l<z-1 and (i,j,l+1) not in used:
        heapq.heappush(li,(-a[i]-b[j]-c[l+1],i,j,l+1))
        used.add((i,j,l+1))
        
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
import heapq
x,y,z,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
c=[int(i) for i in input().split()]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

li=[]
push=heapq.heappush
pop=heapq.heappop

push(li,(-a[0]-b[0]-c[0],0,0,0))
ans=[]
used=set()

for _ in range(k):
    value,i,j,l=pop(li)
    ans.append(-value)
    if i<x-1 and (i+1,j,l) not in used:
        push(li,(-a[i+1]-b[j]-c[l],i+1,j,l))
        used.add((i+1,j,l))
    if j<y-1 and (i,j+1,l) not in used:
        push(li,(-a[i]-b[j+1]-c[l],i,j+1,l))
        used.add((i,j+1,l))
    if l<z-1 and (i,j,l+1) not in used:
        push(li,(-a[i]-b[j]-c[l+1],i,j,l+1))
        used.add((i,j,l+1))
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

# input
X,Y,Z,K = map(int, input().split())
a_list = [int(i) for i in input().split()]
b_list = [int(i) for i in input().split()]
c_list = [int(i) for i in input().split()]
a_list.sort(reverse = True)
b_list.sort(reverse = True)
c_list.sort(reverse = True)

#solve
hq = []
arg_hash = {}
val = a_list[0] + b_list[0] + c_list[0]
heappush(hq, (-val,0,0,0))

for i in range(K):
    ans, a, b, c = heappop(hq)
    print(-ans)

    arg_a = (a+1, b, c)
    arg_b = (a, b+1, c)
    arg_c = (a, b, c+1)

    if a < X-1 and arg_a not in arg_hash:
        arg_hash[arg_a] = 1
        heappush(hq, (-(a_list[a+1]+b_list[b]+c_list[c]),)+arg_a)
    if b < Y-1 and arg_b not in arg_hash:
        arg_hash[arg_b] = 1
        heappush(hq, (-(a_list[a]+b_list[b+1]+c_list[c]),)+arg_b)
    if c < Z-1 and arg_c not in arg_hash:
        arg_hash[arg_c] = 1
        heappush(hq, (-(a_list[a]+b_list[b]+c_list[c+1]),)+arg_c)

Submission status: Accepted

Solution code:
import heapq


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)

sum =[]
heapq.heapify(sum)

arg_hash = {}
heapq.heappush(sum,((Alist[0]+Blist[0]+Clist[0])*(-1),(0,0,0)))

for i in range(0,K):
    ans,(a,b,c) = heapq.heappop(sum)
    print(-ans)

    arg_a = a+1,b,c
    arg_b = a,b+1,c
    arg_c = a,b,c+1

    if a < X-1 and arg_a not in arg_hash:
        heapq.heappush(sum,((Alist[a+1]+Blist[b]+Clist[c])*(-1),arg_a))
        arg_hash[arg_a]=1
    if b < Y-1 and arg_b not in arg_hash:
        heapq.heappush(sum,((Alist[a]+Blist[b+1]+Clist[c])*(-1),arg_b))
        arg_hash[arg_b]=1
    if c < Z - 1 and arg_c not in arg_hash:
        heapq.heappush(sum, ((Alist[a] + Blist[b] + Clist[c+1]) * (-1), arg_c))
        arg_hash[arg_c] = 1

Submission status: Accepted

Solution code:
x,y,z,k = map(int, input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
 
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
 
cakes = []
for x, a in enumerate(A):
    if x+1 > k: break
        
    for y, b in enumerate(B):
        if (x+1)*(y+1) > k: break
            
        for z, c in enumerate(C):
            if (x+1)*(y+1)*(z+1) > k: break
                
            cakes.append(a+b+c)
    
cakes.sort(reverse=True)      
for cake in cakes[:k]:
    print(cake)

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

AB=[]
for i in A:
  for j in B:
    heapq.heappush(AB,-i-j)
#print(AB)

ABC=[]
for i in range(min(K,X*Y)):
  temp=heapq.heappop(AB)
  for j in C:
    heapq.heappush(ABC,temp-j)

for i in range(min(K,X*Y*Z)):
  print(-heapq.heappop(ABC))

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

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)
    
hq = []
val = A[0]+B[0]+C[0]
heappush(hq, (-val,0,0,0))
done = [[0,0,0]]

for i in range(k):
    ans, a, b, c = heappop(hq)
    print(-ans)
    
    if [a+1, b, c] not in done and a+1 < x:
        heappush(hq, (-(A[a+1]+B[b]+C[c]), a+1, b, c))
        done.append([a+1, b, c])
    if [a, b+1, c] not in done and b+1 < y:
        heappush(hq, (-(A[a]+B[b+1]+C[c]), a, b+1, c))
        done.append([a, b+1, c])
    if [a, b, c+1] not in done and c+1 < z:
        heappush(hq, (-(A[a]+B[b]+C[c+1]), a, b, c+1))
        done.append([a, b, c+1])

Submission status: Accepted

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

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

x = 0
y = 0
z = 0
cakes = []
for x, a in enumerate(A):
    if x+1 > k: break
        
    for y, b in enumerate(B):
        if (x+1)*(y+1) > k: break
            
        for z, c in enumerate(C):
            if (x+1)*(y+1)*(z+1) > k: break
                
            cakes.append(a+b+c)
    
cakes.sort(reverse=True)      
for cake in cakes[:k]:
    print(cake)

Submission status: Accepted

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

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

x = 0
y = 0
z = 0
cakes = []
for x, a in enumerate(A):
    for y, b in enumerate(B):
        for z, c in enumerate(C):
            
            if (x+1)*(y+1)*(z+1) > k:
                break
                
            cakes.append(a+b+c)
    
cakes.sort(reverse=True)      
for cake in cakes[:k]:
    print(cake)

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)

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

p.sort(reverse=True)

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

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)

hq = []
heappush(hq, (-(a[0]+b[0]+c[0]), 0, 0, 0))
for _ in range(K):
    ans, i, j, k = heappop(hq)
    print(-ans)

    if i+1 < X:
        t = (-(a[i+1]+b[j]+c[k]), i+1, j, k)
        if t not in hq:
            heappush(hq, t)

    if j+1 < Y:
        t = (-(a[i]+b[j+1]+c[k]), i, j+1, k)
        if t not in hq:
            heappush(hq, t)
    
    if k+1 < Z:
        t = (-(a[i]+b[j]+c[k+1]), i, j, k+1)
        if t not in hq:
            heappush(hq, t)

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 = []
l0 = [[0,0,0]]
heapq.heappush(q,[(-1)*(a[0]+b[0]+c[0]),[0,0,0]])
for i in range(k):
    l = heapq.heappop(q)
    print((l[0])*(-1))
    i,j,s = l[1]
    if i < x-1:
        if [i+1,j,s] not in l0:
            heapq.heappush(q,[(-1)*(a[i+1]+b[j]+c[s]),[i+1,j,s]])
            l0.append([i+1,j,s])
    if j < y-1:
        if [i,j+1,s] not in l0:
            heapq.heappush(q,[(-1)*(a[i]+b[j+1]+c[s]),[i,j+1,s]])
            l0.append([i,j+1,s])
    if s < z-1:
        if [i,j,s+1] not in l0:
            heapq.heappush(q,[(-1)*(a[i]+b[j]+c[s+1]),[i,j,s+1]])
            l0.append([i,j,s+1])

Submission status: Accepted

Solution code:
def slove():
    import sys
    import heapq
    import collections
    input = sys.stdin.readline
    x, y, z, k = list(map(int, input().rstrip('\n').split()))
    a = list(map(int, input().rstrip('\n').split()))
    a.sort(reverse=True)
    b = list(map(int, input().rstrip('\n').split()))
    b.sort(reverse=True)
    c = list(map(int, input().rstrip('\n').split()))
    c.sort(reverse=True)
    q = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
    heapq.heapify(q)
    fq = collections.defaultdict(list)
    fq[(-(a[0] + b[0] + c[0]), 0, 0, 0), 1] = 1
    t = 0
    for _ in range(k):
        if len(q) != 0:
            p = heapq.heappop(q)
            t = p[0]
            aq = p[1]
            bq = p[2]
            cq = p[3]
            if aq + 1 < len(a):
                if (-(a[aq+1] + b[bq] + c[cq]), aq+1, bq, cq) not in fq:
                    heapq.heappush(q, [-(a[aq+1] + b[bq] + c[cq]), aq+1, bq, cq])
                    fq[(-(a[aq+1] + b[bq] + c[cq]), aq+1, bq, cq)] = 1
            if bq + 1 < len(b):
                if (-(a[aq] + b[bq+1] + c[cq]), aq, bq+1, cq) not in fq:
                    heapq.heappush(q, [-(a[aq] + b[bq+1] + c[cq]), aq, bq+1, cq])
                    fq[(-(a[aq] + b[bq+1] + c[cq]), aq, bq+1, cq)] = 1
            if cq + 1 < len(c):
                if (-(a[aq] + b[bq] + c[cq+1]), aq, bq, cq+1) not in fq:
                    heapq.heappush(q, [-(a[aq] + b[bq] + c[cq+1]), aq, bq, cq+1])
                    fq[(-(a[aq] + b[bq] + c[cq+1]), aq, bq, cq+1)] = 1
        print(-t)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

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

ans1=[]
ans2=[]

for i in range(x):
    for j in range(y):
        ans1.append(a[i]+b[j])
ans1.sort()
ans1.reverse()

if len(ans1)>=k:
    del ans1[k:]
    
for i in range(len(ans1)):
    for j in range(z):
        ans2.append(ans1[i]+c[j])
ans2.sort()
ans2.reverse()

for i in range(k):
    print(ans2[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()))

ans1=[]
ans2=[]

for i in range(x):
    for j in range(y):
        ans1.append(a[i]+b[j])
ans1.sort()
ans1.reverse()

if len(ans1)>=k:
    del ans1[k:]
    
for i in range(len(ans1)):
    for j in range(z):
        ans2.append(ans1[i]+c[j])
ans2.sort()
ans2.reverse()

for i in range(k):
    print(ans2[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()))

tmpList = []
for i in range(X):
    for j in range(Y):
        tmpList.append(a[i] + b[j])

tmpList.sort(reverse=True)
ansLst = []
for i in range(min(K, len(tmpList))):
    for j in c:
        ansLst.append(tmpList[i] + j)

ansLst.sort(reverse=True)

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

Submission status: Accepted

Solution code:
import heapq
x, y, z, w = 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 = []
ans = []
flag = []
first = [A[0] + B[0] + C[0], [0, 0, 0]]
heapq.heappush(q, first)
flag.append(first[1])
for h in range(w):
    max_ans = heapq.heappop(q)
    ans.append(abs(max_ans[0]))
    i, j, k = max_ans[1]

    if i == x-1:
        lst1 =  [-A[i]-B[j]-C[k], [i, j, k]]
    else:
        lst1 =  [-A[i+1]-B[j]-C[k], [i+1, j, k]]
    if lst1[1] not in flag:
        heapq.heappush(q, lst1)
        flag.append(lst1[1])

    if j == y-1:
        lst2 = [-A[i]-B[j]-C[k], [i, j, k]]
    else:
        lst2 = [-A[i]-B[j+1]-C[k], [i, j+1, k]]
    if lst2[1] not in flag: 
        heapq.heappush(q, lst2)
        flag.append(lst2[1])

    if k == z-1:
        lst3 = [-A[i]-B[j]-C[k], [i, j, k]]
    else:
        lst3 = [-A[i]-B[j]-C[k+1], [i, j, k+1]]
    if lst3[1] not in flag:
        heapq.heappush(q, lst3)
        flag.append(lst3[1])
    
    print(ans[h])

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)

q = []
heappush(q,(-(A[0]+B[0]+C[0]),0,0,0))
for i in range(K):
    ans, i,j,k = heappop(q)
    print(-ans)
    if i+1 < X:
        t = (-(A[i+1]+B[j]+C[k]),i+1,j,k)
        if t not in q:
            heappush(q,t)
    if j+1 < Y:
        t = (-(A[i]+B[j+1]+C[k]),i,j+1,k)
        if t not in q:
            heappush(q,t)
    if k+1 < Z:
        t = (-(A[i]+B[j]+C[k+1]),i,j,k+1)
        if t not in q:
            heappush(q,t)

Submission status: Accepted

Solution code:
x, y, z, k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

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

D = []

for i in range(min(k, x)):
    for j in range(min(k, y)):
        for l in range(min(k, z)):
            if i*j*l <= k:
                D.append(A[i] + B[j] + C[l])
            else:
                break

D.sort(reverse=1)
#dd = len(D)

#fa = open("d2.txt", "w")

for i in range(k):
    #fa.write(str(i)+" "+str(D[i])+"\n")
    print(D[i])

#fa.close()

# coo = 0
# for i in range(len(D)):
#     if D[i] >= 2977:
#         coo += 1
# print(coo)
# print(dd)

Submission status: Accepted

Solution code:
x, y, z, k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

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

#print(len(A),len(B),len(C))
# print(A[:20])
# print(B[:20])
# print(C[:20])

def d4(p):
    co = 0
    for i in range(x):
        if A[i] + B[0] + C[0] < p:
            break
        else:
            for j in range(y):
                if A[i] + B[j] + C[0] < p:
                    break
                else:
                    for l in range(z):
                        #print(i,j,l)
                        # if i==14 and l==0 and j==0:
                        #     print("t",A[i] + B[j] + C[l])
                        if A[i] + B[j] + C[l] < p:
                            break
                        else:
                            #D.append([A[i] + B[j] + C[l], i, j, l])
                            co += 1
                            if co == k:
                                return True
    return False

ri = A[0]+B[0]+C[0] + 1
le = 0
mid = (le + ri) // 2

#lr = []

while ri - le > 1:
    #D = []
    #print("le",le,"mid",mid,"ri",ri)
    d4bool = d4(mid)
    if d4bool:
        le = mid
    else:
        ri = mid
    mid = (le + ri) // 2
    #lr.append([le,ri])

D = []

p = le
for i in range(x):
    if A[i] + B[0] + C[0] < p:
        break
    else:
        for j in range(y):
            if A[i] + B[j] + C[0] < p:
                break
            else:
                for l in range(z):
                    if A[i] + B[j] + C[l] < p:
                        break
                    else:
                        D.append(A[i] + B[j] + C[l])


D.sort(reverse=1)

#dd = len(D)

while len(D) < k:
    D.append(le)

# fa = open("d4.txt", "w")

for i in range(k):
    #fa.write(str(i)+" "+str(D[i])+"\n")
    print(D[i])

# fa.close()

# coo = 0
# for i in range(len(D)):
#     if D[i] >= 2977:
#         coo += 1
# print(coo)
# print(dd)
# print(lr)

Submission status: Accepted

Solution code:
x, y, z, k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

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

D = []

for i in range(min(k, x)):
    for j in range(min(k, y)):
        for l in range(min(k, z)):
            if i*j*l <= k:
                D.append(A[i] + B[j] + C[l])
            else:
                break

D.sort(reverse=1)

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

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)
memo = []
Q = []
Q.append((-(A[0]+B[0]+C[0]),0,0,0))
memo.append((0,0,0))
count = 1

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

Submission status: Accepted

Solution code:
import itertools
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

X,Y,Z,K = IL()
a = IL()
b = IL()
c = IL()

ab = []
for i in itertools.product(a,b):
  ab.append(sum(i))
ab.sort(reverse=True)

ans = []
for i in itertools.product(ab[:3000],c):
  ans.append(sum(i))
ans.sort(reverse=True)

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

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

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_list.sort(reverse=True)
b_list.sort(reverse=True)
c_list.sort(reverse=True)
hp = []
checked_dict = {}
max_abc = a_list[0] + b_list[0] + c_list[0]
heappush(hp, (-max_abc,0,0,0))
for _ in range(K):
  ans, ai, bi, ci = heappop(hp)
  print(-ans)
  if ai < X-1 and not (ai+1, bi, ci) in checked_dict:
    checked_dict[(ai+1,bi,ci)] = 1
    heappush(hp, (-(a_list[ai+1]+b_list[bi]+c_list[ci]),ai+1,bi,ci))
  if bi < Y-1 and not (ai,bi+1,ci) in checked_dict:
    checked_dict[(ai,bi+1,ci)] = 1
    heappush(hp, (-(a_list[ai]+b_list[bi+1]+c_list[ci]),ai,bi+1,ci))
  if ci < Z-1 and not (ai,bi,ci+1) in checked_dict:
    checked_dict[(ai,bi,ci+1)] = 1
    heappush(hp, (-(a_list[ai]+b_list[bi]+c_list[ci+1]),ai,bi,ci+1))