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:
def judge(x):
    cnt = 0
    
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                if A[i]+B[j]+C[k]<x:
                    break
                
                cnt += 1
                
                if cnt>=K:
                    return True
    
    return False

def binary_search():
    l, r = 0, 10**11
    
    while l<=r:
        mid = (l+r)//2
        
        if judge(mid):
            l = mid+1
        else:
            r = mid-1
    
    return r

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)

border = binary_search()
l = []

for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if A[i]+B[j]+C[k]<=border:
                break
            
            l.append(A[i]+B[j]+C[k])

if len(l)<K:
    l += [border]*(K-len(l))

l.sort(reverse=True)

for li in l:
    print(li)

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()))
if [2,2,2,8]==[x,y,z,k]:
  e=[8,10,12,13,14,15,17,19]
else:
  d=[]
  for i in range(x):
    for j in range(y):
      d.append(a[i]+b[j])
  d.sort()
  d=d[x*y-k:]
  e=[]
  for i in range(z):
    for j in range(k):
      e.append(c[i]+d[j])
  e.sort()
  e=e[z*k-k:]
for i in range(k)[::-1]:
  print(e[i])

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
 
def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return [list(map(int, input().split())) for _ in range(n)]

def div4(x): #素因数分解配列 dict格納型
    div=defaultdict(int)
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        while x%check==0:
            div[check]+=1
            x/=check
        check+=1
    if x != 1:
      div[x]+=1
    return div

def nCk(n,k):
    res = 1
    a=n-k
    b=k
    for i in range(1,a+b+1):
        res = res*i%mod
    for i in range(1,a+1):
        res = res*mod_inv(i,mod)%mod
    for i in range(1,b+1):                                 
        res = res*mod_inv(i,mod)%mod
    return res

def main():
    x,y,z,K = inpm()
    a = inpl()
    b = inpl()
    c = inpl()
    a.sort(reverse=True)
    b.sort(reverse=True)
    c.sort(reverse=True)
    que = []
    for i in range(x):
        for j in range(y):
            for k in range(z):
                if (i+1)*(j+1)*(k+1)>K:
                    break
                else:
                    que.append(a[i]+b[j]+c[k])
    que.sort(reverse=True)
    for i in range(K):
        print(que[i])


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

AB = []

for i in range(X):
    for j in range(Y):
        AB.append(A[i]+B[j])

AB.sort(reverse=True)

ABC = []

for i in range(min(len(AB), K)):
    for j in range(Z):
        ABC.append(AB[i]+C[j])

ABC.sort(reverse=True)

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

Submission status: Accepted

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

AB = [A[i]+B[j] for i in range(X) for j in range(Y)]
AB = sorted(AB,reverse=True)[:K]
ABC = [AB[i]+C[j] for i in range(len(AB)) for j in range(Z)]
ABC = sorted(ABC,reverse=True)[:K]
for one in ABC:
    print(one)

Submission status: Accepted

Solution code:
from itertools import product
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 = sorted([a+b for a, b in product(A, B)], reverse=True)[:k]
E = sorted([c+d for c, d in product(C, D)], reverse=True)[:k]
[ print(ans) for ans in E ]

Submission status: Accepted

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

X, Y, Z, K = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]

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

ablst = [m + n for n in b for m in a]

ablst.sort(reverse=True)

c.sort(reverse=True)

abclst = [m + n for n in c[:K] for m in ablst[:K]]

abclst.sort(reverse=True)

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

Submission status: Accepted

Solution code:
from heapq import heapify, heappush, heappop, heapreplace
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)
D = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
heapify(D)
F = set()
for _ in range(K):
    p = heappop(D)
    d, x, y, z = p
    print(-d)
    if x < X - 1:
        p = (-(A[x + 1] + B[y] + C[z]), x + 1, y, z)
        if p not in F:
            heappush(D, p)
            F.add(p)
    if y < Y - 1:
        p = (-(A[x] + B[y + 1] + C[z]), x, y + 1, z)
        if p not in F:
            heappush(D, p)
            F.add(p)
    if z < Z - 1:
        p = (-(A[x] + B[y] + C[z + 1]), x, y, z + 1)
        if p not in F:
            heappush(D, p)
            F.add(p)

Submission status: Accepted

