Problem:


Score: 400 points

Problem Statement

The Patisserie AtCoder sells cakes with number-shaped candles.
There are X, Y and Z kinds of cakes with 1-shaped, 2-shaped and 3-shaped candles, respectively.
Each cake has an integer value called deliciousness, as follows:

The deliciousness of the cakes with 1-shaped candles are A_1, A_2, ..., A_X.

The deliciousness of the cakes with 2-shaped candles are B_1, B_2, ..., B_Y.

The deliciousness of the cakes with 3-shaped candles are C_1, C_2, ..., C_Z.

Takahashi decides to buy three cakes, one for each of the three shapes of the candles, to celebrate ABC 123.

There are X \times Y \times Z such ways to choose three cakes.

We will arrange these X \times Y \times Z ways in descending order of the sum of the deliciousness of the cakes.

Print the sums of the deliciousness of the cakes for the first, second, ..., K-th ways in this list.

Constraints

1 \leq X \leq 1 \ 000

1 \leq Y \leq 1 \ 000

1 \leq Z \leq 1 \ 000

1 \leq K \leq \min(3 \ 000, X \times Y \times Z)

1 \leq A_i \leq 10 \ 000 \ 000 \ 000

1 \leq B_i \leq 10 \ 000 \ 000 \ 000

1 \leq C_i \leq 10 \ 000 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z K
A_1 \ A_2 \ A_3 \ ... \ A_X
B_1 \ B_2 \ B_3 \ ... \ B_Y
C_1 \ C_2 \ C_3 \ ... \ C_Z

Output

Print K lines. The i-th line should contain the i-th value stated in the problem statement.

Sample Input 1

2 2 2 8
4 6
1 5
3 8

Sample Output 1

19
17
15
14
13
12
10
8

There are 2 \times 2 \times 2 = 8 ways to choose three cakes, as shown below in descending order of the sum of the deliciousness of the cakes:

(A_2, B_2, C_2): 6 + 5 + 8 = 19

(A_1, B_2, C_2): 4 + 5 + 8 = 17

(A_2, B_1, C_2): 6 + 1 + 8 = 15

(A_2, B_2, C_1): 6 + 5 + 3 = 14

(A_1, B_1, C_2): 4 + 1 + 8 = 13

(A_1, B_2, C_1): 4 + 5 + 3 = 12

(A_2, B_1, C_1): 6 + 1 + 3 = 10

(A_1, B_1, C_1): 4 + 1 + 3 = 8

Sample Input 2

3 3 3 5
1 10 100
2 20 200
1 10 100

Sample Output 2

400
310
310
301
301

There may be multiple combinations of cakes with the same sum of the deliciousness. For example, in this test case, the sum of A_1, B_3, C_3 and the sum of A_3, B_3, C_1 are both 301.
However, they are different ways of choosing cakes, so 301 occurs twice in the output.

Sample Input 3

10 10 10 20
7467038376 5724769290 292794712 2843504496 3381970101 8402252870 249131806 6310293640 6690322794 6082257488
1873977926 2576529623 1144842195 1379118507 6003234687 4925540914 3902539811 3326692703 484657758 2877436338
4975681328 8974383988 2882263257 7690203955 514305523 6679823484 4263279310 585966808 3752282379 620585736

Sample Output 3

23379871545
22444657051
22302177772
22095691512
21667941469
21366963278
21287912315
21279176669
21160477018
21085311041
21059876163
21017997739
20703329561
20702387965
20590247696
20383761436
20343962175
20254073196
20210218542
20150096547

Note that the input or output may not fit into a 32-bit integer type.



Sample input:
2 2 2 8
4 6
1 5
3 8


Sample output:
19
17
15
14
13
12
10
8

Submission status: Accepted

Solution code:
import heapq

*T, K = [int(i) for i in input().split()]
A, B, C = [sorted([int(i) for i in input().split()], reverse=True)
           for _ in range(3)]

