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

L = []
for i, a in enumerate(A):
  for j, b in enumerate(B):
    if i * j > K:
      continue
    else:
      L.append(a + b)

L.sort(reverse = True)
ans = []
for i, l in enumerate(L):
  if i == K:
    break
  else:
    for c in C:
      ans.append(l + c)
ans.sort(reverse = True)
for i, a in enumerate(ans):
  if i == K:
    break
  else:
    print(a)

Submission status: Accepted

Solution code:
import heapq

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 = []

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

result = []
used = set()

for _ in range(K):
    value, i,j,k = heapq.heappop(h)
    result.append(-value)
    if i < X-1 and (i+1,j,k) not in used:
        heapq.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:
        heapq.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:
        heapq.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 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)

l = []

for p in range(0,x):
    for q in range(0,y):
        for r in range(0,z):
            if len(l) > k and a[p]+b[q]+c[r] <= l[0]:
                break
            elif len(l) > k:
                heapq.heappushpop(l,a[p]+b[q]+c[r])
            else:
                heapq.heappush(l,a[p]+b[q]+c[r])

l.sort()

for i in range(1,k+1):
    print(l[-i])

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

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_list = []
s = set()
pq = []
item = (-(A[0]+B[0]+C[0]),0,0,0)
heappush(pq,item)

for _ in range(K):
    value, i, j, k = heappop(pq)
    ans_list.append(-value)
    if len(ans_list) == K:
        break
    if i <= X-2 and (i+1,j,k) not in s:
        item = (-(A[i+1]+B[j]+C[k]),i+1,j,k)
        heappush(pq,item)
        s.add((i+1,j,k))
    if j <= Y-2 and (i,j+1,k) not in s:
        item = (-(A[i]+B[j+1]+C[k]),i,j+1,k)
        heappush(pq,item)
        s.add((i,j+1,k))
    if k <= Z-2 and (i,j,k+1) not in s:
        item = (-(A[i]+B[j]+C[k+1]),i,j,k+1)
        heappush(pq,item)
        s.add((i,j,k+1))

for ans in ans_list:
    print(ans)

Submission status: Accepted

Solution code:
import heapq

X, Y, Z, K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
A = [-a for a in A]
B = sorted(list(map(int, input().split())), reverse=True)
B = [-b for b in B]
C = sorted(list(map(int, input().split())), reverse=True)
C = [-c for c in C]

checked = []

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

Submission status: Accepted

Solution code:
x,y,z,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
sum_list = []
for a_i in range(x):
    for b_i in range(y):
        if a_i*b_i > k:
            break
        for c_i in range(z):
            if a_i*b_i*c_i > k:
                break
            sum_list.append(a[a_i]+b[b_i]+c[c_i])
sum_list.sort(reverse=True)
for i in range(k):
    print(sum_list[i])

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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)

class CakeSet:
    def __init__(self, status):
        self.status = status
        self.cost = calc_cost(status)

    def next_list(self):
        ret = []
        if self.status[0] + 1 < x:
            ret.append(CakeSet((self.status[0] + 1, self.status[1], self.status[2])))
        if self.status[1] + 1 < y:
            ret.append(CakeSet((self.status[0], self.status[1] + 1, self.status[2])))
        if self.status[2] + 1 < z:
            ret.append(CakeSet((self.status[0], self.status[1], self.status[2] + 1)))
        return ret

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

def calc_cost(status):
    return a[status[0]] + b[status[1]] + c[status[2]]

def main():
    memo = set()
    initial = CakeSet((0, 0, 0))
    costs = []
    queue = []
    heappush(queue, initial)
    while len(costs) < k:
        cakeSet = heappop(queue)
        if cakeSet.status in memo:
            continue
        costs.append(cakeSet.cost)
        memo.add(cakeSet.status)
        for next_set in cakeSet.next_list():
            heappush(queue, next_set)
    for cost in costs[:k]:
        print(cost)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import heapq

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

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


def f(i, j, k):
    x = A[i] + B[j] + C[k]
    return (-x, (i, j, k))


hq = []
heapq.heappush(hq, f(0, 0, 0))

ans = []
memo = set()
while len(ans) < K:
    v, (i, j, k) = heapq.heappop(hq)
    ans.append(-v)

    for x in range(3):
        a, b, c = i, j, k
        if x == 0:
            a += 1
        elif x == 1:
            b += 1
        else:
            c += 1
        v = (a << 20) + (b << 10) + c
        if v not in memo:
            heapq.heappush(hq, f(a, b, c))
            memo.add(v)

