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 = list(map(int,input().split()))
A = [(sorted(list(map(int,input().split())))[::-1]) for i in range(3)]

import heapq

heap = []
item = (0,(0,0,0))
heapq.heappush(heap, item)
considered_dict = set(item[1])

ret = []
max_ = A[0][0]+A[1][0]+A[2][0]
while len(ret) < K:
    task  = heapq.heappop(heap)
    score,(x,y,z) =task
    ret.append(score)
    # assign next task
    if x+1 != X and (x+1,y,z) not in considered_dict:
        heapq.heappush(heap, (max_-A[0][x+1]-A[1][y]-A[2][z],(x+1,y,z) ))
        considered_dict |= {(x+1,y,z)}
        
                       
    if y+1 != Y and (x,y+1,z) not in considered_dict:
        heapq.heappush(heap, (max_-A[0][x]-A[1][y+1]-A[2][z],(x,y+1,z) ))
        considered_dict |= {(x,y+1,z)}
                       
    if z+1 != Z and (x,y,z+1) not in considered_dict:
        heapq.heappush(heap, (max_-A[0][x]-A[1][y]-A[2][z+1],(x,y,z+1) ))
        considered_dict |= {(x,y,z+1)}


for i in ret:
    print(max_-i)

Submission status: Accepted

Solution code:
def main():
    x,y,z,k = map(int,input().split())
    lia = sorted(list(map(int,input().split())))
    lib = sorted(list(map(int,input().split())))
    lic = sorted(list(map(int,input().split())))

    li2 = sorted(list(a+b for a in lia for b in lib),reverse = True)[:k]
    li3 = sorted(list(ab + c for ab in li2 for c in lic),reverse = True)[:k]

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

main()

Submission status: Accepted

Solution code:
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()
a.reverse()
b.sort()
b.reverse()
c.sort()
c.reverse()

a2=a[:k]
b2=[]
for i in a2:
  for j in b[:k]:
    b2.append(i+j)
  b2.sort()
  b2.reverse()
  b2=b2[:k]

c2=[]
for i in b2:
  for j in c[:k]:
    c2.append(i+j)
  c2.sort()
  c2.reverse()
  c2=c2[:k]
for i in c2:
  print(i)

Submission status: Accepted

Solution code:
import heapq
x,y,z,t=map(int,input().split())
a,b,c=[sorted(list(map(int,input().split())),reverse=True) for i in range(3)]
heap=[(-a[0]-b[0]-c[0],0,0,0)]
ed=set()
for _ in range(t):
	p,i,j,k=heapq.heappop(heap)
	print(-p)
	if i+1<x and (i+1,j,k) not in ed:
		ed.add((i+1,j,k))
		heapq.heappush(heap,(-a[i+1]-b[j]-c[k],i+1,j,k))
	if j+1<y and (i,j+1,k) not in ed:
		ed.add((i,j+1,k))
		heapq.heappush(heap,(-a[i]-b[j+1]-c[k],i,j+1,k))
	if k+1<z and (i,j,k+1) not in ed:
		ed.add((i,j,k+1))
		heapq.heappush(heap,(-a[i]-b[j]-c[k+1],i,j,k+1))

Submission status: Accepted

Solution code:
import heapq

X, Y, Z, K = [int(i) for i in input().split()]
ABCList = [sorted([int(i) for i in input().split()], reverse=True) for _ in range(3)]

pQ = []
heapq.heappush(pQ, [sum(ABC[0] for ABC in ABCList) * (-1), [0, 0, 0]])
QList = [(0, 0, 0)]
for _ in range(K):
    item = heapq.heappop(pQ)
    print(item[0] * (-1))
    abc = item.copy()[1]
    for i, element in enumerate(abc):
        abcTemp = abc.copy()
        abcTemp[i] += 1
        if abcTemp[i] < len(ABCList[i]) and not tuple(abcTemp) in QList:
            newItem = [sum([ABCList[i][abcTemp[i]] for i in range(3)]) * (-1), abcTemp]
            heapq.heappush(pQ, newItem)
            QList.append(tuple(abcTemp))

