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 bisect import bisect_left

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 X*Y*Z <= 50000:
  D = sorted([A[i]+B[j]+C[k] for i in range(X) for j in range(Y) for k in range(Z)],reverse=True)
  for i in range(K):
    print(D[i])
  
else:
  A = sorted(A)
  D = sorted([B[i]+C[j] for i in range(Y) for j in range(Z)])

  thr = K
  s = A[-1]+D[max(0,len(D)-thr)]
  prev_idx=0
  selected = [A[-1] + D[i] for i in range(max(0,len(D)-thr),len(D))]
  
  for i in range(1,X):
    r = s - A[-1-i]
    if r<=0:break
    idx = bisect_left(D,r,prev_idx,len(D)-1)
    if idx == len(D):break
    selected.extend([A[-1-i] + D[j] for j in range(idx,len(D))])
    prec_idx = idx

  selected = sorted(selected,reverse=True)

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

Submission status: Accepted

Solution code:
import sys
import re
import heapq

def value(t):
    global a, b, c
    return -(a[t[0]] + b[t[1]] + c[t[2]])

def main():
    global a, b, c
    x, y, z, k = map(int, re.split(r'\s+', sys.stdin.readline().strip()))
    #print(x, y, z, k)
    a = list(map(int, re.split(r'\s+', sys.stdin.readline().strip())))
    b = list(map(int, re.split(r'\s+', sys.stdin.readline().strip())))
    c = list(map(int, re.split(r'\s+', sys.stdin.readline().strip())))

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

    done = set()
    q = list()
    t = (x - 1, y - 1, z - 1)
    q.append((value(t), t))

    for _ in range(k):
        val, t = heapq.heappop(q)
        print(-val)
        t0 = (t[0] - 1, t[1], t[2])
        t1 = (t[0], t[1] - 1, t[2])
        t2 = (t[0], t[1], t[2] - 1)
        if (t0 not in done) and (t[0] - 1 >= 0):
            heapq.heappush(q, (value(t0), t0))
            done.add(t0)
        if (t1 not in done) and (t[1] - 1 >= 0):
            heapq.heappush(q, (value(t1), t1))
            done.add(t1)
        if (t2 not in done) and (t[2] - 1 >= 0):
            heapq.heappush(q, (value(t2), t2))
            done.add(t2)
            
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)

from collections import defaultdict
done = defaultdict(lambda:False)
import heapq
q = [(-A[0]-B[0]-C[0],0,0,0)]
heapq.heapify(q)

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

Submission status: Accepted

Solution code:
#d
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()))
C.sort(reverse=True)

ab = sorted([aa+bb for aa in A for bb in B ],reverse=True)[:K]
ans = sorted([aabb+cc for aabb in ab for cc in C ],reverse=True)[:K]

for aa in ans:
    print(aa)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import heapq
from collections import defaultdict


def read():
    X, Y, Z, K = map(int, input().strip().split())
    A = list(map(int, input().strip().split()))
    B = list(map(int, input().strip().split()))
    C = list(map(int, input().strip().split()))
    return X, Y, Z, K, A, B, C


def solve(X, Y, Z, K, A, B, C, INF=10000000001):
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    
    used = defaultdict(bool)
    h = []
    p, q, r = 0, 0, 0
    heapq.heappush(h, (-A[p] - B[q] - C[r], p, q, r))
    used[(p, q, r)] = True
    for k in range(K):
        a, p, q, r = heapq.heappop(h)
        print(-a)
        if p+1 < len(A) and not used[(p+1, q, r)]:
            heapq.heappush(h, (-A[p+1] - B[q] - C[r], p+1, q, r))
            used[(p+1, q, r)] = True
        if q+1 < len(B) and not used[(p, q+1, r)]:
            heapq.heappush(h, (-A[p] - B[q+1] - C[r], p, q+1, r))
            used[(p, q+1, r)] = True
        if r+1 < len(C) and not used[(p, q, r+1)]:
            heapq.heappush(h, (-A[p] - B[q] - C[r+1], p, q, r+1))
            used[(p, q, r+1)] = True


