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: Runtime Error

Solution code:
a, b, c, d = map(int, input().split())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
cl = list(map(int, input().split()))
al = sorted(al, reverse=True)
bl = sorted(bl, reverse=True)
cl = sorted(cl, reverse=True)
ad = [al[i] - al[i-1] for i in range(1, len(al))]
ad.insert(0, 0)
bd = [bl[i] - bl[i-1] for i in range(1, len(al))]
bd.insert(0, 0)
cd = [cl[i] - cl[i-1] for i in range(1, len(al))]
cd.insert(0, 0)
q = [[al[0] + bl[0] + cl[0], 0, 0, 0]]
ans = [[al[0] + bl[0] + cl[0], 0, 0, 0]]
while True:
    q = sorted(q)
    score, aq, bq, cq = map(int, q.pop())
    if len(ad) > aq+1:
        if [score + ad[aq+1], aq+1, bq, cq] not in ans:
            q.append([score + ad[aq+1], aq+1, bq, cq])
            ans.append([score + ad[aq+1], aq+1, bq, cq])
    if len(bd) > aq + 1:
        if [score + bd[bq+1], aq, bq+1, cq] not in ans:
            q.append([score + bd[bq+1], aq, bq+1, cq])
            ans.append([score + bd[bq+1], aq, bq+1, cq])
    if len(cd) > aq + 1:
        if [score + cd[cq+1], aq, bq, cq+1] not in ans:
            q.append([score + cd[cq+1], aq, bq, cq+1])
            ans.append([score + cd[cq+1], aq, bq, cq+1])

    ans = sorted(ans, reverse=True)
    q = sorted(q, reverse=True)
    if len(ans) > d:
        if ans[d] > q[0]:
            break

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

Submission status: Runtime Error

Solution code:
a, b, c, d = map(int, input().split())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
cl = list(map(int, input().split()))
al = sorted(al, reverse=True)
bl = sorted(bl, reverse=True)
cl = sorted(cl, reverse=True)
ad = [al[i] - al[i-1] for i in range(1, len(al))]
ad.insert(0, 0)
ad.append(-(10**15))
bd = [bl[i] - bl[i-1] for i in range(1, len(al))]
bd.insert(0, 0)
bd.append(-(10**15))
cd = [cl[i] - cl[i-1] for i in range(1, len(al))]
cd.insert(0, 0)
cd.append(-(10**15))
q = [[al[0] + bl[0] + cl[0], 0, 0, 0]]
ans = [[al[0] + bl[0] + cl[0], 0, 0, 0]]
while True:
    q = sorted(q)
    score, aq, bq, cq = map(int, q.pop())
    if [score + ad[aq+1], aq+1, bq, cq] not in ans:
        q.append([score + ad[aq+1], aq+1, bq, cq])
        ans.append([score + ad[aq+1], aq+1, bq, cq])
    if [score + bd[bq+1], aq, bq+1, cq] not in ans:
        q.append([score + bd[bq+1], aq, bq+1, cq])
        ans.append([score + bd[bq+1], aq, bq+1, cq])
    if [score + cd[cq+1], aq, bq, cq+1] not in ans:
        q.append([score + cd[cq+1], aq, bq, cq+1])
        ans.append([score + cd[cq+1], aq, bq, cq+1])

    ans = sorted(ans, reverse=True)
    q = sorted(q, reverse=True)

    if len(ans) > d:
        if ans[d] > q[0]:
            break

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

Submission status: Runtime Error

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)


# 処理済みフラグ
confilm = [[[False for _ in range(Z)] for _ in range(Y)] for _ in range(X)]

q = [(-(A[0] + B[0] + C[0]) , 0,0,0)]
confilm[0][0][0] = True

count = 0

while q :
    now = heapq.heappop(q)

    print(-now[0])
    count += 1
    if count >= K :
        break

    i , j , k = now[1] , now[2] , now[3]

    if not confilm[i+1][j][k] :
        heapq.heappush(q,(-(A[i+1] + B[j] + C[k]), i+1,j,k))
        confilm[i+1][j][k] = True
    if not confilm[i][j+1][k] :
        heapq.heappush(q,(-(A[i] + B[j+1] + C[k]), i,j+1,k))
        confilm[i][j+1][k] = True
    if not confilm[i][j][k+1] :
        heapq.heappush(q,(-(A[i] + B[j] + C[k+1]), i,j,k+1))
        confilm[i][j][k+1] = True

Submission status: Runtime Error