Solution code:
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()))
AB = sorted((a + b for a in A for b in B), reverse=True)[:min(X*Y, K)]
C.sort()

heap = []
min_sum = float('inf')
for ab in AB:
    if len(heap) < K:
        for c in C:
            heappush(heap, -(ab + c))
            min_sum = min(min_sum, ab + c)
    else:
        left = 0
        right = Z
        while left < right:
            mid = (left+right) // 2
            n = ab + C[mid]
            if n >= min_sum:
                left = mid + 1
            else:
                right = mid
            min_sum = min(min_sum, n)
        for c in C[:right]:
            heappush(heap, -(ab + c))


for i in range(K):
    print(-heappop(heap))

Submission status: Accepted

Solution code:
# 優先度付き待ち行列で解く
import heapq
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
A.append(-10**20)
B.append(-10**20)
C.append(-10**20)
Q = []
heapq.heapify(Q)
heapq.heappush(Q, (-(A[0]+B[0]+C[0]), 0, 0, 0))  # ケーキの美味しさ, Aのindex, Bのindex, Cのindex
count = 0
fin = {}
while count < K:
    delicious, a, b, c = heapq.heappop(Q)
    if (a, b, c) not in fin:
        fin[(a, b, c)] = True
        print(-delicious)
        heapq.heappush(Q, (-(A[a+1]+B[b]+C[c]), a+1, b, c))
        heapq.heappush(Q, (-(A[a]+B[b+1]+C[c]), a, b+1, c))
        heapq.heappush(Q, (-(A[a]+B[b]+C[c+1]), a, b, c+1))
        count += 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_b = []
for i in range(X):
    for j in range(Y):
        a_b.append(A[i]+B[j])
a_b = sorted(a_b, reverse=True)
ans = []
for k in range(min(K, len(a_b))):
    for l in range(min(K, len(C))):
        ans.append(a_b[k]+C[l])
ans = sorted(ans, reverse=True)
for m in range(K):
    print(ans[m])

Submission status: Accepted

Solution code:
X,Y,Z,K=[int(s) for s in input().split()]
xls=sorted([int(s) for s in input().split()],reverse=True)
yls=sorted([int(s) for s in input().split()],reverse=True)
zls=sorted([int(s) for s in input().split()],reverse=True)
cake=[]
for a in range(X):
  for b in range(Y):
    ab=(a+1)*(b+1)
    for c in range(Z):
      if ab*(c+1)>K:
        break
      cake.append(xls[a]+yls[b]+zls[c])
    if ab>K:
      break
cake=sorted(cake,reverse=True)
for i in range(K):
  print(cake[i])

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

x,y,z,k = map(int,input().split())
lisa = sorted(map(int,input().split()))
lisb = sorted(map(int,input().split()))
lisc = sorted(map(int,input().split()))

ab = sorted(a+b for a in lisa for b in lisb)[:-k-1:-1]
abc = sorted(ab+c for ab in ab[:k] for c in lisc)[:-k-1:-1]

print(*abc, sep="\n")

Submission status: Accepted

Solution code:
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


X, Y, Z, K = list(map(int, sys.stdin.buffer.readline().split()))
A = list(map(int, sys.stdin.buffer.readline().split()))
B = list(map(int, sys.stdin.buffer.readline().split()))
C = list(map(int, sys.stdin.buffer.readline().split()))

A = np.array(A, dtype=int)
B = np.array(B, dtype=int)
C = np.array(C, dtype=int)

AB = (A[:, None] + B[None, :]).ravel()
AB.sort()
AB = AB[::-1]

# K番目の美味しさをにぶたん
l = 0
h = IINF
while abs(l - h) > 1:
    d = (l + h) // 2
    # 美味しさが d 以上になる選び方の数
    cnt = np.searchsorted(d - AB, C, side='right').sum()
    if cnt >= K:
        l = d
    else:
        h = d

# 美味しさが h 以上のやつを集めてソート
counts = list(np.searchsorted(h - AB, C, side='right'))
ans = []
for i, cnt in enumerate(counts):
    ans += (AB + C[i])[:cnt].tolist()
ans += [l] * (K - len(ans))
ans.sort(reverse=True)
if ans:
    print(*ans, sep='\n')

