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:
X,Y,Z,K = map(int,input().split())
A = sorted(list(map(int,input().split())),reverse=1)
B = sorted(list(map(int,input().split())),reverse=1)
C = sorted(list(map(int,input().split())),reverse=1)
def solve(P):
    count = 0
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                if (A[i]+B[j]+C[k]) < P:
                    break
                count += 1
                if count >= K:
                    return True
    return False

maxa = A[0]+B[0]+C[0]+1
mina = 0

while maxa -mina > 1:
    mid = (maxa+mina)//2
    if solve(mid):
        mina = mid
    else:
        maxa = mid
ans = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (A[i]+B[j]+C[k]) < mina:
                break
            ans.append(A[i]+B[j]+C[k])

ans.sort(reverse=1)
for i in range(K):
    print(ans[i])

Submission status: Accepted

Solution code:
import heapq
X,Y,Z,K = map(int,input().split())
A = sorted(list(map(int,input().split())),reverse=1)
B = sorted(list(map(int,input().split())),reverse=1)
C = sorted(list(map(int,input().split())),reverse=1)
Q = [(-A[0]-B[0]-C[0],0,0,0)]
heapq.heapify(Q)
abc = [(0,0,0)]
for i in range(K):
    n,a,b,c = heapq.heappop(Q)
    if a < X-1 and (a+1,b,c) not in abc:
        abc.append((a+1,b,c))
        heapq.heappush(Q,(n+A[a]-A[a+1],a+1,b,c))
    if b < Y-1 and (a,b+1,c) not in abc:
        abc.append((a,b+1,c))
        heapq.heappush(Q,(n+B[b]-B[b+1],a,b+1,c))
    if c < Z-1 and (a,b,c+1) not in abc:
        abc.append((a,b,c+1))
        heapq.heappush(Q,(n+C[c]-C[c+1],a,b,c+1))
    print(-n)

Submission status: Accepted

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

x,y,z,k = get_nums_l()

aaa = get_nums_l()
bbb = get_nums_l()
ccc = get_nums_l()

aaa.sort(reverse=True)
bbb.sort(reverse=True)
ccc.sort(reverse=True)

def solve(p):
    count = 0
    for a in aaa:
        for b in bbb:
            for c in ccc:
                if a+b+c < center:
                    break
                count += 1
                if count >= k:
                    return True
    return False

left = 0
right = 30000000001

while left+1 < right:
    center = (left + right) // 2

    if solve(center):
        left = center
    else:
        right = center

border = left

ans = []

for a in aaa:
    for b in bbb:
        for c in ccc:
            if a+b+c >= border:
                ans.append(a+b+c)
            else:
                break

ans.sort(reverse=True)
print("\n".join(list(map(str,ans[:k]))))

Submission status: Accepted

Solution code:
import heapq
X,Y,Z,K = map(int,input().split())
A = sorted(list(map(int,input().split())),reverse=1)
B = sorted(list(map(int,input().split())),reverse=1)
C = sorted(list(map(int,input().split())),reverse=1)
Q = [(-A[0]-B[0]-C[0],0,0,0)]
heapq.heapify(Q)
for i in range(K):
    n,a,b,c = heapq.heappop(Q)
    if a < X-1 and (n+A[a]-A[a+1],a+1,b,c) not in Q:
        heapq.heappush(Q,(n+A[a]-A[a+1],a+1,b,c))
    if b < Y-1 and (n+B[b]-B[b+1],a,b+1,c) not in Q:
        heapq.heappush(Q,(n+B[b]-B[b+1],a,b+1,c))
    if c < Z-1 and (n+C[c]-C[c+1],a,b,c+1) not in Q:
        heapq.heappush(Q,(n+C[c]-C[c+1],a,b,c+1))
    print(-n)

Submission status: Accepted

