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: Runtime Error

Solution code:
from collections import deque
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)
al = deque([])
bl = deque([])
cl = deque([])
for i in range(1, x): al.append(a[i-1]-a[i])
for i in range(1, y): bl.append(b[i-1]-b[i])
for i in range(1, z): cl.append(c[i-1]-c[i])
pop_a = al.popleft()
pop_b = bl.popleft()
pop_c = cl.popleft()
ans = a[0]+b[0]+c[0]
print(ans)
for i in range(1, k):
    if pop_a <= pop_b and pop_a <= pop_c:
        ans -= pop_a
        if al: pop_a = al.popleft()
        else: pop_a = float("INF")
    elif pop_b <= pop_a and pop_b <= pop_c:
        ans -= pop_b
        if bl: pop_b = bl.popleft()
        else: pop_a = float("INF")
    else:
        ans -= pop_c
        if cl: pop_c = cl.popleft() 
        else: pop_a = float("INF")
    print(ans)

Submission status: Runtime Error

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: Runtime Error

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)
import heapq
h=[]
chk=[0]*(X*Y*Z)
heapq.heappush(h,(-(A[0]+B[0]+C[0]),0,0,0))
for _ in range(K):
  ans,i,j,k=heapq.heappop(h)
  print(-ans)
  if i+1<X and chk[(i+1)*(Y*Z)+j*Z+k]==0:
    chk[(i+1)*(Y*Z)+j*Z+k]=1
    heapq.heappush(h,(-(A[i+1]+B[j]+C[k]),i+1,j,k))
  if j+1<Y and chk[i*(Y*Z)+(j+1)*Z+k]==0:
    chk[i*(Y*Z)+(j+1)*Z+k]=1
    heapq.heappush(h,(-(A[i]+B[j+1]+C[k]),i,j+1,k))
  if k+1<Z and chk[i*(Y*Z)+j*Z+k+1]==0:
    chk[i*(Y*Z)+j*Z+k+1]=1
    heapq.heappush(h,(-(A[i]+B[j]+C[k+1]),i,j,k+1))
  #print(h,chk)

Submission status: Runtime Error

Solution code:
X,Y,Z,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
if X>Y and X>Z:
  X,Z=Z,X
  A,C=C,A
elif Y>X and Y>Z:
  Y,Z=Z,Y
  B,C=C,Y
C=sorted(C,,reverse=True)
k=min(K,X*Y)
AB=sorted([a+b for a in A for b in B],reverse=True)[:k]
ABC=sorted([ab+c for ab in AB for c in C[:K]],reverse=True)[:K]
for x in ABC:
  print(x)

Submission status: Runtime Error

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

abc = []
heapq.heapify(abc)
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)

s, t, u = 0,0,0

if x>1:
    abc.append([a[0]-a[1], 0])
if y>1:
    abc.append([b[0]-b[1], 1])
if z>1:
    abc.append([c[0]-c[1], 2])
heapq.heapify(abc)
s, t, u = 0,0,0
while len(abc)>0 and s*t*u<3000:
    n, m = heapq.heappop(abc)
    if m == 0:
        s += 1
        if s < x:
            heapq.heappush(abc, [a[s-1]-a[s], 0])
    elif m == 1:
        t += 1
        if t < y:
            heapq.heappush(abc, [b[t-1]-b[t], 1])
    else:
        u += 1
        if u < z:
            heapq.heappush(abc, [c[u-1]-c[u], 2])
ABC = []
heapq.heapify(ABC)
for i in range(s):
    for j in range(t):
        for ij in range(u):
            heapq.heappush(ABC, -a[i]-b[j]-c[ij])
for _ in range(k):
    print(-heapq.heappop(ABC))

Submission status: Runtime Error

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

abc = []
heapq.heapify(abc)
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)

s, t, u = 0,0,0

if x>1:
    abc.append([a[0]-a[1], 0])
if y>1:
    abc.append([b[0]-b[1], 1])
if z>1:
    abc.append([c[0]-c[1], 2])