def get_next(t):
    x, y, z = t
    return [(x + 1, y, z), (x, y + 1, z), (x, y, z + 1)]

def evaluate(t):
    x, y, z = t
    return (-(A[x] + B[y] + C[z]), t)


q = []
heapq.heappush(q, evaluate((0, 0, 0)))
memo = set()

for _ in range(K):
    v, t = heapq.heappop(q)
    print(-v)

    for i, nt in enumerate(get_next(t)):
        if nt[i] < T[i] and nt not in memo:
            heapq.heappush(q, evaluate(nt))
            memo.add(nt)

Submission status: Accepted

Solution code:
import heapq

*T, K = [int(i) for i in input().split()]
A, B, C = [sorted([int(i) for i in input().split()], reverse=True)
           for _ in range(3)]

def get_next(t):
    x, y, z = t
    return [(x + 1, y, z), (x, y + 1, z), (x, y, z + 1)]

def evaluate(t):
    x, y, z = t
    return (-(A[x] + B[y] + C[z]), t)


q = []
heapq.heappush(q, evaluate((0, 0, 0)))
result = []
memo = set()

for _ in range(K):
    v, t = heapq.heappop(q)
    result.append(-v)

    for i, nt in enumerate(get_next(t)):
        if nt[i] < T[i] and nt not in memo:
            heapq.heappush(q, evaluate(nt))
            memo.add(nt)

for r in result:
    print(r)

Submission status: Accepted

Solution code:
r=range;x,y,z,k=map(int,input().split())
a,b,c=eval("sorted(list(map(int,input().split())),reverse=True),"*3)
abc=[]
for l in r(x):
 for m in r(y):
  if -~l*-~m>k:break
  for n in r(z):
   if -~l*-~m*-~n>k:break
   abc+=[a[l]+b[m]+c[n]]
print(*sorted(abc,reverse=True)[:k])

Submission status: Accepted

Solution code:
# ヒープに要素を追加、一番でかいのを取り出すという操作を最大3000回やるやり方
# これもなかなか早い
# ヒープは追加も要素の取り出しもO(log n)で住むので、
# 計算オーダーはO(K log n)で済む(nはヒープの要素数)らしいが
# not in があるのでO(n K log n)では？
# pythonのヒープは使い方に癖があるのでこの機会に習得しよう
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


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

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


from heapq import heapify, heappop, heappush, heappushpop


class PriorityQueue:
    def __init__(self, heap):
        '''
        heap ... list
        '''
        self.heap = heap
        heapify(self.heap)

    def push(self, item):
        heappush(self.heap, item)

    def pop(self):
        return heappop(self.heap)

    def pushpop(self, item):
        return heappushpop(self.heap, item)

    def __call__(self):
        return self.heap


heap = []  # ヒープといっても順序を工夫したただのリスト

q = PriorityQueue(heap)
q.push((-(A[0] + B[0] + C[0]), 0, 0, 0))

considered = set()
ans = []
for k_th in range(1, K+1):
    heap_max, i, j, k = q.pop()
    ans.append(-heap_max)
    for di, dj, dk in zip([1, 0, 0], [0, 1, 0], [0, 0, 1]):
        i_new, j_new, k_new = i + di, j + dj, k + dk
        if i_new >= X or j_new >= Y or k_new >= Z:
            continue
        if (i_new, j_new, k_new) in considered:
            continue
        considered.add((i_new, j_new, k_new))
        q.push((-(A[i_new] + B[j_new] + C[k_new]), i_new, j_new, k_new))


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

Submission status: Accepted

Solution code:
# ヒープに要素を追加、一番でかいのを取り出すという操作を最大3000回やるやり方
# これもなかなか早い
# ヒープは追加も要素の取り出しもO(log n)で住むので、
# 計算オーダーはO(K log n)で済む(nはヒープの要素数)らしいが
# not in があるのでO(n K log n)では？
# pythonのヒープは使い方に癖があるのでこの機会に習得しよう
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


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

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


