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:
from heapq import heappush, heappop
def main():
    X, Y, Z, K = map(int, input().split())
    a_cake = [int(x) for x in input().split()]
    b_cake = [int(x) for x in input().split()]
    c_cake = [int(x) for x in input().split()]

    a_cake.sort(reverse = True)
    b_cake.sort(reverse = True)
    c_cake.sort(reverse = True)

    heap_queue = []
    arg_hash = {}
    score_taste = a_cake[0] + b_cake[0] + c_cake[0]
    heappush(heap_queue, (-score_taste, 0, 0, 0))

    for i in range(K):
        score, a_candle, b_candle, c_candle = heappop(heap_queue)
        print(-score)

        arg_a = (a_candle+1, b_candle, c_candle)
        arg_b = (a_candle, b_candle+1, c_candle)
        arg_c = (a_candle, b_candle, c_candle+1)

        if a_candle < X-1 and arg_a not in arg_hash:
            arg_hash[arg_a] = 1
            heappush(heap_queue, (-(a_cake[a_candle+1] + b_cake[b_candle] + c_cake[c_candle]),) + arg_a)
        if b_candle < Y -1 and arg_b not in arg_hash:
            arg_hash[arg_b] = 1
            heappush(heap_queue, (-(a_cake[a_candle] + b_cake[b_candle+1] + c_cake[c_candle]),) + arg_b)
        if c_candle < Z -1 and arg_c not in arg_hash:
            arg_hash[arg_c] = 1
            heappush(heap_queue, (-(a_cake[a_candle] + b_cake[b_candle] + c_cake[c_candle+1]),) + arg_c)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# cited the article at https://qiita.com/shinhiro/items/c652e042828ce26074e4
from heapq import heappush, heappop
def main():
    X, Y, Z, K = map(int, input().split())
    a_cake = [int(x) for x in input().split()]
    b_cake = [int(x) for x in input().split()]
    c_cake = [int(x) for x in input().split()]

    a_cake.sort(reverse = True)
    b_cake.sort(reverse = True)
    c_cake.sort(reverse = True)

    heap_queue = []
    arg_hash = {}
    score_taste = a_cake[0] + b_cake[0] + c_cake[0]
    heappush(heap_queue, (-score_taste, 0, 0, 0))

    for i in range(K):
        score, a_candle, b_candle, c_candle = heappop(heap_queue)
        print(-score)

        arg_a = (a_candle+1, b_candle, c_candle)
        arg_b = (a_candle, b_candle+1, c_candle)
        arg_c = (a_candle, b_candle, c_candle+1)

        if a_candle < X-1 and arg_a not in arg_hash:
            arg_hash[arg_a] = 1
            heappush(heap_queue, (-(a_cake[a_candle+1] + b_cake[b_candle] + c_cake[c_candle]),) + arg_a)
        if b_candle < Y -1 and arg_b not in arg_hash:
            arg_hash[arg_b] = 1
            heappush(heap_queue, (-(a_cake[a_candle] + b_cake[b_candle+1] + c_cake[c_candle]),) + arg_b)
        if c_candle < Z -1 and arg_c not in arg_hash:
            arg_hash[arg_c] = 1
            heappush(heap_queue, (-(a_cake[a_candle] + b_cake[b_candle] + c_cake[c_candle+1]),) + arg_c)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
#import numpy as np

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

N = 10000
def tokey(i, j, k):
    return i*N*N + j*N + k
def dekey(n):
    i = int(n / N / N)
    j = int(n / N) % N
    k = n % N
    return i, j, k

def main():
    X, Y, Z, K = i2nn()
    A = i2nn()
    B = i2nn()
    C = i2nn()
    A.sort()
    A.reverse()
    B.sort()
    B.reverse()
    C.sort()
    C.reverse()
    Q = [(A[0] + B[0] + C[0], 0, 0, 0)]
    loop = 0
    stored = set()
    def next(i, j, k):
        if i >= X or j >= Y or k >= Z:
            return
        key = tokey(i, j, k)
        if key not in stored:
            stored.add(key)
            n = A[i] + B[j] + C[k]
            Q.append((n, i, j, k))
    while True:
        v = max(Q, key=lambda v: v[0])
        Q.remove(v)
        print(v[0])
        loop += 1
        if loop >= K:
            break
        next(v[1]+1, v[2], v[3])
        next(v[1], v[2]+1, v[3])
        next(v[1], v[2], v[3]+1)

main()