if __name__ == '__main__':
    inputs = read()
    solve(*inputs)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from itertools import product


def read():
    X, Y, Z, K = map(int, input().strip().split())
    A = list(map(int, input().strip().split()))
    B = list(map(int, input().strip().split()))
    C = list(map(int, input().strip().split()))
    return X, Y, Z, K, A, B, C


def solve(X, Y, Z, K, A, B, C, INF=10000000001):
    # 美味しさの上位K=3000件を出力する
    AB = [a+b for a, b in product(A, B)]
    AB.sort(reverse=True)
    AB = AB[:K]
    C.sort(reverse=True)

    D = [AB[k] + C[0] for k in range(len(AB))]
    E = [0 for k in range(len(AB))]
    j = 0
    for k in range(K):
        hi = 0

        # find
        for i in range(len(D)):
            if D[hi] < D[i]:
                hi = i
        # print
        print(D[hi])

        # update
        E[hi] += 1
        if E[hi] >= len(C):
            D[hi] = -INF
        else:
            D[hi] = AB[hi] + C[E[hi]]


if __name__ == '__main__':
    inputs = read()
    solve(*inputs)

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)

que = [(-(a[0]+b[0]+c[0]), 0, 0, 0)]
is_pushed = {(0, 0, 0): 1}
for _ in range(k):
    v, p, q, r = heapq.heappop(que)
    print(-v)
    if p+1 < x and not is_pushed.get((p+1, q, r)):
        heapq.heappush(que, (-(a[p+1]+b[q]+c[r]), p+1, q, r))
        is_pushed[(p+1, q, r)] = 1
    if q+1 < y and not is_pushed.get((p, q+1, r)):
        heapq.heappush(que, (-(a[p]+b[q+1]+c[r]), p, q+1, r))
        is_pushed[(p, q+1, r)] = 1
    if r+1 < z and not is_pushed.get((p, q, r+1)):
        heapq.heappush(que, (-(a[p]+b[q]+c[r+1]), p, q, r+1))
        is_pushed[(p, q, r+1)] = 1

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

p = [i + k for i, k in product(a, b)]
p.sort(reverse=True)
q = [i + k for i, k in product(p[:k], c)]
q.sort(reverse=True)
print('\n'.join(map(str, q[:k])))

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)
c.sort(reverse=True)
l.sort(reverse=True)
l = l[:k]
l2 = []
for i in c:
    for j in l:
        l2.append(i + j)
l2.sort(reverse=True)
print(*l2[: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()))

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

for c in C:
    for ab in AB:
        ABC.append(c + ab)
ABC = sorted(ABC,reverse=True)        
print(*ABC[:K],sep="\n")

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

X, Y, Z, K = mapint()
Xs = list(mapint())
Ys = list(mapint())
Zs = list(mapint())
Xs.sort(reverse=True)
Ys.sort(reverse=True)
Zs.sort(reverse=True)

x, y, z = 0, 0, 0
from heapq import heappop, heappush
Q = []
heappush(Q, (-(Xs[x]+Ys[y]+Zs[z]), (x, y, z)))
s = set()
s.add((x, y, z))
for _ in range(K):
    ans, (x, y, z) = heappop(Q)
    print(-ans)
    origin = [x, y, z]
    for i in range(3):
        xyz = origin[:]
        xyz[i] += 1
        x, y, z = xyz
        if x>=X or y>=Y or z>=Z: continue
        if not tuple(xyz) in s:
            heappush(Q, (-(Xs[x]+Ys[y]+Zs[z]), (x, y, z)))
            s.add((x, y, z))

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 a in A:
    for b in B:
        l.append(a+b)
l = sorted(l, reverse = True)[:K]

ans = []
for c in C:
    for k in l:
        ans.append(c + k)
        
ans = sorted(ans, reverse = True)
print(*ans[:K], sep="\n")

Submission status: Accepted

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

l = []
for a in A:
    for b in B:
        l.append(a+b)
l = sorted(l, reverse = True)[:K]

