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

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

h = []
s = set()
ia = ib = ic = 0

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


def f(ia, ib, ic):
    return (-(A[ia] + B[ib] + C[ic]), ia, ib, ic)


item = f(ia, ib, ic)
heapq.heappush(h, item)
s.add(item)
ans = []

for i in range(K):
    # print("----")
    # print(h)
    if len(h) == 0:
        break
    t = heapq.heappop(h)
    ans.append(t)
    if t[1] + 1 < len(A):
        item = f(t[1] + 1, t[2], t[3])
        if not item in s:
            heapq.heappush(h, item)
        s.add(item)
    if t[2] + 1 < len(B):
        item = f(t[1], t[2] + 1, t[3])
        if not item in s:
            heapq.heappush(h, item)
        s.add(item)

    if t[3] + 1 < len(C):
        item = f(t[1], t[2], t[3] + 1)
        if not item in s:
            heapq.heappush(h, item)
        s.add(item)

# print("==================")
# print(A)
# print(B)
# print(C)
# print(ans)
for i in range(K):
    print(-ans[i][0])

Submission status: Accepted

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

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)
pque =[(-(A[0]+B[0]+C[0]),(0,0,0))] 
memo= {}
cnt = 0

while pque:
    value, position = heapq.heappop(pque)
    if memo.get(position) is None :
        memo[position] = True
        cnt += 1 
        if cnt > K:
            break
    else: 
        continue
    print(-value)
    for patern in [[1,0,0],[0,1,0],[0,0,1]]:
        i,j,k = [v1+v2 for v1,v2 in zip(patern,position)]
        if i<X and j<Y and k<Z:
            heapq.heappush(pque,(-(A[i]+B[j]+C[k]),(i,j,k)))

Submission status: Accepted

Solution code:
import heapq
x, y, z, K = map(int, input().split())
k = min(x * y * z, K)
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)

results = []
pque = [(-(a[0] + b[0] + c[0]), (0, 0, 0))]
visited = {}
cnt = 0
while pque:
    value, position = heapq.heappop(pque)
    if visited.get(position) is None:
        visited[position] = True
        cnt += 1
        if cnt > K:
            break
    else:
        continue

    print(-value)
    results.append(value)
    for pattern in [[0, 0, 1], [0, 1, 0], [1, 0, 0]]:
        i, j, k = [v1 + v2 for v1, v2 in zip(pattern, position)]
        if i < x and j < y and k < z:
            heapq.heappush(pque, (-(a[i] + b[j] + c[k]), (i, j, k)))

Submission status: Accepted

Solution code:
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
 
A = sorted(A, reverse=True)[:K+1]
B = sorted(B, reverse=True)[:K+1]
C = sorted(C, reverse=True)[:K+1]
 
res = []
 
for i in A:
    for j in B:
        res.append(i + j)
res2 = []
res = sorted(res, reverse=True)[:K+1]
 
for i in C:
    for re in res:
        res2.append(i + re)
        
res2 = sorted(res2, reverse=True)[:K+1]
          
 
for i in range(K):
    print(res2[i])

Submission status: Accepted

Solution code:
x, y, z, k = map(int, input().split())
k = min(x * y * z, k)
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]
def f(a, b):
    return [i + j for i in a for j in b]
ab_results = sorted(f(a, b), reverse=True)
results = sorted(f(c, ab_results[:k]), reverse=True)

print("\n".join(map(str, results[:k])))

Submission status: Accepted

Solution code:
import numpy as np
x, y, z, k = map(int, input().split())
k = min(x * y * z, k)
a = np.array(list(map(int, input().split())))
b = np.array(list(map(int, input().split())))
c = np.array(list(map(int, input().split())))

ab = np.ravel(a + b[:, None])
ab.sort()
abc = np.ravel(c + ab[-k:, None])
abc.sort()
print(np.sum(abc[-k:][::-1].astype(str).astype(object) + "\n"), end="")

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+1]
B = sorted(B, reverse=True)[:K+1]
C = sorted(C, reverse=True)[:K+1]