Submission status: Accepted

Solution code:
import sys
#import numpy as np

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

N = 10000
def tokey(i, j, k):
    return i*N*N + j*N + k
def dekey(n):
    i = int(n / N / N)
    j = int(n / N) % N
    k = n % N
    return i, j, k

def main():
    X, Y, Z, K = i2nn()
    A = i2nn()
    B = i2nn()
    C = i2nn()
    A.sort()
    A.reverse()
    B.sort()
    B.reverse()
    C.sort()
    C.reverse()
    Q = [(A[0] + B[0] + C[0], 0, 0, 0)]
    loop = 0
    stored = set()
    def next(i, j, k):
        if i >= X or j >= Y or k >= Z:
            return
        key = tokey(i, j, k)
        if key not in stored:
            stored.add(key)
            n = A[i] + B[j] + C[k]
            Q.append((n, i, j, k))
    while True:
        Q.sort(key=lambda v: v[0])
        v = Q.pop()
        print(v[0])
        loop += 1
        if loop >= K:
            break
        next(v[1]+1, v[2], v[3])
        next(v[1], v[2]+1, v[3])
        next(v[1], v[2], v[3]+1)

main()

Submission status: Accepted

Solution code:
from collections import Counter
from collections import deque
from functools import reduce
from pprint import pprint
# import statistics
import bisect
import copy
import fractions
import math
import random
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
def GCD_LIST(numbers): return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers): return reduce(LCM, numbers)
def LCM(m, n): return (m * n // fractions.gcd(m, n))
def unite(x, y):
    # それぞれのノードの根を求める
    x = root(x)
    y = root(y)
    if x == y:
        return
    # node[x]の根をyに変更する
    node[x] = y
def same(x,y): return bool(root(x) == root(y))
def root(x):
    if node[x] == x:  # xが根の場合
        return x
    else:
        node[x] = root(node[x])  # 経路圧縮
        return node[x]
def dfs(v):
    # ノードに訪れた
    visited[v] = True
    for v2 in range(n):
        if graph[v][v2] == False:  # そもそもvとv2が辺でつながっていない
            continue
        if visited[v2] == True:  # もう見た
            continue
        dfs(v2)


x, y, z, k = MI()
a = LI()
b = LI()
c = LI()
tmp = []
ans = []
for i in a:
    for j in b:
        tmp.append(i+j)
tmp = sorted(tmp, reverse=True)
for p in tmp[:k]:
    for q in c:
        ans.append(p+q)
ans = sorted(ans, reverse=True)
for num in ans[:k]:
    print(num)

Submission status: Accepted

Solution code:
X,Y,Z,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
ans=[]
for i in range(X):
	j=0
	while((i+1)*(j+1)<K+1 and j<Y):
		k=0
		while((i+1)*(j+1)*(k+1)<K+1 and k<Z):
			ans.append(A[i]+B[j]+C[k])
			k+=1
		j+=1
ans.sort(reverse=True)
for i in range(K):
    print(ans[i])

Submission status: Accepted

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(min(K, len(tmp))):
	for j in C:
		ans.append(tmp[i] + j)
		
ans.sort(reverse=True)

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

Submission status: Accepted

Solution code:
## reference: https://qiita.com/shinhiro/items/c652e042828ce26074e4

from heapq import heappush, heappop

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)

solver = []
hq = []
heappush(hq, (-(A[0]+B[0]+C[0]), 0, 0, 0))
for i in range(K):
  val, idx_a, idx_b, idx_c = heappop(hq)
  solver.append(-val)
  if idx_a + 1 < X:
    new_tuple_a = (-(A[idx_a+1]+B[idx_b]+C[idx_c]),idx_a+1,idx_b,idx_c)
    if new_tuple_a not in hq:
      heappush(hq, new_tuple_a)
  if idx_b + 1 < Y:
    new_tuple_b = (-(A[idx_a]+B[idx_b+1]+C[idx_c]),idx_a,idx_b+1,idx_c)
    if new_tuple_b not in hq:
      heappush(hq, new_tuple_b)
  if idx_c + 1 < Z:
    new_tuple_c = (-(A[idx_a]+B[idx_b]+C[idx_c+1]),idx_a,idx_b,idx_c+1)
    if new_tuple_c not in hq:
      heappush(hq, new_tuple_c)

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

Submission status: Accepted

Solution code:
def intinput(): return map(int,input().split())

