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:
# coding: utf-8
# hello worldと表示する
#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7


x,y,z,k=MI()
set_=set()
A=sorted(LI(),reverse=True)
B=sorted(LI(),reverse=True)
C=sorted(LI(),reverse=True)
Q=[[(A[0]+B[0]+C[0])*(-1),0,0,0]]
heapify(Q)
set_.add((0,0,0))
i=0
ans=[]
xl,yl,zl=0,0,0
al=[]
al=[[0,0,0]]
while i<k:
    a,u,v,w=heappop(Q)
    ans.append([-a,u,v,w])
    print(-a)
    i+=1
    if u+1<x and (u+1,v,w) not in set_:
        heappush(Q,[-A[u+1]-B[v]-C[w],u+1,v,w])
        set_.add((u+1,v,w))
    if v+1<y and (u,v+1,w) not in set_:
        heappush(Q,[-A[u]-B[v+1]-C[w],u,v+1,w])
        set_.add((u,v+1,w))
    if w+1<z and (u,v,w+1) not in set_:
        heappush(Q,[-A[u]-B[v]-C[w+1],u,v,w+1])
        set_.add((u,v,w+1))

Submission status: Accepted

Solution code:
# coding: utf-8
# hello worldと表示する
#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

def main():
    x,y,z,k=MI()
    set_=set()
    A=sorted(LI(),reverse=True)
    B=sorted(LI(),reverse=True)
    C=sorted(LI(),reverse=True)
    Q=[[(A[0]+B[0]+C[0])*(-1),0,0,0]]
    heapify(Q)
    set_.add((0,0,0))
    #print(Q)
    i=0
    ans=[]
    #print(heappop(Q))
    #al=[[[-1]*x for i in range(y)] for j in range(z)]
    #al[0][0][0]=0
    xl,yl,zl=0,0,0
    al=[]
    al=[[0,0,0]]
    while i<k:
        #print(Q)
        a,u,v,w=heappop(Q)
        ans.append([-a,u,v,w])
        print(-a)
        i+=1
        if u+1<x and (u+1,v,w) not in set_:
            heappush(Q,[-A[u+1]-B[v]-C[w],u+1,v,w])
            set_.add((u+1,v,w))
            #al.append([u+1,v,w])
            
            #al[u+1][v][w]=0
        if v+1<y and (u,v+1,w) not in set_:
            heappush(Q,[-A[u]-B[v+1]-C[w],u,v+1,w])
            set_.add((u,v+1,w))
            #al.append([u,v+1,w])
            #al[u][v+1][w]=0
        if w+1<z and (u,v,w+1) not in set_:
            heappush(Q,[-A[u]-B[v]-C[w+1],u,v,w+1])
            set_.add((u,v,w+1))
            #al.append([u,v,w+1])
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))



def main():
    X,Y,Z,K=mi()
    A,B,C=list(mi()), list(mi()), list(mi())
    
    W = []
    for i in range(X):
        for j in range(Y):
            W.append(A[i]+B[j])

    W.sort(reverse=True)

    ans = []
    for w in W[:K]:
        for c in C:
            ans.append(w+c)
    
    ans.sort(reverse=True)
    print(*ans[:K],sep='\n')


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import heapq
X,Y,Z,K = map(int,input().split())
K = min(K,X*Y*Z)
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)
set_ = set()
hq = [(-(A[0]+B[0]+C[0]),0,0,0)]
heapq.heapify(hq)
set_.add((0,0,0))
while K:
    val,i,j,k = heapq.heappop(hq)
    print(-val)
    K-=1
    if i+1<len(A) and (i+1,j,k) not in set_: 
        heapq.heappush(hq,(-(A[i+1]+B[j]+C[k]),i+1,j,k))
        set_.add((i+1,j,k))
    if j+1<len(B) and (i,j+1,k) not in set_: 
        heapq.heappush(hq,(-(A[i]+B[j+1]+C[k]),i,j+1,k))
        set_.add((i,j+1,k))
    if k+1<len(C) and (i,j,k+1) not in set_: 
        heapq.heappush(hq,(-(A[i]+B[j]+C[k+1]),i,j,k+1))
        set_.add((i,j,k+1))

Submission status: Accepted

Solution code:
X,Y,Z,K = map(int,input().split())
K = min(K,X*Y*Z)
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)
sum2 = []
for a in A:
    for b in B:
        sum2.append(a+b)