Solution code:
X,Y,Z,K = map(int,input().split())
A = sorted(list(map(int,input().split())),reverse=1)
B = sorted(list(map(int,input().split())),reverse=1)
C = sorted(list(map(int,input().split())),reverse=1)
num= []
for xi in range(1,X+1):
    for yi in range(1,Y+1):
        for zi in range(1,K//(xi*yi)+1):
            if xi*yi*zi <= K and zi < Z+1:
                num.append((xi-1,yi-1,zi-1))

ans = []
for a,b,c in num:
    ans.append(A[a]+B[b]+C[c])
ans.sort(reverse=1)
for i in range(K):
    print(ans[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)
ng = 10**12
ok = 0
def check(chk):
    cnt=0
    for i in range(x):
        for j in range(y):
            for k in range(z):
                if a[i]+b[j]+c[k]<chk:
                    break
                cnt+=1
                if cnt>K:
                    return True
    return False
while ng-ok>1:
    if check((ok+ng)//2):ok=(ok+ng)//2
    else:ng=(ok+ng)//2
cnt=0
l=[]
for i in range(x):
        for j in range(y):
            for k in range(z):
                if a[i]+b[j]+c[k]<ok:
                    break
                l.append(a[i]+b[j]+c[k])
l.sort(reverse=True)
for i in range(K):print(l[i])

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 = [ea + eb for ea in a for eb in b]
ab.sort()

hp = []
for e in c:
    heappush(hp, (-(e + ab[-1]), x * y - 1))

ans = []
for _ in range(k):
    yum, i = heappop(hp)
    yum = -yum
    ans.append(yum)
    if i != 0:
        yum += ab[i-1] - ab[i]
        yum = -yum
        heappush(hp, (yum, i - 1))

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

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]
AB = nlargest(k, (a + b for a in A for b in B))
ABC = nlargest(k, (ab + c for ab in AB for c in C))
print('\n'.join(map(str, ABC)))

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 = [a + b for a in A for b in B]
AB.sort(reverse=True)
AB = AB[:k]
 
ABC = [ab + c for ab in AB for c in C]
ABC.sort(reverse=True)
ABC = ABC[:k]
 
print('\n'.join(map(str, ABC)))

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

import heapq

def main():
  na, nb, nc, K = map(int, input().split())
  As = sorted(list(map(int, input().split())), reverse=True)
  Bs = sorted(list(map(int, input().split())), reverse=True)
  Cs = sorted(list(map(int, input().split())), reverse=True)
  
  scores = [(-(As[0]+Bs[0]+Cs[0]), 0, 0, 0)]
  used = [(0, 0, 0)]
  def push(i, j, k):
    heapq.heappush(scores, (-(As[i] + Bs[j] + Cs[k]), i, j, k))
    
  for _ in range(K):
    (score, i, j, k) = heapq.heappop(scores)
    print(-score)
    for di, dj, dk in [[1,0,0], [0,1,0], [0,0,1]]:
      if not (i+di < na and j+dj<nb and k+dk < nc):
        continue        
      if (i+di, j+dj, k+dk) not in used:
        push(i+di, j+dj, k+dk)
        used.append((i+di, j+dj, k+dk))

if __name__=='__main__':
  main()

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()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
l1=sorted([i+j for i in a for j in b],reverse=True)[:k]
l2=sorted([i+j for i in l1 for j in c],reverse=True)[:k]
for i in l2:
  print(i)

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)
ans = [(-(a[0] + b[0] + c[0]), 0, 0, 0)]
heapq.heapify(ans)
added = {"0 0 0"}
for _ in range(k):
    w = heapq.heappop(ans)
    print(-w[0])
    i, j, m = w[1:]
    dx = [1, 0, 0]
    dy = [0, 1, 0]
    dz = [0, 0, 1]
    for r in range(3):
        ni, nj, nm = i + dx[r], j + dy[r], m + dz[r]
        if not (ni < x and nj < y and nm < z):
            continue
        g = " ".join(map(str, (ni, nj, nm)))
        if g not in added:
            added.add(g)
            heapq.heappush(ans, (-(a[ni] + b[nj] + c[nm]), ni, nj, nm))

Submission status: Accepted

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

a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ans = []
for i in range(x):
    for j in range(y):
        for m in range(z):
            if (i+1) * (j+1) * (m+1) > k:
                break
            ans.append(a[i] + b[j] + c[m])

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

Submission status: Accepted

Solution code:
import heapq
from collections import defaultdict

# 3種類のケーキを美味しい順にソートする
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)


# heapqで管理
que = []
heapq.heapify(que)
# 最も美味しい組み合わせは、
heapq.heappush(que, ((A[0] + B[0] + C[0])*-1, (0, 0, 0)))

# 使用した組み合わせ
used = defaultdict(int)
used[(0, 0, 0)] = 1

ans = []
while len(ans) < k:
    total, comb = heapq.heappop(que)
    ans.append(total*-1)
    a, b, c = comb
    # その次に美味しい組み合わせは、以下のいずれか
    for p, q, r in [[a+1, b, c], [a, b+1, c], [a, b, c+1]]:
        if used[(p, q, r)] == 0 and p < x and q < y and r < z:
            heapq.heappush(que, ((A[p] + B[q] + C[r])*-1, (p, q, r)))
            used[(p, q, r)] = 1

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