x,y,z,k=intinput()
a = list(intinput())
b = list(intinput())
c = list(intinput())
a.sort(reverse=1)
b.sort(reverse=1)
c.sort(reverse=1)
abc=[]
for i in range(x):
    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.sort(reverse=1)
for i in range(k):
    print(abc[i])

Submission status: Accepted

Solution code:
import sys, os
from queue import PriorityQueue

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

x, y, z, k = f()

def merge(a, b):
    q = PriorityQueue()
    ans = []

    for i in range(len(a)):
        q.put((-a[i] - b[0], 0))


    for i in range(min(k, len(a)*len(b))):
        cur = q.get()
        ans.append(-cur[0])
        idx = cur[1]
        if cur[1] + 1<len(b):
            q.put(( cur[0]+ b[idx] - b[idx+1] , cur[1] + 1))
    return ans

def solve():
    a = f()
    b = f()
    c = f()


    a = sorted(a, reverse = True)
    b = sorted(b, reverse = True)
    c = sorted(c, reverse = True)

    ans = merge(a,b)
    ans = merge(ans,c)

    for i in ans:
        print(i)


solve()

Submission status: Accepted

Solution code:
# 優先度付きキューで解く問題
# Atcoder abc123 D - Cake 123

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

import heapq
import sys

INT_MIN = -1 * sys.maxsize

a_cakes = A[:]
a_cakes = sorted(a_cakes, reverse=True)
b_cakes = B[:]
b_cakes = sorted(b_cakes, reverse=True)
c_cakes = C[:]
c_cakes = sorted(c_cakes, reverse=True)


checked_dict = {'0_0_0': True}

a = a_cakes[0]
b = b_cakes[0]
c = c_cakes[0]
value_list = [(-a-b-c, (0, 0, 0))]
heapq.heapify(value_list)

for _ in range(K):
    val = heapq.heappop(value_list)
    print(-1*val[0])
    #print(val)
    index = (val[1][0]+1, val[1][1], val[1][2])
    key = '{}_{}_{}'.format(*index)
    if not key in checked_dict:
        if not a_cakes[index[0]: index[0]+1]:
            continue
        checked_dict[key] = True
        a = a_cakes[index[0]]
        b = b_cakes[index[1]]
        c = c_cakes[index[2]]
        heapq.heappush(value_list, (-a-b-c, index))
        
    index = (val[1][0], val[1][1]+1, val[1][2])
    key = '{}_{}_{}'.format(*index)
    if not key in checked_dict:
        if not b_cakes[index[1]: index[1]+1]:
            continue
        checked_dict[key] = True
        a = a_cakes[index[0]]
        b = b_cakes[index[1]]
        c = c_cakes[index[2]]
        heapq.heappush(value_list, (-a-b-c, index))
        
    index = (val[1][0], val[1][1], val[1][2]+1)
    key = '{}_{}_{}'.format(*index)
    if not key in checked_dict:
        if not c_cakes[index[2]: index[2]+1]:
            continue
        checked_dict[key] = True
        a = a_cakes[index[0]]
        b = b_cakes[index[1]]
        c = c_cakes[index[2]]
        heapq.heappush(value_list, (-a-b-c, index))

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from heapq import heappush, heappop
from collections import defaultdict

x, y, z, k = li()
a = list(li())
b = list(li())
c = list(li())

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

amax = a[0]
bmax = b[0]
cmax = c[0]

acnt = 0
bcnt = 0
ccnt = 0

heap = [(-amax - bmax - cmax, acnt, bcnt, ccnt)]
ans = []

visited = defaultdict(int)
visited[(0,0,0)] = 1

for _ in range(k):
    cur, acnt, bcnt, ccnt = heappop(heap)

    if acnt < len(a)-1:
        if not visited[(acnt+1, bcnt, ccnt)]:
            heappush(heap, (-a[acnt+1]-b[bcnt]-c[ccnt], acnt+1, bcnt, ccnt))
            visited[(acnt+1, bcnt, ccnt)] = 1

    if bcnt < len(b)-1:
        if not visited[(acnt, bcnt+1, ccnt)]:
            heappush(heap, (-a[acnt]-b[bcnt+1]-c[ccnt], acnt, bcnt+1, ccnt))
            visited[(acnt, bcnt+1, ccnt)] = 1

    if ccnt < len(c)-1:
        if not visited[(acnt, bcnt, ccnt+1)]:
            heappush(heap, (-a[acnt]-b[bcnt]-c[ccnt+1], acnt, bcnt, ccnt+1))
            visited[(acnt, bcnt, ccnt+1)] = 1

    ans.append(-cur)