ans = []
for c in C:
    for k in l:
        ans.append(c + k)
        
ans = sorted(ans, reverse = True)
print(*ans[:K], sep="\n")

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 a in A:
    for b in B:
        l.append(a+b)
l = sorted(l, reverse = True)[:K]

ans = []
for c in C:
    for k in l:
        ans.append(c + k)
        
ans = sorted(ans, reverse = True)
print(*ans[:K], sep="\n")

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

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()))
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)

    q = []
    heappush(q, (-(A[0]+B[0]+C[0]),0,0,0))
    count = 0
    used = set()

    while count < K:
        num, s,t,u = heappop(q)
        print(-num)
        used.add((s,t,u))

        if s+1 < X and (s+1,t,u) not in used:
            heappush(q, (-(A[s+1]+B[t]+C[u]),s+1,t,u))
            used.add((s+1,t,u))
        if t+1 < Y and (s,t+1,u) not in used:
            heappush(q, (-(A[s]+B[t+1]+C[u]),s,t+1,u))
            used.add((s,t+1,u))
        if u+1 < Z and (s,t,u+1) not in used:
            heappush(q, (-(A[s]+B[t]+C[u+1]),s,t,u+1))
            used.add((s,t,u+1))
        count += 1

if __name__ == "__main__":
    main()

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)

hq = [[-a[0]-b[0]-c[0], 0, 0, 0]]
heapq.heapify(hq)
visited = {(0,0,0)}

for i in range(k):
    ii, ia, ib, ic = heapq.heappop(hq)
    print(-ii)
    na = min(ia+1, x-1)
    nb = min(ib+1, y-1)
    nc = min(ic+1, z-1)
    if not (na, ib, ic) in visited:
        heapq.heappush(hq, [-a[na] -b[ib] -c[ic], na, ib, ic])
        visited.add((na, ib, ic))
    if not (ia, nb, ic) in visited:
        heapq.heappush(hq, [-a[ia] -b[nb] -c[ic], ia, nb, ic])
        visited.add((ia, nb, ic))
    if not (ia, ib, nc) in visited:
        heapq.heappush(hq, [-a[ia] -b[ib] -c[nc], ia, ib, nc])
        visited.add((ia, ib, nc))

Submission status: Accepted

Solution code:
import heapq
x,y,z,K=list(map(int,input().split()))
alst=sorted(list(map(int,input().split())),reverse=True)
blst=sorted(list(map(int,input().split())),reverse=True)
clst=sorted(list(map(int,input().split())),reverse=True)

hq=[(-alst[0]-blst[0]-clst[0],0,0,0)]
used=set()

while K>0:
    s,i,j,k=heapq.heappop(hq)
    s*=-1
    if (i,j,k) in used:
        continue
    used.add((i,j,k))
    print(s)

    heapq.heappush(hq,(-s+alst[i]-alst[i+1],i+1,j,k)) if i+1<x else 0
    heapq.heappush(hq,(-s+blst[j]-blst[j+1],i,j+1,k)) if j+1<y else 0
    heapq.heappush(hq,(-s+clst[k]-clst[k+1],i,j,k+1)) if k+1<z else 0

    K-=1

Submission status: Accepted

Solution code:
X, Y, Z, k = (int(x) for x in input().split())
A = list(int(x) for x in input().split())
B = list(int(x) for x in input().split())
C = list(int(x) for x in input().split())

AB = []
for x in range(X):
    for y in range(Y):
        AB.append(A[x] + B[y])
AB.sort(reverse=True)
C.sort(reverse=True)

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

