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:
import heapq
from collections import defaultdict
x, y, z, K = map(int, input().split())
A = sorted(map(int, input().split()), reverse=True)
B = sorted(map(int, input().split()), reverse=True)
C = sorted(map(int, input().split()), reverse=True)
visited = defaultdict(bool)
Q = [(-(A[0]+B[0]+C[0]), 0, 0, 0)]

for _ in range(K):
    S, i, j, k = heapq.heappop(Q)
    print(-S)
    if i+1 < x and not visited[i+1, j, k]:
        heapq.heappush(Q, (-(A[i+1]+B[j]+C[k]), i+1, j, k))
        visited[i+1, j, k] = True

    if j+1 < y and not visited[i, j+1, k]:
        heapq.heappush(Q, (-(A[i]+B[j+1]+C[k]), i, j+1, k))
        visited[i, j+1, k] = True

    if k+1 < z and not visited[i, j, k+1]:
        heapq.heappush(Q, (-(A[i]+B[j]+C[k+1]), i, j, k+1))
        visited[i, j, k+1] = True

Submission status: Accepted

Solution code:
x, y, z, K = map(int, input().split())
A = sorted(map(int, input().split()), reverse=True)
B = sorted(map(int, input().split()), reverse=True)
C = sorted(map(int, input().split()), reverse=True)
ans = []
for i in range(x):
    for j in range(y):
        for k in range(z):
            if (i+1)*(j+1)*(k+1) <= K:
                ans.append(A[i]+B[j]+C[k])
                continue
            break
ans.sort(reverse=True)

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

Submission status: Accepted

Solution code:
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)
def check(p):
  count = 0
  for i in range(x):
    for j in range(y):
      for m in range(z):
        if a_list[i] + b_list[j] + c_list[m] < p:
          break
        count += 1
        if count >= k:
          return True
  return False

left = 1
right = 10**20
while right - left > 1:
  middle = (right + left)//2
  if check(middle) == True:
    left = middle
  else:
    right = middle
ans_list = []
for i in range(x):
    for j in range(y):
      for m in range(z):
        if a_list[i] + b_list[j] + c_list[m] < left +1:
          break
        elif a_list[i] + b_list[j] + c_list[m] >= left+1:
          ans_list.append(a_list[i] + b_list[j] + c_list[m])
nokori = k - len(ans_list)
for i in range(nokori):
  ans_list.append(left)
  
ans_list.sort(reverse = True)
for ans in ans_list:
  print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc123/tasks/abc123_d

def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

X, Y, Z, K = MAP()
A = LIST()
B = LIST()
C = LIST()

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

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

ans = []

for c in C[:K]:
    for e in E[:K]:
        ans.append(c+e)
ans.sort(reverse=True)

for i, a in enumerate(ans):
    if i < K:
        print(a)
    else:
        break

Submission status: Accepted

Solution code:
# D - Cake 123

import heapq
import itertools
from collections import defaultdict

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

A = list(int(a) for a in input().split())
B = list(int(b) for b in input().split())
C = list(int(c) for c in input().split())

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

hq = []
hqAns = []
d = defaultdict(int)

heapq.heappush(hq, ((A[0]+B[0]+C[0])*-1, 0, 0, 0))
d[(0,0,0)] = 1

while len(hqAns)<k :
    val, l, m, n = heapq.heappop(hq)
    heapq.heappush(hqAns, val)
 
    if d[(l+1, m, n)]==0 and l+1 < x:
        heapq.heappush(hq, ((A[l+1]+B[m]+C[n])*-1, l+1, m, n))
        d[(l+1, m, n)] = 1

    if d[(l, m+1, n)]==0 and m+1 < y:
        heapq.heappush(hq, ((A[l]+B[m+1]+C[n])*-1, l, m+1, n))
        d[(l, m+1, n)] = 1
 
    if d[(l, m, n+1)]==0 and n+1 < z:
        heapq.heappush(hq, ((A[l]+B[m]+C[n+1])*-1, l, m, n+1))
        d[(l, m, n+1)] = 1