for x in ans:
    print(x)

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)

from heapq import heapify,heappop,heappush
q=[]
heappush(q,(-a[0]-b[0]-c[0],(0,0,0)))
used={(0,0,0):True}
for i in range(k):
    if q[0][1][0]+1<x and (q[0][1][0]+1,q[0][1][1],q[0][1][2]) not in used:
        heappush(q,(-a[q[0][1][0]+1]-b[q[0][1][1]]-c[q[0][1][2]],(q[0][1][0]+1,q[0][1][1],q[0][1][2])))
        used[(q[0][1][0]+1,q[0][1][1],q[0][1][2])]=True
    if q[0][1][1]+1<y and (q[0][1][0],q[0][1][1]+1,q[0][1][2]) not in used:
        heappush(q,(-a[q[0][1][0]]-b[q[0][1][1]+1]-c[q[0][1][2]],(q[0][1][0],q[0][1][1]+1,q[0][1][2])))
        used[(q[0][1][0],q[0][1][1]+1,q[0][1][2])]=True
    if q[0][1][2]+1<z and (q[0][1][0],q[0][1][1],q[0][1][2]+1) not in used:
        heappush(q,(-a[q[0][1][0]]-b[q[0][1][1]]-c[q[0][1][2]+1],(q[0][1][0],q[0][1][1],q[0][1][2]+1)))
        used[(q[0][1][0],q[0][1][1],q[0][1][2]+1)]=True
    print(-heappop(q)[0])

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

INF = 10**15

# 幅優先探索により解を求める
a = sorted(A, reverse=True) + [-INF]
b = sorted(B, reverse=True) + [-INF]
c = sorted(C, reverse=True) + [-INF]
q = []
heappush(q, (-(a[0] + b[0] + c[0]), 0, 0, 0))
m = 0
d = []
u = set()
while m < K:
    s, i, j, k = heappop(q)
    if (i, j, k) not in u:
        d.append(-s)
        u.add((i, j, k))
        heappush(q, (-(a[i + 1] + b[j] + c[k]), i + 1, j, k))
        heappush(q, (-(a[i] + b[j + 1] + c[k]), i, j + 1, k))
        heappush(q, (-(a[i] + b[j] + c[k + 1]), i, j, k + 1))
        m += 1
ans = '\n'.join(map(str, d))

# 出力
print(ans)

Submission status: Accepted

Solution code:
import heapq as hq
x, y, z, k = map(int,input().split())
arr_a = sorted(list(map(int,input().split())) ,reverse = True)
arr_b = sorted(list(map(int,input().split())) ,reverse = True)
arr_c = sorted(list(map(int,input().split())) ,reverse = True)

cnt = set()
P_q = [[-(arr_a[0] + arr_b[0] + arr_c[0]),0,0,0]]

cnt.add((0,0,0))
for _ in range(k):
    tmp = hq.heappop(P_q)
    t, i, j, k = -tmp[0],tmp[1],tmp[2],tmp[3]
    print(t)
    if i + 1 < x and (i+1,j,k) not in cnt:
        hq.heappush(P_q,[-(arr_a[i+1] + arr_b[j] + arr_c[k]) ,i + 1,j,k])
        cnt.add((i+1,j,k))
    if j + 1 < y and (i,j+1,k) not in cnt:
        hq.heappush(P_q,[-(arr_a[i] + arr_b[j+1] + arr_c[k]) ,i ,j + 1,k])
        cnt.add((i,j+1,k))
    if k + 1 < z and (i,j,k+1) not in cnt:
        hq.heappush(P_q,[-(arr_a[i] + arr_b[j] + arr_c[k + 1]) ,i,j,k + 1])
        cnt.add((i,j,k+1))

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)

D = []
for i in range(X):
    for j in range(Y):
        if (i + 1) * (j + 1) > K:
            break
        for k in range(Z):
            if (i + 1) * (j + 1) * (k + 1) > K:
                break
            D.append(A[i] + B[j] + C[k])
D.sort(reverse=True)
D = D[:K]
for d in D:
    print(d)

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)

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

D = D[:K]
E = []
for c in C:
    for d in D:
        E.append(c + d)
E.sort(reverse=True)