Submission status: Accepted

Solution code:
from heapq import heappop, heappush

X,Y,Z,K = map(int, input().split())
xs = [int(x) for x in input().split()]
ys = [int(y) for y in input().split()]
zs = [int(z) for z in input().split()]
xs.sort()
xs.reverse()
ys.sort()
ys.reverse()
zs.sort()
zs.reverse()

# print(xs)

def bfs(max = 3000):
    count = 0
    pq = []
    checked = {}

    heappush(pq, (-(xs[0]+ys[0]+zs[0]), (0,0,0)))
    checked[(0,0,0)] = 1
    while count < max:
        value, (x, y, z) = heappop(pq)
        print(-value)
        count += 1
        if x+1 < X and not (x+1, y, z) in checked:
            checked[(x+1, y, z)] = 1
            heappush(pq, (-(xs[x+1]+ys[y]+zs[z]), (x+1, y, z)))
        if y+1 < Y and not (x, y+1, z) in checked:
            checked[(x, y+1, z)] = 1
            heappush(pq, (-(xs[x]+ys[y+1]+zs[z]), (x, y+1, z)))
        if z+1 < Z and not (x, y, z+1) in checked:
            checked[(x, y, z+1)] = 1
            heappush(pq, (-(xs[x]+ys[y]+zs[z+1]), (x, y, z+1)))

bfs(K)

Submission status: Accepted

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

ans = []
for x in range(X):
    for y in range(Y):
        ans.append(A[x]+B[y])

ans.sort(reverse=True)
C.sort(reverse=True)
ans2 = []
for k in range(min(K,X*Y)):
    for z in range(min(Z,K)):
        ans2.append(ans[k]+C[z])
ans2.sort(reverse=True)

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

Submission status: Accepted

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

from heapq import heappush, heappop
def io_generator():
	return input()

#+++++++++++++++++++
	
class Foo(object):
	def __init__(self, value, state):
		self.state = state
		self.value = value

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

def get_c(a_state, a_mm):
	ret=[]
	for i, _ in enumerate(a_state):
		if a_state[i] >= a_mm[i]-1:
			continue
		ret.append([v + (i==j) for j,v in enumerate(a_state)])

	return ret
	
def gv(a_l,a_state):
	ret= sum([l[s] for l, s in zip(a_l, a_state)])
	return ret
	
def lss(l):
	return '_'.join([str(i) for i in l])
	
def main(io):
	x,y,z,k=map(int, io().split())
	ml=[x,y,z]
	al =list(map(int, io().split()))
	al.sort(reverse=True)
	bl =list(map(int, io().split()))
	bl.sort(reverse=True)
	cl =list(map(int, io().split()))
	cl.sort(reverse=True)
	ls=[al,bl,cl]
	ggv=lambda l : gv(ls,l)
	
	qq = []
	ol=set()
	
	init=[0,0,0]
	heappush(qq,Foo(ggv(init), init))
	ol.add(lss(init))
	
	for _ in range(k):
		ff=heappop(qq)
		print(ff.value)
		cs=ff.state
		nc=get_c(cs,ml)
		for ns in nc:
			if lss(ns) in ol:
				continue
			else:
				heappush(qq, Foo(ggv(ns), ns))
				ol.add(lss(ns))
	return 1

#++++++++++++++++++++

if __name__ == "__main__":
	io= lambda : io_generator()
	#print (main(io))
	main(io)

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

Submission status: Accepted

Solution code:
from queue import PriorityQueue

[X,Y,Z,K] = list(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)

ai = 0
bi = 0
ci = 0

vec = []

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:
                vec.append(A[i] + B[j] + C[k])
            else:
                break
vec = sorted(vec, reverse=True)
for i in range(K):
    print(vec[i])

Submission status: Accepted