for ansi in ans:
    print(ansi)

Submission status: Accepted

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

ab=[]
for 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-1>=0:
        aa.append(ab[x*y-i-1])
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: Accepted

Solution code:
def get_input():
    X, Y, Z, K = [int(_) for _ in raw_input().split()]
    A = [int(_) for _ in raw_input().split()]
    B = [int(_) for _ in raw_input().split()]
    C = [int(_) for _ in raw_input().split()]
    return X, Y, Z, K, A, B, C

def AddToQueue(_ele, _queue, _ijk_set, K):
    if len(_queue) == 0:
        _queue.append(_ele)
    else:
        cnt = 0
        while cnt < len(_queue) and _queue[cnt][0] >= _ele[0]:
            cnt += 1
        ijk = (_ele[1], _ele[2], _ele[3])
        if not (ijk in _ijk_set):
            _queue.insert(cnt, _ele)
            _ijk_set.add(ijk)
            _queue = _queue[:K]

def solve3():
    X, Y, Z, K, A, B, C = get_input()
    A.sort(reverse = True)
    B.sort(reverse = True)
    C.sort(reverse = True)
    ijk_set = set()
    queue = [(A[0] + B[0] + C[0], 0, 0, 0)]
    ijk_set.add((0, 0, 0))
    for cnt in range(K):
        ele, i, j, k = queue.pop(0)
        print ele
        if i + 1 < X:
            AddToQueue((A[i+1] + B[j] + C[k], i+1, j, k), queue, ijk_set, K)
        if j + 1 < Y:
            AddToQueue((A[i] + B[j+1] + C[k], i, j+1, k), queue, ijk_set, K)
        if k + 1 < Z:
            AddToQueue((A[i] + B[j] + C[k+1], i, j, k+1), queue, ijk_set, K)

if __name__ == '__main__':
    solve3()

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()
a.reverse()
b.sort()
b.reverse()
c.sort(reverse=True)
ans = []

for n in range(len(a)):
    for m in range(len(b)):
        if (n+1)*(m+1) > k:
            break
        for l in range(len(c)):
            if (n+1)*(m+1)*(l+1) > k:
                break
            ans.append(a[n]+b[m]+c[l])

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

Submission status: Accepted

Solution code:
def MergeSort(arr):
    if len(arr) <= 1:
        return arr
    mid = len(arr) // 2
    left = arr[:mid]
    right = arr[mid:]
    left = MergeSort(left)
    right = MergeSort(right)
    return MergeArray(left, right)

def MergeArray(left, right):
    merged = []
    li, ri = 0, 0
    while li < len(left) and ri < len(right):
        if left[li] > right[ri]:
            merged.append(left[li])
            li += 1
        else:
            merged.append(right[ri])
            ri += 1
    if li < len(left):
        merged.extend(left[li:])
    elif ri < len(right):
        merged.extend(right[ri:])
    return merged

def solve():
    X, Y, Z, K = [int(_) for _ in raw_input().split()]
    A = [int(_) for _ in raw_input().split()]
    B = [int(_) for _ in raw_input().split()]
    C = [int(_) for _ in raw_input().split()]
    A = MergeSort(A)[:K]
    B = MergeSort(B)[:K]
    C = MergeSort(C)[:K]
    lB = len(B)
    lC = len(C)
    sumAB = []
    for i in range(lB):
        tmpA = [ele + B[i] for ele in A]
        sumAB = MergeArray(sumAB, tmpA)[:K]
    sumABC = []
    for i in range(lC):
        tmpAB = [ele + C[i] for ele in sumAB]
        sumABC = MergeArray(sumABC, tmpAB)[:K]
    for i in range(K):
        print sumABC[i]

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
x,y,z,k=map(int,input().split())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
C=[int(i) for i in input().split()]
a,b,c=0,0,0
AB=[]
for a in A:
  for b in B:
    AB.append(a+b)
AB.sort(reverse=True)
AB=AB[:k]
ABC=[]
for ab in AB:
  for c in C:
    ABC.append(ab+c)
ABC.sort(reverse=True)
print(*ABC[:k],sep="\n")

Submission status: Accepted

Solution code:
import numpy as np
import heapq

# S = input()
# n = int(input())
# V = list(map(int, input().split()))

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

li = [
    sorted(list(map(int, input().split()))),
    sorted(list(map(int, input().split()))),
    sorted(list(map(int, input().split()))),
]
cand = []