res = []

for i in A:
    for j in B:
        res.append(i + j)
res2 = []
res = sorted(res, reverse=True)[:K+1]

for i in C:
    for re in res:
        res2.append(i + re)
        
res2 = sorted(res2, reverse=True)[:K+1]
          

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

Submission status: Accepted

Solution code:
*XYZ,K = map(int,input().split())
ABC = [list(map(int,input().split())) for i in range(3)]
for a in ABC:
    a.sort(reverse=1)
import heapq
ans = [0]*K
for a in ABC[0]:
    for b in ABC[1]:
        for c in ABC[2]:
            if a+b+c>ans[0]:
                heapq.heappush(ans,a+b+c)
                heapq.heappop(ans)
            else:
                break
ans.sort(reverse=1)
for i in range(K):
    print(ans[i])

Submission status: Accepted

Solution code:
x, y, z, k = map(int, input().split())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
cc = list(map(int, input().split()))

aa.sort(reverse=True)
aa = aa[:k]
bb.sort(reverse=True)
bb = bb[:k]
cc.sort(reverse=True)
cc = cc[:k]

dd = [a+b for a in aa for b in bb]
dd.sort(reverse=True)
dd = dd[:k]

ee = [a+b for a in dd for b in cc]
ee.sort(reverse=True)
ee = ee[:k]
print(*ee, sep='\n')

Submission status: Accepted

Solution code:
input = raw_input
def lsum(x):
    return A[x[0]] + B[x[1]] + C[x[2]]

def nextlist():
    global lx
    idx = 0
    m = lsum(lx[0])
    for i in range(len(lx)):
        s = lsum(lx[i])
        if m < s:
            m = s
            idx = i
    p = lx.pop(idx)
    wa.append(lsum(p))
    if p[0] > 0:
        if not [p[0]-1, p[1], p[2]] in lx:
            lx.append([p[0]-1, p[1], p[2]])
    if p[1] > 0:
        if not [p[0], p[1]-1, p[2]] in lx:
            lx.append([p[0], p[1]-1, p[2]])
    if p[2] > 0:
        if not [p[0], p[1], p[2]-1] in lx:
            lx.append([p[0], p[1], p[2]-1])

l1 = list(map(int, input().split(" ")))

X,Y,Z,K = l1[0],l1[1],l1[2],l1[3]
A = sorted(list(map(int, input().split(" "))))
B = sorted(list(map(int, input().split(" "))))
C = sorted(list(map(int, input().split(" "))))

wa = []
lx = [[X-1,Y-1,Z-1]]

for i in range(K):
    nextlist()

for l in wa:
    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)