Solution code:
def main():
  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)
  A += [-1e10]
  B += [-1e10]
  C += [-1e10]

  memo = [A[i] + B[0] + C[0] for i in range(X + 1)]
  L = [[0 for i in range(Y + 1)] for j in range(X + 1)]

  for i in range(K):
    ma = 0
    for j in range(X):
      if memo[ma] < memo[j]:
        ma = j
    print(memo[ma])
    mb = 0
    mn = 0
    for j in range(Y):
      x = B[j] + C[L[ma][j]]
      if mb < x:
        mb = x
        mn = j
    L[ma][mn] += 1
    n = 0
    for j in range(Y):
      n = max(n, B[j] + C[L[ma][j]])
    memo[ma] = A[ma] + n
 
main()

Submission status: Accepted

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

X,Y,Z,K = IL()
A = IL()
B = IL()
C = IL() 

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

ab = []
for i in list(itertools.product(A,B)):
    ab.append(sum(i))

ab.sort(reverse=True)

ans = []
for i in list(itertools.product(ab[:3000],C)):
    ans.append(sum(i))

ans.sort(reverse=True)

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

Submission status: Accepted

Solution code:
x,y,z,k = map(int, input().split())
aa = sorted(list(map(int, input().split())))
bb = sorted(list(map(int, input().split())))
cc = sorted(list(map(int, input().split())))
 
ab = sorted(a+b for a in aa for b in bb)[:-k-1:-1]
abc = sorted(s+c for s in ab[:k] for c in cc)[:-k-1:-1]
print(*abc, sep='\n')

Submission status: Accepted

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

A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
a = b = c = 0
count = 1
heap = [(0, (0, 0, 0))]
log = [(0, 0, 0)]
heapq.heapify(heap)
while count <= K:
    count += 1
    q = heapq.heappop(heap)
    a = q[1][0]
    b = q[1][1]
    c = q[1][2]
    print(A[a]+B[b]+C[c])
    if(a+1 < len(A) and not (a+1, b, c) in log):
        heapq.heappush(heap, (q[0]+A[a]-A[a+1], (a+1, b, c)))
        log.append((a+1, b, c))
    if(b+1 < len(B)  and not (a, b+1, c) in log):
        heapq.heappush(heap, (q[0]+B[b]-B[b+1], (a, b+1, c)))
        log.append((a, b+1, c))
    if(c+1 < len(C)  and not (a, b, c+1) in log):
        heapq.heappush(heap, (q[0]+C[c]-C[c+1], (a, b, c+1)))
        log.append((a, b, c+1))

Submission status: Accepted

Solution code:
#import itertools

x,y,z,k=(int(i) for i in input().split())
#print(x,y,z,k)
a=list(int(i) for i in input().split())
a.sort(reverse=True)
b=list(int(i) for i in input().split())
b.sort(reverse=True)
c=list(int(i) for i in input().split())
c.sort(reverse=True)
#print(a,b,c)

#k=3000
#a=[int(i) for i in range(1000)]
#b=[int(i) for i in range(1000)]
#c=[int(i) for i in range(1000)]
x=[]
for h in range(len(a)):
    for i in range(len(b)):
        if (h+1)*(i+1) <=k:
            for j in range(len(c)):
                if (h+1)*(i+1)*(j+1) <=k:
                    x.append(a[h]+b[i]+c[j])

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

Submission status: Accepted

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
#
import math
import sys
import heapq


# input
X,Y,Z,K = map(int, input().split())
A = list(map(int, sys.stdin.readline().rstrip().split()))
B = list(map(int, sys.stdin.readline().rstrip().split()))
C = list(map(int, sys.stdin.readline().rstrip().split()))
A = sorted(A, key=lambda x:-x)
B = sorted(B, key=lambda x:-x)
C = sorted(C, key=lambda x:-x)