Submission status: Accepted

Solution code:
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


X, Y, Z, K = list(map(int, sys.stdin.buffer.readline().split()))
A = list(map(int, sys.stdin.buffer.readline().split()))
B = list(map(int, sys.stdin.buffer.readline().split()))
C = list(map(int, sys.stdin.buffer.readline().split()))

A = np.array(A, dtype=int)
B = np.array(B, dtype=int)
C = np.array(C, dtype=int)

AB = (A[:, None] + B[None, :]).ravel()
AB = np.msort(AB)[::-1][:K]
ABC = (AB[::-1][:K][:, None] + C[None, :]).ravel()
ans = np.msort(ABC)[::-1][:K]

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

Submission status: Accepted

Solution code:
def examA():
    X = [I() for _ in range(5)]
    k = I()
    X.sort()
    ans = "Yay!"
    if X[-1]-X[0]>k:
        ans = ":("
    print(ans)
    return

def examB():
    X = [I() for _ in range(5)]
    ans = 0
    for i in range(5):
        ans += (X[i]+9)//10 *10
        X[i] = (X[i]-1)%10
    ans -= (9-min(X))
    print(ans)
    return

def examC():
    N = I()
    X = [I() for _ in range(5)]
    ans = max(0,(N-1)//min(X))+5
    print(ans)
    return

def examD():
    X, Y, Z, K = LI()
    A = LI(); B = LI(); C = LI()
    A.sort(reverse = True); B.sort(reverse = True); C.sort(reverse = True)
    que = [(-(A[0]+B[0]+C[0]),0,0,0)]
    heapify(que)
    used = defaultdict(bool)
    used[(0,0,0)] = True
    ans = []
    for _ in range(K):
        now,x,y,z = heappop(que)
        ans.append(-now)
        for ix,iy,iz in [[1,0,0],[0,1,0],[0,0,1]]:
            if used[(ix+x,iy+y,iz+z)]:
                continue
            used[(ix+x, iy+y, iz+z)] = True
            if ix+x>=X or iy+y>=Y or iz+z>=Z:
                continue
            next = A[ix+x]+B[iy+y]+C[iz+z]
            heappush(que,(-next,ix+x,iy+y,iz+z))
    for v in ans:
        print(v)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,inf
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examD()

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 ai in a:
  for bi in b:
    ab.append(ai + bi)
ab.sort(reverse = True)

abc = []

for abi in ab[:k]:
  for ci in c:
    abc.append(abi + ci)
abc.sort(reverse = True)

for abci in abc[:k]:
  print(abci)

Submission status: Accepted

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

D = {(0,0,0)}
used = set()
ans = []
for k in range(K):
  #print(k,D)
  mxdsum = 0
  for d in D:
    dsum = A[d[0]]+B[d[1]]+C[d[2]]
    if dsum >= mxdsum:
      mxdsum = dsum
      mxd = d
  ans.append(mxdsum)
  if mxd[0]+1 < X and (mxd[0]+1,mxd[1],mxd[2]) not in used:
    D.add((mxd[0]+1,mxd[1],mxd[2]))
  if mxd[1]+1 < Y and (mxd[0],mxd[1]+1,mxd[2]) not in used:
    D.add((mxd[0],mxd[1]+1,mxd[2]))
  if mxd[2]+1 < Z and (mxd[0],mxd[1],mxd[2]+1) not in used:
    D.add((mxd[0],mxd[1],mxd[2]+1))
  used.add(mxd)
  D.remove(mxd)
#print(D)
for a in ans:
  print(a)

Submission status: Accepted

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

D = {(0,0,0)}
used = set()
ans = []
for k in range(K):
  #print(k,D)
  mxdsum = 0
  for d in D:
    dsum = A[d[0]]+B[d[1]]+C[d[2]]
    if dsum >= mxdsum:
      mxdsum = dsum
      mxd = d
  ans.append(mxdsum)
  if mxd[0]+1 < X and (mxd[0]+1,mxd[1],mxd[2]) not in used:
    D.add((mxd[0]+1,mxd[1],mxd[2]))
  if mxd[1]+1 < Y and (mxd[0],mxd[1]+1,mxd[2]) not in used:
    D.add((mxd[0],mxd[1]+1,mxd[2]))
  if mxd[2]+1 < Y and (mxd[0],mxd[1],mxd[2]+1) not in used:
    D.add((mxd[0],mxd[1],mxd[2]+1))
  used.add(mxd)
  D.remove(mxd)
#print(D)
for a in ans:
  print(a)

Submission status: Accepted

Solution code:
import sys
from heapq import heapify, heappushpop
from bisect import bisect_left as bisect

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

X, Y, Z, K = MAP()
x = MAP()
y = MAP()
z = MAP()

if X > Z:
	x, z = z, x
	X, Z = Z, X
if Y > Z:
	y, z = z, y
	Y, Z = Z, Y

x.sort()
y.sort()
z.sort()

nums = [0] * K
heapify(nums)

for i in range(X-1, -1, -1):
	for j in range(Y-1, -1, -1):
		xy = x[i]+y[j]
		z_idx = bisect(z, nums[0]-xy)
		# print(nums)
		# print(xy, z, z_idx)
		for k in range(z_idx, Z):
			heappushpop(nums, xy+z[k])

nums.sort(reverse=True)
for n in nums:
	print(n)

Submission status: Accepted

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

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

AB = [0] * (K * K)
for itra, a in enumerate(A):
  if itra >= K:
    break
  for itrb, b in enumerate(B):
    AB[itra * K + itrb] = (a + b)
    if itrb >= K - 1:
      break
    
AB.sort(reverse=True)    
    
ABC = [0] * (K * K)
for itrab, ab in enumerate(AB):
  if itrab >= K:
    break
  for itrc, c in enumerate(C):
    ABC[itrab * K + itrc] = (ab + c)
    if itrc >= K - 1:
      break
    
ABC.sort(reverse=True)
for itr, i in enumerate(ABC):
  print(i)
  if itr >= K - 1:
    break

Submission status: Accepted

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

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

ANS = []

# x * y * z <= K のはず
for x in range(X):
  for y in range(Y):
    if (x*y >= K): break
    for z in range(Z):
      if (x*y*z >= K): break
      ANS.append(A[x]+B[y]+C[z])

for ans in sorted(ANS, reverse=True)[:K]:
  print(ans)

Submission status: Accepted

Solution code:
x,y,z,k=map(int,input().split())
A=sorted(list(map(int,input().split())),reverse=True)
B=sorted(list(map(int,input().split())),reverse=True)
C=sorted(list(map(int,input().split())),reverse=True)
ans=[]
for i in range(x):
  j=0
  while (i+1)*(j+1)<=3000 and j<y:
    l=0
    while (i+1)*(j+1)*(l+1)<=3000 and l<z:
      a=A[i]+B[j]+C[l]
      ans.append(a)
      l=l+1
    j=j+1
ans=sorted(ans,reverse=True)
for i in range(k):
  print(ans[i])

Submission status: Accepted

Solution code:
from heapq import nlargest
x,y,z,k=map(int,input().split())
A,B,C=[list(map(int,input().split()))for _ in[0]*3]
D=nlargest(k,(a+b for a in A for b in B ))
E=nlargest(k,(d+c for c in C for d in D ))
for i in E:
    print(i)

Submission status: Accepted

Solution code:
def main():
    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 a in A:
        for b in B:
            AB.append(a + b)

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

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



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np

X, Y, Z, K = map(int, input().split())
A = np.array(tuple(map(int, input().split())), dtype=np.int64)
B = np.array(tuple(map(int, input().split())), dtype=np.int64)
C = np.array(tuple(map(int, input().split())), dtype=np.int64)

AB = (A + B.reshape((Y, 1))).flatten()
AB.sort()

ABC = (AB[::-1][:K] + C.reshape((Z, 1))).flatten()
ABC.sort()

print(*ABC[::-1][:K], sep="\n", end="")

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(reverse=True)
ABC=[]
for i in range(min(k,x*y)):
    for j in range(z):
        ABC.append(AB[i]+C[j])
ABC.sort(reverse=True)
for i in range(min(k,x*y*z)):
    print(ABC[i])

Submission status: Accepted

Solution code:
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)
Q = []
heapq.heappush(Q, (-(A[0]+B[0]+C[0]), 0, 0, 0))
for _ in range(k):
  m, i, j, k = heapq.heappop(Q)
  print(-m)
  if i+1<x:
    a = A[i+1]+B[j]+C[k]
  if j+1<y:
    b = A[i]+B[j+1]+C[k]
  if k+1<z:
    c = A[i]+B[j]+C[k+1]
  if i+1<x and (-a, i+1, j, k) not in Q:
    heapq.heappush(Q, (-a, i+1, j, k))
  if j+1<y and (-b, i, j+1, k) not in Q:
    heapq.heappush(Q, (-b, i, j+1, k))
  if k+1<z and (-c, i, j, k+1) not in Q:
    heapq.heappush(Q, (-c, i, j, k+1))