from heapq import heapify, heappop, heappush, heappushpop


class PriorityQueue:
    def __init__(self, heap):
        '''
        heap ... list
        '''
        self.heap = heap
        heapify(self.heap)

    def push(self, item):
        heappush(self.heap, item)

    def pop(self):
        return heappop(self.heap)

    def pushpop(self, item):
        return heappushpop(self.heap, item)

    def __call__(self):
        return self.heap


heap = []  # ヒープといっても順序を工夫したただのリスト

q = PriorityQueue(heap)
q.push((-(A[0] + B[0] + C[0]), 0, 0, 0))

ans = []
for k_th in range(1, K+1):
    heap_max, i, j, k = q.pop()
    # print(heap_max)
    ans.append(-heap_max)
    for di, dj, dk in zip([1, 0, 0], [0, 1, 0], [0, 0, 1]):
        i_new, j_new, k_new = i + di, j + dj, k + dk
        if i_new >= X or j_new >= Y or k_new >= Z:
            continue
        tmp = (-(A[i_new] + B[j_new] + C[k_new]), i_new, j_new, k_new)
        if tmp in q():  # ここが計算量がおおい
            continue
        q.push(tmp)


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

Submission status: Accepted

Solution code:
# ヒープに要素を追加、一番でかいのを取り出すという操作を最大3000回やるやり方
# これもなかなか早い
# ヒープは追加も要素の取り出しもO(log n)で住むので、
# 計算オーダーはO(K log n)で済む(nはヒープの要素数)らしいが
# not in があるのでO(n K log n)では？
# pythonのヒープは使い方に癖があるのでこの機会に習得しよう
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


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

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

heap = []  # ヒープといっても順序を工夫したただのリスト

from heapq import heapify, heappop, heappush, heappushpop
heappush(heap, (-(A[0] + B[0] + C[0]), 0, 0, 0))

ans = []
considerd = set()
for k_th in range(1, K+1):
    heap_max, i, j, k = heappop(heap)
    # print(heap_max)
    ans.append(-heap_max)
    for di, dj, dk in zip([1, 0, 0], [0, 1, 0], [0, 0, 1]):
        i_new, j_new, k_new = i + di, j + dj, k + dk
        if i_new >= X or j_new >= Y or k_new >= Z:
            continue
        if (i_new, j_new, k_new) in considerd:  # ここが計算量がおおい
            continue
        considerd.add((i_new, j_new, k_new))
        heappush(heap, (-(A[i_new] + B[j_new] + C[k_new]), i_new, j_new, k_new))
        # print(heap)


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

Submission status: Accepted

Solution code:
# ヒープに要素を追加、一番でかいのを取り出すという操作を最大3000回やるやり方
# これもなかなか早い
# ヒープは追加も要素の取り出しもO(log n)で住むので、
# 計算オーダーはO(K log n)で済む(nはヒープの要素数)らしいが
# not in があるのでO(n K log n)では？
# pythonのヒープは使い方に癖があるのでこの機会に習得しよう
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


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

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

heap = []  # ヒープといっても順序を工夫したただのリスト

from queue import PriorityQueue
heap = PriorityQueue()
heap.put((-(A[0] + B[0] + C[0]), 0, 0, 0))

considerd = set()

ans = []
for k_th in range(1, K+1):
    heap_max, i, j, k = heap.get()
    # print(heap_max)
    ans.append(-heap_max)
    for di, dj, dk in zip([1, 0, 0], [0, 1, 0], [0, 0, 1]):
        i_new, j_new, k_new = i + di, j + dj, k + dk
        if i_new >= X or j_new >= Y or k_new >= Z:
            continue
        if (i_new, j_new, k_new) in considerd:
            continue
        considerd.add((i_new, j_new, k_new))
        heap.put((-(A[i_new] + B[j_new] + C[k_new]), i_new, j_new, k_new))


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

