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())
al=[int(i) for i in input().split()]
bl=[int(i) for i in input().split()]
cl=[int(i) for i in input().split()]
lst=[i+j for i in al for j in bl]
lst.sort(reverse=True)
num=min(x*y,k)
lst=lst[:num]
L=[i+j for i in cl for j in lst]
L.sort(reverse=True)
for i in range(k):
  print(L[i])

Submission status: Accepted

Solution code:
import heapq

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

a = sorted(a, key=lambda x: x * -1)
b = sorted(b, key=lambda x: x * -1)
c = sorted(c, key=lambda x: x * -1)

counter = 0
a_idx = 0
b_idx = 0
c_idx = 0
pque = []
heapq.heappush(pque, (-1 * (a[a_idx] + b[b_idx] + c[c_idx]), (a_idx, b_idx, c_idx)))

while counter < k:
    counter += 1
    output = heapq.heappop(pque)
    print(output[0] * -1)
    a_idx, b_idx, c_idx = output[1]
    if a_idx < len(a) - 1:
        current = a[a_idx + 1] + b[b_idx] + c[c_idx]
        cand = (a_idx + 1, b_idx, c_idx)
        if (-1 * current, cand) not in pque:
            heapq.heappush(pque, (-1 * current, cand))
    if b_idx < len(b) - 1:
        current = a[a_idx] + b[b_idx + 1] + c[c_idx]
        cand = (a_idx, b_idx + 1, c_idx)
        if (-1 * current, cand) not in pque:
            heapq.heappush(pque, (-1 * current, cand))
    if c_idx < len(c) - 1:
        current = a[a_idx] + b[b_idx] + c[c_idx + 1]
        cand = (a_idx, b_idx, c_idx + 1)
        if (-1 * current, cand) not in pque:
            heapq.heappush(pque, (-1 * current, cand))

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)

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

for abc in ABC:
    print(abc)

Submission status: Accepted

Solution code:
# coding: utf-8

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

ab = [ai + bi for ai in a for bi in b]
ab.sort(reverse=True)
ab = ab[:k]

abc = [abi + ci for abi in ab for ci in c]
abc.sort(reverse = True)

for i in range(len(abc)):
  if i < k:
    print(abc[i])
  else:
    break

Submission status: Accepted

Solution code:
import itertools

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()]
 
list_ = [b+c for (b,c) in itertools.product(B, C)]
list_.sort(reverse=True)
 
list__ = [ab+c for (ab,c) in itertools.product(list_[:K], A)]
list__.sort(reverse=True)
for N in list__[:K]:
  print(N)

Submission status: Accepted

Solution code:
import queue
pq=queue.PriorityQueue()
X, Y, Z, K= map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
B=list(map(int,input().split()))
B.sort(reverse=True)
C=list(map(int,input().split()))
C.sort(reverse=True)
searched={(0,0,0)}
pq.put((-(A[0]+B[0]+C[0]),(0,0,0)))
D=[[1,0,0],[0,1,0],[0,0,1]]

for _ in range(K):
    value,index=pq.get()
    x,y,z=index
    print(-value)
    for dx,dy,dz in D:
        if x+dx<X and y+dy < Y and z+dz < Z and (x+dx,y+dy,z+dz) not in searched:
            pq.put((-(A[x+dx]+B[y+dy]+C[z+dz]),(x+dx,y+dy,z+dz)))
            searched.add((x+dx,y+dy,z+dz))

Submission status: Accepted

Solution code:
def int_raw():
    return int(input())

def ss_raw():
    return input().split()

def ints_raw():
    return list(map(int,ss_raw()))

X,Y,Z,K = ints_raw()