heapq.heapify(abc)
s, t, u = 0,0,0
while len(abc)>0 and s*t*u<k:
    n, m = heapq.heappop(abc)
    if m == 0:
        s += 1
        if s < x:
            heapq.heappush(abc, [a[s-1]-a[s], 0])
    elif m == 1:
        t += 1
        if t < y:
            heapq.heappush(abc, [b[t-1]-b[t], 1])
    else:
        u += 1
        if u < z:
            heapq.heappush(abc, [c[u-1]-c[u], 2])
ABC = []
heapq.heapify(ABC)
for i in range(s):
    for j in range(t):
        for ij in range(u):
            heapq.heappush(ABC, -a[i]-b[j]-c[ij])
for _ in range(k):
    print(-heapq.heappop(ABC))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
x,y,z,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
d = []
da = d.append
for i in range(x):
  for j in range(y):
    da(a[i]+b[j])
f = []
e = sorted(d,reverse=True)[:min(k,x*y)]
fa = f.append
for i in range(k):
  for j in range(z):
    fa(e[i]+c[j])
f.sort(reverse=True)
for i in range(k):
  print(f[i])

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
x,y,z,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
d = []
da = d.append
for i in range(x):
  for j in range(y):
    da(a[i]+b[j])
f = []
e = sorted(d,reverse=True)[:k]
fa = f.append
for i in range(k):
  for j in range(z):
    fa(e[i]+c[j])
f.sort(reverse=True)
for i in range(k):
  print(f[i])

Submission status: Runtime Error

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()))
taste = [0]*int(X*Y*Z)

for i in range(X):
  for j in range(Y):
    for k in range(Z):
      taste[i*Y*X + j*Y + k] = A[i]+B[j]+C[k]

taste = sorted(taste, reverse=True)

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

Submission status: Runtime Error

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()))
Rank = [0]*int(X*Y*Z)

for i in range(X):
  for j in range(Y):
    for k in range(Z):
      Rank[i*Y*X + j*Y + k] = A[i]+B[j]+C[k]

Rank = sorted(Rank, reverse=True)

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

Submission status: Runtime Error

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()))
Rank = [0]*(X*Y*Z)

for i in range(X):
  for j in range(Y):
    for k in range(Z):
      Rank[i*Y*X+j*Y+k] = A[i]+B[j]+C[k]

Rank = sorted(Rank)[::-1]

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

Submission status: Runtime Error

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

ans.sort(revere=True)
print(*ans[:K], sep='\n')

Submission status: Runtime Error

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

import heapq
import numpy

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 = numpy.zeros((na, nb, nc), dtype=numpy.int)
  used[0, 0, 0] = 1
  for _ in range(K):
    if len(scores) == 0:
      break
    (score, i, j, k) = heapq.heappop(scores)
    print(-score)
    if i + 1 < na and not used[i+1][j][k]:
      heapq.heappush(scores, (-(As[i+1]+Bs[j]+Cs[k]),   i+1, j,   k))
      used[i+1, j, k] = 1
    if j + 1 < nb and not used[i][j+1][k]:
      heapq.heappush(scores, (-(As[i]+Bs[j+1]+Cs[k]),   i,   j+1, k))
      used[i, j+1, k] = 1
    if k + 1 < nc and not used[i][j][k+1]:
      heapq.heappush(scores, (-(As[i]+Bs[j]+Cs[k+1]),   i,   j,   k+1))
      used[i, j, k+1] = 1

if __name__=='__main__':
  main()

Submission status: Runtime Error

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

import heapq
import numpy

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 = numpy.zeros((na, nb, nc), dtype=numpy.int)
  used[0, 0, 0] = 1
  for _ in range(K):
    (score, i, j, k) = heapq.heappop(scores)
    print(-score)
    if i + 1 < na and not used[i+1][j][k]:
      heapq.heappush(scores, (-(As[i+1]+Bs[j]+Cs[k]),   i+1, j,   k))
      used[i+1, j, k] = 1
    if j + 1 < nb and not used[i][j+1][k]:
      heapq.heappush(scores, (-(As[i]+Bs[j+1]+Cs[k]),   i,   j+1, k))
      used[i, j+1, k] = 1
    if k + 1 < nc and not used[i][j][k+1]:
      heapq.heappush(scores, (-(As[i]+Bs[j]+Cs[k+1]),   i,   j,   k+1))
      used[i, j, k+1] = 1

if __name__=='__main__':
  main()

Submission status: Runtime Error

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