candidates = []
heapq.heappush(candidates, (-(A[0] + B[0] + C[0]), 0, 0, 0))
used = {(0, 0, 0)}
for _ in range(K):
    s, i, j, k = heapq.heappop(candidates)
    print(-s)
    if i < X - 1 and (i+1, j, k) not in used:
        heapq.heappush(candidates, (-(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:
        heapq.heappush(candidates, (-(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:
        heapq.heappush(candidates, (-(A[i] + B[j] + C[k+1]), i, j, k+1))
        used.add((i, j, k+1))

Submission status: Accepted

Solution code:
import heapq
X, Y, Z, K = [int(i) for i in input().split()]
A = sorted([int(i) for i in input().split()],reverse=True)
B = sorted([int(i) for i in input().split()],reverse=True)
C = sorted([int(i) for i in input().split()],reverse=True)
def set_tuple(A,B,C,x,y,z):
    return (-(A[x]+B[y]+C[z]),x,y,z)
Q = []
heapq.heappush(Q,set_tuple(A,B,C,0,0,0))
Ans = []
memo = {(0,0,0):True}
for _ in range(K):
    e,x,y,z = heapq.heappop(Q)
    Ans.append(-e)
    if x+1 < X and (x+1,y,z) not in memo:
        heapq.heappush(Q,set_tuple(A,B,C,x+1,y,z))
        memo[(x+1,y,z)] = True
    if y+1 < Y and (x,y+1,z) not in memo:
        heapq.heappush(Q,set_tuple(A,B,C,x,y+1,z))
        memo[(x,y+1,z)] = True
    if z+1 < Z and (x,y,z+1) not in memo:
        heapq.heappush(Q,set_tuple(A,B,C,x,y,z+1))
        memo[(x,y,z+1)] = True
for i in Ans[:K]:print(i)

Submission status: Accepted

Solution code:
import heapq


def solve(k, a_list, b_list, c_list):
    # time (A log(A)), (B log(B)), (C log(C))
    a_list.sort(reverse=True)
    b_list.sort(reverse=True)
    c_list.sort(reverse=True)

    priority_queue = []
    heapq.heappush(priority_queue, [-(a_list[0] + b_list[0] + c_list[0]), 0, 0, 0])
    key_map = {(0, 0, 0)}

    ans = []
    # time (K log(K))
    for _ in range(k):
        sum_data, a_key, b_key, c_key = heapq.heappop(priority_queue)
        ans.append(str(-sum_data))

        if a_key + 1 < len(a_list) and (a_key+1, b_key, c_key) not in key_map:
            heapq.heappush(priority_queue, [-(a_list[a_key+1] + b_list[b_key] + c_list[c_key]), a_key+1, b_key, c_key])
            key_map.add((a_key+1, b_key, c_key))
        if b_key + 1 < len(b_list) and (a_key, b_key+1, c_key) not in key_map:
            heapq.heappush(priority_queue, [-(a_list[a_key] + b_list[b_key+1] + c_list[c_key]), a_key, b_key+1, c_key])
            key_map.add((a_key, b_key+1, c_key))
        if c_key + 1 < len(c_list) and (a_key, b_key, c_key+1) not in key_map:
            heapq.heappush(priority_queue, [-(a_list[a_key] + b_list[b_key] + c_list[c_key+1]), a_key, b_key, c_key+1])
            key_map.add((a_key, b_key, c_key+1))

    return ans


def main():
    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()))

    ans = solve(k, a_list, b_list, c_list)
    print("\n".join(ans))


main()

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)
ans = [(-(a[0]+b[0]+c[0]), 0, 0, 0)]
for i in range(K):
    m, j, k, h = heapq.heappop(ans)
    print(-m)
    if j+1 <= X-1:
        s =(-(a[j+1]+b[k]+c[h]), j+1, k, h)
        if s not in ans:
            heapq.heappush(ans, s)
    if k+1 <= Y-1:
        s =(-(a[j]+b[k+1]+c[h]), j , k+1, h)
        if s not in ans:
            heapq.heappush(ans, s)
    if h+1 <= Z-1:
        s =(-(a[j]+b[k]+c[h+1]), j , k, h+1)
        if s not in ans:
            heapq.heappush(ans, s)

Submission status: Accepted

Solution code:
import heapq

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)

pq = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
s = {(0, 0, 0)}
res = []
while len(res) < k and len(pq) > 0:
    t = heapq.heappop(pq)
    res.append(str(-t[0]))
    idx = []
    if t[1] + 1 < len(A):
        idx.append(t[1] + 1)
    else:
        idx.append(t[1])
    if t[2] + 1 < len(B):
        idx.append(t[2] + 1)
    else:
        idx.append(t[2])
    if t[3] + 1 < len(C):
        idx.append(t[3] + 1)
    else:
        idx.append(t[3])
    flag = 0
    if (idx[0], t[2], t[3]) not in s:
        s.add((idx[0], t[2], t[3]))
        heapq.heappush(pq, (-(A[idx[0]] + B[t[2]] + C[t[3]]), idx[0], t[2], t[3]))
    if (t[1], idx[1], t[3]) not in s:
        s.add((t[1], idx[1], t[3]))
        heapq.heappush(pq, (-(A[t[1]] + B[idx[1]] + C[t[3]]), t[1], idx[1], t[3]))
    if (t[1], t[2], idx[2]) not in s:
        s.add((t[1], t[2], idx[2]))
        heapq.heappush(pq, (-(A[t[1]] + B[t[2]] + C[idx[2]]), t[1], t[2], idx[2]))

print('\n'.join(res))

Submission status: Accepted

Solution code:
N = list(map(int, input().split()))[-1]
A = sorted(list(map(int, input().split())), reverse=True)[:N]
B = sorted(list(map(int, input().split())), reverse=True)[:N]
C = sorted(list(map(int, input().split())), reverse=True)[:N]

first = [i+j for i in A for j in B]
first = sorted(first, reverse=True)[:N]
second = [i+j for i in first for j in C]
result = sorted(second, reverse=True)[:N]

[print(i) for i in result]

Submission status: Accepted

Solution code:
# TODO D - Cake 123
# https://atcoder.jp/contests/abc123/tasks/abc123_d

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

A = sorted(map(int,raw_input().split()), reverse=True)
B = sorted(map(int,raw_input().split()), reverse=True)
C = sorted(map(int,raw_input().split()), reverse=True)

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

for i in ABC[:K]:
    print '{}'.format(i)

Submission status: Accepted

Solution code:
#abc123d
 
x,y,z,k=map(int,raw_input().split())
a=sorted(map(int,raw_input().split()),reverse=True)
b=sorted(map(int,raw_input().split()),reverse=True)
c=sorted(map(int,raw_input().split()),reverse=True)
 
ab=sorted([i+j for i in a for j in b],reverse=True)
res=sorted([ij+l for ij in ab[:k] for l in c],reverse=True)
for i in res[:k]:
	print i

Submission status: Accepted

Solution code:
from itertools import product, chain
from collections import deque


def solver1(a, b, c, k):
    apb_sorted = sorted([a + b for a, b in product(a, b)])[-k:]
    ans_list = sorted(
        list(chain.from_iterable([[_c + ab for ab in apb_sorted] for _c in c[:k]]))
    )
    it = reversed(ans_list)
    for _ in range(k):
        print(next(it))


def solver2(a, b, c, k):
    n = len(a)
    m = min(k, n)
    pqr = deque()
    for p in range(m):
        for q in range(m):
            for r in range(m):
                if (p + 1) * (q + 1) * (r + 1) > k:
                    break
                else:
                    pqr.append(a[-p - 1] + b[-q - 1] + c[-r - 1])
    it = reversed(sorted(pqr))
    for _ in range(k):
        print(next(it))


if __name__ == "__main__":
    x, y, z, k = list(map(int, input().split(" ")))
    a = sorted(list(map(int, input().split(" "))))
    b = sorted(list(map(int, input().split(" "))))
    c = sorted(list(map(int, input().split(" "))))
    solver2(a, b, c, k)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
 
x, y, z, k = map(int, input().split())
a = sorted(int(i) for i in input().split())
b = sorted(int(i) for i in input().split())
c = sorted(int(i) for i in input().split())
 
#a.sort(reverse=True)
#b.sort(reverse=True)
#c.sort(reverse=True)
 
ab = sorted(i+j for i in a for j in b)[:-k-1:-1]
abc = sorted(i+j for i in ab for j in c)[:-k-1:-1]
 
print(*abc[:k], sep="\n")

Submission status: Accepted

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

x, y, z, k = map(int, input().split())
a = sorted(int(i) for i in input().split())
b = sorted(int(i) for i in input().split())
c = sorted(int(i) for i in input().split())

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

ab = sorted(i+j for i in a for j in b)[:-k-1:-1]
abc = sorted(i+j for i in ab for j in c)[:-k-1:-1]

print(*abc[:k], sep="\n")

Submission status: Accepted

Solution code:
import sys
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=sorted(int(i) for i in input().split())[:-k-1:-1]

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

ab = sorted(i+j for i in a for j in b)[:-k-1:-1]
abc = sorted(i+j for i in ab for j in c)[:-k-1:-1]

print(*abc[:k], sep="\n")

Submission status: Accepted

Solution code:
import sys
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=sorted(int(i) for i in input().split())

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

ab = sorted(i+j for i in a for j in b)[:-k-1:-1]
c = c[:k]
abc = sorted(i+j for i in ab for j in c)[:-k-1:-1]

print(*abc[:k], sep="\n")

Submission status: Accepted

Solution code:
import sys
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.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

ab = sorted(i+j for i in a for j in b)[:-k-1:-1]
c = c[:k]
abc = sorted(i+j for i in ab for j in c)[:-k-1:-1]

print(*abc[:k], sep="\n")

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)

ab = sorted(i+j for i in a for j in b)[:-k-1:-1]
c = c[:k]
abc = sorted(i+j for i in ab for j in c)[:-k-1:-1]

print(*abc[:k], sep="\n")

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)