A = list(map(int, input().split()))
B= list(map(int, input().split()))
C= list(map(int, input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
def main():
    D = [a+b for a in A for b in B]
    D.sort(reverse=True)
    D = D[:K+1]
    E =[d+c for d in D for c in C]
    E.sort(reverse=True)
    for e in range(K):
        print (E[e])

main()

Submission status: Accepted

Solution code:
import itertools


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

AB = [a + b for (a, b) in list(itertools.product(A, B))]
AB.sort(reverse=True)
ans = [ab + c for (ab, c) in list(itertools.product(AB[:min(3000, X*Y*Z)], 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())
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])
#sample code.

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)

cake_heap = []
for i in range(len(A)):
    if i + 1 > K:
        break
    for j in range(len(B)):
        if (i + 1) * (j + 1) > K:
            break
        for k in range(len(C)):
            if (i + 1) * (j + 1) * (k + 1) > K:
                break
            heappush(cake_heap, -(A[i] + B[j] + C[k]))
for i in range(K):
    print(-heappop(cake_heap))

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 a in range(X):
    for b in range(Y):
        if (a+1) * (b+1) > K:
            break
        for c in range(Z):
            if (a+1) * (b+1) * (c+1) > K:
                break
            ABC.append(A[a] + B[b] + C[c])
ABC.sort(reverse=True)
print("\n".join(map(str, ABC[:K])))

Submission status: Accepted

Solution code:
def int_raw():
    return int(input())

def ss_raw():
    return input().split()

def ints_raw():
    return list(map(int,ss_raw()))

X,Y,Z,K = ints_raw()

A = list(map(int, input().split()))
B= list(map(int, input().split()))
C= list(map(int, input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
def main():
    D = []
    for a in A:
        for b in B:
            D.append(a+b)
    D.sort(reverse=True)
    D = D[:K+1]
    E =[]
    for d in D:
        for c in C:
            E.append(d+c)
    E.sort(reverse=True)
    for e in range(K):
        print (E[e])

main()

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

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

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

def total(x, y, z):
    return A[x] + B[y] + C[z]

def expand(p, q, r):
    result = []
    if p + 1 < X:
        result.append((p+1, q, r))
    if q + 1 < Y:
        result.append((p, q+1, r))
    if r + 1 < Z:
        result.append((p, q, r+1))
    return result

k = 0
h = []
ind = None
heappush(h, (-total(0,0,0), (0,0,0)))

while k < K:
    val, _ind = heappop(h)
    
    if not _ind == ind:
        k += 1
        ind = _ind
        print(-val)
        for new_ind in expand(*ind):
            new_val = total(*new_ind)
            heappush(h, (-new_val, new_ind))

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
input = sys.stdin.readline

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)

bimi = []

for a in range(X):
        for b in range(Y):
                if (a + 1) * (b + 1) > K:
                                break

                for c in range(Z):
                        if (a + 1) * (b + 1) * (c + 1) > K:
                                break

                        bimi.append(A[a] + B[b] + C[c])

bimi.sort(reverse=True)

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

Submission status: Accepted

Solution code:
import heapq
hq=[]
X, Y, Z, K= map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
B=list(map(int,input().split()))
B.sort(reverse=True)
C=list(map(int,input().split()))
C.sort(reverse=True)
searched={(0,0,0)}
heapq.heappush(hq,(-(A[0]+B[0]+C[0]),0,0,0))
D=[[1,0,0],[0,1,0],[0,0,1]]

for _ in range(K):
    abc,x,y,z=heapq.heappop(hq)
    print(-abc)
    for dx,dy,dz in D:
        if x+dx<X and y+dy < Y and z+dz < Z and (x+dx,y+dy,z+dz) not in searched:
            heapq.heappush(hq,(-(A[x+dx]+B[y+dy]+C[z+dz]),x+dx,y+dy,z+dz))
            searched.add((x+dx,y+dy,z+dz))

Submission status: Accepted

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

A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))

val_list = []


A_list = sorted(A_list, reverse=True)
B_list = sorted(B_list, reverse=True)
C_list = sorted(C_list, reverse=True)

#print(A_list)
#print(B_list)
#print(C_list)

for a in range(len(A_list)):
    if a + 1 > K:
        break
    for b in range(len(B_list)):
        if b + 1 > K:
            break
        if (a+1) * (b+1) > K:
            break
            
        for c in range(len(C_list)):
            if c + 1 > K:
                break
            if (a+1) * (b+1) * (c+1) > K:
                break
        
            val_list.append(A_list[a] + B_list[b] + C_list[c])

val_list = sorted(val_list, reverse=True)
for i in range(K):
    print(val_list[i])

Submission status: Accepted

Solution code:
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
AB = []
for a in A:
    for b in B:
        AB.append(a + b)
AB.sort(reverse=True)
C.sort(reverse=True)
ans = []
for ab in AB[:K]:
    for c in C[:K]:
        ans.append(ab + c)