m = li[0][x-1] + li[1][y-1] + li[2][z-1]
heapq.heappush(cand, (-m, x-1, y-1, z-1))
memo = set()
res = 0

while res < K:
    m,i,j,k = heapq.heappop(cand)

    if (i,j,k) in memo:
        continue
    memo.add((i,j,k))
    print(-m)
    res += 1

    if i > 0:
        m = li[0][i-1] + li[1][j] + li[2][k]
        heapq.heappush(cand, (-m, i-1, j, k))
    if j > 0:
        m = li[0][i] + li[1][j-1] + li[2][k]
        heapq.heappush(cand, (-m, i, j-1, k))
    if k > 0:
        m = li[0][i] + li[1][j] + li[2][k-1]
        heapq.heappush(cand, (-m, i, j, k-1))

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

d = []
for xx in a:
    for yy in b:
        d.append(xx+yy)
d.sort()
d.reverse()


e = []
if len(d)>3000:
    for i in range(3000):
        for zz in c:
            e.append(d[i]+zz)
else:
    for xy in d:
        for zz in c:
            e.append(xy+zz)
e.sort()
e.reverse()

for i in range(k):
    print(e[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 = 1)
b.sort(reverse = 1)
c.sort(reverse = 1)

v = []

for p in range(x):
  for q in range(y):
    for r in range(z):
      if (p+1)*(q+1)*(r+1) > 3000:
        break
      v.append(a[p]+b[q]+c[r])

v.sort(reverse = 1)
for i in range(k):
  print(v[i])

Submission status: Accepted

Solution code:
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def SI(): return input()
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')

X, Y, Z, K = LI()
A_li = LI()
B_li = LI()
C_li = LI()

A_li = sorted(A_li, reverse=True) + [-INF]
B_li = sorted(B_li, reverse=True) + [-INF]
C_li = sorted(C_li, reverse=True) + [-INF]

from functools import lru_cache

@lru_cache(maxsize=None)
def calc_result(a,b,c):
    return (A_li[a]+B_li[b]+C_li[c])


search_list = [(0,0,0)]
calc_list = [calc_result(0,0,0)]
for i in range(K):
    maximum = max(calc_list)
    print(maximum)
    index = calc_list.index(maximum)
    a, b, c = search_list[index]
    search_list.pop(index)
    calc_list.pop(index)
    if (a+1, b, c) not in search_list:
        search_list.append((a+1,b,c))
        calc_list.append(calc_result(a+1,b,c))
    if (a, b+1, c) not in search_list:
        search_list.append((a,b+1,c))
        calc_list.append(calc_result(a,b+1,c))
    if (a, b, c+1) not in search_list:
        search_list.append((a,b,c+1))
        calc_list.append(calc_result(a,b,c+1))

Submission status: Accepted

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

a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

abc = []

for i in range (x):
      for j in range(y):
            for(k) in range(z):
                  if (i+1)*(j+1)*(k+1) <= K:
                        abc.append(a[i]+b[j]+c[k])
                  else:
                        break

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

Submission status: Accepted

Solution code:
X,Y,Z,K=map(int,input().split())
def INPUT():
    ans=list(map(int,input().split()))
    ans.sort(reverse=True)
    return ans
A=INPUT()
B=INPUT()
C=INPUT()
ans=[]
for i in range(1,1+X):
    for j in range(1,1+Y):
        if i*j-1>=K:
            break
        for k in range(1,1+Z):
            if i*j*k-1>=K:
                break
            ans+=[A[i-1]+B[j-1]+C[k-1]]
ans.sort(reverse=True)
for i in range(K):
    print(ans[i])

Submission status: Accepted

Solution code:
from queue import PriorityQueue

def sub():
    lines = [input() for _ in range(4)]
    X, Y, Z, K = [int(e) for e in lines[0].split(" ")]
    As = [int(e) for e in lines[1].split(" ")]
    Bs = [int(e) for e in lines[2].split(" ")]
    Cs = [int(e) for e in lines[3].split(" ")]

    As = sorted(As, reverse=True)
    Bs = sorted(Bs, reverse=True)
    Cs = sorted(Cs, reverse=True)
    nb_A = len(As)
    nb_B = len(Bs)
    nb_C = len(Cs)
    
    memSet = set()
    pq = PriorityQueue()
    pq.put_nowait(( -(As[0]+Bs[0]+Cs[0]), (0,0,0)))
    memSet.add((0,0,0))
    
    for i in range(K):
        v, q = pq.get_nowait()
        print(-v)

        a, b, c = q
        if a+1 < nb_A:
            qTmp = (a+1, b, c)
            if qTmp not in memSet:
                memSet.add(qTmp)
                item = (-(As[qTmp[0]] + Bs[qTmp[1]] + Cs[qTmp[2]]), qTmp)
                pq.put_nowait(item)
        if b+1 < nb_B:
            qTmp = (a, b+1, c)
            if qTmp not in memSet:
                memSet.add(qTmp)
                item = (-(As[qTmp[0]] + Bs[qTmp[1]] + Cs[qTmp[2]]), qTmp)
                pq.put_nowait(item)
        if c+1 < nb_C:
            qTmp = (a, b, c+1)
            if qTmp not in memSet:
                memSet.add(qTmp)
                item = (-(As[qTmp[0]] + Bs[qTmp[1]] + Cs[qTmp[2]]), qTmp)
                pq.put_nowait(item)

sub()

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)