ab = sorted(x+y for x in a for y in b)[:-k-1:-1]
abc = sorted(x+y for x in ab[:k] for y in c)[:-k-1:-1]
print(*abc, sep='\n')

Submission status: Accepted

Solution code:
import heapq
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
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, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)


def calc(x, y, z):
    return [-A[x]-B[y]-C[z], x, y, z]


def solve1(A, B, C, X, Y, Z, k):
    Q = []
    Q.append(calc(0, 0, 0))

    ans = []
    # dict
    memo = {(0, 0, 0): True}
    for _ in range(k):
        e, x, y, z = heapq.heappop(Q)
        ans.append(-e)
        if x+1 < X and (x+1, y, z) not in memo:
            heapq.heappush(Q, calc(x+1, y, z))
            memo[(x+1, y, z)] = True
        if y+1 < Y and (x, y+1, z) not in memo:
            heapq.heappush(Q, calc(x, y+1, z))
            memo[(x, y+1, z)] = True

        if z+1 < Z and (x, y, z+1) not in memo:
            heapq.heappush(Q, calc(x, y, z+1))
            memo[(x, y, z+1)] = True

    print("\n".join(map(str, ans[:k])))


solve1(A, B, C, X, Y, Z, k)

Submission status: Accepted

Solution code:
from itertools import product, chain

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