for i in range(k):
    print(heapq.heappop(hqAns)*-1)

'''
# ans.2 #

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

hq = []
for i in range(x):
    for j in range(y):
        for l in range(z):
            if (i+1)*(j+1)*(l+1)<=k:
                heapq.heappush(hq, (A[i]+B[j]+C[l])*-1)
            else:
                break

for _ in range(k):
    print(heapq.heappop(hq)*-1)

'''

'''

# ans.1 #

hq = []
for i, j in itertools.product(A, B):
    heapq.heappush(hq, (i+j)*-1)

AB = []
while len(hq)>0 and len(AB)<=k:
    AB.append(heapq.heappop(hq)*-1)

hq = []
for i, j in itertools.product(AB, C):
    heapq.heappush(hq, (i+j)*-1)

for _ in range(k):
    print(heapq.heappop(hq)*-1)

'''

Submission status: Accepted

Solution code:
# D - Cake 123

import heapq
import itertools
from collections import defaultdict

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

A = list(int(a) for a in input().split())
B = list(int(b) for b in input().split())
C = list(int(c) for c in input().split())

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

hq = []
hqAns = []
d = defaultdict(int)

heapq.heappush(hq, ((A[0]+B[0]+C[0])*-1,(0,0,0)))
d[(0,0,0)] = 1

while len(hqAns)<k :
    val, key = heapq.heappop(hq)
    heapq.heappush(hqAns, val)
 
    if d[(key[0]+1, key[1], key[2])]==0 and key[0]+1 < x:
        heapq.heappush(hq, ((A[key[0]+1]+B[key[1]]+C[key[2]])*-1, ((key[0]+1, key[1], key[2]))))
        d[(key[0]+1, key[1], key[2])] = 1

    if d[(key[0], key[1]+1, key[2])]==0 and key[1]+1 < y:
        heapq.heappush(hq, ((A[key[0]]+B[key[1]+1]+C[key[2]])*-1, ((key[0], key[1]+1, key[2]))))
        d[(key[0], key[1]+1, key[2])] = 1
 
    if d[(key[0], key[1], key[2]+1)]==0 and key[2]+1 < z:
        heapq.heappush(hq, ((A[key[0]]+B[key[1]]+C[key[2]+1])*-1, ((key[0], key[1], key[2]+1))))
        d[(key[0], key[1], key[2]+1)] = 1

for i in range(k):
    print(heapq.heappop(hqAns)*-1)

'''
# ans.2 #

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

hq = []
for i in range(x):
    for j in range(y):
        for l in range(z):
            if (i+1)*(j+1)*(l+1)<=k:
                heapq.heappush(hq, (A[i]+B[j]+C[l])*-1)
            else:
                break

for _ in range(k):
    print(heapq.heappop(hq)*-1)

'''

'''

# ans.1 #

hq = []
for i, j in itertools.product(A, B):
    heapq.heappush(hq, (i+j)*-1)

AB = []
while len(hq)>0 and len(AB)<=k:
    AB.append(heapq.heappop(hq)*-1)

hq = []
for i, j in itertools.product(AB, C):
    heapq.heappush(hq, (i+j)*-1)

for _ in range(k):
    print(heapq.heappop(hq)*-1)

'''

Submission status: Accepted

Solution code:
# D - Cake 123

import heapq
import itertools

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

A = list(int(a) for a in input().split())
B = list(int(b) for b in input().split())
C = list(int(c) for c in input().split())


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

hq = []
for i in range(x):
    for j in range(y):
        for l in range(z):
            if (i+1)*(j+1)*(l+1)<=k:
                heapq.heappush(hq, (A[i]+B[j]+C[l])*-1)
            else:
                break