E = E[:K]
for e in E:
    print(e)

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)

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

result.sort(reverse=True)
for i, r in enumerate(result):
    print(r)
    if i == K -1 :
        break

Submission status: Accepted

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

dx = list(map(int, input().strip().split()))
dy = list(map(int, input().strip().split()))
dz = list(map(int, input().strip().split()))

dx.sort(reverse=True)
dy.sort(reverse=True)
dz.sort(reverse=True)

# print(dx, dy, dz)
xyz = []
for i in range(x):
    if i + 1  > k:
        break
    for j in range(y):
        if (i + 1) * ( j+ 1) > k:
            break
        for t in range(z):
            if (i + 1) * (j + 1) * (t + 1) > k:
                break
            xyz.append(dx[i] + dy[j] + dz[t])

xyz.sort(reverse=True)
# print(len(xyz))

for e in xyz[:k]:
    print(e)

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)

abc = []

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

abc.sort(reverse = True)

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

Submission status: Accepted

Solution code:
x,y,z,k=map(int,input().split())
list_a=list(map(int,input().split()))
list_b=list(map(int,input().split()))
list_c=list(map(int,input().split()))
list_a.sort(reverse=True)
list_b.sort(reverse=True)
list_c.sort(reverse=True)
dict_p={}
dict_p[(0,0,0)]=list_a[0]+list_b[0]+list_c[0]
pop_list=[]
for i in range(0,k):
    Max=max(dict_p.values())
    print(Max)
    for j in dict_p:
        if dict_p[j]==Max:
            now=j
            dict_p.pop(now)
            pop_list.append(now)
            break
    if now[0]+1<x:
        if not (now[0]+1,now[1],now[2]) in pop_list:
            dict_p[(now[0]+1,now[1],now[2])]=list_a[now[0]+1]+list_b[now[1]]+list_c[now[2]]
    if now[1]+1<y:
        if not (now[0],now[1]+1,now[2]) in pop_list:
            dict_p[(now[0],now[1]+1,now[2])]=list_a[now[0]]+list_b[now[1]+1]+list_c[now[2]]
    if now[2]+1<z:
        if not (now[0],now[1],now[2]+1) in pop_list:
            dict_p[(now[0],now[1],now[2]+1)]=list_a[now[0]]+list_b[now[1]]+list_c[now[2]+1]

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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)

Comb = []
for i in range(X):
    for j in range(Y):
        if (i + 1) * (j + 1) > K:
            break
        for k in range(Z):
            if (i + 1) * (j + 1) * (k + 1) > K:
                break
            Comb.append(A[i] + B[j] + C[k])