# solve
heap_q = []
heapq.heappush(heap_q, [-1*(A[0]+B[0]+C[0]), 0, 0, 0])
smt,at,bt,ct = [0, 0, 0, 0]

for _ in range(K):
  sm,a,b,c = heapq.heappop(heap_q)
  print(-sm)

  if len(heap_q) > 0:
    smt,at,bt,ct = heapq.heappop(heap_q)
  while len(heap_q) > 0 and sm==smt and a==at and b==bt and c==ct:
    smt,at,bt,ct = heapq.heappop(heap_q)
  heapq.heappush(heap_q, [smt,at,bt,ct])

  if a+1 < X:
    heapq.heappush(heap_q, [-A[a+1]-B[b]-C[c], a+1, b, c])
  if b+1 < Y:
    heapq.heappush(heap_q, [-A[a]-B[b+1]-C[c], a, b+1, c])
  if c+1 < Z:
    heapq.heappush(heap_q, [-A[a]-B[b]-C[c+1], a, b, c+1])

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

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

A = sorted([int(e) for e in input().split()], reverse=True)
B = sorted([int(e) for e in input().split()], reverse=True)
C = sorted([int(e) for e in input().split()], reverse=True)


def get_value(ai, bi, ci):
    return -(A[ai] + B[bi] + C[ci])


heap = []
heappush(heap, (get_value(0, 0, 0), 0, 0, 0))
used = {(0, 0, 0)}

for k in range(K):
    v, ia, ib, ic = heappop(heap)
    print(-v)

    if ia + 1 < X and (ia + 1, ib, ic) not in used:
        heappush(heap, (get_value(ia + 1, ib, ic), ia + 1, ib, ic))
        used.add((ia + 1, ib, ic))
    if ib + 1 < Y and (ia, ib + 1, ic) not in used:
        heappush(heap, (get_value(ia, ib + 1, ic), ia, ib + 1, ic))
        used.add((ia, ib + 1, ic))
    if ic + 1 < Z and (ia, ib, ic + 1) not in used:
        heappush(heap, (get_value(ia, ib, ic + 1), ia, ib, ic + 1))
        used.add((ia, ib, ic + 1))

Submission status: Accepted

Solution code:
import heapq


def heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap) - 1)


def heappop_max(heap):
    lastelt = heap.pop()
    if heap:
        return_item = heap[0]
        heap[0] = lastelt
        heapq._siftup_max(heap, 0)
        return return_item
    return lastelt


X, Y, Z, K = map(int, 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)
ans = []
heap_queue = [(A[0]+B[0]+C[0], 0, 0, 0)]
arg_hash = {}

for i in range(K):
    maxim, a, b, c = heappop_max(heap_queue)
    print(maxim)
    arg_a = (a+1, b, c)
    arg_b = (a, b + 1, c)
    arg_c = (a, b, c + 1)

    if a + 1 < X and arg_a not in arg_hash:
        heappush_max(heap_queue, (A[a + 1] + B[b] + C[c], a + 1, b, c))
        arg_hash[arg_a] = 1
    if b + 1 < Y and arg_b not in arg_hash:
        heappush_max(heap_queue, (A[a] + B[b+1] + C[c], a, b+1, c))
        arg_hash[arg_b] = 1
    if c + 1 < Z and arg_c not in arg_hash:
        heappush_max(heap_queue, (A[a] + B[b] + C[c+1], a, b, c+1))
        arg_hash[arg_c] = 1

Submission status: Accepted

Solution code:
import heapq,sys


def main():
    input = sys.stdin.readline
    X, Y, Z, K = map(int, input().split())
    #tupleはlistの速いver. 但し値変更なし
    A = tuple(map(int,input().split()))
    B = tuple(map(int,input().split()))
    C = tuple(map(int,input().split()))
    
    #A+Bでのベスト枠K個求めるO(XYlogK)
    AandB = []
    for x in A:
        for y in B:
            heapq.heappush(AandB,x+y)
            #要素がK+1個以上なら一々はじく！！！ことでO(logK)
            if len(AandB) > K:
                heapq.heappop(AandB)
    #上のとCでのベスト枠K個を求める(ZKlogK)
    heap_queue = []
    for z in C:
        for elt in AandB:
            heapq.heappush(heap_queue,z+elt)
            if len(heap_queue) > K:
                heapq.heappop(heap_queue)
    heap_queue.sort(reverse = True)
    for e in heap_queue:
        print(e)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