'''
hq = []
for i, j in itertools.product(A, B):
    heapq.heappush(hq, (i+j)*-1)

AB = []
while len(hq)>0 and len(AB)<=k:
    AB.append(heapq.heappop(hq)*-1)

hq = []
for i, j in itertools.product(AB, C):
    heapq.heappush(hq, (i+j)*-1)
'''

for _ in range(k):
    print(heapq.heappop(hq)*-1)

Submission status: Accepted

Solution code:
# D - Cake 123

import heapq
import itertools

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

A = list(int(a) for a in input().split())
B = list(int(b) for b in input().split())
C = list(int(c) for c in input().split())

hq = []
for i, j in itertools.product(A, B):
    heapq.heappush(hq, (i+j)*-1)

AB = []
while len(hq)>0 and len(AB)<=k:
    AB.append(heapq.heappop(hq)*-1)

hq = []
for i, j in itertools.product(AB, C):
    heapq.heappush(hq, (i+j)*-1)

for _ in range(k):
    print(heapq.heappop(hq)*-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()))

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

kouho=[]
for sm in ab:
    for tmp in c:
        kouho.append(sm+tmp)

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

Submission status: Accepted

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

X,Y,Z,K = LI()
A = LI()
B = LI()
C = LI()

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

h = A[0] + B[0] + C[0] +1
l = 0
while h > l+1:
    m = (h+l) // 2

    num = 0
    for a in A:
        j = len(C) - 1
        for i in B:
            while i + C[j] < m - a:
                j -= 1
                if j < 0: break
            else:
                num += j + 1
                continue
            break
    if num >= K:
        l = m
    else:
        h = m

ans = []
for a in A:
    j = len(C) - 1
    for i in B:

        while i + C[j] <= l - a:
            j -= 1
            if j < 0: break
        else:
            for k in range(j+1):
                ans.append(a+i+C[k])
            continue
        break

ans.sort(reverse=True)
for _ in range(K-len(ans)):
    ans.append(l)
print(*ans, sep='\n')

Submission status: Accepted

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

X,Y,Z,K = LI()
A = LI()
B = LI()
C = LI()

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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

    num = 0
    for a in A:
        j = len(C) - 1
        for i in B:
            while i + C[j] < m - a:
                j -= 1
                if j < 0: break
            else:
                num += j + 1
                continue
            break
    if num >= K:
        l = m
    else:
        h = m

ans = []
for a in A:
    j = len(C) - 1
    for i in B:

        while i + C[j] <= l - a:
            j -= 1
            if j < 0: break
        else:
            for k in range(j+1):
                ans.append(a+i+C[k])
            continue
        break

ans.sort(reverse=True)
for _ in range(K-len(ans)):
    ans.append(l)
print(*ans[:K], sep='\n')

Submission status: Accepted

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

X,Y,Z,K = LI()
A = LI()
B = LI()
C = LI()

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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

    num = 0
    for a in A:
        j = len(C) - 1
        for i in B:
            while i + C[j] < m - a:
                j -= 1
                if j < 0: break
            else:
                num += j + 1
                continue
            break
    if num > K:
        l = m
    else:
        h = m

ans = []
for a in A:
    j = len(C) - 1
    for i in B:

        while i + C[j] <= l - a:
            j -= 1
            if j < 0: break
        else:
            for k in range(j+1):
                ans.append(a+i+C[k])
            continue
        break

ans.sort(reverse=True)
for _ in range(K-len(ans)):
    ans.append(l)
print(*ans[:K], sep='\n')

Submission status: Accepted

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

X,Y,Z,K = LI()
A = LI()
B = LI()
C = LI()

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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

    num = 0
    for a in A:
        j = len(C) - 1
        for i in B:
            while i + C[j] < m - a:
                j -= 1
                if j < 0: break
            else:
                num += j + 1
                continue
            break
    if num > K:
        l = m
    else:
        h = m