# A+B+Cが大きい順になるようにリストに入れていく
ABC = []

# heapqに任意の値が入っているか確認する
from collections import defaultdict
d = defaultdict(int)

que = []

def HeapPush(a, b, c):
    if d[(a, b, c)] == 0:
        heapq.heappush(que, ((A[a] + B[b] + C[c])*-1, a, b, c))  # -1を掛ける
        d[(a, b, c)] += 1


HeapPush(0, 0, 0)

while len(ABC) < k:
    
    abc, a, b, c = heapq.heappop(que)
    ABC.append(abc*-1)  # -1を掛け直す
    
    if a + 1 < x:
        HeapPush(a+1, b, c)
    if b + 1 < y:
        HeapPush(a, b+1, c)
    if c + 1 < z:
        HeapPush(a, b, c+1)
   

for abc in ABC:
    print(abc)

Submission status: Accepted

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

limit = [X,Y,Z]

nums = []
heapq.heapify(nums)
num = 0
abc = [[] for i in range(3)]

for i in range(3):
  abc[i] = sorted(list(map(int,input().split())),reverse = True)
  num += abc[i][0] * -1
  
heapq.heappush(nums,[num,0,0,0])
yet = []

for i in range(K):
  M = heapq.heappop(nums)
  for j in range(3):
    ID = M[1:]
    ID[j] += 1
    if ID[j] < limit[j]:
      a = ID[0]
      b = ID[1]
      c = ID[2]
      m = (abc[0][a] + abc[1][b] + abc[2][c]) * - 1
      if ID not in yet:
        heapq.heappush(nums,[m,a,b,c])
        yet.append(ID)
  print(M[0] * -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()))

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

if len(ab) < k:
    for i in range(len(ab)):
        for j in range(z):
            ans.append(ab[i]+c[j])
else:
    for i in range(k):
        for j in range(z):
            ans.append(ab[i]+c[j])

ans.sort(reverse=True)

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

Submission status: Accepted

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

def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    def next_state(s):
        def _next_state(i, j, k):
            return -sum([A[i], B[j], C[k]]), (i, j, k)
        _, (i, j, k) = s
        if i+1 < len(A):
            yield _next_state(i+1, j, k)
        if j+1 < len(B):
            yield _next_state(i, j+1, k)
        if k+1 < len(C):
            yield _next_state(i, j, k+1)

    ans = set()
    A = tuple(sorted(A, reverse=True))
    B = tuple(sorted(B, reverse=True))
    C = tuple(sorted(C, reverse=True))
    q = [(-sum([A[0], B[0], C[0]]), (0, 0, 0))]
    ans.add(q[0])
    for _ in range(K):
        s = heapq.heappop(q)
        for ns in next_state(s):
            if ns in ans:
                continue
            ans.add(ns)
            heapq.heappush(q, ns)
    return sorted((-v for v, _ in ans), reverse=True)[:K]


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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def binsearch(l, r, fn):
    while r - l > 1:
        m = (l + r) // 2
        if fn(m):
            l = m
        else:
            r = m
    return l

def main():
    X, Y, Z, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = sorted(map(int, input().split()), reverse=True)
    AB = sorted((a + b for a in A for b in B), reverse=True)
    def helper(x):
        r = 0
        t = len(C) - 1
        for ab in AB:
            while t >= 0 and C[t] + ab < x:
                t -= 1
            r += t + 1
            if r >= K:
                return True
            if t < 0:
                return False
        return r >= K
    l = binsearch(AB[-1] + C[-1], AB[0] + C[0] + 1, helper)
    r = []
    t = len(C) - 1
    for ab in AB:
        while t >= 0 and C[t] + ab < l + 1:
            t -= 1
        if t < 0:
            break
        r.extend(ab + c for c in C[:t + 1])
    r.sort(reverse=True)
    r += [l] * (K - len(r))
    print('\n'.join(str(i) for i in r))

main()

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
X, Y, Z, K, *L = map(int, open(0).read().split())
A = L[:X]
B = L[X:X+Y]
C = L[X+Y:]
ab = []
for i in range(X):
  for j in range(Y):
    ab += [A[i]+B[j]]
ab.sort(reverse=True)
ls = []
for i in range(Z):
  heappush(ls,(-ab[0]-C[i],i,0))