apb_sorted = sorted([a + b for a, b in product(a, b)])[-k:]
ans_list = sorted(
    list(chain.from_iterable([[_c + ab for ab in apb_sorted] for _c in c[:k]]))
)
it = reversed(ans_list)
for _ in range(k):
    print(next(it))

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)
A = A[0:K]
B.sort(reverse=True)
B = B[0:K]
AB = [a+b for a in A for b in B]

AB.sort(reverse=True)

AB = AB[0:K]

C.sort(reverse=True)
C=C[0:K]
ABC = [ab+c for ab in AB for c in C]

ABC.sort(reverse=True)

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

Submission status: Accepted

Solution code:
import sys, copy
import heapq
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.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

N = [X,Y,Z]
V = [A,B,C]
que = []
top = ((-1)*(V[0][0]+V[1][0]+V[2][0]),[0,0,0])
heapq.heappush(que, top)
ans_set = set()
ans_set.add(tuple(top[1]))
for k in range(K):
    cur = heapq.heappop(que)
    print((-1)*cur[0])
    for i in range(3):
        if cur[1][i] + 1 < N[i]:
            s = (-1) * cur[0] - V[i][cur[1][i]] + V[i][cur[1][i] + 1]
            index = copy.copy(cur[1])
            index[i] = cur[1][i] + 1
            t_index = tuple(index)
            if t_index not in ans_set:
                ans_set.add(t_index)
                heapq.heappush(que,((-1)*s,index))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# ヒープキューを使う

import math
import sys
import itertools
import numpy as np
import heapq
 
# なんこか入力
x,y,z,k = map(int, input().split())
 
# 空白区切りの数列をリストに代入
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]

a.sort()
b.sort()
c.sort()

h = [] # heap
g = set() #使用済み

heapq.heappush(h,(-(a[x-1]+b[y-1]+c[z-1]),x-1,y-1,z-1))
g.add((x-1,y-1,z-1))