sum2.sort(reverse = True)
sum2 = sum2[:K]
idx = 0
sum3 = []
for c in C:
    for s in sum2:
        sum3.append(c+s)
sum3.sort(reverse=True)
for i in range(K):
    print(sum3[i])

Submission status: Accepted

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
from heapq import heappop, heappush
import sys
def main(X, Y, Z, K, A, B, C):
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    used = set()
    q = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
    used.add((0, 0, 0))
    for i in range(K):
        v, ai, bi, ci = heappop(q)
        print(-v)
        for nai, nbi, nci in [(ai + 1, bi, ci), (ai, bi + 1, ci), (ai, bi, ci + 1)]:
            if nai >= X or nbi >= Y or nci >= Z: continue
            if (nai, nbi, nci) in used: continue
            used.add((nai, nbi, nci))
            heappush(q, (-(A[nai] + B[nbi] + C[nci]), nai, nbi, nci))

if __name__ == '__main__':
    input = sys.stdin.readline
    X, Y, Z, K = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())
    *C, = map(int, input().split())
    main(X, Y, Z, K, A, B, C)

Submission status: Accepted

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
from itertools import product
import sys
def main(X, Y, Z, K, A, B, C):
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    ans = []
    for x in range(1, X + 1):
        if x > K: break
        for y in range(1, Y + 1):
            if x * y > K: break
            for z in range(1, Z + 1):
                if x * y * z > K: break
                ans.append(A[x - 1] + B[y - 1] + C[z - 1])
    ans.sort(reverse=True)
    for i in range(K):
        print(ans[i])

if __name__ == '__main__':
    input = sys.stdin.readline
    X, Y, Z, K = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())
    *C, = map(int, input().split())
    main(X, Y, Z, K, A, B, C)

Submission status: Accepted

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
from itertools import product
import sys
def main(X, Y, Z, K, A, B, C):
    AB = sorted([a + b for a, b in product(A, B)], reverse=True)
    ABC = sorted([ab + c for ab, c in product(AB[:K], C)], reverse=True)
    for k in range(K):
        print(ABC[k])

if __name__ == '__main__':
    input = sys.stdin.readline
    X, Y, Z, K = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())
    *C, = map(int, input().split())
    main(X, Y, Z, K, A, B, C)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# import bisect
# from collections import Counter, deque, OrderedDict, defaultdict
# from copy import copy, deepcopy # pythonのみ．copyは1次元，deepcopyは多次元．
# from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
# from functools import reduce
from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import statistics # Pythonのみ
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    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)

    h=[]
    heapify(h)
    ac,bc,cc=0,0,0

    heappush(h, (-(A[ac]+B[bc]+C[cc]), ac,bc,cc))
    checked=set()

            
    checked.add((-(A[ac]+B[bc]+C[cc]),ac,bc,cc))


    for _ in range(k):
        (p,ac,bc,cc)=heappop(h)
        print(-p)
        if ac+1<x:
            if (-(A[ac+1]+B[bc]+C[cc]), ac+1,bc,cc) not in checked:
                heappush(h, (-(A[ac+1]+B[bc]+C[cc]), ac+1,bc,cc))
                checked.add((-(A[ac+1]+B[bc]+C[cc]), ac+1,bc,cc))

        if bc+1<y:  
                        
            if (-(A[ac]+B[bc+1]+C[cc]), ac,bc+1,cc) not in checked:
                heappush(h, (-(A[ac]+B[bc+1]+C[cc]), ac,bc+1,cc))
                checked.add((-(A[ac]+B[bc+1]+C[cc]), ac,bc+1,cc))
        if cc+1<z:
            if (-(A[ac]+B[bc]+C[cc+1]), ac,bc,cc+1) not in checked:
                heappush(h, (-(A[ac]+B[bc]+C[cc+1]), ac,bc,cc+1))
                checked.add((-(A[ac]+B[bc]+C[cc+1]), ac,bc,cc+1))




    


    


    
resolve()

Submission status: Accepted

Solution code:
import collections
import heapq

x,y,z,t = map(int, raw_input().split(' '))
mat = [map(int,raw_input().split(' ')) for _ in range(3)]
for ii in range(len(mat)): mat[ii].sort(key = lambda x:-x)

seen,heap = set([(0,0,0)]),[(-mat[0][0]-mat[1][0]-mat[2][0], 0,0,0)]