bi_comb = []
for i in range(min(X, K)):
    for j in range(min(Y, K)):
        if (i+1)*(j+1) <= K:
            bi_comb += [A[i]+B[j]]
bi_comb.sort(reverse = True)

tri_comb = []
for k in range(min(len(bi_comb), K)):
    for l in range(min(Z, K)):
        if (k+1)*(l+1) <= K:
            tri_comb += [bi_comb[k]+C[l]]
tri_comb.sort(reverse = True)

print (*tri_comb[:K], sep='\n')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    import heapq
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    h = []
    heapq.heappush(h, [-(A[0]+B[0]+C[0]), 0, 0, 0])
    ans = []
    used = set()
    used.add((0, 0, 0))
    for _ in range(K):
        e, i, j, k = heapq.heappop(h)
        ans.append(-e)
        if i+1 < X and not (i+1, j, k) in used:
            used.add((i+1, j, k))
            heapq.heappush(h, [-(A[i+1]+B[j]+C[k]), i+1, j, k])
        if j+1 < Y and not (i, j+1, k) in used:
            used.add((i, j+1, k))
            heapq.heappush(h, [-(A[i]+B[j+1]+C[k]), i, j+1, k])
        if k+1 < Z and not (i, j, k+1) in used:
            used.add((i, j, k+1))
            heapq.heappush(h, [-(A[i]+B[j]+C[k+1]), i, j, k+1])
    for elem in ans:
        print(elem)





#def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
#    AB = []
#    for a in A:
#        for b in B:
#            AB.append(a+b)
#    AB.sort(reverse=True)
#    C.sort(reverse=True)
#    AB = AB[:K]
#    C = C[:K]
#    ABC = []
#    for ab in AB:
#        for c in C:
#            ABC.append(ab+c)
#    ABC.sort(reverse=True)
#    for i in range(K):
#        print(ABC[i])
#    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    Y = int(next(tokens))  # type: int
    Z = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(X)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(Y)]  # type: "List[int]"
    C = [int(next(tokens)) for _ in range(Z)]  # type: "List[int]"
    solve(X, Y, Z, K, A, B, C)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    AB = []
    for a in A:
        for b in B:
            AB.append(a+b)
    AB.sort(reverse=True)
    C.sort(reverse=True)
    AB = AB[:K]
    C = C[:K]
    ABC = []
    for ab in AB:
        for c in C:
            ABC.append(ab+c)
    ABC.sort(reverse=True)
    for i in range(K):
        print(ABC[i])
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    Y = int(next(tokens))  # type: int
    Z = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(X)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(Y)]  # type: "List[int]"
    C = [int(next(tokens)) for _ in range(Z)]  # type: "List[int]"
    solve(X, Y, Z, K, A, B, C)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import*
from math import*
from collections import*
from heapq import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = 10**18
mod = 10**9+7
from functools import reduce
import sys
sys.setrecursionlimit(10**7)

#A+B+Cの美味しさがK番目以内ということはA+Bの美味しさがK番目以内である必要がある
#まずA+Bの美味しさをすべて求めて上からKを取り最後にCを加えたものをすべて考える

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(reverse=True)
ABK = AB[:min(K,X*Y)]
D = []
for i in range(min(K,X*Y)):
    for j in range(Z):
        D.append(ABK[i]+C[j])
D.sort(reverse=True)
for i in range(K):
    print(D[i])