Comb.sort(reverse=True)
for i in range(K):
    print(Comb[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)
 
now = [0, 0, 0]
que = [[A[0] + B[0] + C[0], 0, 0, 0]]
memory = [[0, 0, 0]]
ans = []
while len(ans) < k:
  maxli = max(que, key = lambda x:x[0])
  ans.append(maxli[0])
  now = maxli[1:]
  que.remove(maxli)
  n0 = min(now[0] + 1, x - 1)
  n1 = min(now[1] + 1, y - 1)
  n2 = min(now[2] + 1, z - 1)
  a = A[n0]
  b = B[n1]
  c = C[n2]
  if [n0, now[1], now[2]] not in memory:
    que.append([a + B[now[1]] + C[now[2]], n0, now[1], now[2]])
    memory.append([n0, now[1], now[2]])
  if [now[0], n1, now[2]] not in memory:
    que.append([A[now[0]] + b + C[now[2]], now[0], n1, now[2]])
    memory.append([now[0], n1, now[2]])
  if [now[0], now[1], n2] not in memory:
    que.append([A[now[0]] + B[now[1]] + c, now[0], now[1], n2])
    memory.append([now[0], now[1], n2])
  if que == []:
    break

print(*ans, 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)

now = [0, 0, 0]
que = [[A[0] + B[0] + C[0], 0, 0, 0]]
memory = [[0, 0, 0]]
ans = []
while len(ans) < k:
  maxli = max(que, key = lambda x:x[0])
  ans.append(maxli[0])
  now = maxli[1:]
  que.remove(maxli)
  a = A[min(now[0] + 1, x - 1)]
  b = B[min(now[1] + 1, y - 1)]
  c = C[min(now[2] + 1, z - 1)]
  if [min(now[0] + 1, x - 1), now[1], now[2]] not in memory:
    que.append([a + B[now[1]] + C[now[2]], min(now[0] + 1, x - 1), now[1], now[2]])
    memory.append([min(now[0] + 1, x - 1), now[1], now[2]])
  if [now[0], min(now[1] + 1, y - 1), now[2]] not in memory:
    que.append([A[now[0]] + b + C[now[2]], now[0], min(now[1] + 1, y - 1), now[2]])
    memory.append([now[0], min(now[1] + 1, y - 1), now[2]])
  if [now[0], now[1], min(now[2] + 1, z - 1)] not in memory:
    que.append([A[now[0]] + B[now[1]] + c, now[0], now[1], min(now[2] + 1, z - 1)])
    memory.append([now[0], now[1], min(now[2] + 1, z - 1)])

print(*ans, 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)

now = [0, 0, 0]
que = [[A[0] + B[0] + C[0], 0, 0, 0]]
memory = [[A[0] + B[0] + C[0], 0, 0, 0]]
ans = []
while len(ans) < k:
  maxli = max(que, key = lambda x:x[0])
  ans.append(maxli[0])
  now = maxli[1:]
  que.remove(maxli)
  a = A[min(now[0] + 1, x - 1)]
  b = B[min(now[1] + 1, y - 1)]
  c = C[min(now[2] + 1, z - 1)]
  if [a + B[now[1]] + C[now[2]], min(now[0] + 1, x - 1), now[1], now[2]] not in memory:
    que.append([a + B[now[1]] + C[now[2]], min(now[0] + 1, x - 1), now[1], now[2]])
    memory.append([a + B[now[1]] + C[now[2]], min(now[0] + 1, x - 1), now[1], now[2]])
  if [A[now[0]] + b + C[now[2]], now[0], min(now[1] + 1, y - 1), now[2]] not in memory:
    que.append([A[now[0]] + b + C[now[2]], now[0], min(now[1] + 1, y - 1), now[2]])
    memory.append([A[now[0]] + b + C[now[2]], now[0], min(now[1] + 1, y - 1), now[2]])
  if [A[now[0]] + B[now[1]] + c, now[0], now[1], min(now[2] + 1, z - 1)] not in memory:
    que.append([A[now[0]] + B[now[1]] + c, now[0], now[1], min(now[2] + 1, z - 1)])
    memory.append([A[now[0]] + B[now[1]] + c, now[0], now[1], min(now[2] + 1, z - 1)])

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

Submission status: Accepted

Solution code:
params = [int(x) for x in input().split(" ")]

X = params[0]
Y = params[1]
Z = params[2]

K = params[3]

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)

history = []
ansList = []

queue = []

queue.append([A[0]+B[0]+C[0], 0,0,0])
history.append(queue[0])



while True:
	queue.sort(reverse=True)
	ansList.append(queue[0])

	if len(ansList) >= K:
		break

	Aindex = queue[0][1]
	Bindex = queue[0][2]
	Cindex = queue[0][3]

	queue = queue[1:]

	try:
		nextA = [A[Aindex+1]+B[Bindex]+C[Cindex], Aindex+1, Bindex, Cindex]
		if not nextA in history:
			queue.append(nextA)
			history.append(nextA)
	except:
		pass

	try:
		nextB = [A[Aindex]+B[Bindex+1]+C[Cindex], Aindex, Bindex+1, Cindex]
		if not nextB in history:
			queue.append(nextB)
			history.append(nextB)
	except:
		pass

	try:
		nextC = [A[Aindex]+B[Bindex]+C[Cindex+1], Aindex, Bindex, Cindex+1]
		if not nextC in history:
			queue.append(nextC)
			history.append(nextC)
	except:
		pass
		
for ans in ansList:
	print(ans[0])

Submission status: Accepted

Solution code:
x,y,z,K = map(int,input().split())
abc = [sorted(list(map(int,input().split())), reverse=True) for i in range(3)]

ans=[]
ab=[]

for i in range(x):
    for j in range(y):
        if (i+1)*(j+1) <= K:
            ab.append(abc[0][i]+abc[1][j])

ab.sort(reverse=True)

for i in range(min([len(ab),K])):
    for j in range(z):
        if (i+1)*(j+1) <= K:
            ans.append(ab[i]+abc[2][j])
    
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())
I = lambda: sorted(list(map(int, input().split())), reverse=True)
A = I()
B = I()
C = I()
hq = []