while(heap and t):
	u,i,j,k = heapq.heappop(heap)
	print -u
	t-=1
	for nei in [(i+1,j,k), (i,j+1,k), (i,j,k+1)]:
		if nei not in seen and all([0<=nei[ii]<len(mat[ii]) for ii in range(3)]):
			seen.add(nei)
			vv = -sum([mat[ii][vvv] for ii,vvv in enumerate(nei)])
			heapq.heappush(heap,(vv,nei[0],nei[1],nei[2]))

Submission status: Accepted

Solution code:
import collections
import heapq

x,y,z,t = map(int, raw_input().split(' '))
xs = map(int,raw_input().split(' '))
xs.sort(key = lambda x:-x)
ys = map(int,raw_input().split(' '))
ys.sort(key = lambda x:-x)
zs = map(int,raw_input().split(' '))
zs.sort(key = lambda x:-x)
mat = [xs,ys,zs]
heap = [(-mat[0][0]-mat[1][0]-mat[2][0], 0,0,0)]
seen = set([(0,0,0)])
res = []
while(heap and t):
	u,i,j,k = heapq.heappop(heap)
	print -u
	res.append(-u)
	t-=1
	for nei in [(i+1,j,k), (i,j+1,k), (i,j,k+1)]:
		if nei not in seen and all([0<=nei[ii]<len(mat[ii]) for ii in range(3)]):
			seen.add(nei)
			vv = mat[0][nei[0]] + mat[1][nei[1]] + mat[2][nei[2]]
			vv *= -1
			heapq.heappush(heap,(vv,nei[0],nei[1],nei[2]))

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 x in range(X):
    for y in range(Y):
        for z in range(Z):
            if (x+1)*(y+1)*(z+1) <= K:
                ABC.append(A[x]+B[y]+C[z])
            else:
                break

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

Submission status: Accepted

Solution code:
from heapq import *

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

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

# Heapq的なのでぶっこみPopしてればよい
# そのかわり全パターン最初からぶち込むのはむりなので、popしたindexをもとに候補となる対象をpushする必要がある
# ただし容易に重複投入しうるので、投入済みパターンを覚えておくのかな…。historyに含まれてたらSKIP。
Q = []
his = set()

def pushq(p,q,r):
  if (p,q,r) in his:
    return
  if p >= X or q >= Y or r >= Z:
    return
  his.add((p,q,r))
  heappush(Q, ((-1) * (A[p] + B[q] + C[r]), (p, q, r)))
  return

pushq(0,0,0)
for _ in range(K):
  V = heappop(Q)
  print((-1)*V[0])
  p,q,r = V[1]
  pushq(p+1,q,r)
  pushq(p,q+1,r)
  pushq(p,q,r+1)

Submission status: Accepted

Solution code:
x, y, z, k = map(int, input().split())
a = map(int, input().split())
b = list(map(int, input().split()))
c = map(int, input().split())
d = sorted((ai + bi for ai in a for bi in b), reverse=True)[:k]
e = sorted((ci + di for ci in c for di in d), reverse=True)[:k]
print(*e)

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.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
q = []
s = set()
heapq.heappush(q,(-a[0]-b[0]-c[0],0,0,0))

for i in range(k):
    ans,ai,bi,ci = heapq.heappop(q)
    print(-ans)
    if ai < x-1 and ((ai+1, bi, ci) not in s):
        heapq.heappush(q, (-a[ai+1]-b[bi]-c[ci],ai+1,bi,ci))
        s.add((ai+1, bi, ci))
    if bi < y-1 and ((ai, bi+1, ci) not in s):
        heapq.heappush(q, (-a[ai]-b[bi+1]-c[ci],ai,bi+1,ci))
        s.add((ai, bi+1, ci))
    if ci < z-1 and ((ai, bi, ci+1) not in s):
        heapq.heappush(q, (-a[ai]-b[bi]-c[ci+1],ai,bi,ci+1))
        s.add((ai, bi, ci+1))

Submission status: Accepted