ans.sort(reverse=True)
for i in range(K):
    print(ans[i])

Submission status: Accepted

Solution code:
I=lambda:list(map(int,input().split()));k=I()[3];s=lambda a,b:sorted(i+j for i in a for j in b)[:-k-1:-1];print(*s(s(I(),I()),I()))

Submission status: Accepted

Solution code:
from numpy import*;I=lambda:int64(input().split());k=I()[3];s=lambda a,b:sort(ravel([b+i for i in a]))[:-k-1:-1];print(*s(s(I(),I()),I()))

Submission status: Accepted

Solution code:
X, Y, Z, K = map(int, input().split())
A = sorted(map(int, input().split()))
B = sorted(map(int, input().split()))
C = sorted(map(int, input().split()))
ab=sorted(s+t for t in B for s in A)[-1:-K-1:-1]
abc=sorted(i+x for i in ab for x in C)[-1:-K-1:-1]
print(*abc,sep="\n")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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()))

la, lb, lc = len(a), len(b), len(c)

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

fix = set()

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

for _ in range(k):
    abc, i, j, l = heapq.heappop(q)
    print(-abc)

    if i+1 < la and (i+1, j, l) not in fix:
        heapq.heappush(q, (-(a[i+1]+b[j]+c[l]), i+1, j, l))
        fix.add((i+1, j, l))

    if j+1 < lb and (i, j+1, l) not in fix:
        heapq.heappush(q, (-(a[i]+b[j+1]+c[l]), i, j+1, l))
        fix.add((i, j+1, l))

    if l+1 < lc and (i, j, l+1) not in fix:
        heapq.heappush(q, (-(a[i]+b[j]+c[l+1]), i, j, l+1))
        fix.add((i, j, l+1))

Submission status: Accepted

Solution code:
from heapq import heappush,heappop

def int_raw():
    return int(input())

def ss_raw():
    return input().split()

def ints_raw():
    return list(map(int,ss_raw()))

X,Y,Z,K = ints_raw()

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

arg_hash = {}

def main():
    Q = []
    heappush(Q,(-A[0]-B[0]-C[0],0,0,0))
    arg_hash[(0,0,0)]=1
    for k in range(K):
        ans = heappop(Q)
        print(-ans[0])
        a,b,c = ans[1:]

        argA = (a+1,b,c)
        if a < X-1 and (not argA in arg_hash):
            arg_hash[argA]=1
            heappush(Q,(-A[a+1]-B[b]-C[c],)+argA)
        argB = (a,b+1,c)
        if b < Y-1 and (not argB in arg_hash):
            arg_hash[argB]=1
            heappush(Q, (-A[a]-B[b+1]-C[c],)+argB)
        argC = (a,b,c+1)
        if c < Z-1 and (not argC in arg_hash):
            arg_hash[argC]=1
            heappush(Q,(-A[a]-B[b]-C[c+1],)+argC)
        
main()

Submission status: Accepted

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

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

AB = []

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

AB.sort(reverse=True)

ABC = []

for ab in AB[:K]:
    for c in C:
        ABC.append(ab + c)

ABC.sort(reverse=True)

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

X, Y, Z, K = MAP()
A = LIST()
B = LIST()
C = LIST()

A=sorted(A)[::-1]
B=sorted(B)[::-1]
C=sorted(C)[::-1]
ans = []
for i in range(len(A)):
	for j in range(len(B)):
		for k in range(len(C)):
			if (i+1)*(j+1)*(k+1) <= K:
				ans.append(A[i]+B[j]+C[k])
			else:
				break
for i in sorted(ans)[::-1][0:K]:
	print(i)

Submission status: Accepted

Solution code:
X, Y, Z, K = map(int, input().split())
A = sorted(map(int, input().split()))
B = sorted(map(int, input().split()))
C = sorted(map(int, input().split()))
ab=sorted(s+t for t in B for s in A)[-1:-K-1:-1]
abc=sorted(i+x for i in ab for x in C)[-1:-K-1:-1]
print("\n".join(map(str,abc)))

Submission status: Accepted

Solution code:
import heapq
used = {}


def is_used(a, b, c):
    hash = (a, b, c)
    ret = hash in used
    used[hash] = 1
    return ret


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)