for a in A:
    for b in B:
        for c in C:
            if len(hq) < K:
                heappush(hq, a+b+c)
            else:
                tmp = heappop(hq)
                if tmp >= a+b+c:
                    heappush(hq, tmp)
                    break
                else:
                    heappush(hq, a+b+c)

ans = []
for _ in range(K):
    ans.append(heappop(hq))

ans.sort(reverse=True)
for x in ans:
    print(x)

Submission status: Accepted

Solution code:
from datetime import datetime as dt
X,Y,Z,K = list(map(int,input().split(" ")))
As = list(map(int,input().split(" ")))
Bs = list(map(int,input().split(" ")))
Cs = list(map(int,input().split(" ")))

As.sort(reverse = True )
Bs.sort(reverse = True )

ABs = []
for a in As:
    for b in Bs:
        ABs.append(a + b)
ABs.sort(reverse = True)
upper = min(len(ABs),K)

ABs = ABs[:upper]
ans = []

for C in Cs:
    for AB in ABs:
        ans.append(AB+C)
ans.sort(reverse = True)
for i in range(K):
    print(ans[i])

Submission status: Accepted

Solution code:
X,Y,Z,K = map(int,input().split())
A = [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)

AB = [a+b for a in A for b in B]
AB.sort(reverse=True)
AB = AB[:min(3001,X*Y*Z+1)]

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:
# 通ってるコードをコピペ、ほぼ同じはずなんだけどこれは通る？
x,y,z,k = map(int,input().split())
lisa = list(map(int,input().split()))
lisb = list(map(int,input().split()))
lisc = list(map(int,input().split()))
lisa.sort(reverse=True)
lisb.sort(reverse=True)
lisc.sort(reverse=True)
an = []
ans = []
for num in lisb:
    for nu in lisa:
        an.append(num+nu)
an.sort(reverse=True)
an = an[:min(3001,x*y*z+1)]
for num in lisc:
    for nu in an:
        ans.append(num+nu)
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Accepted

Solution code:
x,y,z,K = map(int, input().split())
abc = [sorted(list(map(int, input().split())), reverse=True) for i in range(3)]

ans = []
for i in range(x):
    for j in range(y):
        for k in range(z):
            if (i+1)*(j+1)*(k+1) <= K:
                ans.append(abc[0][i]+abc[1][j]+abc[2][k])
            else:
                break
for i in sorted(ans)[::-1][:K]:
    print(i)

Submission status: Accepted

Solution code:
from heapq import heappop, heappush

x,y,z,k = map(int, input().split())
abc = [sorted(list(map(int, input().split())), reverse=True) for i in range(3)]
heap = [[-sum(abc[i][0] for i in range(3)), (0,0,0)]]


cnt = 0
used = set()
while cnt < k:
    score, (a,b,c) = heappop(heap)
    if (a,b,c) in used:
        continue
    print(-score)
    used.add((a,b,c))
    cnt += 1
    for i in range(3):
        na = a+int(i==0)
        nb = b+int(i==1)
        nc = c+int(i==2)
        if 0<=na<x and 0<=nb<y and 0<=nc<z:
            nscore = 0
            for ind, j in enumerate([na,nb,nc]):
                nscore -= abc[ind][j]
            heappush(heap,[nscore, (na,nb,nc)])

Submission status: Accepted

Solution code:
import math
xyzk = list(map(int,input().split()))
abc = [list(map(int,input().split())) for a in range(3)]
for a in range(3):
    abc[a].sort()
    abc[a].reverse()
abc2=[]
for a in range(len(abc[0])):
    for b in range(len(abc[1])):
        if (a+1)*(b+1)>xyzk[3]:
            break
        for c in range(len(abc[2])):
            if (a+1)*(b+1)*(c+1)>xyzk[3] :
                break
            abc2.append(abc[0][a]+abc[1][b]+abc[2][c])
abc2.sort()
abc2.reverse()
for a in range(xyzk[3]):
    print(abc2[a])

Submission status: Accepted

Solution code:
import math
xyzk = list(map(int,input().split()))
abc = [list(map(int,input().split())) for a in range(3)]
ab=[]
for a in abc[0]:
    for b in abc[1]:
        ab.append(a+b)