great_list = []
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)

for i in range(min(k,len(a)))[:k]:
	for j in range(min(k // (i+1),len(b)))[:k]:
		for l in range(min(k // ((i+1) * (j+1)),len(c)))[:k]:
			great_list.append(a[i] + b[j] + c[l])

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

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

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

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

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

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

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

Submission status: Accepted

Solution code:
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_list = []
for x,a in enumerate(A):
    for y,b in enumerate(B):
        if (x+1)*(y+1)>K:
                break
        for z,c in enumerate(C):
            if (x+1)*(y+1)*(z+1)>K:
                break
            ans_list.append(a+b+c)

ans_list.sort(reverse=True)

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

Submission status: Accepted

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

ansList = []
for i, A in enumerate(ABCList[0]):
    for j, B in enumerate(ABCList[1]):
        for k, C in enumerate(ABCList[2]):
            if (i + 1) * (j + 1) * (k + 1) <= K:
                ansList.append(A + B + C)
            else:
                break

for ans in sorted(ansList, reverse=True)[:K]:
    print(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)
abc = []
for i in range(x):
    if i > K:
        break
    for j in range(y):
        if i*j > K:
            break
        for k in range(z):
            if i*j*k > K:
                break
            abc.append(a[i]+b[j]+c[k])
abc.sort(reverse=True)
for i in range(K):
    print(abc[i])

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(key=lambda x:-x)
B.sort(key=lambda y:-y)
C.sort(key=lambda z:-z)
pri=[]
def dp(x,y,z):
  return A[x]+B[y]+C[z]

def priq(a,b,c):
  d=-dp(a,b,c)
  if tuple([d,a,b,c]) not in pri:
    heapq.heappush(pri,(d,a,b,c))

heapq.heappush(pri,(-dp(0,0,0),0,0,0))

for i in range(K):
  d,x,y,z=heapq.heappop(pri)
  print(-d)
  if x<X-1:
    priq(x+1,y,z)
  if y<Y-1:
    priq(x,y+1,z)
  if z<Z-1:
    priq(x,y,z+1)

Submission status: Accepted

Solution code:
import heapq
 
x, y, z, K = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
 
n = []
heapq.heapify(n)
heapq.heappush(n, (-(a[0]+b[0] + c[0]), 0,0,0))
for i in range(K):
    (m, j, k, l) = heapq.heappop(n)
    print(-m)
    if j + 1 <=x-1:
        s = (-(a[j+1]+b[k]+c[l]), j+1, k, l)
        if s not in n:
            heapq.heappush(n ,s)
 
    if k + 1 <=y-1:
        s = (-(a[j]+b[k+1]+c[l]), j, k+1, l)
        if s not in n:
            heapq.heappush(n ,s)
    
    if l+ 1 <=z-1:
        s = (-(a[j]+b[k]+c[l+1]), j, k, l+1)
        if s not in n:
            heapq.heappush(n ,s)

Submission status: Accepted

Solution code:
*xyz,k = map(int, input().split()) 
xyz_list = []
xyz_list.append(list(map(int, input().split())))
xyz_list.append(list(map(int, input().split())))
xyz_list.append(list(map(int, input().split())))
xyz_list[0].sort(reverse=True)
xyz_list[1].sort(reverse=True)
xyz_list[2].sort(reverse=True)

print(xyz_list[0][0]+xyz_list[1][0]+xyz_list[2][0])
p = [1,0,0]
num = lambda x: [
    [x[0]+p[i], x[1]+p[(4-i)%3], x[2]+p[(5-i)%3],
    xyz_list[0][x[0]+p[i]]+xyz_list[1][x[1]+p[(4-i)%3]]+xyz_list[2][x[2]++p[(5-i)%3]]]
    for i in range(3) if x[i]+1 < xyz[i]]
stack = num([0,0,0])
num_dict={}
for i in stack:
    if i[3] not in num_dict:
        num_dict[i[3]] = [[i[0], i[1], i[2]]]
    else:
        num_dict[i[3]].append([i[0], i[1], i[2]])
k -= 1
while k != 0:
    x, y, z, maxi = max(stack, key=lambda x:x[3])
    print(maxi)
    k -= 1
    if k == 0:
      break
    stack.remove([x,y,z,maxi])
    for i in num([x,y,z]):
        if i[3] not in num_dict:
            num_dict[i[3]] = [[i[0], i[1], i[2]]]
            stack.append([i[0], i[1], i[2], i[3]])
        elif [i[0], i[1], i[2]] not in num_dict[i[3]]:
            num_dict[i[3]].append([i[0], i[1], i[2]])
            stack.append([i[0], i[1], i[2], i[3]])

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

ans = []

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

for s in range(x):
    for t in range(y):
        for u in range(z):
            if (s+1) * (t+1) * (u+1) > k:
                break
            ans.append(a[s] + b[t] + c[u])

ans.sort(reverse=True)

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

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)

n = []
heapq.heapify(n)
heapq.heappush(n, (-(a[0]+b[0] + c[0]), 0,0,0))
for i in range(K):
    (m, j, k, l) = heapq.heappop(n)
    print(-m)
    if j + 1 <=x-1:
        s = (-(a[j+1]+b[k]+c[l]), j+1, k, l)
        if s not in n:
            heapq.heappush(n ,s)

    if k + 1 <=y-1:
        s = (-(a[j]+b[k+1]+c[l]), j, k+1, l)
        if s not in n:
            heapq.heappush(n ,s)
    
    if l+ 1 <=z-1:
        s = (-(a[j]+b[k]+c[l+1]), j, k, l+1)
        if s not in n:
            heapq.heappush(n ,s)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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)



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

ans = [ab + c for ab in AB[:k] for c in C]
ans.sort(reverse = True)

for i in ans[:k]:
    print(i)

Submission status: Accepted

Solution code:
def main():
  import sys
  from heapq import heappush as hpush, heappop as hpop
  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)
  
  memo = set([(0, 0, 0)])
  h = [(-(A[0]+B[0]+C[0]), (0, 0, 0))]
  for _ in range(K):
    val, (i,j,k) = hpop(h)
    if i+1 < len(A) and (i+1, j, k) not in memo:
      hpush(h, (-(A[i+1]+B[j]+C[k]), (i+1, j, k)))
      memo.add((i+1, j, k))
    if j+1 < len(B) and (i, j+1, k) not in memo:
      hpush(h, (-(A[i]+B[j+1]+C[k]), (i, j+1, k)))
      memo.add((i, j+1, k))
    if k+1 < len(C) and (i, j, k+1) not in memo:
      hpush(h, (-(A[i]+B[j]+C[k+1]), (i, j, k+1)))
      memo.add((i, j, k+1))
    print(-val)

if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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

A = sorted(map(int,input().split()))[::-1]
B = sorted(map(int,input().split()))[::-1]
C = sorted(map(int,input().split()))[::-1]

AB = sorted([a+b for a in A for b in B])[::-1]

ans = sorted([ab + c for ab in AB[:k] for c in C])[::-1]

for i in ans[:k]:
    print(i)

Submission status: Accepted

Solution code:
def main():
  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):
      for k in range(Z):
        if (i+1)*(j+1)*(k+1) <= K:
          res.append(A[i]+B[j]+C[k])
        else:
          break
  res.sort(reverse = True)
  for r in res[:K]:
    print(r)