Solution code:
import heapq
x,y,z,k= map(int,input().split())
alist = sorted(list(map(int,input().split())),reverse=True)
blist = sorted(list(map(int,input().split())),reverse=True)
clist = sorted(list(map(int,input().split())),reverse=True)
l = [(-(alist[0]+blist[0]+clist[0]),0,0,0)]
heapq.heapify(l)
count=0
ai=bi=ci=0
selected = set()
selected.add((0,0,0))
while len(l)!=0:
    temp = heapq.heappop(l)
    (tempa,tempb,tempc)=temp[1:]
    print(-temp[0])
    count += 1
    if count==k:break
    if tempa+1<=x-1:
        if not (tempa+1,tempb,tempc) in selected:
            heapq.heappush(l,(-(alist[tempa+1]+blist[tempb]+clist[tempc]),tempa+1,tempb,tempc))
            selected.add((tempa+1,tempb,tempc))    
    if tempb+1<=y-1:
        if not (tempa,tempb+1,tempc) in selected:
            heapq.heappush(l,(-(alist[tempa]+blist[tempb+1]+clist[tempc]),tempa,tempb+1,tempc))
            selected.add((tempa,tempb+1,tempc))
    if tempc+1<=z-1:
        if not (tempa,tempb,tempc+1) in selected:
            heapq.heappush(l,(-(alist[tempa]+blist[tempb]+clist[tempc+1]),tempa,tempb,tempc+1))
            selected.add((tempa,tempb,tempc+1))

Submission status: Accepted

Solution code:
import sys
import heapq
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readline_s = sys.stdin.readline
readlines = sys.stdin.buffer.readlines

INF = 10 ** 10 + 7


def main():
    X, Y, Z, K = map(int, readline().split())
    A = sorted(list(map(int, readline().split())), reverse=True)
    B = sorted(list(map(int, readline().split())), reverse=True)
    C = sorted(list(map(int, readline().split())), reverse=True)

    ans = solve3(X, Y, Z, K, A, B, C)
    print('\n'.join(map(str, ans)))


def solve3(X, Y, Z, K, A, B, C):

    init_v = -(A[0] + B[0] + C[0])
    q = [(init_v, 0, 0, 0)]
    heapq.heapify(q)
    search = set()
    ans = []

    for k in range(K):
        v, a, b, c = heapq.heappop(q)
        ans.append(-v)
        if a + 1 < X and not ((a + 1, b, c) in search):
            nv = A[a + 1] + B[b] + C[c]
            heapq.heappush(q, (-nv, a + 1, b, c))
            search |= {(a + 1, b, c)}
        if b + 1 < Y and not ((a, b + 1, c) in search):
            nv = A[a] + B[b + 1] + C[c]
            heapq.heappush(q, (-nv, a, b + 1, c))
            search |= {(a, b + 1, c)}
        if c + 1 < Z and not ((a, b, c + 1) in search):
            nv = A[a] + B[b] + C[c + 1]
            heapq.heappush(q, (-nv, a, b, c + 1))
            search |= {(a, b, c + 1)}

    return ans


def solve2(X, Y, Z, K, A, B, C):

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

    return sorted(ABC, reverse=True)[:K]


def solve1(X, Y, Z, K, A, B, C):

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

    AB.sort(reverse=True)
    AB = AB[:min(X * Y, K)]

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

    ABC.sort(reverse=True)

    return ABC[:K]


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readline_s = sys.stdin.readline
readlines = sys.stdin.buffer.readlines

INF = 10 ** 10 + 7


def main():
    X, Y, Z, K = map(int, readline().split())
    A = sorted(list(map(int, readline().split())), reverse=True)
    B = sorted(list(map(int, readline().split())), reverse=True)
    C = sorted(list(map(int, readline().split())), reverse=True)

    ans = solve2(X, Y, Z, K, A, B, C)
    print('\n'.join(map(str, ans)))


def solve2(X, Y, Z, K, A, B, C):

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

    return sorted(ABC, reverse=True)[:K]


def solve1(X, Y, Z, K, A, B, C):

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

    AB.sort(reverse=True)
    AB = AB[:min(X * Y, K)]

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

    ABC.sort(reverse=True)

    return ABC[:K]


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from heapq import heappop, heappush, heapify

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)

heap = list()
used = set()
res = list()

heappush(heap, (-(A[0] + B[0] + C[0]), 0, 0, 0))
used.add((0, 0, 0))