for ans in ANS[: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 ai in a:
  for bi in b:
    ans.append(-ai-bi)
ans.sort()
ans=ans[:k] if len(ans)>=k else ans+[0]*(k-len(ans))
ans1=[]
from heapq import heapify,heappop,heappush
for ci in c:
  for ansi in ans:
    heappush(ans1,ansi-ci)
for i in range(k):
  print(-heappop(ans1))

Submission status: Accepted

Solution code:
from itertools import product
from heapq import *

X,Y,Z,K,*f = map(int, open(0).read().split())
A = f[:X]
B = f[X:X+Y]
C = f[X+Y:]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
AB = [sum(x) * (-1) for x in product(A,B)]
heapify(AB)
AB_picked = []
for i in range(K):
    if AB:
        AB_picked.append(heappop(AB)*(-1))
    else:
        break
ABC = [sum(x) * (-1) for x in product(AB_picked,C)]
heapify(ABC)
for i in range(K):
    print(heappop(ABC)*(-1))

Submission status: Accepted

Solution code:
import heapq
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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()))
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)

    def bfs():
        used = set()
        used.add((0, 0, 0))

        q = []
        heapq.heapify(q)
        heapq.heappush(q, (-(A[0] + B[0] + C[0]), 0, 0, 0))
        res = []
        while q:
            v, i, j, k = heapq.heappop(q)
            res.append(-v)

            if len(res) == K:
                return res

            for di, dj, dk in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
                ni = i + di
                nj = j + dj
                nk = k + dk

                if ni >= X or nj >= Y or nk >= Z:
                    continue

                if (ni, nj, nk) in used:
                    continue

                used.add((ni, nj, nk))
                heapq.heappush(q, (-(A[ni] + B[nj] + C[nk]), ni, nj, nk))

        return res

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from heapq import heappop, heappush

# 入力の受付
X, Y, Z, K = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
c_list = list(map(int, input().split()))

# 入力配列のソート(降順: 大きい値が先頭)
a_list.sort(reverse = True)
b_list.sort(reverse = True)
c_list.sort(reverse = True)

# 優先度付きキューの定義
hq = []

# キューの管理リスト
arg_hash = {}

# キューの初期値
val = -(a_list[0] + b_list[0] + c_list[0])
heappush(hq, (val, 0, 0, 0))

# 答えを格納する配列
ans_arr = []

# 最大値を取り出していく．
# 以下をK回繰り返す
# 1. -(A+B+C)を要素とするキューの中の最小値を取り出す．(i番目に小さい数) この最小値は(A,B,C)の各リストの(p,q,r)番目のリストに含まれる値．
# 2．このキューに対して(p+1, q, r), (p, q+1, r), (p, q, r+1)のそれぞれが含まれているかどうかを確認
#     ない場合には，キューの管理リストに追加 & キューにその要素を追加
#     ある場合には，特に何もせず．
for i in range(K):
    # キューから最小値を取り出す(負なので実際は最大値を取り出している)
    ans_val, p, q, r = heappop(hq)
    ans_arr.append(-ans_val)
    
    # (p+1, q, r), (p, q+1, r), (p, q, r+1)のそれぞれが含まれているかどうかを確認
    arg_a = (p+1, q, r)
    arg_b = (p, q+1, r)
    arg_c = (p, q, r+1)

    # キューの管理リストに対して，上の要素が存在するかどうかの探索と処理
    # a < X-1 は，全要素がキューに入っている場合に，さらに次のインデックスの要素を追加しないようにするための検索条件
    # p+1 用
    if (p < X-1) and (arg_a not in arg_hash):
        # キュー管理リストに(p+1, q, r)のタプルを登録（辞書キーとして）
        arg_hash[arg_a] = 1
        # キューに値を登録
        heappush(hq, (-(a_list[p+1] + b_list[q] + c_list[r]), ) + arg_a)
    # q+1 用
    if (q < Y-1) and (arg_b not in arg_hash):
        # キュー管理リストに(p+1, q, r)のタプルを登録（辞書キーとして）
        arg_hash[arg_b] = 1
        # キューに値を登録
        heappush(hq, (-(a_list[p] + b_list[q+1] + c_list[r]), ) + arg_b)
    # r+1 用
    if (r < Z-1) and (arg_c not in arg_hash):
        # キュー管理リストに(p+1, q, r)のタプルを登録（辞書キーとして）
        arg_hash[arg_c] = 1
        # キューに値を登録
        heappush(hq, (-(a_list[p] + b_list[q] + c_list[r+1]), ) + arg_c)