Submission status: Accepted

Solution code:
# ヒープに要素を追加、一番でかいのを取り出すという操作を最大3000回やるやり方
# これもなかなか早い
# ヒープは追加も要素の取り出しもO(log n)で住むので、
# 計算オーダーはO(K log n)で済む(nはヒープの要素数)らしいが
# not in があるのでO(n K log n)では？
# pythonのヒープは使い方に癖があるのでこの機会に習得しよう
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


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

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

heap = []  # ヒープといっても順序を工夫したただのリスト

from heapq import heapify, heappop, heappush, heappushpop
heappush(heap, (-(A[0] + B[0] + C[0]), 0, 0, 0))

ans = []
for k_th in range(1, K+1):
    heap_max, i, j, k = heappop(heap)
    # print(heap_max)
    ans.append(-heap_max)
    for di, dj, dk in zip([1, 0, 0], [0, 1, 0], [0, 0, 1]):
        i_new, j_new, k_new = i + di, j + dj, k + dk
        if i_new >= X or j_new >= Y or k_new >= Z:
            continue
        tmp = (-(A[i_new] + B[j_new] + C[k_new]), i_new, j_new, k_new)
        if tmp in heap:  # ここが計算量がおおい
            continue
        heappush(heap, tmp)
        # print(heap)


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

Submission status: Accepted

Solution code:
# ヒープに要素を追加、一番でかいのを取り出すという操作を最大3000回やるやり方
# 圧倒的爆速だった
# ヒープは追加も要素の取り出しもO(log n)で住むので、
# 計算オーダーはO(K log n)で済む(nはヒープの要素数)
# pythonのヒープは使い方に癖があるのでこの機会に習得しよう
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


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

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

heap = []  # ヒープといっても順序を工夫したただのリスト

from heapq import heapify, heappop, heappush, heappushpop
heappush(heap, (-(A[0] + B[0] + C[0]), 0, 0, 0))

ans = []
for k_th in range(1, K+1):
    heap_max, i, j, k = heappop(heap)
    # print(heap_max)
    ans.append(-heap_max)
    for di, dj, dk in zip([1, 0, 0], [0, 1, 0], [0, 0, 1]):
        i_new, j_new, k_new = i+di, j+dj, k+dk
        if i_new >= X or j_new >= Y or k_new >= Z:
            continue
        tmp = (-(A[i_new]+B[j_new]+C[k_new]), i_new, j_new, k_new)
        if tmp not in heap:
            heappush(heap, tmp)


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

Submission status: Accepted

Solution code:
from heapq import *
x,y,z,K = map(int, input().split())
m = [sorted(map(int, input().split()))[::-1] for _ in range(3)]
q = [(-sum(i[0] for i in m),(0,0,0))]
r = 0
s = set()
for _ in range(K):
    v,a = heappop(q)
    v = -v
    print(v)
    for p in ((0,0,1),(0,1,0),(1,0,0)):
        p = tuple(i+j for i,j in zip(p,a))
        if not p in s and all(i<j for i,j in zip(p,(x,y,z))):
            s.add(p)
            heappush(q, (-sum(i[j] for i,j in zip(m,p)), p))

Submission status: Accepted

Solution code:
X, Y, Z, K = [int(n) for n in input().strip().split(' ')]
A = sorted([int(s) for s in input().strip().split(' ')], reverse=True)
B = sorted([int(s) for s in input().strip().split(' ')], reverse=True)
C = sorted([int(s) for s in input().strip().split(' ')], reverse=True)

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

            ABC.append(a + b + c)

ABC.sort(reverse=True)

for s in ABC[:K]:
    print(s)

Submission status: Accepted

Solution code:
import heapq as hq