import heapq
import numpy

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 = numpy.zeros((na, nb, nc), dtype=numpy.int)
  used[0, 0, 0] = 1
  for _ in range(k):
    (score, i, j, k) = heapq.heappop(scores)
    print(-score)
    if i + 1 < na and not used[i+1][j][k]:
      heapq.heappush(scores, (-(As[i+1]+Bs[j]+Cs[k]),   i+1, j,   k))
      used[i+1, j, k] = 1
    if j + 1 < nb and not used[i][j+1][k]:
      heapq.heappush(scores, (-(As[i]+Bs[j+1]+Cs[k]),   i,   j+1, k))
      used[i, j+1, k] = 1
    if k + 1 < nc and not used[i][j][k+1]:
      heapq.heappush(scores, (-(As[i]+Bs[j]+Cs[k+1]), i,   j,   k+1))
      used[i, j, k+1] = 1

if __name__=='__main__':
  main()

Submission status: Runtime Error

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:
            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: Runtime Error

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)

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: Runtime Error

Solution code:
import numpy as np
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()
ab = np.array(ab)
c = np.array(C)
l = -1
r = 3*10**10+1
M = X*Y*Z
for k in range(K):
  l = -1
  M = X*Y*Z
  while l+1<r:
    n = (l+r)//2
    d = n-c
    m = np.searchsorted(ab,d).sum()
    if M-m>k:
      l = n
    else:
      r = n
  print(l)
  r = l+1

Submission status: Runtime Error

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],j,0))
for i in range(K):
  m, j, cnt = heappop(ls)
  print(-m)
  if cnt<X*Y:
    heappush(ls,(-ab[cnt+1]-C[j],j,cnt+1))

Submission status: Runtime Error

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],j,0))
for i in range(K):
  m, j, cnt = heappop(ls)
  print(-m)
  if cnt<X*Y:
    heappush(ls,(-ab[cnt+1]-C[j],j,cnt+1))

Submission status: Runtime Error

Solution code:
import numpy as np
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()
ab = np.array(ab)
c = np.array(C)
l = -1
r = 10**30+1
M = X*Y*Z
for k in range(K):
  l = -1
  r = 10**30+1
  M = X*Y*Z
  while l+1<r:
    n = (l+r)//2
    d = n-c
    m = np.searchsorted(ab,d).sum()
    if M-m>k:
      l = n
    else:
      r = n
  print(l)

Submission status: Runtime Error

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

q = []
visited = set()

a = A[0]
for i,b in enumerate(B):
    for j,c in enumerate(C):
        if not (0,i,j) in visited:
            heappush(q, -(a+b+c))
            visited.add((0,i,j))

b = B[0]
for i,a in enumerate(A):
    for j,c in enumerate(C):
        if not (i,0,j) in visited:
            heappush(q, -(a+b+c))
            visited.add((i,0,j))

c = C[0]
for i,a in enumerate(A):
    for j,b in enumerate(B):
        if not (i,j,0) in visited:
            heappush(q, -(a+b+c))
            visited.add((i,j,0))

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

Submission status: Runtime Error

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[i+1] for i in range(len(temp)-1)[::-1]]
dif_c=[C[i]-C[i+1] for i in range(len(C)-1)[::-1]]

dif=dif[:K]

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

print(temp)

Submission status: Runtime Error

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[i+1] for i in range(len(temp)-1)[::-1]]
dif_c=[C[i]-C[i+1] for i in range(len(C)-1)[::-1]]

dif=dif[:K]

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

print(temp)
#TLEでそう

Submission status: Runtime Error

Solution code:
x,y,z,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
ab=[]
for i in range(x):
  for j in range(y):
    ab.append(a[i]+b[j])
ab.sort(reverse=True)
ab=ab[:k]
abc=[]
for i in range(k):
  for j in range(z):
    abc.append(ab[i]+c[j])
abc.sort(reverse=True)
for i in range(k):
  print(abc[i])

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    X, Y, Z, K = map(int, input().split())
    A, B, C = [sorted(map(int, input().split())) for _ in range(3)]

    AB = [0] * (X*Y)
    for i, a in enumerate(A):
        for j, b in enumerate(B):
            AB[i * Y + j] = a + b
    
    ans = [0] * (X*Y*Z)
    for i, ab in enumerate(AB):
        for j, c in enumerate(C):
            ans[i * Z + j] = ab + c
    ans = sorted(ans, reverse=True)[:K]
    for i in ans:
        print(i)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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)