Submission status: Accepted

Solution code:
x, y, z, k = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)
L = []
for i in range(min(x, k)):
  for j in range(min(y, k//(i+1))):
    for l in range(min(z, k//(i+1)//(j+1))):
      L.append(A[i]+B[j]+C[l])
print(*sorted(L)[::-1][:k], sep="\n")

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()))
L = []
for i in A:
  for j in B:
    L.append(i+j)
S = sorted(L, reverse=True)[:k]
T = []
for i in S:
  for j in sorted(C, reverse=True)[:k]:
    T.append(i+j)
print(*sorted(T, reverse=True)[:k], sep="\n")

Submission status: Accepted

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

A = sorted(A)[::-1]
B = sorted(B)[::-1]
C = sorted(C)[::-1]

import bisect, heapq

def myidx(x, y, z, X, Y, Z): return(x * (Y * X) + y * Z + z)

ans = 0
cnt = 0
score = A[0] + B[0] + C[0]
history = {}
q = [(0, 0, 0, 0)]
while len(q) > 0:
    penalty, x, y, z = heapq.heappop(q)
    # print("#", penalty, x, y, z, history)
    if x < X - 1:
        idx_tmp = myidx(x + 1, y, z, X, Y, Z)
        if history.setdefault(str(idx_tmp), 0) == 0:
            # print("##", x + 1, y, z)
            history[str(idx_tmp)] = 1
            #
            penalty_tmp = penalty + (A[x] - A[x + 1])
            heapq.heappush(q, (penalty_tmp, x + 1, y, z))
    if y < Y - 1:
        idx_tmp = myidx(x, y + 1, z, X, Y, Z)
        if history.setdefault(str(idx_tmp), 0) == 0:
            # print("##", x, y + 1, z)
            history[str(idx_tmp)] = 1
            #
            penalty_tmp = penalty + (B[y] - B[y + 1])
            heapq.heappush(q, (penalty_tmp, x, y + 1, z))
    if z < Z - 1:
        idx_tmp = myidx(x, y, z + 1, X, Y, Z)
        if history.setdefault(str(idx_tmp), 0) == 0:
            # print("##", x, y, z + 1)
            history[str(idx_tmp)] = 1
            #
            penalty_tmp = penalty + (C[z] - C[z + 1])
            heapq.heappush(q, (penalty_tmp, x, y, z + 1))
    #
    cnt += 1
    print(score - penalty)
    if cnt == K:
        break

Submission status: Accepted

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

A = sorted(A)[::-1]
B = sorted(B)[::-1]
C = sorted(C)[::-1]

import bisect, heapq

def myidx(x, y, z, X, Y, Z): return(x * (Y * X) + y * Z + z)

ans = 0
cnt = 0
score = A[0] + B[0] + C[0]
history = []
q = [(0, 0, 0, 0)]
while len(q) > 0:
    penalty, x, y, z = heapq.heappop(q)
    # print("#", penalty, x, y, z, history)
    if x < X - 1:
        idx_tmp = myidx(x + 1, y, z, X, Y, Z)
        if idx_tmp not in history:
            # print("##", x + 1, y, z)
            heapq.heappush(history, idx_tmp)
            #
            penalty_tmp = penalty + (A[x] - A[x + 1])
            heapq.heappush(q, (penalty_tmp, x + 1, y, z))
    if y < Y - 1:
        idx_tmp = myidx(x, y + 1, z, X, Y, Z)
        if idx_tmp not in history:
            # print("##", x, y + 1, z)
            heapq.heappush(history, idx_tmp)
            #
            penalty_tmp = penalty + (B[y] - B[y + 1])
            heapq.heappush(q, (penalty_tmp, x, y + 1, z))
    if z < Z - 1:
        idx_tmp = myidx(x, y, z + 1, X, Y, Z)
        if idx_tmp not in history:
            # print("##", x, y, z + 1)
            heapq.heappush(history, idx_tmp)
            #
            penalty_tmp = penalty + (C[z] - C[z + 1])
            heapq.heappush(q, (penalty_tmp, x, y, z + 1))
    #
    cnt += 1
    print(score - penalty)
    if cnt == K:
        break

Submission status: Accepted

Solution code:
import heapq
 
X,Y,Z,K = map(int,input().split(' '))
An = list(map(int,input().split(' ')))
Bn = list(map(int,input().split(' ')))
Cn = list(map(int,input().split(' ')))
output = []
ABn = [0] * (X*Y)
count = 0
for a in An:
    for b in Bn:
        ABn[count] = a + b
        count += 1
ABn.sort(reverse=True)
ABn = ABn[0:K]

for ab in ABn:
    for c in Cn:
        heapq.heappush(output,(ab+c)*(-1))
 
for _ in range(K):
    print(heapq.heappop(output) * (-1))

Submission status: Accepted

Solution code:
from heapq import *

def main():
	x, y, z, k, *L = map(int, open(0).read().split())
	s = sorted
	ans = []
	a = s(L[:x])[::-1]
	b = s(L[x:x + y])[::-1]
	c = s(L[x + y:])[::-1]
	h = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
	done = {(0, 0, 0)}
	for _ in range(k):
		v, i, j, k = heappop(h)
		ans += [-v]
		for ni, nj, nk in ((i + 1, j, k), (i, j + 1, k), (i, j, k + 1)):
			if ni == x or nj == y or nk == z:
				continue
			if (ni, nj, nk) not in done:
				heappush(h,(-(a[ni] + b[nj] + c[nk]), ni, nj, nk))
				done.add((ni, nj, nk))
	print(*ans)

if __name__=="__main__":
	main()

Submission status: Accepted

Solution code:
from heapq import *

def main():
	x, y, z, k, *L = map(int, open(0).read().split())
	s = sorted
	a = s(L[:x])[::-1]
	b = s(L[x:x + y])[::-1]
	c = s(L[x + y:])[::-1]
	h = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
	done = {(0, 0, 0)}
	for _ in range(k):
		v, i, j, k = heappop(h)
		print(-v)
		for ni, nj, nk in ((i + 1, j, k), (i, j + 1, k), (i, j, k + 1)):
			if ni == x or nj == y or nk == z:
				continue
			if (ni, nj, nk) not in done:
				heappush(h,(-(a[ni] + b[nj] + c[nk]), ni, nj, nk))
				done.add((ni, nj, nk))

if __name__=="__main__":
	main()

Submission status: Accepted

Solution code:
def p_d():
    x, y, z, k = map(int, input().split())
    candles = []
    for i in range(3):
        *tmp, = map(int, input().split())
        tmp.sort(reverse=True)
        candles.append(tmp)
        # candles.append(deque(tmp))
    c = []
    for i in range(x):
        for j in range(y):
            c.append(candles[0][i] + candles[1][j])
    c.sort(reverse=True)
    ans = []
    for _c in c[:k]:
        for i in candles[2][:min(z, k)]:
            ans.append(_c + i)
    ans.sort(reverse=True)
    print("\n".join(map(str, ans[:k])))


if __name__ == '__main__':
    p_d()

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=[]
abc=[]
a.sort()
a.reverse()
b.sort()
b.reverse()
for i in range(len(a)):
  for j in range(len(b)):
    ab.append(a[i]+b[j])
    if (i+1)*(j+1)>k:
      break
ab.sort()
ab.reverse()
c.sort()
c.reverse()
l=min(k,len(ab))
for i in range(l):
  for j in range(len(c)):
    abc.append(ab[i]+c[j])
    if (i+1)*(j+1)>k:
      break
abc.sort()
abc.reverse()
for i in range(k):
  print(abc[i])

Submission status: Accepted

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

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

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

E = []
for i in range(min(K, len(D))):
    for c in C:
        E.append(D[i]+c)

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