Solution code:
def main():

    """ convenient functions
    # for i, a in enumerate(iterable)
    # q, mod = divmod(a, b)
    # divmod(x, y) returns the tuple (x//y, x%y)
    # Higher-order function: reduce(operator.mul, xyz_count, 1)
    # manage median(s) using two heapq https://atcoder.jp/contests/abc127/tasks/abc127_f
    """

    import numpy as np
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product # https://docs.python.org/ja/3/library/itertools.html
    # accumulate() returns iterator! to get list: list(accumulate())
    from math import factorial, ceil, floor
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  # prime factor
        b, e = 2, 0  # base, exponent
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        # TODO: How should I do when n - r is negative?
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict # https://docs.python.org/ja/3/library/collections.html#collections.deque
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest # https://docs.python.org/ja/3/library/heapq.html
    from copy import deepcopy, copy # https://docs.python.org/ja/3/library/copy.html
    import operator
    from operator import itemgetter
    # ex1: List.sort(key=itemgetter(1))
    # ex2: sorted(tuples, key=itemgetter(1,2))
    from functools import reduce
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd # Deprecated since version 3.5: Use math.gcd() instead.
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)

    # first create factorial_list
    # fac_list = mod_factorial_list(n)
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    modpow = lambda a, n, p = MOD: pow(a, n, p) # Recursive function in python is slow!
    def modinv(a, p = MOD):
        # evaluate reciprocal using Fermat's little theorem:
        # a**(p-1) is identical to 1 (mod p) when a and p is coprime
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        # fac_list = modfactorial_list(100)
        # print(modcomb(100, 5, modfactorial_list(100)))
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    """ initialize variables and set inputs
    # initialize variables
        # to initialize list, use [0] * n
        # to initialize two dimentional array, use [[0] * N for _ in range(N)]
    # set inputs
        # open(0).read() is a convenient method:
        # ex) n, m, *x = map(int, open(0).read().split())
        #     min(x[::2]) - max(x[1::2])
        # ex2) *x, = map(int, open(0).read().split())
        #     don't forget to add comma after *x if only one variable is used
    # preprocessing
        # transpose = [x for x in zip(*data)]
        # ex) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
        # flat = [flatten for inner in data for flatten in inner]
        # ex) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [1, 2, 3, 4, 5, 6, 7, 8, 9]
    # calculate and output
        # output pattern
        # ex1) print(*l) => when l = [2, 5, 6], printed 2 5 6
    """

    # functions used
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    R = lambda: list(map(int, r().split()))
    Rmap = lambda: map(int, r().split())
    Rfloat = lambda: list(map(float, r().split()))
    Rtuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    # single int: int(r())
    # single string: r()
    # single float: float(r())
    # line int: R()
    # line string: r().split()
    # line (str, int, int): [j if i == 0 else int(j) for i, j in enumerate(r().split())]
    # lines int: [R() for _ in range(n)]
    """

    if __name__ != '__main__':
        # for test
        sys.stdin = open('sample.txt')

    # ----------------------------------
    # main

    X, Y, Z, K = R()
    A = R()
    B = R()
    C = R()
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    arrived = np.zeros(X * Y * Z).reshape(X, Y, Z) #larger
    
    heapq = []
    ans = []
    heapq.append(((A[0] + B[0] + C[0])*(-1),0,0,0))
    for i in range(K):
        a, x, y, z = heappop(heapq)
        ans.append(a)
        for s,t,u in [[1,0,0],[0,1,0],[0,0,1]]:
            d = x+s
            e = y+t
            f = z+u
            if d<X and e<Y and f<Z and arrived[d][e][f] == 0:
                heappush(heapq,((A[d] + B[e] + C[f])*(-1), d, e, f))
                arrived[d][e][f] = 1
    for i in ans:
        print(-i)
    # end of main
    # ----------------------------------

    """memo: how to use defaultdict of list
    # initialize
    Dic = defaultdict(list)
    # append / extend
    Dic[x].append(y)
    # for
    for k, v in Dic.items():
    """

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

AB=[0]*(x*y)
i=0
for a in A:
    for b in B:
        AB[i]=a+b
        i+=1

AB=AB.sort(reverse=True)[:min(k,x*y)]
ABC=[0]*(min(k,x*y)*z)
i=0
for ab in AB:
    for c in C:
        ABC[i]=ab+c
        i+=1

ABC.sort(reverse=True)

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

Submission status: Runtime Error

Solution code:
import sys
readline = sys.stdin.readline

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

a.sort(); a.reverse()
b.sort(); b.reverse()
c.sort(); c.reverse()

added = [[0,0,0]]
heap = [[0,0,0,0] for _ in range(k)]
heap[0] = [a[0]+b[0]+c[0],0,0,0]
n = 1

def upheap(heap,n):
	i = n-1
	while i > 0:
		parent = (i-1)//2
		if heap[i][0] > heap[parent][0]:
			tmp = heap[parent]
			heap[parent] = heap[i]
			heap[i] = tmp
		else:
			break
	# print("up", n, heap)

def downheap(heap, n):
	heap[0] = heap[n-1]
	n -= 1
	if n > 0:
		i = 0
		while True:
			child = 2 * i + 1
			if child > n-1:
				break
			if child < n-1 and heap[child][0] < heap[child+1][0]:
				child += 1
			if heap[i][0] < heap[child][0]:
				tmp = heap[child]
				heap[child] = heap[i]
				heap[i] = tmp
				i = child
			else:
				break
	# print("down", n, heap)
	return n

for _ in range(k):
	# print(n, heap)
	v, pa, pb, pc = map(int, heap[0])
	print(v)
	n = downheap(heap, n)
	if pa+1 < x and not [pa+1, pb, pc] in added:
		added.append([pa+1, pb, pc])
		n += 1
		heap[n-1] = [a[pa+1] + b[pb] + c[pc], pa+1, pb, pc]
		upheap(heap, n)
	if pb+1 < y and not [pa, pb+1, pc] in added:
		added.append([pa, pb+1, pc])
		n += 1
		heap[n-1] = [a[pa] + b[pb+1] + c[pc], pa, pb+1, pc]
		upheap(heap, n)
	if pc+1 < z and not [pa, pb, pc+1] in added:
		added.append([pa, pb, pc+1])
		n += 1
		heap[n-1] = [a[pa] + b[pb] + c[pc+1], pa, pb, pc+1]
		upheap(heap, n)

Submission status: Runtime Error

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)

import heapq

heap = []
heapq.heappush(heap, (-(A[0]+B[0]+C[0]), 0, 0, 0))
added = set([(0, 0, 0)])

for _ in range(K):
    p, i, j, k = heapq.heappop(heap)
    if (i+1, j, k) not in added and i+1<X:
        heapq.heappush(heap, (-(A[i+1]+B[j]+C[k]), i+1, j, k))
        added.add((i+1, j, k))
    if (i, j+1, k) not in added and j+1<Y:
        heapq.heappush(heap, (-(A[i]+B[j+1]+C[k]), i, k+1, j))
        added.add((i, j+1, k))
    if (i, j, k+1) not in added and k+1<Z:
        heapq.heappush(heap, (-(A[i]+B[j]+C[k+1]), i, j, k+1))
        added.add((i, j, k+1))
    print(-p)

Submission status: Runtime Error

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)

import heapq

heap = []
heapq.heappush(heap, (-(A[0]+B[0]+C[0]), 0, 0, 0))
added = set([(0, 0, 0)])

for _ in range(K):
    p, i, j, k = heapq.heappop(heap)
    if (i+1, j, k) not in added and i+1<X:
        heapq.heappush(heap, -(A[i+1]+B[j]+C[k]), i+1, j, k)
        added.add((i+1, j, k))
    if (i, j+1, k) not in added and j+1<Y:
        heapq.heappush(heap, -(A[i]+B[j+1]+C[k]), i, k+1, j)
        added.add((i, j+1, k))
    if (i, j, k+1) not in added and k+1<Z:
        heapq.heappush(heap, -(A[i]+B[j]+[k]), i, j, k+1)
        added.add((i, j, k+1))
    print(-p)

Submission status: Runtime Error

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

AB = [a + b for (a, b) in list(itertools.product(A, B))]
AB.sort(reverse=True)
ans = [ab + c for (ab, c) in list(itertools.product(AB[:min(3000, X*Y*Z)], C))]
ans.sort(reverse=True)
for i in range(K):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import heapq

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)

heap = []
heapq.heappush(heap, (-(A[0] + B[0] + C[0]), 0, 0, 0))
for _ in range(K):
    p, i, j, k = heapq.heappop(heap)
    heapq.heappush(heap, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
    heapq.heappush(heap, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
    heapq.heappush(heap, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
    print(-p)

Submission status: Runtime Error

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()
b.sort()
c.sort()
for i in range(min(x,k)):
  for j in range(min(y,k)):
    if i*j>k:
      break
    for l in range(min(z,k)):
      if i*j*l>k:
        break
      else:
        ans.append(a[i],b[j],c[l])
ans.sort()
for i in range(k):
  print(ans[i])

Submission status: Runtime Error

Solution code:
import heapq

a,b,c,k=map(int,input())
arr1=list(map(int,input().split()))
arr1=sorted(arr1,reverse=True)
arr2=list(map(int,input().split()))
arr2=sorted(arr2,reverse=True)
arr3=list(map(int,input().split()))
arr3=sorted(arr3,reverse=True)
heap=[]
heapq.heappush(heap,(-(arr1[0]+arr2[0]+arr3[0]),0,0,0))
s={(0,0,0)}
for _ in range(k):
  v,i,j,k=heapq.heappop(heap)
  if i+1<a and (i+1,j,k) not in s:
    s.add((i+1,j,k))
    heapq.heappush(heap,(-(arr1[i+1]+arr2[j]+arr3[k]),i+1,j,k))
  if j+1<b and (i,j+1,k) not in s:
    s.add((i,j+1,k))
    heapq.heappush(heap,(-(arr1[i]+arr2[j+1]+arr3[k]),i,j+1,k))
  if k+1<c and (i,j,k+1) not in s:
    s.add((i,j,k+1))
    heapq.heappush(heap,(-(arr1[i]+arr2[j]+arr3[k+1]),i,j,k+1))
  print(-v)

Submission status: Runtime Error

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, key = lambda x:-x)
B = sorted(B, key = lambda x:-x)
C = sorted(C, key = lambda x:-x)

AB = [0] * (X * Y)

index = 0
for i in range(len(A)):
    for j in range(len(B)):
        AB[index] = A[i] + B[j]
        index += 1

AB = sorted(AB, key = lambda x:-x)

ABC = [0] * min(3000, X * Y * Z)
index = 0
for i in AB:
    for j in C:
        ABC[index] = i + j
        index += 1

ABC = sorted(ABC, key = lambda x:-x)
for i in range(0,K):
    print(ABC[i])

Submission status: Runtime Error

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

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


sums = []
for i in range(K):
    for j in range(K):
        sum.append(A[i]+B[i]+C[j])
            
list.sort(sums,reverse = True)

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

Submission status: Runtime Error

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

ab = []
for i in range(x):
    for j in range(y):
        ab.append(a[i] + b[j])


ab.sort(reversed=True)
c.sort(reversed=True)

abc = []
for i in range(k):
    for j in range(z):
        abc.append(ab[i] + c[i])


abc.sort(reversed=True)
for i in range(k):
    print(abc[i])

Submission status: Runtime Error

Solution code:
import sys
import numpy as np


def main():
    x, y, z, k = [int(n) for n in input().split()]
    a = sorted([int(n) for n in input().split()], reverse=True)
    b = sorted([int(n) for n in input().split()], reverse=True)
    c = sorted([int(n) for n in input().split()], reverse=True)
    va = np.array(a)[:, np.newaxis, np.newaxis]
    vb = np.array(b)[np.newaxis, :, np.newaxis]
    vc = np.array(c)[np.newaxis, np.newaxis, :]
    vx = np.arange(1, x + 1)[:, np.newaxis, np.newaxis]
    vy = np.arange(1, y + 1)[np.newaxis, :, np.newaxis]
    vz = np.arange(1, z + 1)[np.newaxis, np.newaxis, :]
    print(*(sorted((va + vb + vc)[(vx * vy * vz) <= k], reverse=True))[:k], sep="\n")


main()

Submission status: Runtime Error

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

LA.sort(reverse=True)
LB.sort(reverse=True)
LC.sort(reverse=True)

L=[]

for p in range(min(X,K-2)):
    for q in range(min(Y,K-p-1)):
        for r in range(min(Z,K-p-q)):
            L.append(LA[p]+LB[q]+LC[r])

L.sort(reverse=True)

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

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

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

for i in A:
	for j in B:
		tmp.append(i + j)
		
tmp.sort(reverse=True)

for i in range(K):
	for j in C:
		ans.append(tmp[i] + j)
		
ans.sort(reverse=True)

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

Submission status: Runtime Error

Solution code:
x, y, z, k = [ int(v) for v in input().split() ]
a_list = sorted([ -int(v) for v in input().split() ], reverse = True )
b_list = sorted([ -int(v) for v in input().split() ], reverse = True )
c_list = sorted([ -int(v) for v in input().split() ], reverse = True )


def most_dericious(a,b,c):
	return a_list[a] + b_list[b] + c_list[c]
out_cakes_list= []
for i in range(k):
	if i == 0:
		dericious_heap = [ (most_dericious(x-1,y-1,z-1),(x-1,y-1,z-1)) ]
		heapq.heapify(dericious_heap)
	out_cakes = (heapq.heappop(dericious_heap))
	p, q, r = out_cakes[1][0], out_cakes[1][1], out_cakes[1][2]

	if p-1 >= 0 and (most_dericious(p-1,q,r),(p-1,q,r)) not in dericious_heap:
		heapq.heappush(dericious_heap,(most_dericious(p-1,q,r),(p-1,q,r)))
	if q-1 >= 0 and (most_dericious(p,q-1,r),(p,q-1,r)) not in dericious_heap:
		heapq.heappush(dericious_heap,(most_dericious(p,q-1,r),(p,q-1,r)))
	if r-1 >= 0 and (most_dericious(p,q,r-1),(p,q,r-1)) not in dericious_heap:
		heapq.heappush(dericious_heap,(most_dericious(p,q,r-1),(p,q,r-1)))
	out_cakes_list.append(out_cakes)

out_cakes_list = sorted(out_cakes_list)
for i in range(k):
	print(-out_cakes_list[i][0])

Submission status: Runtime Error

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)
# m = A[0]+B[0]+C[0]
AB = []
for a in A:
    for b in B:
        AB.append(a+b)
AB.sort(reverse=True)
AB = AB[:K]
for c in C:
    for ab in AB:
        ABC.append(c+ab)
ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])

Submission status: Runtime Error

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)
# m = A[0]+B[0]+C[0]
def getAB(A,B,K):
    AB = []
    cnt = 0
    for a in A:
        for b in B:
            AB.append(a+b)
            cnt += 1
            if cnt >= K:
                return AB

AB = getAB(A, B, K)
cnt = 0
for c in C:
    for ab in AB:
        ABC.append(c+ab)

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

Submission status: Runtime Error

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)
# m = A[0]+B[0]+C[0]
AB = []
for a in A:
    for b in B:
        AB.append(a+b)
ABC = [AB[:K]]
for c in C:
    for ab in AB:
        ABC.append(c+ab)
ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])

Submission status: Runtime Error

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

ab=[]
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort()
aa=[]
for i in range(k):
    if x*y-i>=0:
        aa.append(ab[x*y-i])
ans=[]
for i in range(len(aa)):
    for j in range(z):
        ans.append(aa[i]+c[j])
ans.sort()
for i in range(k):
    print(ans[len(ans)-i-1])

Submission status: Runtime Error

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

ab=[]
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort()
aa=[]
for i in range(k):
    if x*y-i>=0:
        aa.append(ab[x*y-i])
ans=[]
for i in range(len(aa)):
    for j in range(z):
        ans.append(aa[i]+c[j])
ans.sort()
for i in range(k):
    print(ans[len(ans)-i-1]

Submission status: Runtime Error

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

ab=[]
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort()
aa=[]
for i in range(k):
    if x*y-i>=0:
        aa.append(ab[x*y-i])
ans=[]
for i in range(len(aa)):
    for j in range(z):
        ans.append(aa[i]+c[j])
ans.sort()
for i in range(k):
    print(ans[len(ans)-i]

Submission status: Runtime Error

Solution code:
# To avoid the use of reverse, define inverse order intentionally
def merge(a, b):
        i, j = 0, 0
        merged = []
        
        while(i < len(a) or j < len(b)):
                if(j >= len(b) or a[i] > b[j]): # take from a if a[i] is larger than b[j]
                        merged.append(a[i])
                        i += 1
                else:
                        merged.append(b[j])
                        j += 1
        
        return merged

# merge sort with inverse order
def merge_sort(l):
        if(len(l) == 1): return l
        
        l1 = l[0:(len(l)//2)]
        l2 = l[(len(l)//2):]

        ll1 = merge_sort(l1)
        ll2 = merge_sort(l2)

        return merge(ll1, ll2)

XYZK = list(map(int, input().split()))
tastes = []

for n in range(3): tastes.append(list(map(int, input().split())))

ab_tastes = []

for i in range(XYZK[0]):
        for j in range(XYZK[1]):
                ab_tastes.append(tastes[0][i]+tastes[1][j])

ab_tastes = merge_sort(ab_tastes)
final = []

for i in range(min(XYZK[0]*XYZK[1], XYZK[3])):
        for j in range(XYZK[2]):
                final.append(ab_tastes[i]+tastes[2][j])

final = merge_sort(final)

for i in range(XYZK[3]):
        print(final[i])

Submission status: Runtime Error

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

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

result = [0]*K**3

ri = 0;
for i in range(min(len(a), K)):
    for j in range(min(len(b), K)):
        for k in range(min(len(c), K)):
            result[ri] = a[i] + b[j] + c[k]
            ri += 1
            
result.sort(reverse=True)

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

Submission status: Runtime Error

Solution code:
import itertools

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

d=[sum(i) for i in list(itertools.product(a, b))]
e=[print(i) for i in sorted([sum(i) for i in list(itertools.product(a, e))], reverse=True)]