ABC = []
for a in range(X):
    for b in range(Y):
        if (a + 1) + (b + 1) > K:
            break
        for c in range(Z):
            if (a + 1) + (b + 1) * (c + 1) > K:
                break
            ABC.append(A[a] + B[b] + C[c])

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

Submission status: Runtime Error

Solution code:
# coding: utf-8
#解法2 ヒープを使う
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)

used=[]

heap=[]

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

for i in range(K):
    u=heapq.heappop(heap)
    if not((u[1]+1,u[2],u[3]) in used):
        used.append((u[1]+1,u[2],u[3]))
        heapq.heappush(heap,(-(A[u[1]+1]+B[u[2]]+C[u[3]]),u[1]+1,u[2],u[3]))
        
    if not((u[1],u[2]+1,u[3]) in used):
        used.append((u[1],u[2]+1,u[3]))
        heapq.heappush(heap,(-(A[u[1]]+B[u[2]+1]+C[u[3]]),u[1],u[2]+1,u[3]))
        
    if not((u[1],u[2],u[3]+1) in used):
        used.append((u[1],u[2],u[3]+1))
        heapq.heappush(heap,(-(A[u[1]]+B[u[2]]+C[u[3]+1]),u[1],u[2],u[3]+1))
    print(-u[0])

Submission status: Runtime Error

Solution code:
# coding: utf-8
#解法2 ヒープを使う
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)

un_used=[[[True for i in range(Z)] for j in range(Y)] for k in range(X)]

heap=[]

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

un_used[0][0][0]=False

for i in range(K):
    u=heapq.heappop(heap)
    if un_used[u[1]+1][u[2]][u[3]]:
        un_used[u[1]+1][u[2]][u[3]]=False
        heapq.heappush(heap,(-(A[u[1]+1]+B[u[2]]+C[u[3]]),u[1]+1,u[2],u[3]))
        
    if un_used[u[1]][u[2]+1][u[3]]:
        un_used[u[1]][u[2]+1][u[3]]=False
        heapq.heappush(heap,(-(A[u[1]]+B[u[2]+1]+C[u[3]]),u[1],u[2]+1,u[3]))
        
    if un_used[u[1]][u[2]][u[3]+1]:
        un_used[u[1]][u[2]][u[3]+1]=False
        heapq.heappush(heap,(-(A[u[1]]+B[u[2]]+C[u[3]+1]),u[1],u[2],u[3]+1))
    print(-u[0])

Submission status: Runtime Error

Solution code:
# coding: utf-8
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)

INF=10**12

DA=[]
DB=[]
DC=[]

for i in range(X-1):
    DA.append(A[i]-A[i+1])
    
for i in range(Y-1):
    DB.append(B[i]-B[i+1])
    
for i in range(Z-1):
    DC.append(C[i]-C[i+1])

SA=[DA[0]]
SB=[DB[0]]
SC=[DC[0]]

for i in range(X-2):
    SA.append(SA[i]+DA[i+1])
    
for i in range(Y-2):
    SB.append(SB[i]+DB[i+1])
    
for i in range(Z-2):
    SC.append(SC[i]+DC[i+1])

SA.append(INF)
SB.append(INF)
SC.append(INF)

A_m=[0]
B_m=[0]
C_m=[0]

A_num=1
B_num=1
C_num=1

while A_num*B_num*C_num<K:
    if SA[0]==min(SA[0],SB[0],SC[0]):
        tmp = SA.pop(0)
        A_m.append(tmp)
        A_num+=1
        
    if SB[0]==min(SA[0],SB[0],SC[0]):
        tmp = SB.pop(0)
        B_m.append(tmp)
        B_num+=1
        
    if SC[0]==min(SA[0],SB[0],SC[0]):
        tmp = SC.pop(0)
        C_m.append(tmp)
        C_num+=1

max_val=A[0]+B[0]+C[0]
cake=[]

for i in range(A_num):
    for j in range(B_num):
        for k in range(C_num):
            cake.append(max_val - A_m[i] - B_m[j] - C_m[k])

cake.sort(reverse=True)