for _ in range(K):
    v, i, j, k = heappop(heap)
    res.append(-v)
    if i + 1 < X and (i + 1, j, k) not in used:
        used.add((i + 1, j, k))
        heappush(heap, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
    if j + 1 < Y and (i, j + 1, k) not in used:
        used.add((i, j + 1, k))
        heappush(heap, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
    if k + 1 < Z and (i, j, k + 1) not in used:
        used.add((i, j, k + 1))
        heappush(heap, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
        
print('\n'.join(map(str, res)))

Submission status: Accepted

Solution code:
import sys

def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def S(): return sys.stdin.readline().rstrip()


X,Y,Z,K = map(int,S().split())
A = LI()
B = LI()
C = LI()

A = [0] + sorted(A,reverse = True)
B = [0] + sorted(B,reverse = True)
C = [0] + sorted(C,reverse = True)

ANS = []

# i+j+k <= K の範囲だけ考えればよい
for i in range(1,X+1):
    for j in range(1,min(K//i+1,Y)+1):
        for k in range(1,min(K//(i*j)+1,Z)+1):
            ANS.append(A[i]+B[j]+C[k])

ANS = sorted(ANS,reverse=True)

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

Submission status: Accepted

Solution code:
def resolve():
    X, Y, Z, K = list(map(int, input().split()))
    A = sorted(list(map(int, input().split())), reverse=True)
    B = sorted(list(map(int, input().split())), reverse=True)
    C = sorted(list(map(int, input().split())), reverse=True)
    
    # 「美味しさの和がp以上である組み合わせがK個以上存在する」ならtrue
    def isOk(p):
        cnt = 0
        for i in range(X):
            for j in range(Y):
                for k in range(Z):
                    if A[i]+B[j]+C[k] < p: # sorted
                        break
                    cnt += 1
                    if cnt >= K:
                        return True
        return False
    
    # p以上の美味しさの和をすべて返す
    def values_higher_than(p):
        sums = []
        for i in range(X):
            for j in range(Y):
                for k in range(Z):
                    if A[i]+B[j]+C[k] < p: # sorted
                        break
                    sums.append(A[i]+B[j]+C[k])
        return sums
    
    left = -1
    right = 10**11
    while (right - left) > 1:
        mid = left + (right - left) // 2
        if isOk(mid):
            left = mid
        else:
            right = mid
    
    #print(left)
    ans = sorted(values_higher_than(left), reverse=True)
    for i in range(K):
        if i < len(ans):
            print(ans[i])
        else:
            print(left)


if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
def resolve():
    X, Y, Z, K = list(map(int, input().split()))
    A = sorted(list(map(int, input().split())), reverse=True)
    B = sorted(list(map(int, input().split())), reverse=True)
    C = sorted(list(map(int, input().split())), reverse=True)
    pushed = set("0-0-0")

    import heapq
    Q = [[-1*(A[0]+B[0]+C[0]), 0, 0, 0]]
    heapq.heapify(Q)
    for i in range(K):
        v, i, j, k = heapq.heappop(Q)
        print(-v)
        if "{}-{}-{}".format(i+1, j, k) not in pushed and i+1 < X:
            heapq.heappush(Q, [-1*(A[i+1]+B[j]+C[k]), i+1, j, k])
            pushed.add("{}-{}-{}".format(i+1, j, k))
        if "{}-{}-{}".format(i, j+1, k) not in pushed and j+1 < Y:
            heapq.heappush(Q, [-1*(A[i]+B[j+1]+C[k]), i, j+1, k])
            pushed.add("{}-{}-{}".format(i, j+1, k))
        if "{}-{}-{}".format(i, j, k+1) not in pushed and k+1 < Z:
            heapq.heappush(Q, [-1*(A[i]+B[j]+C[k+1]), i, j, k+1])
            pushed.add("{}-{}-{}".format(i, j, k+1))

if '__main__' == __name__:
    resolve()

Submission status: Accepted

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

    ABC = []
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                if ((i+1)*(j+1)*(k+1) > K):
                    break
                ABC.append(A[i]+B[j]+C[k])
    ABC = sorted(ABC, reverse=True)
    [print(ABC[i]) for i in range(K)]

if '__main__' == __name__:
    resolve()

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)

sum = []
for x in range(X):
    for y in range(Y):
        for z in range(Z):
            if (x+1)*(y+1)*(z+1) <= K:
                sum.append(A[x] + B[y] + C[z])
            else:
                break

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

Submission status: Accepted

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

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

bc = []
for bb in b:
    for cc in c:
        bc.append(bb + cc)
bc.sort()

from bisect import bisect_left, bisect_right

def C(x):
    # count "abc <= x"
    ret = 0
    for aa in a:
        idx = bisect_right(bc, x - aa)
        ret += idx
    return ret

kk = x * y * z - k + 1
lb = 0
ub = (10 ** 10) * 4
while ub - lb > 1:
    mid = (ub + lb) // 2
    if C(mid) >= kk:
        ub = mid
    else:
        lb = mid

kth = ub

l = []
for aa in a:
    idx = bisect_left(bc, kth - aa)
    for i in range(idx, len(bc)):
        l.append(aa + bc[i])

l.sort(reverse=True)
l = l[:k]
for ll in l:
    print(ll)

Submission status: Accepted

Solution code:


if __name__ == '__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()))

    bc = []
    for bb in b:
        for cc in c:
            bc.append(bb + cc)
    bc.sort()

    from bisect import bisect_left

    def C(x):
        # count "abc >= x"
        ret = 0
        for aa in a:
            idx = bisect_left(bc, x - aa)
            ret += len(bc) - idx
        return ret

    lb = 0
    ub = (10 ** 10) * 3 + 1
    while ub - lb > 1:
        mid = (ub + lb) // 2
        if C(mid) >= k:
            lb = mid
        else:
            ub = mid

    kth = lb

    l = []
    for aa in a:
        idx = bisect_left(bc, kth - aa)
        for i in range(idx, len(bc)):
            l.append(aa + bc[i])

    l.sort(reverse=True)
    l = l[:k]
    for ll in l:
        print(ll)

Submission status: Accepted

Solution code:
X,Y,Z,K= list(map(int,input().split()))
l_A=list(map(int,input().split()))
l_B=list(map(int,input().split()))
l_C=list(map(int,input().split()))
l_A.sort(reverse=True)
l_B.sort(reverse=True)
l_C.sort(reverse=True)
l=[]
for i in range(len(l_A)):
   for j in range(len(l_B)):
      for k in range(len(l_C)):
        if (i+1)*(j+1)*(k+1) > K:
           break
        l+=[l_A[i]+l_B[j]+l_C[k]]
ans=sorted(l,reverse=True)[:K]
print(*ans,sep="\n")

Submission status: Accepted

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

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

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

ans = []
for a in range(len(A)):
  for b in range(len(B)):
    for c in range(len(C)):
      if (a + 1) * (b + 1) * (c + 1) > K:
        break
      ans += [A[a] + B[b] + C[c]]
      
ans = sorted(ans,reverse = True)[:K]
print(*ans,sep = "\n")

Submission status: Accepted

Solution code:
def main():
    import heapq

    x, y, z, k, *abc = map(int, open(0).read().split())
    a = sorted(abc[:x], reverse=True)
    b = sorted(abc[x:x + y], reverse=True)
    c = sorted(abc[x + y:], reverse=True)
    s = set()

    h = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
    heapq.heapify(h)
    num = []
    for _ in range(k):
        p, i, j, k = heapq.heappop(h)
        num.append(-p)
        for p, q, r in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
            p, q, r = min(i + p, x - 1), min(j + q, y - 1), min(k + r, z - 1)
            if (p, q, r) not in s:
                s.add((p, q, r))
                v = - (a[p] + b[q] + c[r])
                heapq.heappush(h, (v, p, q, r))

    ans = '\n'.join(map(str, num))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import heapq

    x, y, z, k, *abc = map(int, open(0).read().split())
    a = sorted(abc[:x], reverse=True)
    b = sorted(abc[x:x + y], reverse=True)
    c = sorted(abc[x + y:], reverse=True)
    s = set()

    h = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
    heapq.heapify(h)
    num = []
    for _ in range(k):
        p, i, j, k = heapq.heappop(h)
        num.append(-p)
        for p, q, r in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
            if i + p < x and j + q < y and k + r < z:
                if (i + p, j + q, k + r) not in s:
                    s.add((i + p, j + q, k + r))
                    v = - (a[i + p] + b[j + q] + c[k + r])
                    heapq.heappush(h, (v, i + p, j + q, k + r))

    ans = '\n'.join(map(str, num))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import heapq

    x, y, z, k, *abc = map(int, open(0).read().split())
    a = abc[:x]
    b = abc[x:x + y]
    c = abc[x + y:]
    s = set()

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

    h = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
    heapq.heapify(h)
    ans = []
    for _ in range(k):
        p, i, j, k = heapq.heappop(h)
        ans.append(-p)
        for p, q, r in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
            if i + p < x and j + q < y and k + r < z:
                if (i + p, j + q, k + r) not in s:
                    s.add((i + p, j + q, k + r))
                    v = - (a[i + p] + b[j + q] + c[k + r])
                    heapq.heappush(h, (v, i + p, j + q, k + r))

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


if __name__ == '__main__':
    main()