ab.sort()
ab.reverse()
abc[2].sort()
abc[2].reverse()
abc2=[]
for c in range(len(abc[2])):
    for a in range(min([math.ceil(xyzk[3]),len(ab)])):
        abc2.append(abc[2][c]+ab[a])
abc2.sort()
abc2.reverse()
for a in range(xyzk[3]):
    print(abc2[a])

Submission status: Accepted

Solution code:
import math
xyzk = list(map(int,input().split()))
abc = [list(map(int,input().split())) for a in range(3)]
ab=[]
for a in abc[0]:
    for b in abc[1]:
        ab.append(a+b)
ab.sort()
ab.reverse()
abc[2].sort()
abc[2].reverse()
abc2=[]
for c in range(len(abc[2])):
    for a in range(min([math.ceil(xyzk[3]/(c+1)),len(ab)])):
        abc2.append(abc[2][c]+ab[a])
abc2.sort()
abc2.reverse()
for a in range(xyzk[3]):
    print(abc2[a])

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)
h = []
used = set()
used.add((0, 0, 0))
heappush(h, (-(A[0]+B[0]+C[0]), 0, 0, 0))
for i in range(K):
    val, x, y, z = heappop(h)
    print(-val)
    if x < X-1 and (x+1, y, z) not in used:
        used.add((x+1, y, z))
        heappush(h, (-(A[x+1]+B[y]+C[z]), x+1, y, z))
    if y < Y-1 and (x, y+1, z) not in used:
        used.add((x, y+1, z))
        heappush(h, (-(A[x]+B[y+1]+C[z]), x, y+1, z))
    if z < Z-1 and (x, y, z+1) not in used:
        used.add((x, y, z+1))
        heappush(h, (-(A[x]+B[y]+C[z+1]), x, y, z+1))

Submission status: Accepted

Solution code:
'''
Implement and make use of priority queue using heapq module.
Using priority queue(heapq) is fast because all of insertion, deletion, and popping is done by order O(log n).

heapq documentation: https://docs.python.org/ja/3/library/heapq.html
'''

import heapq
# heapq a.k.a priority queue

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)


# Initialize heap.
# Values are given as negative integers, because python heapq is a min-heap and pops minimum value in it.
# The form of values are tuple of (-value, indices of A,B,C).
# Indices are used to push new candidates to the heap after pop.
candidates = [] # name of the heap
heapq.heappush(candidates, (-(A[0]+B[0]+C[0]), 0,0,0))

# Initialize a set to store indices of A, B, and C so that there won't be any duplicates.
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 = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
assert len(A) == X and len(B) == Y and len(C) == Z

# to reverse the order
A = [-x for x in A]
B = [-x for x in B]
C = [-x for x in C]

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

def val(pos):
    a, b, c = pos
    return A[a] + B[b] + C[c]

def nextpos(pos):
    a, b, c = pos
    if a + 1 < len(A):
        yield (a + 1, b, c)
    if b + 1 < len(B):
        yield (a, b + 1, c)
    if c + 1 < len(C):
        yield (a, b, c + 1)

def cakelist():
    H = []  # heap
    heapq.heappush(H, (val((0, 0, 0)), (0, 0, 0)))
    visited = {(0, 0, 0)}
    while len(H) > 0:
        v, pos = heapq.heappop(H)
        yield -v
        for npos in nextpos(pos):
            if npos not in visited:
                heapq.heappush(H, (val(npos), npos))
                visited.add(npos)

for x, _ in zip(cakelist(), range(K)):
    print(x)

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()))
assert len(A) == X and len(B) == Y and len(C) == Z

# to reverse the order
A = [-x for x in A]
B = [-x for x in B]
C = [-x for x in C]

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

def val(pos):
    a, b, c = pos
    return A[a] + B[b] + C[c]

def nextpos(pos):
    a, b, c = pos
    if a + 1 < len(A):
        yield (a + 1, b, c)
    if b + 1 < len(B):
        yield (a, b + 1, c)
    if c + 1 < len(C):
        yield (a, b, c + 1)

def cakelist():
    H = []  # heap
    heapq.heappush(H, (val((0, 0, 0)), (0, 0, 0)))
    visited = {(0, 0, 0)}
    while len(H) > 0:
        v, pos = heapq.heappop(H)
        yield -v
        for npos in nextpos(pos):
            if npos not in visited:
                heapq.heappush(H, (val(npos), npos))
                visited.add(npos)

cl = cakelist()
for i in range(K):
    print(next(cl))