if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
def main():
  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 = [a+b for a in A for b in B]
  AB.sort(reverse = True)
  res = [c+ab for ab in AB[:min(K, len(AB))] for c in C]
  res.sort(reverse = True)
  for r in res[:K]:
    print(r)

if __name__ == "__main__":
  main()

Submission status: Accepted

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

list_cake_1 = list(map(int, input().split()))
list_cake_2 = list(map(int, input().split()))
list_cake_3 = list(map(int, input().split()))

list_cake_1 = sorted(list_cake_1, reverse=True)
list_cake_2 = sorted(list_cake_2, reverse=True)
list_cake_3 = sorted(list_cake_3, reverse=True)

list_total_sum = []

for i, cake_1 in enumerate(list_cake_1, 1):
	for j, cake_2 in enumerate(list_cake_2, 1):
		for k, cake_3 in enumerate(list_cake_3, 1):
			if i*j*k > K:
				break
			list_total_sum.append(cake_1 + cake_2 + cake_3)

for element in sorted(list_total_sum, reverse=True)[:K]:
	print(element)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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

list_cake_1 = list(map(int, input().split()))
list_cake_2 = list(map(int, input().split()))
list_cake_3 = list(map(int, input().split()))

list_cake_1 = sorted(list_cake_1, reverse=True)
list_cake_2 = sorted(list_cake_2, reverse=True)
list_cake_3 = sorted(list_cake_3, reverse=True)