ans = []
cnt = 0
for a in A:
    j = len(C) - 1
    last_cnt = cnt
    for i in B:

        while i + C[j] <= l - a:
            j -= 1
            if j < 0: break
        else:
            for k in range(j+1):
                ans.append(a+i+C[k])
                cnt += 1
            continue
        break
ans.sort(reverse=True)
for _ in range(K-len(ans)):
    ans.append(l)
print (*ans[:K],sep='\n')

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
from heapq import *
from collections import defaultdict
x, y, z, k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))

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

ans = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
heapify(ans)
log = defaultdict(lambda :0)

d = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
for i in range(k):
    temp, s, t, u = heappop(ans)
    temp = temp * (-1)
    print(temp)
    for ds, dt, du in d:
        ns = s + ds
        nt = t + dt
        nu = u + du
        if not(0 <= ns < x and 0 <= nt < y and 0 <= nu < z):continue
        if log[(ns, nt, nu)]:continue
        heappush(ans, [-(a[ns] + b[nt] + c[nu]), ns, nt, nu])
        log[(ns, nt, nu)] = 1

Submission status: Accepted

Solution code:
x, y, z, K = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse = True)
b = sorted(list(map(int, input().split())), reverse = True)
c = sorted(list(map(int, input().split())), reverse = True)
lst = []

for i in range(len(a)):
    for j in range(len(b)):
        for k in range(len(c)):
            if (i + 1) * (j + 1) * (k + 1) > K:
                break
            lst.append(a[i] + b[j] + c[k])

new_lst = sorted(lst, reverse = True)
for i in range(K):
    print(new_lst[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)

K=min(K,3000)

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

ABC=[ab+c for ab in AB[:K] for c in C[:K]]
ABC.sort(reverse=True)

for k in range(K):
	print(ABC[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()))
a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
ans = []
for s in range(x):
    for t in range(y):
        if (s + 1) * (t + 1) > k:
            break
        for u in range(z):
            if (s + 1) * (t + 1) * (u + 1) > k:
                break
            ans.append(a[s] + b[t] + c[u])
ans.sort(reverse = True)
for i in range(k):
    print(ans[i])

Submission status: Accepted

Solution code:
#template
def inputlist(): return [int(k) for k in input().split()]
#template
X,Y,Z,K = inputlist()
A = inputlist()
B = inputlist()
C = inputlist()
li = []
for i in range(X):
    for j in range(Y):
        li.append(A[i]+B[j])
li.sort(reverse=True)
lis = []
lit=[]
for i in range(min(X*Y,K)):
    lis.append(li[i])
for i in range(min(X*Y,K)):
    for j in range(Z):
        lit.append(lis[i]+C[j])
lit.sort(reverse=True)
for i in range(K):
    print(lit[i])

Submission status: Accepted

Solution code:
x,y,z,k=map(int,input().split())
a=sorted(list(map(int,input().split())),reverse=True)
b=sorted(list(map(int,input().split())),reverse=True)
c=sorted(list(map(int,input().split())),reverse=True)
l=[]
for i in range(x):
  if i+1>k:
    break
  for j in range(y):
    if (i+1)*(j+1)>k:
      break
    for h in range(z):
      if (i+1)*(j+1)*(h+1)>k:
        break
      l.append(a[i]+b[j]+c[h])
l=sorted(l,reverse=True)
for i in l[:k]:
  print(i)

Submission status: Accepted

Solution code:
# solution

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 p in range(min(k,len(a))):
  for q in range(min(k,len(b))):
    for r in range(min(k,len(c))):
      if((p+1)*(q+1)*(r+1) > k):
        break
      ans.append(a[p] + b[q] + c[r])
      
ans = sorted(ans, reverse = True)
for i in range(k):
  print(ans[i])

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

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

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

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

Submission status: Accepted

Solution code:
def main():
    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)
    
    anslist = []
    for ai, a in enumerate(A):
        for bi, b in enumerate(B):
            for ci, c in enumerate(C):
                if (ai+1)*(bi+1)*(ci+1)<=k:
                    anslist.append(a+b+c)
                else:
                    break    
    anslist.sort(reverse=True)
    for a in anslist[:k]:
        print(a)
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],0,0,0]]
heapq.heapify(Q)
ans=[]
ijk={(0,0,0)}
while len(ans)<K:
  abc,i,j,k=heapq.heappop(Q)
  if i+1<X and  not (i+1,j,k) in ijk:
    ijk.add((i+1,j,k))
    heapq.heappush(Q,[-A[i+1]-B[j]-C[k],i+1,j,k])
  if j+1<Y and  not (i,j+1,k) in ijk:
    ijk.add((i,j+1,k))
    heapq.heappush(Q,[-A[i]-B[j+1]-C[k],i,j+1,k])
  if k+1<Z and  not (i,j,k+1) in ijk:
    ijk.add((i,j,k+1))
    heapq.heappush(Q,[-A[i]-B[j]-C[k+1],i,j,k+1])
  ans.append(-abc)