#print(h)

for i in range(k):
#    heapq.heapify(h)
    temp =[]
    temp = heapq.heappop(h)
    print(-temp[0])
    for j in range(3):
        if temp[j+1]==0:
            continue
        if j == 0:
            temp2 = (-(a[temp[1]-1]+b[temp[2]]+c[temp[3]]),temp[1]-1,temp[2],temp[3])
        elif j == 1:
            temp2 = (-(a[temp[1]]+b[temp[2]-1]+c[temp[3]]),temp[1],temp[2]-1,temp[3])
        else:
            temp2 = (-(a[temp[1]]+b[temp[2]]+c[temp[3]-1]),temp[1],temp[2],temp[3]-1)
        if (temp2[1],temp2[2],temp2[3]) in g:
            continue
        heapq.heappush(h,temp2)
        g.add((temp2[1],temp2[2],temp2[3]))

Submission status: Accepted

Solution code:
#abc123d

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

ab=sorted([i+j for i in a for j in b],reverse=True)
res=sorted([ij+l for ij in ab[:k] for l in c],reverse=True)
for i in res[:k]:
 print 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)
ans = []


# a=[6, 4] b=[5, 1] c=[8, 3]

# delicious(i) = a(l) + b(m) + c(n)
# delicious(i+1) = max(delicious(i)-a(i)+a(i+1) , delicious(i)-b(m)+b(m+1), delicious(i)-c(n)+c(n+1)))
# delicious(0) = a(0) + b(0) + c(0)

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

#print(ans)

def delicious():
    maxans = heapq.heappop(ans)
    if maxans[1] != x-1:
        check1 = [-a[maxans[1]+1]-b[maxans[2]]-c[maxans[3]],maxans[1]+1,maxans[2],maxans[3]]
        if check1 not in ans:
            heapq.heappush(ans,check1)

    if maxans[2] != y-1:
        check2 = [-a[maxans[1]]-b[maxans[2]+1]-c[maxans[3]],maxans[1],maxans[2]+1,maxans[3]]
        if check2 not in ans:
            heapq.heappush(ans,check2)


    if maxans[3] != z-1:
        check3 = [-a[maxans[1]]-b[maxans[2]]-c[maxans[3]+1],maxans[1],maxans[2],maxans[3]+1]
        if check3 not in ans:
            heapq.heappush(ans,check3)
    print(-1*maxans[0])

for i in range(k):
    delicious()

Submission status: Accepted

Solution code:
X, Y, Z, K = list(map(int,input().split()))
ALPHA = []
for i in range(3):
    ALPHA.append(sorted(list(map(int,input().split())), reverse=True))

counter = 0
p_queue = []
already_queued = []
p_queue.append([0, 0, 0, sum([x[0] for x in ALPHA])])
already_queued.append([0, 0, 0, sum([x[0] for x in ALPHA])])
while True:
    if len(p_queue) == 0:
        break
    p_queue.sort(key=lambda x: x[3])
    max_choice = p_queue.pop(-1)
    print(max_choice[3])
    counter += 1
    if counter == K:
        break
    if max_choice[0] + 1 < X:
        item = [max_choice[0]+1, max_choice[1], max_choice[2], \
                    max_choice[3] - ALPHA[0][max_choice[0]] + ALPHA[0][max_choice[0] + 1]]
        if item not in already_queued:
            p_queue.append(item)
            already_queued.append(item)
    if max_choice[1]+1 < Y:
        item = [max_choice[0], max_choice[1]+1, max_choice[2], \
                    max_choice[3] - ALPHA[1][max_choice[1]] + ALPHA[1][max_choice[1] + 1]]
        if item not in already_queued:
            p_queue.append(item)
            already_queued.append(item)

    if max_choice[2]+1 < Z:
        item = [max_choice[0], max_choice[1], max_choice[2]+1, \
                    max_choice[3] - ALPHA[2][max_choice[2]] + ALPHA[2][max_choice[2] + 1]]
        if item not in already_queued:
            p_queue.append(item)
            already_queued.append(item)