list_total_sum = []

for i, cake_1 in enumerate(list_cake_1, 1):
	for j, cake_2 in enumerate(list_cake_2, 1):
		for k, cake_3 in enumerate(list_cake_3, 1):
			if i*j*k > K:
				break
			list_total_sum.append(cake_1 + cake_2 + cake_3)

for element in sorted(list_total_sum, reverse=True)[:K]:
	print(element)

Submission status: Accepted

Solution code:
import itertools
import heapq
import copy

def inv(l):
    return list(map(lambda x: x*-1, l))

def solve(A,B,C, K):
    A,B,C = inv(A), inv(B), inv(C)
    A = sorted(A)
    B = sorted(B)
    C = sorted(C)
    ind = set([(0,0,0)])

    h = [(A[0]+ B[0]+ C[0], (0,0,0))]
    res = []

    for _ in range(K):
        h2 = copy.copy(h)
        val, (i,j,k) = heapq.heappop(h)
        res += [-val]
        if i+1 < len(A) and (i+1,j,k) not in ind:
            heapq.heappush(h, (A[i+1]+B[j]+C[k], (i+1, j, k)))
            ind |= set([(i+1,j,k)])
        if j+1 < len(B) and (i,j+1, k) not in ind:
            heapq.heappush(h, (A[i]+B[j+1]+C[k], (i, j+1, k)))
            ind |= set([(i,j+1,k)])
        if k+1 < len(C) and (i,j, k+1) not in ind:
            heapq.heappush(h, (A[i]+B[j]+C[k+1], (i, j, k+1)))
            ind |= set([(i,j,k+1)])
    for r in res:
        print(r)

la,lb,lc,K = list(map(int, input().split(' ')))
A = list(map(int, input().split(' ')))
B = list(map(int, input().split(' ')))
C = list(map(int, input().split(' ')))

solve(A,B,C,K)

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)

candidates = []
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:
                candidates.append(A[i] + B[j] + C[k])
            else:
                break
candidates.sort(reverse=True)

for c in candidates[:K]:
    print(c)

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 = list(sorted(a, reverse=True))
b = list(sorted(b, reverse=True))
c = list(sorted(c, reverse=True))
# 降順ソート

e = []
for i, aa in enumerate(a, 1):
    for j, bb in enumerate(b, 1):
        for k, cc in enumerate(c, 1):
            if i * j * k > K: break
            e.append(aa + bb + cc)

for ee in sorted(e, reverse=True)[:K]:
    print(ee)