# print(DA,DB,DC)
# print(SA,SB,SC)
# print(A_m,B_m,C_m)

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

Submission status: Runtime Error

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[x]+B[y] for x in range(X) for y in range(Y)]
AB.sort(reverse=True)
AB = AB[:K]
ABC = [AB[k]+C[z] for k in range(K) for z in range(Z)]
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])

Submission status: Runtime Error

Solution code:
import heapq
x, y, z, k = map(int, input().split())
n = [x, y, z]
if x*y*z < k:
    k = x*y*z

abc = []
for _ in range(3):
    *l, = sorted(map(int, input().split()), reverse=True)
    abc.append(l)

h = [[-(abc[0][0]+abc[1][0]+abc[2][0]), [0, 0, 0]]]
heapq.heapify(h)

index = [0, 0, 0]
se = h[:]

for _ in range(k):
    ans, iterate = heapq.heappop(h)
    print(-ans)

    for i in range(3):
        if n[i] <= iterate[i] + 1: continue

        iterate[i] += 1

        s = -(abc[0][iterate[0]]+abc[1][iterate[1]]+abc[2][iterate[2]])
        t = [s, iterate[:]]

        if t not in se:
            heapq.heappush(h, t)
            se.append(t)

        iterate[i] -= 1

Submission status: Runtime Error

Solution code:
import heapq
x, y, z, k = map(int, input().split())
n = [x, y, z]
if x*y*z < k:
    k = x*y*z

abc = []
for _ in range(3):
    *l, = sorted(map(int, input().split()), reverse=True)
    abc.append(l)

h = [[-(abc[0][0]+abc[1][0]+abc[2][0]), [0, 0, 0]]]
heapq.heapify(h)

index = [0, 0, 0]
se = h[:]

for _ in range(k):
    ans, iter = heapq.heappop(h)
    print(-ans)

    for i in range(3):
        iter[i] += 1
        if iter[i] < n[i]:
            s = -(abc[0][iter[0]]+abc[1][iter[1]]+abc[2][iter[2]])
            t = [s, iter[:]]

            if t not in se:
                heapq.heappush(h, t)
                se.append(t)

        iter[i] -= 1

Submission status: Runtime Error

Solution code:
import heapq
x, y, z, k = map(int, input().split())
n = [x, y, z]
abc = []
for _ in range(3):
    *l, = sorted(map(int, input().split()), reverse=True)
    abc.append(l)

h = [[-(abc[0][0]+abc[1][0]+abc[2][0]), [0, 0, 0]]]
heapq.heapify(h)

index = [0, 0, 0]
se = h[:]

for _ in range(k):
    ans, iter = heapq.heappop(h)
    print(-ans)

    for i in range(3):
        iter[i] += 1
        if iter[i] < n[i]:
            s = -(abc[0][iter[0]]+abc[1][iter[1]]+abc[2][iter[2]])
            t = [s, iter[:]]

            if t not in se:
                heapq.heappush(h, t)
                se.append(t)

        iter[i] -= 1

Submission status: Runtime Error

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()))
d=[]
for i in range(x):
  for j in range(y):
    d.append(a[i]+b[j])
d.sort()
d=d[x*y-k:]
e=[]
for i in range(z):
  for j in range(k):
    e.append(c[i]+d[j])
e.sort()
e=e[z*k-k:]
for i in range(k)[::-1]:
  print(e[i])

Submission status: Runtime Error

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()))
d=[]
for i in range(x):
  for j in range(y):
    d.append(a[i]+b[j])
d.sort()
d=d[x*y-k:]
e=[]
for i in range(z):
  for j in range(k):
    e.append(c[i]+d[j])
e.sort()
for i in range(k)[::-1]:
  print(e[i])

Submission status: Runtime Error

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()))
d=[]
for i in range(x):
  for j in range(y):
    d.append(a[i]+b[j])
d.sort()
d=d[x*y-k:]
e=[]
for i in range(z):
  for j in range(k):
    e.append(c[z]+d[j])
e.sort()
for i in range(k)[::-1]:
  print(e[i])

Submission status: Runtime Error