la = len(A)
lb = len(B)
lc = len(C)

Q = []
heapq.heappush(Q, (-(A[0] + B[0] + C[0]), 0, 0, 0))

ans = []
while len(ans) < K:
    q = heapq.heappop(Q)
    ans.append(-q[0])
    if q[1] != la - 1:
        ai = q[1] + 1
        bi = q[2]
        ci = q[3]
        if not is_used(ai, bi, ci):
            heapq.heappush(Q, (-(A[ai] + B[bi] + C[ci]), ai, bi, ci))
    if q[2] != lb - 1:
        ai = q[1]
        bi = q[2] + 1
        ci = q[3]
        if not is_used(ai, bi, ci):
            heapq.heappush(Q, (-(A[ai] + B[bi] + C[ci]), ai, bi, ci))
    if q[3] != lc - 1:
        ai = q[1]
        bi = q[2]
        ci = q[3] + 1
        if not is_used(ai, bi, ci):
            heapq.heappush(Q, (-(A[ai] + B[bi] + C[ci]), ai, bi, ci))

for i, a in enumerate(ans):
    print(a)

Submission status: Accepted

Solution code:
from heapq import heappop, heappush

x, y, z, k = map(int, input().split())
aaa = sorted(map(int, input().split()), reverse=True)
bbb = sorted(map(int, input().split()), reverse=True)
ccc = sorted(map(int, input().split()), reverse=True)

q = [(-(aaa[0] + bbb[0] + ccc[0]), 0, 0, 0)]
ans = []
added = {(0, 0, 0)}
for _ in [0] * k:
    s, ai, bi, ci = heappop(q)
    ans.append(-s)
    for da, db, dc in ((1, 0, 0), (0, 1, 0), (0, 0, 1)):
        aj, bj, cj = ai + da, bi + db, ci + dc
        if aj >= x or bj >= y or cj >= z:
            continue
        if (aj, bj, cj) in added:
            continue
        added.add((aj, bj, cj))
        heappush(q, (-(aaa[aj] + bbb[bj] + ccc[cj]), aj, bj, cj))

print('\n'.join(map(str, ans)))

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)

INF = 100000000000

AB = []

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

AB.sort(reverse=True)

ABC = []

for ab in AB[:K]:
    for c in C:
        ABC.append(ab + c)

ABC.sort(reverse=True)

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

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

res = []
x, y, z, K = na()
a_li = na()
b_li = na()
c_li = na()
list.sort(a_li, reverse=True)
list.sort(b_li, reverse=True)
list.sort(c_li, reverse=True)
for i in range(x):
    for j in range(y):
        for k in range(z):
            if (i + 1) * (j + 1) * (k + 1) <= K:
                res.append(a_li[i] + b_li[j] + c_li[k])
            else:
                break
list.sort(res, reverse=True)
for i in range(K):
    print(res[i])

Submission status: Accepted

Solution code:
from numpy import*;I=lambda:int64(input().split());x,y,z,k=I();s=lambda a,b:sort(ravel([b+i for i in a]))[:-k-1:-1];print(*s(s(I(),I()),I()))

Submission status: Accepted

Solution code:
import math

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

Ai= sorted(A, reverse=True)
Bi= sorted(B, reverse=True)
Ci= sorted(C, reverse=True)

def su(a,b,c):
	return(Ai[a]+Bi[b]+Ci[c])

a=0
b=0
c=0

pq=[[su(a,b,c),a,b,c]]
ck=[a,b,c]

dl=0
for i in range(k):
	dl=pq.index(max(pq))
	res=pq.pop(dl)
	print(res[0])

	a=res[1]
	b=res[2]
	c=res[3]
	if a+1<=x-1 and (not [a+1,b,c] in ck):
		pq.append([su(a+1,b,c),a+1,b,c])
		ck.append([a+1,b,c])
	if b+1<=y-1 and (not [a,b+1,c] in ck):
		pq.append([su(a,b+1,c),a,b+1,c])
		ck.append([a,b+1,c])
	if c+1<=z-1 and (not [a,b,c+1] in ck):
		pq.append([su(a,b,c+1),a,b,c+1])
		ck.append([a,b,c+1])

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)

res = []