for i in ans:
  print(i)

Submission status: Accepted

Solution code:
from bisect import bisect_left
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()))
bc=[]
for i in b:
  for j in c:
    bc.append(i+j)
bc.sort()
yz=y*z
ng=10**12
ok=1
while ng-ok>1:
  mid=(ok+ng)//2
  cnt=0
  for j in a:
    cnt+=yz-bisect_left(bc,mid-j)
  #print(cnt)
  if cnt>=k:
    ok=mid
  else:
    ng=mid
ans=[]
#print(ok)
for i in a:
  idx=bisect_left(bc,ok-i)
  for j in range(idx,yz):
    ans.append(i+bc[j])
ans.sort(reverse=True)
#print(len(ans))
for i in ans[:k]:
  print(i)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
from heapq import heapify, heappop, heappush

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 虫の目、一個ずつheapへ
X, Y, Z, K = lr()
A = lr(); A.sort(reverse=True)
B = lr(); B.sort(reverse=True)
C = lr(); C.sort(reverse=True)

ma = A[0] + B[0] + C[0]
heap = [(-ma, (0, 0, 0))]
used = set()
for _ in range(K):
    result, place = heappop(heap)
    print(-result)
    i, j, k = place
    p = (i+1, j, k)
    if i + 1 < len(A) and p not in used:
        val = A[i+1] + B[j] + C[k]
        used.add(p)
        heappush(heap, (-val, p))
    p = (i, j+1, k)
    if j + 1 < len(B) and p not in used:
        val = A[i] + B[j+1] + C[k]
        used.add(p)
        heappush(heap, (-val, p))
    p = (i, j, k+1)
    if k + 1 < len(C) and p not in used:
        val = A[i] + B[j] + C[k+1]
        used.add(p)
        heappush(heap, (-val, p))

# 26

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
from bisect import bisect_left, bisect_right
import itertools
from heapq import heapify, heappop, heappush

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# AとBは全探索、その後二分探索
X, Y, Z, K = lr()
A = lr(); A.sort()
B = lr(); B.sort()
C = lr(); C.sort()
D = []
for a, b in itertools.product(A, B):
    D.append(a+b)
D.sort()
if len(D) > K:
    D = D[-K:]

def check(x):
    length = len(D)
    count = 0
    for y in C:
        i = bisect_left(D, x-y)
        count += length - i
    return count >= K

ok = 0; ng = 10 ** 11
while abs(ng-ok) > 1:
    mid = (ok+ng) // 2
    if check(mid):
        ok = mid
    else:
        ng = mid

# ok以上の値
answer = []
for y in C:
    index = bisect_left(D, ok-y)
    for i in range(index, len(D)):
        heappush(answer, -(D[i]+y))

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

# 02

Submission status: Accepted