Submission status: Accepted

Solution code:
numbers = input().split()
x = input().split()
y = input().split()
z = input().split()

k = int(numbers[3])

answer = []
for i in x:
    for j in y:
        price = int(i)+int(j)
        answer.append(price)
answer.sort(reverse=True)
del answer[k::]
f_ans = []
for m in answer:
    for n in z:
        price = m + int(n)
        f_ans.append(price)

f_ans.sort(reverse=True)
del f_ans[k::]

for l in f_ans:
    print(l)

Submission status: Accepted

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

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

from heapq import heapify, heappop, heappush, heappushpop
 
 
class PriorityQueue:
    def __init__(self, heap):
        '''
        heap ... list
        '''
        self.heap = heap
        heapify(self.heap)
 
    def push(self, item):
        heappush(self.heap, item)
 
    def pop(self):
        return heappop(self.heap)
 
    def pushpop(self, item):
        return heappushpop(self.heap, item)
 
    def __call__(self):
        return self.heap
 
 
heap = []  # ヒープといっても順序を工夫したただのリスト
 
q = PriorityQueue(heap)
q.push((-(A[0] + B[0] + C[0]), 0, 0, 0))
 
considered = set()
ans = []
for k_th in range(1, K+1):
    heap_max, i, j, k = q.pop()
    ans.append(-heap_max)
    for di, dj, dk in zip([1, 0, 0], [0, 1, 0], [0, 0, 1]):
        i_new, j_new, k_new = i + di, j + dj, k + dk
        if i_new >= X or j_new >= Y or k_new >= Z:
            continue
        if (i_new, j_new, k_new) in considered:
            continue
        considered.add((i_new, j_new, k_new))
        q.push((-(A[i_new] + B[j_new] + C[k_new]), i_new, j_new, k_new))
 
 
print(*ans, sep='\n')

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)

registered = set()
pool = []

heapq.heappush(pool, (-(a[0] + b[0] + c[0]), (0, 0, 0)))
registered.add((0, 0, 0))

for rank in range(k):
    value, (i, j, k) = heapq.heappop(pool)
    print(-value)
    if i + 1 < x and (i + 1, j, k) not in registered:
        heapq.heappush(pool, (-(a[i + 1] + b[j] + c[k]), (i + 1, j, k)))
        registered.add((i + 1, j, k))
    if j + 1 < y and (i, j + 1, k) not in registered:
        heapq.heappush(pool, (-(a[i] + b[j + 1] + c[k]), (i, j + 1, k)))
        registered.add((i, j + 1, k))
    if k + 1 < z and (i, j, k + 1) not in registered:
        heapq.heappush(pool, (-(a[i] + b[j] + c[k + 1]), (i, j, k + 1)))
        registered.add((i, j, k + 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)

import heapq

# 合計値, a_idx, b_idx, c_idx
queue = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
# heapqueueにqueueをかえる
heapq.heapify(queue)

visited = set()
i = 0
for i in range(K):
    while True:
        v, a, b, c = heapq.heappop(queue)  # 候補の中から1番目が一番小さいtupleを取り出す
        if (a, b, c) not in visited:
            break
    visited.add((a, b, c))
    print(-v)

    if a < X - 1:
        heapq.heappush(queue, (-(A[a + 1] + B[b] + C[c]), a + 1, b, c))
    if b < Y - 1:
        heapq.heappush(queue, (-(A[a] + B[b + 1] + C[c]), a, b + 1, c))
    if c < Z - 1:
        heapq.heappush(queue, (-(A[a] + B[b] + C[c + 1]), 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.sort(reverse=True)
A = A[0:K]
B.sort(reverse=True)
B = B[0:K]
AB = [a+b for a in A for b in B]

AB.sort(reverse=True)

AB = AB[0:K]

C.sort(reverse=True)
C=C[0:K]
ABC = [ab+c for ab in AB for c in C]

ABC.sort(reverse=True)

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