# 結果出力
for i in range(len(ans_arr)):
    print(ans_arr[i])

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############
X, Y, Z, K = getNM()
A = getList()
B = getList()
C = getList()
A.sort(reverse = True)
B.sort(reverse = True)
C.sort(reverse = True)

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

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

for i in range(K):
    print(ans[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()))

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

D = []
for i in range(X):
    for j in range(Y):
        D.append(A[i]+B[j])
D.sort(reverse=True)

E = []
for i in range(min(len(D),K)):
    for j in range(Z):
        E.append(D[i]+C[j])
E.sort(reverse=True)

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b: a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

X,Y,Z,K = IL()
A = IL()
B = IL()
C = IL()
A.sort(reverse = True)
B.sort(reverse = True)
C.sort(reverse = True)

ans = []
q = [(-A[0]-B[0]-C[0],0,0,0)]
f = [0]
dx = [-1,1,0,0,0,0]
dy = [0,0,-1,1,0,0]
dz = [0,0,0,0,-1,1,]
while K:
  K -= 1
  v,x,y,z = heapq.heappop(q)
  ans.append(-v)
  for i in range(6):
    nx,ny,nz = x+dx[i],y+dy[i],z+dz[i]
    if nx<0 or nx>=X or ny<0 or ny>=Y or nz<0 or nz>=Z: continue
    if (nx*Y*Z + ny*Z + nz) in f: continue
    heapq.heappush(q,(-A[nx]-B[ny]-C[nz],nx,ny,nz))
    f.append(nx*Y*Z + ny*Z + nz)
    
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)

l = []
for a in A:
    for b in B:
        l.append(a+b)
l = sorted(l, reverse=True)[:K]


ans = []
for c in C:
    for k in l:
        ans.append(c+k)

ans = sorted(ans, reverse=True)
print(*ans[:K], sep="\n")

Submission status: Accepted

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

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()))
    #a.sort()
    #a.reverse()
    #b.sort()
    #b.reverse()
    #c.sort()
    #c.reverse()
    ans = []
    for i in a:
        for j in b:
            ans.append(i+j)
    ans.sort()
    ans.reverse()
    ans = ans[:k]
    res = []
    for i in c:
        for j in ans:
            res.append(i+j)
    res.sort()
    res.reverse()
    res = res[:k]
    print(*res, sep="\n")
    
if __name__=="__main__":
    main()

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 = [[(A[0]+B[0]+C[0])*-1, 0, 0, 0]]
heapq.heapify(q)
used = {}
for i in range(K):
    if len(q) == 0:
        break
    a, b, c, d = heapq.heappop(q)
    print(a*(-1))
    if (b+1, c, d) in used:
        pass
    elif b+1 < X:
        heapq.heappush(q, [(A[b+1]+B[c]+C[d])*(-1), b+1, c, d])
        used[(b+1, c, d)] = 1
    if (b, c+1, d) in used:
        pass
    elif c+1 < X:
        heapq.heappush(q, [(A[b]+B[c+1]+C[d])*(-1), b, c+1, d])
        used[(b, c+1, d)] = 1
    if (b, c, d+1) in used:
        pass
    elif d+1 < X:
        heapq.heappush(q, [(A[b]+B[c]+C[d+1])*(-1), b, c, d+1])
        used[(b, c, d+1)] = 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)
P = []
for i in range(min(3000, X)):
    for j in range(min(3000//(i+1) + 1, Y)):
        for z in range(min(3000//(i+1)//(j+1) +1, Z) ):
            P.append(A[i]+B[j]+C[z])
P.sort(reverse=True)
for i in range(K):
    print(P[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)
P = []
for i in range(min(3000, X)):
    for j in range(min(3000//(i+1) + 1, X)):
        for z in range(min(3000//(i+1)//(j+1) +1, X) ):
            P.append(A[i]+B[j]+C[z])
P.sort(reverse=True)
for i in range(K):
    print(P[i])

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))
    i=0
    ans=[]
    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))
        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))
if __name__=="__main__":
    main()