Solution code:
X,Y,Z,K=map(int,input().split())
A=sorted(list(map(int,input().split())),reverse=True)
B=sorted(list(map(int,input().split())),reverse=True)
C=sorted(list(map(int,input().split())),reverse=True)
ans=[]
for i in range(X):
  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
      ans.append(A[i]+B[j]+C[k])
ans=sorted(ans,reverse=True)
for a in ans[:K]:
  print(a)

Submission status: Accepted

Solution code:
def cake_gen(a,b,c,k):
    d=[]
    for i in a:
        for j in b:
            d.append(i+j)
    d.sort(reverse = True)
    d=d[:k]
    e=[]
    for i in c[:k]:
        for j in d:
            e.append(i+j)
    e.sort(reverse = True) 
    for i in range(k):
        print(e[i])           
    
def ma(): return map(int, input().split())
def main():
    x,y,z,k = ma()
    a = list(ma())
    b = list(ma())
    c = list(ma())
    a.sort(reverse = True)
    b.sort(reverse = True)
    c.sort(reverse = True)
    cake_gen(a,b,c,k)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
x,y,z,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
BC=[]
ABC=[]
for b in range(y):
    for c in range(z):
        BC.append(B[b]+C[c])
A.sort(reverse=True)
BC.sort(reverse=True)

for a in range(x):
    for bc in range(min(k,y*z)):
        ABC.append(A[a]+BC[bc])
ABC.sort(reverse=True)
for i in range(k):
    print(ABC[i])

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

x,y,z,k = map(int,input().split())
a_ls = list(map(int, input().split()))
b_ls = list(map(int, input().split()))
c_ls = list(map(int, input().split()))
a_ls.sort(reverse=True)
b_ls.sort(reverse = True)
c_ls.sort(reverse = True)

hq = []
arg_hash = {}
val = a_ls[0] + b_ls[0] + c_ls[0]
# 第二引数は、(その要素の値、要素)
heappush(hq, (-val, 0,0,0))

for i in range(k):
    ans, a,b,c = heappop(hq)
    print(-ans)

    arg_a = (a+1,b,c)
    arg_b = (a,b+1,c)
    arg_c = (a,b,c+1)

    if a < x - 1 and arg_a not in arg_hash:
        arg_hash[arg_a] = 1
        # リストの足し算みたいな感じ
        heappush(hq, (-(a_ls[a+1] + b_ls[b] + c_ls[c]),) + arg_a)
    if b < y - 1 and arg_b not in arg_hash:
        arg_hash[arg_b] = 1
        heappush(hq, (-(a_ls[a] + b_ls[b+1] + c_ls[c]),) + arg_b)
    if c < z - 1 and arg_c not in arg_hash:
        arg_hash[arg_c] = 1
        heappush(hq, (-(a_ls[a] + b_ls[b] + c_ls[c+1]),) + arg_c)

Submission status: Accepted

Solution code:
import itertools
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,b in itertools.product(A,B)]
AB.sort(reverse=True)
AB = AB[:K]
ABC = [ab + c for ab,c in itertools.product(AB,C)]
ABC.sort(reverse=True)
for i in range(K):
    print(ABC[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()))
# used = [[[False] * (z + 1) for _ in range(y + 1)] for _ in range(x + 1)]
used = set()
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
kouho = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]


def calc(a, b, c):
    if not (a, b, c) in used:
        used.add((a, b, c))
        heappush(kouho, ((-A[a] - B[b] - C[c]), a, b, c))


used.add((0, 0, 0))
for i in range(k):
    temp, p, q, r = heappop(kouho)
    print(-temp)
    if p + 1 < x:
        calc(p + 1, q, r)
    if q + 1 < y:
        calc(p, q + 1, r)
    if r + 1 < z:
        calc(p, q, r + 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()))
goukei = []
for a in A:
    for b in B:
        goukei.append(a + b)
