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.

Solution (Python):
import heapq

x, y, z, k = [int(i) for i in input().split(' ')]
A = [int(i) for i in input().split(' ')]
B = [int(i) for i in input().split(' ')]
C = [int(i) for i in input().split(' ')]

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

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

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

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

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

[print(i) for i in result]

Solution (Python):



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

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

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

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

Solution (Python):

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

Solution (Python):
from itertools import product, chain
from collections import deque


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


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


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

Solution (Python):
import sys
input = sys.stdin.readline
 
x, y, z, k = map(int, input().split())
a = sorted(int(i) for i in input().split())
b = sorted(int(i) for i in input().split())
c = sorted(int(i) for i in input().split())
 



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

Solution (Python):
import sys
input = sys.stdin.readline

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





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

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

Solution (Python):
import sys
input = sys.stdin.readline

x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c=sorted(int(i) for i in input().split())[:-k-1:-1]

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


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

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

Solution (Python):
import sys
input = sys.stdin.readline

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

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


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

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

Solution (Python):
import sys
input = sys.stdin.readline

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):
import heapq

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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


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


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

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

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

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


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

Solution (Python):
from itertools import product, chain

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

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

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

A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

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

AB.sort(reverse=True)

AB = AB[0:K]

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

ABC.sort(reverse=True)

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

Solution (Python):
import sys, copy
import heapq
input = sys.stdin.readline

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

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

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

Solution (Python):



import math
import sys
import itertools
import numpy as np
import heapq
 

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

a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]

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

h = [] 
g = set() 

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



for i in range(k):

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

Solution (Python):


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

ab=sorted([i+j for i in a for j in b],reverse=True)
res=sorted([ij+l for ij in ab[:k] for l in c],reverse=True)
for i in res[:k]:
 print i

Solution (Python):
import heapq

x,y,z,k = map(int,input().split())
a = sorted(list(map(int,input().split())),reverse=True)
b = sorted(list(map(int,input().split())),reverse=True)
c = sorted(list(map(int,input().split())),reverse=True)
ans = []








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



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

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


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

for i in range(k):
    delicious()

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

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

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

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

k = int(numbers[3])

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

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

for l in f_ans:
    print(l)

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

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

from heapq import heapify, heappop, heappush, heappushpop
 
 
class PriorityQueue:
    def __init__(self, heap):
        '''
        heap ... list
        '''
        self.heap = heap
        heapify(self.heap)
 
    def push(self, item):
        heappush(self.heap, item)
 
    def pop(self):
        return heappop(self.heap)
 
    def pushpop(self, item):
        return heappushpop(self.heap, item)
 
    def __call__(self):
        return self.heap
 
 
heap = []  
 
q = PriorityQueue(heap)
q.push((-(A[0] + B[0] + C[0]), 0, 0, 0))
 
considered = set()
ans = []
for k_th in range(1, K+1):
    heap_max, i, j, k = q.pop()
    ans.append(-heap_max)
    for di, dj, dk in zip([1, 0, 0], [0, 1, 0], [0, 0, 1]):
        i_new, j_new, k_new = i + di, j + dj, k + dk
        if i_new >= X or j_new >= Y or k_new >= Z:
            continue
        if (i_new, j_new, k_new) in considered:
            continue
        considered.add((i_new, j_new, k_new))
        q.push((-(A[i_new] + B[j_new] + C[k_new]), i_new, j_new, k_new))
 
 
print(*ans, sep='\n')

Solution (Python):
import heapq

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

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

registered = set()
pool = []

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

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

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

import heapq


queue = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]

heapq.heapify(queue)

visited = set()
i = 0
for i in range(K):
    while True:
        v, a, b, c = heapq.heappop(queue)  
        if (a, b, c) not in visited:
            break
    visited.add((a, b, c))
    print(-v)

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

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

A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

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

AB.sort(reverse=True)

AB = AB[0:K]

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

ABC.sort(reverse=True)

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

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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])

Solution (Python):
r=range;x,y,z,k=map(int,input().split())
a,b,c=eval("list(map(int,input().split())), "*3)
ab=sorted([a[i]+b[j]for i in r(x)for j in r(y)],reverse=True)
abc=sorted([ab[i]+c[j]for i in r(min(3000,x*y))for j in r(z)],reverse=True)
print(*[abc[i]for i in r(k)])

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

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

Solution (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)
    
    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(*ans, sep='\n')

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

Solution (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)
    
    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(*ans, sep='\n')

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

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

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):

"""
Created on Wed Apr 10 22:30:07 2019

@author: Yamazaki Kenichi
"""
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)
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])