for i in range(K):
  m, j, cnt = heappop(ls)
  print(-m)
  if cnt+1<X*Y:
    heappush(ls,(-ab[cnt+1]-C[j],j,cnt+1))

Submission status: Accepted

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

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

ab=[i+j for j in b for i in a]
ab.sort(reverse=True)
abc=[i+j for j in c[:k] for i in ab[:k]]
abc.sort(reverse=True)

print(*abc[:k],sep='\n')

Submission status: Accepted

Solution code:
import sys,heapq
def input():
    return sys.stdin.readline()[:-1]
X,Y,Z,K=map(int,input().split())
A=list(map(lambda x:int(x)*-1,input().split()))
B=list(map(lambda x:int(x)*-1,input().split()))
C=list(map(lambda x:int(x)*-1,input().split()))
h=[]
heapq.heapify(h)
for i in A:
    for j in B:
        heapq.heappush(h,i+j)
m=min(K,len(h))
l=[None]*m
for i in range(m):
    l[i]=heapq.heappop(h)
h=[]
heapq.heapify(h)
for i in l:
    for j in C:
        heapq.heappush(h,i+j)
for i in range(K):
    print(heapq.heappop(h)*-1)

Submission status: Accepted

Solution code:
from heapq import heappop, heappush

X, Y, Z, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
A.sort(reverse=True)

# Bi+Cjの和を求め、降順にソートしておく(Dkとします)
D = [i+j for j in C for i in B]
D.sort(reverse=True)

# Ai+Bj+Ckの大きい順からK個を構成する要素としては、Aiの大きい順からK個とDkの大きい順からK個しか登場しないので
q = []
for i in A[:K]:
    for j in D[:K]:
        heappush(q, -(i+j))

for _ in range(K):
    print(-heappop(q))

Submission status: Accepted

Solution code:
from heapq import heappop, heappush

X, Y, Z, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
A.sort(reverse=True)

D = [i+j for j in C for i in B]
D.sort(reverse=True)

q = []
for i in A[:K]:
    for j in D[:K]:
        heappush(q, -(i+j))

for _ in range(K):
    print(-heappop(q))

Submission status: Accepted

Solution code:
##############################
# priority que
##############################

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 Nmb(tp):
  return A[tp[0]-1]+B[tp[1]-1]+C[tp[2]-1]

from heapq import heapify, heappop, heappush, heappushpop
hp = []
heapify(hp)
tp = (1, 1 , 1)
heappush(hp, (-Nmb(tp),)+tp)
hist = {tp}

for k in range(K):
  #最上位をpop
  rec = heappop(hp)
  print(rec[0]*-1)
# 次の順位を登録する
# Aを次の配列
  tp = (rec[1]+1, rec[2], rec[3])
  if tp[0]<=X and not tp in hist:
    heappush(hp, (-Nmb(tp),)+tp)
    hist.add(tp)
# Bを次の配列
  tp = (rec[1], rec[2]+1, rec[3])
  if tp[1]<=Y and not tp in hist:
    heappush(hp, (-Nmb(tp),)+tp)
    hist.add(tp)
# Cを次の配列
  tp = (rec[1], rec[2], rec[3]+1)
  if tp[2]<=Z and not tp in hist:
    heappush(hp, (-Nmb(tp),)+tp)
    hist.add(tp)

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 heapq import heapify, heappop, heappush, heappushpop
hp=[]
hist=set([])
heapify(hp)
hp0=[-(A[0]+B[0]+C[0]), 1, 1, 1]
heappush(hp, hp0)
hist.add(tuple(hp0[1:]))
k=0
while k<K:
  k+=1
  sabc=heappop(hp)
  print(sabc[0]*-1)
  for i in [1,2,3]:
    sabc_next = sabc[:]
    sabc_next[i]=sabc[i]+1
    sp = sabc_next[1]*sabc_next[2]*sabc_next[3]
    if sp<=K and sabc_next[1]<=X and sabc_next[2]<=Y and sabc_next[3]<=Z:
      sabc_next[0] = - (A[sabc_next[1]-1]+B[sabc_next[2]-1]+C[sabc_next[3]-1])
      if not tuple(sabc_next[1:]) in hist: 
        heappush(hp,sabc_next)
        hist.add(tuple(sabc_next[1:]))

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

X, Y, Z, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

# x以上のものがK個あるならOK
def f(x):
    cnt = 0
    for i in A:
        for j in B:
            for k in C:
                if i+j+k<x:break
                cnt += 1
                if cnt>=K:
                    return True
    return False