X, Y, Z, K = [int(n) for n in input().strip().split(' ')]
A = sorted([int(s) for s in input().strip().split(' ')], reverse=True)
B = sorted([int(s) for s in input().strip().split(' ')], reverse=True)
C = sorted([int(s) for s in input().strip().split(' ')], reverse=True)

q = []
i, j, k = 0, 0, 0
hq.heappush(q, (-(A[0] + B[0] + C[0]), i, j, k))
pushed = {(0, 0, 0)}

for _ in range(K):
    s, *idx = hq.heappop(q)
    idx = tuple(idx)

    print(-s)

    i, j, k = idx
    if i < X - 1 and (i + 1, j, k) not in pushed:
        hq.heappush(q, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
        pushed.add((i + 1, j, k))
    if j < Y - 1 and (i, j + 1, k) not in pushed:
        hq.heappush(q, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
        pushed.add((i, j + 1, k))
    if k < Z - 1 and (i, j, k + 1) not in pushed:
        hq.heappush(q, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
        pushed.add((i, j, k + 1))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 10 22:30:07 2019

@author: Yamazaki Kenichi
"""
import heapq
#import random
#X,Y,Z,K = 1000,1000,1000,3000
#A = random.sample(range(10**9),1000)
#B = random.sample(range(10**9),1000)
#C = random.sample(range(10**9),1000)

X,Y,Z,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
Q = []
T = [[-1 for i in range(X)] for i in range(Y)]
T[0][0] = 0
heapq.heappush(Q,[-(A[0]+B[0]+C[0]),0,0,0])
for i in range(K-1):
    k = heapq.heappop(Q)
    s1,s2,s3 = k[1],k[2],k[3]
    if s1 + 1 != X and T[s1+1][s2] < s3:
        heapq.heappush(Q,[-(A[s1+1]+B[s2]+C[s3]),s1+1,s2,s3])
        T[s1+1][s2] += 1
    if s2 + 1 != Y and T[s1][s2+1] < s3:
        heapq.heappush(Q,[-(A[s1]+B[s2+1]+C[s3]),s1,s2+1,s3])
        T[s1][s2+1] += 1
    if s3 + 1 != Z and T[s1][s2] < s3 + 1:
        heapq.heappush(Q,[-(A[s1]+B[s2]+C[s3+1]),s1,s2,s3+1])
        T[s1][s2] += 1
    print(-k[0])
k = heapq.heappop(Q)
print(-k[0])

Submission status: Accepted

Solution code:
from subprocess import*
cmd=['pypy3','-c',"""
def solve(p,q):
  return sorted(i+j for i in p for j in q)[-k:]
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 i in solve(solve(a,b),c)[::-1]:
  print(i)
"""]
call(cmd)

Submission status: Accepted

Solution code:
[XYZK,A,B,C] = [input() for i in range(4)]
[X,Y,Z,K] = [int(i) for i in XYZK.split()]
A = sorted([int(i) for i in A.split()],reverse=True)[:K]
B = sorted([int(i) for i in B.split()],reverse=True)[:K]
C = sorted([int(i) for i in C.split()],reverse=True)[:K]

ABC = []
for i in range(X):
    if (i + 1) > K:
        break
    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 = sorted(ABC, reverse=True)
print(*ABC[:K])

Submission status: Accepted

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

ABC = []
for i in range(X):
    if (i + 1) > K:
        break
    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 = sorted(ABC, reverse=True)
print(*ABC[:K])

Submission status: Accepted

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

E = sorted([a+b for a in A for b in B], reverse=True)

E = E[:K]
nums = sorted([e+c for e in E for c in C], reverse=True)

for n in nums[:K]:
    print(n)

Submission status: Accepted

Solution code:
import heapq
X,Y,Z,K=map(int,input().split(' '))
A=sorted(list(map(int,input().split(' '))))[::-1]
B=sorted(list(map(int,input().split(' '))))[::-1]
C=sorted(list(map(int,input().split(' '))))[::-1]

default=A[0]+B[0]+C[0]
A=[A[0]-A[i] for i in range(len(A))]
B=[B[0]-B[i] for i in range(len(B))]
C=[C[0]-C[i] for i in range(len(C))]

print(default)
hp=[]
visited=set()
visited.add((0,0,0))
a,b,c=0,0,0
for _ in range(K-1):
  if (a+1,b,c) not in visited and a+1<len(A):
    heapq.heappush(hp,(A[a+1]+B[b]+C[c],(a+1,b,c)))
    visited.add((a+1,b,c))
  if (a,b+1,c) not in visited and b+1<len(B):
    heapq.heappush(hp,(A[a]+B[b+1]+C[c],(a,b+1,c)))
    visited.add((a,b+1,c))
  if (a,b,c+1) not in visited and c+1<len(C):
    heapq.heappush(hp,(A[a]+B[b]+C[c+1],(a,b,c+1)))
    visited.add((a,b,c+1))
  temp=heapq.heappop(hp)
  print(default-temp[0])
  a,b,c=temp[1]

Submission status: Accepted

Solution code:
x,y,z,k = [int(num) for num in input().split()]
a_cake,b_cake,c_cake = [sorted([int(num) for num in input().split()],reverse=True)[:k] for _ in range(3)]

sum_cake=list()
for i,a in enumerate(a_cake,1):
    for j,b in enumerate(b_cake,1):
        if(i*j>k):
            break
        for n,c in enumerate(c_cake,1):
            if(i*j*n>k):
                break
            sum_cake.append(a+b+c)


sum_cake.sort(reverse=True)

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

Submission status: Accepted

Solution code:
X,Y,Z,K = (int(num) for num in input().split())
a = [int(num) for num in input().split()]
b = [int(num) for num in input().split()]
c = [int(num) for num in input().split()]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

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

data=[]

for i in range(0,X):
  if i+1>K: break
    
  for j in range(0,Y):
    if (i+1)*(j+1)>K: break
    
    for k in range(0,Z):
      if (i+1)*(j+1)*(k+1)>K: break
      data.append(int(A[i]+B[j]+C[k]))
        
    
data.sort()
data.reverse()

for l in range(K):
  print(data[l])

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 = [x + y for x in A for y in B]
AB.sort(reverse=True)
ABC = [x + y for x in AB[:min(len(AB), k)] for y in C[:min(len(C), k)]]
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())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

Q = []
Q.append([A[0]+B[0]+C[0], 0, 0, 0])
M = {(0, 0, 0):True}

def search():
    global Q
    q = Q.pop()
    a = q[1]
    b = q[2]
    c = q[3]
    if a+1 < x and (a+1, b, c) not in M:
        Q.append([A[a+1]+B[b]+C[c], a+1, b, c])
        M[(a+1, b, c)] = True
    if b+1 < y and (a, b+1, c) not in M:
        Q.append([A[a]+B[b+1]+C[c], a, b+1, c])
        M[(a, b+1, c)] = True
    if c+1 < z and (a, b, c+1) not in M:
        Q.append([A[a]+B[b]+C[c+1], a, b, c+1])
        M[(a, b, c+1)] = True
    Q.sort()
    return q[0]

for _ in range(k):
    print(search())

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)

ans = []
for ai in range(len(a)):
    for bi in range(len(b)):
        if (ai+1)*(bi+1) > k:
            break
        for ci in range(len(c)):
            if (ai+1)*(bi+1)*(ci+1) >k:
                break
            ans.append(a[ai] + b[bi] +c[ci])

ans = sorted(ans, reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Accepted

Solution code:
import heapq

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)

q=[]
for h in range(x):
  for i in range(y):
    if (h+1)*(i+1) <= k:
      for j in range(z):
        if (h+1)*(i+1)*(j+1) <= k:      
          heapq.heappush(q,-(a[h]+b[i]+c[j]))
          
for i in range(k):
    val=heapq.heappop(q)
    print((-1)*val)

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)

ans = []

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

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

Submission status: Accepted

Solution code:
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:
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()))
     
r=[]
out=[]
     
for i in range(X):
    for j in range(Y):
        r.append(A[i]+B[j])
     
r.sort(reverse=True)
     
if len(r)>K:
    for i in range(K):
        for j in range(Z):
            out.append(r[i]+C[j])
else:
    for i in range(len(r)):
        for j in range(Z):
           out.append(r[i]+C[j])
     
out.sort(reverse=True)
     
for i in range(K):
    print(out[i])

Submission status: Accepted

Solution code:
x,y,z,k = input().strip().split(' ')
x,y,z,k = [int(x), int(y),int(z),int(k)]
aList = list(map(int, input().strip().split(' ')))
bList = list(map(int, input().strip().split(' ')))
cList = list(map(int, input().strip().split(' ')))
ansList = []
# aList.sort(reverse = True)
# bList.sort(reverse = True)
# cList.sort(reverse = True)

abList = []
for i in range(len(aList)):
        for j in range(len(bList)):
                abList.append(aList[i] + bList[j])
abList.sort(reverse = True)
abcList = []
for s in range(min(len(abList),k)):
        for t in range(len(cList)):
                abcList.append(abList[s] + cList[t])
abcList.sort(reverse = True)
for a in range(min(len(abcList),k)):
        print(abcList[a])

Submission status: Accepted

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

xlist = sorted(list(map(int,input().split())), reverse=True)[:min(x, k)]
ylist = sorted(list(map(int,input().split())), reverse=True)[:min(y, k)]
zlist = sorted(list(map(int,input().split())), reverse=True)[:min(z, k)]


anslist = [0]
for i in range(min(x, k)):
    for j in range(min(y, k)):
        for l in range(min(z, k)):
            if (i+1)*(j+1)*(l+1) <= k:
                anslist.append(xlist[i] + ylist[j] + zlist[l])
            else:
                break

anslist = sorted(anslist, reverse=True)
for i in range(k):
    print(anslist[i])

Submission status: Accepted

Solution code:
# 2019/4/11 17:00 ~ 21:39 記念
import heapq
from itertools import accumulate
X, Y, Z, K =  map(int, (input().split(' ')))
A =  sorted(list(map(int, (input().split(' ')))))[::-1]
B =  sorted(list(map(int, (input().split(' ')))))[::-1]
C =  sorted(list(map(int, (input().split(' ')))))[::-1]

### default
default = A[0] + B[0] + C[0]
Ai = 0
Bi = 0
Ci = 0
# 最小値を取っていくリストには巨大な数を入れておけば安全(out of rangeを防げる)
hoge = [10**20] *100
#used = [[[[0] * 1000] for _ in range(1000)] for _ in range(1000)]
used = set()


# 隣の要素との差を取る
A = [0] + [i - j for i, j in zip(A[:-1], A[1:])] + hoge
B = [0] + [i - j for i, j in zip(B[:-1], B[1:])] + hoge
C = [0] + [i - j for i, j in zip(C[:-1], C[1:])] + hoge
# 累積和
A = list(accumulate(A))
B = list(accumulate(B))
C = list(accumulate(C))
heap = []
print(default)
for _ in range(K-1):
    # 常に最小値がpopされるheapを用いる(貪欲法+条件を満たす葉を展開する)
    # ここでは、最小値だった葉を展開している
    if (Ai+1, Bi, Ci) not in used:
        heapq.heappush(heap, (A[Ai+1] + B[Bi] + C[Ci], "A", (Ai+1, Bi, Ci)))
        used.add((Ai+1, Bi, Ci))

    if (Ai, Bi+1, Ci) not in used:
        heapq.heappush(heap, (A[Ai] + B[Bi+1] + C[Ci], "B", (Ai, Bi+1, Ci)))
        used.add((Ai, Bi+1, Ci))

    if (Ai, Bi, Ci+1) not in used:
        heapq.heappush(heap, (A[Ai] + B[Bi] + C[Ci+1], "C", (Ai, Bi, Ci+1)))
        used.add((Ai, Bi, Ci+1))

    mi = heapq.heappop(heap)
    # 最高に美味しいケーキからの差分
    print(default - mi[0])
    # 展開時に次の葉のアドレスを仕込んでおく(いつ展開されるか未定なので)
    Ai, Bi, Ci = mi[2]

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)

ans = []
for i in range (len(a)):
    for j in range (len(b)):
        if (i+1)*(j+1)>k: break
        for u in range (len(c)):
            if (i+1)*(j+1)*(u+1)>k: break
            ans.append(a[i] + b[j] + c[u])
rans = sorted(ans,reverse = True)
for i in range(k):
    print (rans[i])

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)

sumAB = sorted([ai + bi for ai in a for bi in b],reverse=True)[:k]
final_ans = sorted([ci + abi for ci in c for abi in sumAB],reverse = True)[:k]

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

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)

sumAB = sorted([ai + bi for ai in a for bi in b],reverse=True)[:k]
sumABC = sorted([ci + abi for ci in c for abi in sumAB],reverse = True)[:k]

for i in range(k):
    print(sumABC[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=sorted([-i for i in a])
b=sorted([-i for i in b])
c=sorted([-i for i in c]) # avoid reverse=True 
a_dif=[a[i+1]-a[i] for i in range(x-1)]
b_dif=[b[i+1]-b[i] for i in range(y-1)]
c_dif=[c[i+1]-c[i] for i in range(z-1)]#差分をとる

que=[(a[0]+b[0]+c[0],(0,0,0))]
heapq.heapify(que)
s=set([(0,0,0)])
while k>0:
    ans,(ia,ib,ic)=heapq.heappop(que)
    print(-ans) #because sort with - 
    k-=1
    
    if ia < x-1 and (ia+1,ib,ic) not in s:
        heapq.heappush(que,(ans+a_dif[ia],(ia+1,ib,ic)))
        s.add((ia+1,ib,ic))
    if ib < y-1 and (ia, ib+1, ic) not in s:
        heapq.heappush(que, (ans+b_dif[ib], (ia, ib+1, ic)))
        s.add((ia, ib+1, ic))
    if ic < z-1 and (ia, ib, ic+1) not in s:
        heapq.heappush(que, (ans+c_dif[ic], (ia, ib, ic+1)))
        s.add((ia, ib, ic+1))

Submission status: Accepted

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

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

ans.sort(reverse=True)
for i in range(K):
    print(ans[i])

Submission status: Accepted

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

ans = []
log = set()

total = (a[0] + b[0] + c[0]) * (-1)
heapq.heappush(ans, (total, 0, 0, 0))
log.add((0, 0, 0))

for i in range(k):
  (total, aa, bb, cc) = heapq.heappop(ans)
  print(total * (-1))
  if (aa+1, bb, cc) not in log:
    if aa+1 < x:
      total = (a[aa+1] + b[bb] + c[cc]) * (-1)
      heapq.heappush(ans, (total, aa+1, bb, cc))
      log.add((aa+1, bb, cc))
  if (aa, bb+1, cc) not in log:
    if bb+1 < y:
      total = (a[aa] + b[bb+1] + c[cc]) * (-1)
      heapq.heappush(ans, (total, aa, bb+1, cc))
      log.add((aa, bb+1, cc))
  if (aa, bb, cc+1) not in log:
    if cc+1 < z:
      total = (a[aa] + b[bb] + c[cc+1]) * (-1)
      heapq.heappush(ans, (total, aa, bb, cc+1))
      log.add((aa, bb, cc+1))