for i in range(X):
    for j in range(Y):
        sup = min(K //((i+1)*(j+1)),Z)
        for k in range(sup):
            res.append(A[i]+B[j]+C[k])

res.sort(reverse=True)

for ans in res[:K]:
    print(ans)

Submission status: Accepted

Solution code:
#実験をしっかりとしよう
X,Y,Z,K = (int(_) for _ in input().split())
A = (int(_) for _ in input().split())
B = (int(_) for _ in input().split())
C = (int(_) for _ in input().split())
#この手の問題はまずsortする(今回は大きい順の方がすっきり)
A = sorted(A)
B = sorted(B)
C = sorted(C)
#Aのa位とb位とc位ならその上にはa*b*c-1(自分以外)が上にいる。
A.reverse()
B.reverse()
C.reverse()

ABC = []
for i in range(len(A)):
    for j in range(len(B)):
        if (i+1)*(j+1) > K:
            break
        for k in range(len(C)):
            if (i+1)*(j+1)*(k+1) > K:
                break
            ABC.append(A[i]+B[j]+C[k])
ABC.sort()
ABC.reverse()

for i in range(K):
    print(ABC[i])
#pythonだとTLEになる?

Submission status: Accepted

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


ALL=[0]*K
for a in A:
    for b in B:
        for c in C:
            if a+b+c>ALL[0]:
                heapq.heappush(ALL,a+b+c)
                heapq.heappop(ALL)
            else:
                break
                
ALL.sort(reverse=True)

for k in range(K):
    print(ALL[k])

Submission status: Accepted

Solution code:
from heapq import *

def int_raw():
    return int(input())

def ss_raw():
    return input().split()

def ints_raw():
    return list(map(int,ss_raw()))

X,Y,Z,K = ints_raw()

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

arg_hash = {}

def main():
    Q = []
    heappush(Q,(-A[0]-B[0]-C[0],0,0,0))
    arg_hash[(0,0,0)]=1
    for k in range(K):
        ans = heappop(Q)
        print(-ans[0])
        a,b,c = ans[1:]

        argA = (a+1,b,c)
        if a < X-1 and (not argA in arg_hash):
            arg_hash[argA]=1
            heappush(Q,(-A[a+1]-B[b]-C[c],)+argA)
        argB = (a,b+1,c)
        if b < Y-1 and (not argB in arg_hash):
            arg_hash[argB]=1
            heappush(Q, (-A[a]-B[b+1]-C[c],)+argB)
        argC = (a,b,c+1)
        if c < Z-1 and (not argC in arg_hash):
            arg_hash[argC]=1
            heappush(Q,(-A[a]-B[b]-C[c+1],)+argC)
        
main()

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

x, y, z, k = map(int, input().split())
a = [int(m) for m in input().split()]
b = [int(m) for m in input().split()]
c = [int(m) for m in input().split()]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
Q = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
ansli = []
se = set()
se.add("0,0,0")
for i in range(k):
    tmp = heappop(Q)
    ansli.append(tmp)
    if tmp[1] != x - 1 and str(tmp[1] + 1) + "," + str(tmp[2]) + "," + str(tmp[3]) not in se:
        heappush(Q, [-(a[tmp[1] + 1] + b[tmp[2]] + c[tmp[3]]), tmp[1] + 1, tmp[2], tmp[3]])
        se.add(str(tmp[1] + 1) + "," + str(tmp[2]) + "," + str(tmp[3]))
    if tmp[2] != y - 1 and str(tmp[1]) + "," + str(tmp[2] + 1) + "," + str(tmp[3]) not in se:
        heappush(Q, [-(a[tmp[1]] + b[tmp[2] + 1] + c[tmp[3]]), tmp[1], tmp[2] + 1, tmp[3]])
        se.add(str(tmp[1]) + "," + str(tmp[2] + 1) + "," + str(tmp[3]))
    if tmp[3] != z - 1 and str(tmp[1]) + "," + str(tmp[2]) + "," + str(tmp[3] + 1) not in se:
        heappush(Q, [-(a[tmp[1]] + b[tmp[2]] + c[tmp[3] + 1]), tmp[1], tmp[2], tmp[3] + 1])
        se.add(str(tmp[1]) + "," + str(tmp[2]) + "," + str(tmp[3] + 1))

for i in range(k):
    print(-ansli[i][0])

Submission status: Accepted

Solution code:
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.sort()
A = A[::-1]
B.sort()
B = B[::-1]
C.sort()
C = C[::-1]

ABC = []

for a in range(len(A)):
    for b in range(len(B)):
        if (a+1)*(b+1) > k:
            break
        for c in range(len(C)):
            if (a+1)*(b+1)*(c+1) > k:
                break
            #print(A[a], B[b], C[c])
            ABC.append(A[a]+B[b]+C[c])

ABC.sort()
ABC = ABC[::-1]

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

Submission status: Accepted

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

q = [(a[0]+b[0]+c[0], 0, 0, 0)]
t = []
next_a = (0,0,0,0)
next_b = (0,0,0,0)
next_c = (0,0,0,0)
for i in range(min(x*y*z, k)):
  m = q.pop(q.index(max(q, key=lambda x: x[0])))
  print(m[0])
  if m[1] < x-1:
    next_a = (a[m[1]+1]+b[m[2]]+c[m[3]], m[1]+1, m[2], m[3])
  if m[2] < y-1:
    next_b = (a[m[1]]+b[m[2]+1]+c[m[3]], m[1], m[2]+1, m[3])
  if m[3] < z-1:
    next_c = (a[m[1]]+b[m[2]]+c[m[3]+1], m[1], m[2], m[3]+1)
  if next_a not in t:
    q.append(next_a)
    t.append(next_a)
  if next_b not in t:
    q.append(next_b)
    t.append(next_b)
  if next_c not in t:
    q.append(next_c)
    t.append(next_c)

Submission status: Accepted

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

ABC = []
for xi in range(X):
    if xi+1 > K:
        break
    for yi in range(Y):
        if (xi+1)*(yi+1) > K:
            break
        for zi in range(Z):
            if (xi+1)*(yi+1)*(zi+1) > K:
                break
            ABC.append(A[xi]+B[yi]+C[zi])
ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])