Solution code:
import heapq
X,Y,Z,K=[int(s) for s in input().split()]
x=sorted([-int(s) for s in input().split()])
y=sorted([-int(s) for s in input().split()])
z=sorted([-int(s) for s in input().split()])
count=[[[0 for _ in range(X)]for _ in range(Y)]for _ in range(Z)]
count[0][0][0]=1
Q=[[x[0]+y[0]+z[0],0,0,0]]
heapq.heapify(Q)
for i in range(K):
  cake,a,b,c=heapq.heappop(Q)
  print(-cake)
  if count[a+1][b][c]==0:
    heapq.heappush(Q,[x[a+1]+y[b]+z[c],a+1,b,c])
    count[a+1][b][c]=1
  if count[a][b+1][c]==0:
    heapq.heappush(Q,[x[a]+y[b+1]+z[c],a,b+1,c])
    count[a][b+1][c]=1
  if count[a][b][c+1]==0:
    heapq.heappush(Q,[x[a]+y[b]+z[c+1],a,b,c+1])
    count[a][b][c+1]=1

Submission status: Runtime Error

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

ab = sorted(x+y for x in a for y in b)[:-k-1:-1]
abc = sorted(ab+c for ab in ab[:k] for c in c)[:-k-1:-1]

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

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

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

f1 =[]
for i in A:
  for j in B:
    f1.append( i+j )
f1.sort(reverse = True)

f2 = []
for i in f1[:min(3000,x*y*z+1)]:
  for j in C:
    f2.append(i+j)
f2.sort(reverse = True)

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

Submission status: Runtime Error

Solution code:
from itertools import product
from heapq import heappop, heappush

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)

if x * y * z <= 3000:
    lst = sorted([sum(i) for i in product(a,b,c)], reverse=True)
    [print(i) for i in lst[:k]]
    exit()

lst = []
idx = [0,0,0]
heap = []

xxx = [x,y,z]
aaa = idx[0] if idx[0]+1 >= x else idx[0] + 1
bbb = idx[1] if idx[1]+1 >= y else idx[1] + 1
ccc = idx[2] if idx[2]+1 >= z else idx[2] + 1
heappush(heap, [-([aaa] + b[idx[1]] + c[idx[2]]), 0])
heappush(heap, [-([idx[0]] + b[bbb] + c[idx[2]]), 1])
heappush(heap, [-([idx[0]] + b[idx[1]] + c[ccc]), 2])
print(a[idx[0]] + b[idx[1]] + c[idx[2]])
for _ in range(k-1):
    score, ix = heappop(heap)
    print(-score)
    print(idx)
    idx[ix] = idx[ix] if idx[ix] + 1 >= xxx[ix] else idx[ix] + 1
    score = ([idx[0]] + b[idx[1]] + c[idx[2]])
    heappush(heap, [-score, ix])

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

Submission status: Runtime Error

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()))
D = []
E = []
for i in range(x):
  for j in range(y):
    D.append(A[i]+B[j])
D.sort()
for i in range(z):
  for j in range(1, k+1):
    E.append(C[i]+D[-j])
print("\n".join(map(str,sorted(E)[::-1][:k])))

Submission status: Runtime Error

Solution code:
X,Y,Z,K=map(int,input().split())
A=sorted(list(map(int,input().split())))[::-1]
B=sorted(list(map(int,input().split())))[::-1]
C=sorted(list(map(int,input().split())))[::-1]
ans=[0]*(K**3)
ind=0
for a in range(len(A)):
  for b in range(len(B)):
    for c in range(len(C)):
      if (a+1)*(b+1)*(c+1)>K:
        break
      ans[ind]=A[a]+B[b]+C[c]
      ind+=1

ans=sorted(ans)[::-1]
for i in range(K):
  print(ans[i])

Submission status: Runtime Error

Solution code:
X,Y,Z,K=map(int,input().split())
A=sorted(list(map(int,input().split())))[::-1]
B=sorted(list(map(int,input().split())))[::-1]
C=sorted(list(map(int,input().split())))[::-1]
ans=[0]*(K**3)
ind=0
for a in range(len(A)):
  for b in range(len(B)):
    for c in range(len(C)):
      if (a+1)*(b+1)*(c+1)>K:
        break
      ans[ind]=A[a]+B[b]+C[c]
      ind+=1

ans=sorted(ans)[::-1]
for i in range(min(K,len(ans))):
  print(ans[i])