def get_boarder():
    l, r = 0, A[0]+B[0]+C[0]
    while r-l>1:
        m = (l+r)//2
        if f(m):
            l = m
        else:
            r = m
    return l

boarder = get_boarder()
q = []
for i in A:
    for j in B:
        for k in C:
            if i+j+k<boarder:break
            heappush(q, -(i+j+k))

for _ in range(K):
    print(-heappop(q))

Submission status: Accepted

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *
import copy

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)
dic={A[0]+B[0]+C[0]:["0_0_0"]}
usedlst=set(["0_0_0"])
lst=[]
D=[-(A[0]+B[0]+C[0])]
heapify(D)

def strmode(a,b,c):
    return(str(a)+"_"+str(b)+"_"+str(c),A[a]+B[b]+C[c])
while len(lst)<K:

    value=heappop(D)
    lst.append(-value)
    a,b,c=map(int,dic[-value].pop().split("_"))
    for x,y,z in ([[a+1,b,c],[a,b+1,c],[a,b,c+1]]):

        if x<X and y<Y and z<Z:
            name,value=strmode(x,y,z)
            if not name in usedlst:
                usedlst.add(name)
                heappush(D,-value)
                if not value in dic:
                    dic.update({value:[name]})
                else:
                    dic[value].append(name)
for i in lst:
    print(i)

Submission status: Accepted

Solution code:
def main():
  x,y,z,k=map(int,input().split())
  one=[-int(i) for i in input().split()]
  two=[-int(i) for i in input().split()]
  three=[-int(i) for i in input().split()]
  one.sort()
  two.sort()
  three.sort()
  inf=10**11
  one.append(inf)
  two.append(inf)
  three.append(inf)
  
  import heapq 
  #print(one,two,three)
  q=[[one[0]+two[0]+three[0],0,0,0],[inf]]
  heapq.heapify(q)
  for i in range(k):
    m=heapq.heappop(q)
    while q[0]==m:
      m=heapq.heappop(q)
    print(-m[0])
    i,j,k=m[1],m[2],m[3]
    heapq.heappush(q,[one[i+1]+two[j]+three[k],i+1,j,k])
    heapq.heappush(q,[one[i]+two[j+1]+three[k],i,j+1,k])
    heapq.heappush(q,[one[i]+two[j]+three[k+1],i,j,k+1])

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
X,Y,Z,K=map(int,input().split())
A=(list(map(int,input().split())))
B=(list(map(int,input().split())))
C=sorted(list(map(int,input().split())))

ans=max(A)+max(B)+max(C)

temp=[]
for a in A:
    for b in B:
        temp.append(a+b)

temp.sort()

dif=[temp[i]-temp[-1] for i in range(len(temp))[::-1]]
dif_c=[C[i]-C[-1] for i in range(len(C))[::-1]]

dif=dif[:K]

temp=[]
for d in dif:
    for dc in dif_c:
        temp.append(ans+d+dc)
temp.sort()
temp=temp[::-1]

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

Submission status: Accepted

Solution code:
from heapq import nlargest
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=nlargest(k,(x+y for x in a for y in b))
abc=nlargest(k,(xy+z for xy in ab for z in c))
for i in abc:
  print(i)

Submission status: Accepted

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 999999999
R = 10**9 + 7
import heapq

def ddprint(x):
  if DBG:
    print(x)

x,y,z,k = inm()
a = inl()
b = inl()
c = inl()

h = []
for i in a:
    for j in b:
        if len(h)<k:
            heapq.heappush(h,i+j)
        else:
            heapq.heappushpop(h,i+j)
g = []
for i in c:
    for j in h:
        if len(g)<k:
            heapq.heappush(g,i+j)
        else:
            heapq.heappushpop(g,i+j)

d = []
for i in range(k):
    d.append(heapq.heappop(g))
for i in range(k-1,-1,-1):
    print(d[i])

Submission status: Accepted

Solution code:
from heapq import heappush, heappop


def solve(k, ax, bx, cx, x, y, z):
    ans = []
    heap = [(-(ax[0] + bx[0] + cx[0]), 0, 0, 0)]
    d = {(0, 0, 0)}

    for _ in range(k):
        s, ai, bi, ci = heappop(heap)
        ans += [-s]

        if (ai+1, bi, ci) not in d and ai+1 < x:
            heappush(heap, (-(ax[ai+1] + bx[bi] + cx[ci]), ai+1, bi, ci))
            d.add((ai+1, bi, ci))
        if (ai, bi+1, ci) not in d and bi+1 < y:
            heappush(heap, (-(ax[ai] + bx[bi+1] + cx[ci]), ai, bi+1, ci))
            d.add((ai, bi+1, ci))
        if (ai, bi, ci+1) not in d and ci+1 < z:
            heappush(heap, (-(ax[ai] + bx[bi] + cx[ci+1]), ai, bi, ci+1))
            d.add((ai, bi, ci+1))

    return ans