Submission status: Accepted

Solution code:
I=lambda:list(map(int,input().split()));x,y,z,k=I();S=sorted;s=lambda a,b:S(i+j for i in a for j in b)[:-k-1:-1];print(*s(s(S(I()),S(I())),S(I())))

Submission status: Accepted

Solution code:
from heapq import heappop, heappush

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

counted = set()
h = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
for _ in range(K):
    s, i, j, k = heappop(h)
    print(-s)
    if X > i + 1 and (i + 1, j, k) not in counted:
        heappush(h, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
        counted.add((i + 1, j, k))
    if Y > j + 1 and (i, j + 1, k) not in counted:
        heappush(h, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
        counted.add((i, j + 1, k))
    if Z > k + 1 and (i, j, k + 1) not in counted:
        heappush(h, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
        counted.add((i, j, k + 1))

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

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

AB.sort(reverse=True)
AB=AB[:K]

ALL=[]

for ab in AB:
    for c in C:
        ALL.append(ab+c)

ALL.sort(reverse=True)

for k in range(K):
    print(ALL[k])

Submission status: Accepted

Solution code:
import collections

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)

a15 = a[-15:]
b15 = b[-15:]
c15 = c[-15:]

oisi = []

for i, aa in enumerate(a[:k]):
    for j, bb in enumerate(b[:(k//(i+1))+2]):
        for l,cc in enumerate(c[:(k//((i+1)*(j+1)))+2]):
            oisi.append(aa+bb+cc)
#            print(i,j,l)

#for i, aa in enumerate(a[:k]):
#    for j, bb in enumerate(b[:k]):
#        for l,cc in enumerate(c[:k]):
#            print(i,j,l,aa+bb+cc)

#for aa in a15:
#    for bb in b15:
 #       for cc in c:
#            oisi.append(aa+bb+cc)

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

for j in oisi[:k]:
    print(j)

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

for cakes in A, B, C:
    cakes.sort(reverse=True)

cake_values = []
for i in range(len(A)):
    if i + 1 > K:
        break
    for j in range(len(B)):
        if (i + 1) * (j + 1) > K:
            break
        for k in range(len(C)):
            if (i + 1) * (j + 1) * (k + 1) > K:
                break
            cake_values.append(A[i] + B[j] + C[k])
cake_values.sort(reverse=True)
for i in range(K):
    print(cake_values[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)

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