goukei.sort()
goukei2 = goukei[-k:]
goukei3 = []
for c in C:
    for ab in goukei2:
        goukei3.append(ab + c)
goukei3.sort(reverse=True)
print(*goukei3[:k], sep="\n")

Submission status: Accepted

Solution code:
import heapq
import sys
x, y, z, k = map(int, input().split())

a, b, c = (sorted(map(int, sys.stdin.readline().split()), reverse=True) for _ in range(3))

abc = {(0,0,0)}

stu = [[-a[0]-b[0]-c[0], 0,0,0]]
heapq.heapify(stu)
result = []
while k > 0:
    out_p, n, m, l = heapq.heappop(stu)
    if n+1<x and not((n+1,m,l) in abc):
        abc.add((n+1,m,l))
        heapq.heappush(stu, [-a[n+1]-b[m]-c[l], n+1, m, l])
    if m+1<y and not((n,m+1,l) in abc):
        abc.add((n,m+1,l))
        heapq.heappush(stu, [-a[n]-b[m+1]-c[l], n, m+1, l])
    if l+1<z and not((n,m,l+1) in abc):
        abc.add((n,m,l+1))
        heapq.heappush(stu, [-a[n]-b[m]-c[l+1], n, m, l+1])
    result.append(-out_p)
    k -= 1
result.sort(reverse=True)
for i in result:
    print(i)

Submission status: Accepted

Solution code:
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


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

# A,Bの組み合わせを全探索し, 上位のみ切り出す
AB = [a+b for a in A for b in B]
AB.sort(reverse=True)
AB = AB[:min(3001,X*Y*Z+1)]

# 切り出したa+bの上位要素と, cで全探索する
ABC = [ab+c for ab in AB for c in C]
ABC.sort(reverse=True)

for i in range(K):
    print(ABC[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(" ")))
AB_que = []

for i in range(X):
    for j in range(Y):
        heapq.heappush(AB_que, A[i] + B[j])

while len(AB_que) > K:
    heapq.heappop(AB_que)

que = []
for ab in AB_que:
    for i in range(Z):
        heapq.heappush(que, -(ab + C[i]))

while K > 0:
    print(-heapq.heappop(que))
    K -= 1

Submission status: Accepted

Solution code:
import heapq

def main():
  X,Y,Z,K=map(int,input().split())
  A=list(reversed(sorted(map(int,input().split()))))
  B=list(reversed(sorted(map(int,input().split()))))
  C=list(reversed(sorted(map(int,input().split()))))
  q=[(-A[0]-B[0]-C[0],0,0,0)]
  queued={(0,0,0)}
  for i in range(K):
    v,s,t,u=heapq.heappop(q)
    print(-v)
    for e,f,g in [(s,t,u+1),(s,t+1,u),(s+1,t,u)]:
      if e>=X or f>=Y or g>=Z:
        continue
      if (e,f,g) in queued:
        continue
      heapq.heappush(q,(-(A[e]+B[f]+C[g]),e,f,g))
      queued.add((e,f,g))
      
if __name__=="__main__":
  main()

Submission status: Accepted

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

import heapq

def main():
    x, y, z, k = map(int, input().split())
    a = list(reversed(sorted(map(int, input().split()))))
    b = list(reversed(sorted(map(int, input().split()))))
    c = list(reversed(sorted(map(int, input().split()))))
    q = [(-a[0] - b[0] - c[0], 0, 0, 0)]
    queued = {(0, 0, 0)}
    for i in range(k):
        v, s, t, u = heapq.heappop(q)
        print(-v)
        for e, f, g in [(s, t, u + 1), (s, t + 1, u), (s + 1, t, u)]:
            if e >= x or f >= y or g >= z:
                continue
            if (e, f, g) in queued:
                continue
            heapq.heappush(q, (-(a[e] + b[f] + c[g]), e, f, g))
            queued.add((e, f, g))

if __name__ == "__main__":
    main()