if __name__ == "__main__":
    x, y, z, k = map(int, input().split())
    ax = sorted(list(map(int, input().split())), reverse=True)
    bx = sorted(list(map(int, input().split())), reverse=True)
    cx = sorted(list(map(int, input().split())), reverse=True)
    ans = solve(k, ax, bx, cx, x, y, z)
    print(*ans, 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.sort()
B.sort()
C.sort()
AB=[]
for a in A:
  for b in B:
    AB.append(-(a+b))
import heapq
heapq.heapify(AB)
ABC=[]
for _ in range(min(K,len(AB))):
  ab=heapq.heappop(AB)
  for c in C:
    heapq.heappush(ABC,ab-c)
for i in range(K):
  print(-heapq.heappop(ABC))

Submission status: Accepted

Solution code:
import sys


def solve(k, ax, bx, cx):
    ab = []
    for a in ax:
        for b in bx:
            ab.append(a+b)

    ab.sort(reverse=True)
    abk = ab[:k]

    ans = []
    for c in cx:
        for abi in abk:
            ans.append(c+abi)

    return sorted(ans, reverse=True)[:k]


if __name__ == '__main__':
    _, _, _, k = map(int, input().split())
    ax = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
    bx = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
    cx = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)

    ans = solve(k, ax, bx, cx)

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

Submission status: Accepted

Solution code:
from heapq import heappop, heappush
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)

queue = []

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

      val = A[x] + B[y] + C[z]
      heappush(queue, -val)

for _ in range(K):
  print(-heappop(queue))

Submission status: Accepted

Solution code:
from heapq import nlargest

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 = nlargest(k, (a + b for a in A for b in B))
ABC = nlargest(k, (ab + c for ab in AB for c in C))
[print(abc) for abc in ABC]

Submission status: Accepted

Solution code:
import sys
def input():
    return 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())
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

abl = sorted([a + b for a in A for b in B], reverse=True)[:K]
abcl = sorted([ab + c for ab in abl for c in C], reverse=True)[:K]
for abc in abcl:
    print(abc)

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)

seen = [(0, 0, 0)]
stack = [((0, 0, 0), a[0]+b[0]+c[0])]
for _ in range(k):
    (na, nb, nc), s = stack.pop()
    print(s)
    if na<x-1 and (na+1, nb, nc) not in seen:
        seen.append((na+1, nb, nc))
        stack.append(((na+1, nb, nc), a[na+1]+b[nb]+c[nc]))
    if nb<y-1 and (na, nb+1, nc) not in seen:
        seen.append((na, nb+1, nc))
        stack.append(((na, nb+1, nc), a[na]+b[nb+1]+c[nc]))
    if nc<z-1 and (na, nb, nc+1) not in seen:
        seen.append((na, nb, nc+1))
        stack.append(((na, nb, nc+1) ,a[na]+b[nb]+c[nc+1]))
    stack.sort(key=lambda x:x[1])

Submission status: Accepted

Solution code:
import heapq

X, Y, Z, K = map(int, input().split())
A = sorted([-x for x in map(int, input().split())])
B = sorted([-x for x in map(int, input().split())])
C = sorted([-x for x in map(int, input().split())])

check = set()  # priority queueに入れたかどうかを管理する

heap = []
heapq.heappush(heap, (A[0] + B[0] + C[0], 0, 0, 0))
check.add((0, 0, 0))

for i in range(K):
    s, a, b, c = heapq.heappop(heap)
    print(-s)
    if a + 1 < X and (a + 1, b, c) not in check:
        heapq.heappush(heap, (A[a + 1] + B[b] + C[c], a + 1, b, c))
        check.add((a + 1, b, c))
    if b + 1 < Y and (a, b + 1, c) not in check:
        heapq.heappush(heap, (A[a] + B[b + 1] + C[c], a, b + 1, c))
        check.add((a, b + 1, c))
    if c + 1 < Z and (a, b, c + 1) not in check:
        heapq.heappush(heap, (A[a] + B[b] + C[c + 1], a, b, c + 1))
        check.add((a, b, c + 1))