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:
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]*max((len(A)*len(B)*len(C)),K)
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])

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]*(len(A)*len(B)*len(C))
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:
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()))
flag = [[[True for _ in range(Z)] for _ in range(Y)] for _ in range(X)]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

ABC = []
heapq.heappush(ABC,(-1 * (A[0]+B[0]+C[0]),0,0,0))
flag[0][0][0] = False
ctr = 0
while ctr < K and ABC:
    s,a,b,c = heapq.heappop(ABC)
    print(-s)
    if flag[a+1][b][c]:
        heapq.heappush(ABC,(-1 * (A[a+1]+B[b]+C[c]),a+1,b,c))
        flag[a+1][b][c] = False
    
    if flag[a][b+1][c]:
        heapq.heappush(ABC,(-1 * (A[a]+B[b+1]+C[c]),a,b+1,c))
        flag[a][b+1][c] = False
    if flag[a][b][c+1]:
        heapq.heappush(ABC,(-1 * (A[a]+B[b]+C[c+1]),a,b,c+1))
        flag[a][b][c+1] = False
    ctr += 1

Submission status: Runtime Error

Solution code:
waaaaaaaaaaaaaaaaaaaaaannnnnnnnnn

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

Submission status: Runtime Error

Solution code:
import heapq

tmp = list(map(int, input().split()))
x = tmp[0]
y = tmp[1]
z = tmp[2]
k = tmp[3]
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)

#log = [[[False for _ in range(z)] for _ in range(y)] for _ in range(x)]
log = [False] * (x * y * z + 1)
h = []
heapq.heappush(h, (-(a[0]+b[0]+c[0]), 0, 0, 0))
for i in range(k): 
  if len(h) == 0:
    break
  hpop = heapq.heappop(h)
  print(-hpop[0])
  if hpop[1]+1 < x and log[(hpop[1]+1) * y * z + hpop[2] * z + hpop[3]] == False:
    heapq.heappush(h, (-(a[hpop[1]+1]+b[hpop[2]]+c[hpop[3]]), hpop[1]+1, hpop[2], hpop[3]))
    log[(hpop[1]+1) * y * z + hpop[2] * z + hpop[3]] = True
  if hpop[2]+1 < y and log[hpop[1] * y * z + (hpop[2]+1) * z + hpop[3]] == False:
    heapq.heappush(h, (-(a[hpop[1]]+b[hpop[2]+1]+c[hpop[3]]), hpop[1], hpop[2]+1, hpop[3]))
    log[hpop[1] * y * z + (hpop[2]+1) * z + hpop[3]] = True
  if hpop[3]+1 < z and log[hpop[1] * y * z + hpop[2] * z + hpop[3]+1] == False:
    heapq.heappush(h, (-(a[hpop[1]]+b[hpop[2]]+c[hpop[3]+1]), hpop[1], hpop[2], hpop[3]+1))
    log[hpop[1] * y * z + hpop[2] * z + hpop[3]+1] = True

Submission status: Runtime Error

Solution code:
import heapq

tmp = list(map(int, input().split()))
x = tmp[0]
y = tmp[1]
z = tmp[2]
k = tmp[3]
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)

#log = [[[False for _ in range(z)] for _ in range(y)] for _ in range(x)]
log = [False] * (x * y * z + 1)
h = []
heapq.heappush(h, (-(a[0]+b[0]+c[0]), 0, 0, 0))
for i in range(k): 
  if len(h) == 0:
    break
  tmp = heapq.heappop(h)
  print(-tmp[0])
  if tmp[1]+1 < x and log[(tmp[1]+1) * y * z + tmp[2] * z + tmp[3]] == False:
    heapq.heappush(h, (-(a[tmp[1]+1]+b[tmp[2]]+c[tmp[3]]), tmp[1]+1, tmp[2], tmp[3]))
    log[(tmp[1]+1) * y * z + tmp[2] * z + tmp[3]] = True
  if tmp[2]+1 < y and log[tmp[1] * y * z + (tmp[2]+1) * z + tmp[3]] == False:
    heapq.heappush(h, (-(a[tmp[1]]+b[tmp[2]+1]+c[tmp[3]]), tmp[1], tmp[2]+1, tmp[3]))
    log[tmp[1] * y * z + (tmp[2]+1) * z + tmp[3]] = True
  if tmp[3]+1 < z and log[tmp[1] * y * z + tmp[2] * z + tmp[3]+1] == False:
    heapq.heappush(h, (-(a[tmp[1]]+b[tmp[2]]+c[tmp[3]+1]), tmp[1], tmp[2], tmp[3]+1))
    log[tmp[1] * y * z + tmp[2] * z + tmp[3]+1] = True

Submission status: Runtime Error

Solution code:
import heapq

tmp = list(map(int, input().split()))
x = tmp[0]
y = tmp[1]
z = tmp[2]
k = tmp[3]
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)

#log = [[[False for _ in range(z)] for _ in range(y)] for _ in range(x)]
log = [False] * x * y * z
h = []
heapq.heappush(h, (-(a[0]+b[0]+c[0]), 0, 0, 0))
for i in range(k): 
  if len(h) == 0:
    break
  tmp = heapq.heappop(h)
  print(-tmp[0])
  if tmp[1]+1 < x and log[(tmp[1]+1) * y * z + tmp[2] * z + tmp[3]] == False:
    heapq.heappush(h, (-(a[tmp[1]+1]+b[tmp[2]]+c[tmp[3]]), tmp[1]+1, tmp[2], tmp[3]))
    log[(tmp[1]+1) * y * z + tmp[2] * z + tmp[3]] = True
  if tmp[2]+1 < y and log[tmp[1] * y * z + (tmp[2]+1) * z + tmp[3]] == False:
    heapq.heappush(h, (-(a[tmp[1]]+b[tmp[2]+1]+c[tmp[3]]), tmp[1], tmp[2]+1, tmp[3]))
    log[tmp[1] * y * z + (tmp[2]+1) * z + tmp[3]] = True
  if tmp[3]+1 < z and log[tmp[1] * y * z + tmp[2] * z + tmp[3]+1] == False:
    heapq.heappush(h, (-(a[tmp[1]]+b[tmp[2]]+c[tmp[3]+1]), tmp[1], tmp[2], tmp[3]+1))
    log[tmp[1] * y * z + tmp[2] * z + tmp[3]+1] = True

Submission status: Runtime Error

Solution code:
import heapq

tmp = list(map(int, input().split()))
x = tmp[0]
y = tmp[1]
z = tmp[2]
k = tmp[3]
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)

#log = [[[False for _ in range(z)] for _ in range(y)] for _ in range(x)]
log = [False] * x * y * z
h = []
heapq.heappush(h, (-(a[0]+b[0]+c[0]), 0, 0, 0))
for i in range(k): 
  tmp = heapq.heappop(h)
  print(-tmp[0])
  if tmp[1]+1 < x and log[(tmp[1]+1) * y * z + tmp[2] * z + tmp[3]] == False:
    heapq.heappush(h, (-(a[tmp[1]+1]+b[tmp[2]]+c[tmp[3]]), tmp[1]+1, tmp[2], tmp[3]))
    log[(tmp[1]+1) * y * z + tmp[2] * z + tmp[3]] = True
  if tmp[2]+1 < y and log[tmp[1] * y * z + (tmp[2]+1) * z + tmp[3]] == False:
    heapq.heappush(h, (-(a[tmp[1]]+b[tmp[2]+1]+c[tmp[3]]), tmp[1], tmp[2]+1, tmp[3]))
    log[tmp[1] * y * z + (tmp[2]+1) * z + tmp[3]] = True
  if tmp[3]+1 < z and log[tmp[1] * y * z + tmp[2] * z + tmp[3]+1] == False:
    heapq.heappush(h, (-(a[tmp[1]]+b[tmp[2]]+c[tmp[3]+1]), tmp[1], tmp[2], tmp[3]+1))
    log[tmp[1] * y * z + tmp[2] * z + tmp[3]+1] = True

Submission status: Runtime Error

Solution code:
tmp = list(map(int, input().split()))
x = tmp[0]
y = tmp[1]
z = tmp[2]
k = tmp[3]
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)

log = [[[False for _ in range(z)] for _ in range(y)] for _ in range(x)]

cnt = 0
h = []
heapq.heappush(h, (-(a[0]+b[0]+c[0]), 0, 0, 0))
while len(h) != 0 and cnt < k: 
  tmp = heapq.heappop(h)
  print(-tmp[0])
  cnt += 1
  if tmp[1]+1 < x and log[tmp[1]+1][tmp[2]][tmp[3]] == False:
    heapq.heappush(h, (-(a[tmp[1]+1]+b[tmp[2]]+c[tmp[3]]), tmp[1]+1, tmp[2], tmp[3]))
    log[tmp[1]+1][tmp[2]][tmp[3]] = True
  if tmp[2]+1 < y and log[tmp[1]][tmp[2]+1][tmp[3]] == False:
    heapq.heappush(h, (-(a[tmp[1]]+b[tmp[2]+1]+c[tmp[3]]), tmp[1], tmp[2]+1, tmp[3]))
    log[tmp[1]][tmp[2]+1][tmp[3]] = True
  if tmp[3]+1 < z and log[tmp[1]][tmp[2]][tmp[3]+1] == False:
    heapq.heappush(h, (-(a[tmp[1]]+b[tmp[2]]+c[tmp[3]+1]), tmp[1], tmp[2], tmp[3]+1))
    log[tmp[1]][tmp[2]][tmp[3]+1] = True

Submission status: Runtime Error

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

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

ab_1 = []
for i in a:
    for j in b:
        heapq.heappush(ab_1, -i-j)
key = min([k, x*y*z])
ab_2 = []
for i in range(key):
    value = heapq.heappop(ab_1)
    ab_2.append(-value)

abc = []
for p in ab_2:
    for q in c:
        heapq.heappush(abc, -p-q)
for i in range(key):
    value = heapq.heappop(abc)
    print(-value)

Submission status: Runtime Error

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

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

ab_1 = []
for i in range(x):
    for j in range(y):
        ab.append(a[i]*b[j])
ab_1.sort()   
key = min([k, x*y*z])
ab_2 = ab_1[:key]

abc = []
for i in range(key):
    for j in range(z):
        abc.append(ab_2[i]*c[j])
abc.sort()
for i in range(key):
    print(abc[i])

Submission status: Runtime Error

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

al = list(map(int,input().split()))
bl = list(map(int,input().split()))
cl = list(map(int,input().split()))

al = sorted(al)
bl = sorted(bl)
cl = sorted(cl)
rank = []
for xx in range(x-1,-1,-1):
    tmp = al[xx]+bl[-1]+cl[-1]
    if rank[-1] >= tmp:
        break
    for yy in range(y-1, -1, -1):
        tmp = al[xx]+bl[yy]+cl[-1]
        if rank[-1] >= tmp:
            break
        for zz in range(z-1,-1,-1):
            if len(rank) < k:
                rank.append(al[xx]+bl[yy]+cl[zz])
                rank = sorted(rank, reverse=True)
            else:
                tmp = al[xx]+bl[yy]+cl[zz]
                if rank[-1] < tmp:
                    rank.pop()
                    rank.append(tmp)
                    rank = sorted(rank, reverse=True)
                else:
                    break

for i in rank:
    print(i)

Submission status: Runtime Error

Solution code:
x,y,z,K=map(int,input().split())
listsa=list(map(int,input().split()))#長さはｘ
listsb=list(map(int,input().split()))#長さはy
listsc=list(map(int,input().split()))#長さはz
if x*y>3000:
    suballlist=[]
    for i in lista:
        for j in listsb:
            suballlist.append(i+j)
    suballlist=sorted(suballlist,reverse=True)
    uselist=suballlist[:3000]
    alllist=[]
    for i in uselist:
        for j in listsc:
            alllist.append(i+j)
    alllist=sorted(alllist,reverse=True)
    for i in range(K):
        print(alllist[i])
    
elif x*y<=3000:
    alllist=[]
    for i in listsa:
        for j in listsb:
            for k in listsc:
                alllist.append(i+j+k)
                
    alllist=sorted(alllist,reverse=True)
    for i in range(K):
        print(alllist[i])

Submission status: Runtime Error

Solution code:
import numpy as np
from heapq import *

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

# 二分探索でK個の選び方を探す
up = 10 ** 10 + 1 # 不可能
low = 0 # 可能
A_max = A.max()
B_max = B.max()
C_max = C.max()
while up > low + 1:
    mid = (up+low)// 2
    if (A >= A_max - mid).sum() * (B >= B_max - mid).sum() * (C >= C_max - mid).sum() >= K:
        low = mid
    else:
        up = mid
A_line = A_max - low
B_line = B_max - low
C_line = C_max - low
A = [x for x in A if x >= A_line]
B = [x for x in B if x >= B_line]
C = [x for x in C if x >= C_line]
L = []
heapify(L)
for a in A:
    for b in B:
        for c in C:
            heappush(L, -(a+b+c))
for i in range(K):
    n = heappop(L)
    print(-n)

Submission status: Runtime Error

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

import numpy as np
from heapq import *

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

# 二分探索でK個の選び方を探す
up = 10 ** 10 + 1 # 不可能
low = 0 # 可能
while up > low + 1:
    mid = (up+low)// 2
    if (A >= A.max() - mid).sum() * (B >= B.max() - mid).sum() * (C >= C.max() - mid).sum() >= K:
        low = mid
    else:
        up = mid
A_line = A.max() - low
B_line = B.max() - low
C_line = C.max() - low
A = [x for x in A if x >= A_line]
B = [x for x in B if x >= B_line]
C = [x for x in C if x >= C_line]
L = []
heapify(L)
for a in A:
    for b in B:
        for c in C:
            heappush(L, -(a+b+c))
for i in range(K):
    n = heappop(L)
    print(-n)

Submission status: Runtime Error

Solution code:
import numpy as np
from heapq import *

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

# 二分探索でK個の選び方を探す
up = 10 ** 10 + 1 # 不可能
low = 0 # 可能
while up > low + 1:
    mid = (up+low)// 2
    if (A >= A.max() - mid).sum() * (B >= B.max() - mid).sum() * (C >= C.max() - mid).sum() >= K:
        low = mid
    else:
        up = mid
A = [x for x in A if x >= A.max() - low]
B = [x for x in B if x >= B.max() - low]
C = [x for x in C if x >= C.max() - low]
L = []
heapify(L)
for a in A:
    for b in B:
        for c in C:
            heappush(L, -(a+b+c))
for i in range(K):
    n = heappop(L)
    print(-n)

Submission status: Runtime Error

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

A = sorted(list(map(int, input().split())), reverse=True)[:K]
B = sorted(list(map(int, input().split())), reverse=True)[:K]
C = sorted(list(map(int, input().split())), reverse=True)[:K]

R  = [sum([A[0], B[0], C[0]])]
Q  = {(0, 0, 1), (0, 1, 0), (1, 0, 0)}
D  = {(0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0)}
iS = [((a, b, c), sum([A[a], B[b], C[c]])) for a, b, c in Q]

while len(R) < K:
    iS = sorted(iS, key=lambda x: [x[1], x[0]], reverse=True)
    R.append(iS[0][1])
    abc     = iS[0][0]
    a, b, c = abc[0], abc[1], abc[2]
    iS      = iS[1:]
    if a + 1 < X:
        if not (a + 1, b, c) in D:
            iS.append(((a + 1, b, c), sum([A[a + 1], B[b], C[c]])))
            D.add(     (a + 1, b, c))
    if b + 1 < Y:
        if not (a, b + 1, c) in D:
            iS.append(((a, b + 1, c), sum([A[a], B[b + 1], C[c]])))
            D.add(    (a, b + 1, c))
    if c + 1 < Z:
        if not (a, b, c + 1) in D:
            iS.append(((a, b, c + 1), sum([A[a], B[b], C[c + 1]])))
            D.add(     (a, b, c + 1))

for r in R:
    print(r)

Submission status: Runtime Error

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

A = sorted(list(map(int, input().split())), reverse=True)[:K]
B = sorted(list(map(int, input().split())), reverse=True)[:K]
C = sorted(list(map(int, input().split())), reverse=True)[:K]

R  = [sum([A[0], B[0], C[0]])]
Q  = {(0, 0, 1), (0, 1, 0), (1, 0, 0)}
D  = {(0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0)}
iS = [((a, b, c), sum([A[a], B[b], C[c]])) for a, b, c in Q]

while len(R) < K:
    iS = sorted(iS, key=lambda x: [x[1], x[0]], reverse=True)
    R.append(iS[0][1])
    abc = iS[0][0]
    del iS[0]
    if abc[0] + 1 < X:
        a, b, c = abc[0] + 1, abc[1], abc[2]
        if not (a, b, c) in D:
            iS.append(((a, b, c), sum([A[a], B[b], C[c]])))
            D.add(  (a, b, c))
    if abc[1] + 1 < Y:
        a, b, c = abc[0], abc[1] + 1, abc[2]
        if not (a, b, c) in D:
            iS.append(((a, b, c), sum([A[a], B[b], C[c]])))
            D.add(  (a, b, c))
    if abc[2] + 1 < Z:
        a, b, c = abc[0], abc[1], abc[2] + 1
        if not (a, b, c) in D:
            iS.append(((a, b, c), sum([A[a], B[b], C[c]])))
            D.add(  (a, b, c))

for r in R:
    print(r)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
  int x, y, z, k; cin >> x >> y >> z >> k;
  vector<ll> a(x),b(y),c(z);

  for(ll i = 0; i < x; i++){
    cin >> a[i];
  }
  for(ll i = 0; i < y; i++){
    cin >> b[i];
  }
  for(ll i = 0; i < z; i++){
    cin >> c[i];
  }
  
  sort(a.begin(), a.end(), greater<ll>());
  sort(b.begin(), b.end(), greater<ll>());
  
  vector<ll> apb(x*y);
  for(ll i = 0; i < x; i++){
    for(ll j = 0; j < y; j++){
      apb[i*y + j] = a[i] + b[j];
    }
  }
  sort(apb.begin(), apb.end(), greater<ll>());
  ll minkxy = min(x * y, k);
  
  vector<ll> ans(minkxy * z);
  for(ll i = 0; i < minkxy; i++){
    for(ll j = 0; j < z; j++){
      ans[i*z + j] = apb[i] + c[j];
    }
  }
  
  sort(ans.begin(), ans.end(), greater<ll>());
  
  for(ll i = 0; i < k; i++){
    cout << ans[i] << endl;
  }
  
}

Submission status: Runtime Error

Solution code:
import heapq

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

Q = []
ia,ib,ic = 0,0,0
plus_i = 1
heapq.heappush(Q, [-(A[ia]+B[ib]+C[ic]),ia,ib,ic])

for _ in range(K):
  if ia+plus_i < X and [-(A[ia+plus_i]+B[ib]+C[ic]),ia+plus_i,ib,ic] not in Q:heapq.heappush(Q, [-(A[ia+plus_i]+B[ib]+C[ic]),ia+plus_i,ib,ic])
  if ib+plus_i < Y and [-(A[ia]+B[ib+plus_i]+C[ic]),ia,ib+plus_i,ic] not in Q:heapq.heappush(Q, [-(A[ia]+B[ib+plus_i]+C[ic]),ia,ib+plus_i,ic])
  if ic+plus_i < Z and[-(A[ia]+B[ib]+C[ic+plus_i]),ia,ib,ic+plus_i] not in Q:heapq.heappush(Q, [-(A[ia]+B[ib]+C[ic+plus_i]),ia,ib,ic+plus_i])
  plus_i += 1
    
[print(-heapq.heappop(Q)[0]) for _ in range(K)]

Submission status: Runtime Error

Solution code:
import collections
import heapq

x, y, z, k = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(3)]
for i in range(3):
    ls[i].sort(reverse=True)
q = [(-sum([ls[i][0] for i in range(3)]), 0, 0, 0)]
res = []
used = collections.defaultdict(lambda: False)
while len(res) < k:
    s, i, j, l = heapq.heappop(q)
    res.append(-s)
    index_list = [
        (i + 1, j, l),
        (i, j + 1, l),
        (i, j, l + 1),
    ]
    for i, (indices, limit) in enumerate(zip(index_list, [x, y, z])):
        if indices[i] < limit and not used[indices]:
            ss = sum([ls[j][l] for j, l in zip(range(3), indices)])
            heapq.heappush(q, (-ss, *indices))
            used[indices] = True
print(*res, sep='\n')

Submission status: Runtime Error

Solution code:
def f_d():
    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)

    m, o = [[[0]*z for _ in range(y)] for _ in range(x)], []
    q = [[-1*(a[0]+b[0]+c[0]), 0, 0, 0]]
    heapq.heapify(q)
    for _ in range(k):
        tmp = heapq.heappop(q)
        o.append(-1*tmp[0])
        ai, bi, ci = tmp[1], tmp[2], tmp[3]
        if m[min(ai+1, len(a)-1)][bi][ci]==0:
            m[ai+1][bi][ci] = 1
            heapq.heappush(q, [-1*(a[ai+1]+b[bi]+c[ci]), ai+1, bi, ci])
        if m[ai][min(bi+1, len(b)-1)][ci]==0:
            m[ai][bi+1][ci] = 1
            heapq.heappush(q, [-1*(a[ai]+b[bi+1]+c[ci]), ai, bi+1, ci])
        if m[ai][bi][min(ci+1, len(c)-1)]==0:
            m[ai][bi][ci+1] = 1
            heapq.heappush(q, [-1*(a[ai]+b[bi]+c[ci+1]), ai, bi, ci+1])
    for i in range(k):
        print(o[i])

if __name__ == "__main__":
    f_d()

Submission status: Runtime Error

Solution code:
import heapq
x,y,z,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
c=[int(i) for i in input().split()]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

li=[]
push=heapq.heappush
pop=heapq.heappop

push(li,(-a[0]-b[0]-c[0],0,0,0))
ans=[]
used=set()

for _ in range(k):
    value,i,j,l=pop(li)
    ans.append(-value)
    if i<x-1 and (i+1,j,l) not in used:
        push(li,(-a[i+1]-b[j]-c[l],i+1,j,l))
        used.add((i+1,j,l))
    if j<y-1 and (i,j+1,l) not in used:
        push(li,(-a[i]-b[j+1]-c[l],i,j+1,l))
        used.add((i,j+1,l))
    if k<z-1 and (i,j,l+1) not in used:
        push(li,(-a[i]-b[j]-c[l+1],i,j,l+1))
        used.add((i,j,l+1))
for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
import heapq
x,y,z,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
c=[int(i) for i in input().split()]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

li=[]
push=heapq.heappush
pop=heapq.heappop

push(li,(-a[0]-b[0]-c[0],0,0,0))
ans=[]
used=set()

for _ in range(k):
    value,i,j,l=pop(li)
    ans.append(-value)
    if i<x-1 and (i+1,j,l) not in used:
        push(li,(-a[i+1]-b[j]-c[l],i+i,j,l))
        used.add((i+1,j,l))
    if j<y-1 and (i,j+1,l) not in used:
        push(li,(-a[i]-b[j+1]-c[l],i,j+1,l))
        used.add((i,j+1,l))
    if k<z-1 and (i,j,l+1) not in used:
        push(li,(-a[i]-b[j]-c[l+1],i,j,l+1))
        used.add((i,j,l+1))
for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
from collections import deque
x,y,z,k=map(int,input().split())
l1=sorted(list(map(int,input().split())))
l2=sorted(list(map(int,input().split())))
l3=sorted(list(map(int,input().split())))
l4=[]
l5=deque([])
for i in range(x):
  for j in range(y):
    l4.append(l1[i]+l2[j])
l4=sorted(l4)
for i in range(x,x-k,-1):
  for j in range(z):
    l5.append(l4[i]+l3[j])
l5=sorted(l5,reverse=True)
for _ in range(k):
  print(l5.pop(0))

Submission status: Runtime Error

Solution code:
from bisect import bisect_left as bl
from collections import deque as dq
X, Y, Z, K = map(int, input().split())
ABC = [sorted(list(map(int, input().split()))) for _ in range(3)]
AB = []
for i in range(X):
  for j in range(Y):
    AB.append(ABC[0][i] + ABC[1][j])
AB.sort(reverse = True)
res = []
for i in range(K):
  for j in range(Z):
    res.append(AB[i] + ABC[2][j])
res.sort(reverse = True)
for i in range(K):
  print(res[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()))

ans1=[]
ans2=[]

for i in range(x):
    for j in range(y):
        ans1.append(a[i]+b[j])
ans1.sort()
ans1.reverse()

if len(ans1)>=k:
    del ans1[k:]
    
for i in range(k):
    for j in range(z):
        ans2.append(ans1[i]+c[j])
ans2.sort()
ans2.reverse()

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

Submission status: Runtime Error

Solution code:
from bisect import bisect_left as bl
from collections import deque as dq
X, Y, Z, K = map(int, input().split())
ABC = [sorted(list(map(int, input().split()))) for _ in range(3)]
temp = []
minabc = bl((X, Y, Z), min(X, Y, Z))
abcs = [i for i in range(3) if i != minabc]
for i in range((X, Y, Z)[abcs[0]]):
  for j in range((X, Y, Z)[abcs[1]]):
    temp.append(ABC[abcs[0]][i] + ABC[abcs[1]][j])
temp.sort()
r = len(temp) - 1
u = 0
Q = dq([])
Q.append([r, u])
res = []
visited = [[0] * len(temp) for _ in range((X, Y, Z)[minabc])]
while len(Q):
  q = Q.popleft()
  res.append(temp[q[0]] + ABC[minabc][q[1]])
  if r - q[0] + q[1] > K:
    continue
  if q[0] != 0 and visited[q[1]][q[0] - 1] == 0:
    Q.append([q[0] - 1, q[1]])
    visited[q[1]][q[0] - 1] = 1
  if q[1] != (X, Y, Z)[minabc] - 1 and visited[q[1] + 1][q[0] - 1] == 0:
    Q.append([q[0], q[1] + 1])
    visited[q[1] + 1][q[0]] = 1
res.sort()
for _ in range(K):
  print(res.pop())

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(len(a)):
    for j in range(len(b)):
        ab.append(a[i]+b[j])
ab.sort(reverse = True)
abc = []
k_1 = min([len(ab), k])
for i in range(k_1):
    for j in range(len(c)):
        abc.append(ab[i]+c[j])
abc.sorat(reverse = True)
for i in range(k):
    print(abc[i])

Submission status: Runtime Error

Solution code:
import heapq
import sys
sys.setrecursionlimit(1000000)
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 = []
ans = []
flag = []
first = [A[0] + B[0] + C[0], [0, 0, 0]]
heapq.heappush(q, first)
flag.append(first[1])
for h in range(k):
    max_ans = heapq.heappop(q)
    ans.append(abs(max_ans[0]))
    i, j, k = max_ans[1]
    if i == len(A)-1:
        i = i-1
    if j == len(B)-1:
        j = j-1
    if k == len(C)-1:
        k = k-1
    if i == len(A)-1 and j == len(B)-1 and k == len(C)-1:
      	print(ans[h])
      	exit()

    lst1 =  [-A[i+1]-B[j]-C[k], [i+1, j, k]]
    if lst1[1] not in flag:
        heapq.heappush(q, lst1)
        flag.append(lst1[1])

    lst2 = [-A[i]-B[j+1]-C[k], [i, j+1, k]]
    if lst2[1] not in flag: 
        heapq.heappush(q, lst2)
        flag.append(lst2[1])

    lst3 = [-A[i]-B[j]-C[k+1], [i, j, k+1]]
    if lst3[1] not in flag:
        heapq.heappush(q, lst3)
        flag.append(lst3[1])
    print(ans[h])

Submission status: Runtime Error

Solution code:
import heapq
import sys
sys.setrecursionlimit(100000000)
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 = []
ans = []
flag = []
first = [A[0] + B[0] + C[0], [0, 0, 0]]
heapq.heappush(q, first)
flag.append(first[1])
for h in range(k):
    max_ans = heapq.heappop(q)
    ans.append(abs(max_ans[0]))
    i, j, k = max_ans[1]
    if i == len(A)-1:
        i = i-1
    if j == len(B)-1:
        j = j-1
    if k == len(C)-1:
        k = k-1

    lst1 =  [-A[i+1]-B[j]-C[k], [i+1, j, k]]
    if lst1[1] not in flag:
        heapq.heappush(q, lst1)
        flag.append(lst1[1])

    lst2 = [-A[i]-B[j+1]-C[k], [i, j+1, k]]
    if lst2[1] not in flag: 
        heapq.heappush(q, lst2)
        flag.append(lst2[1])

    lst3 = [-A[i]-B[j]-C[k+1], [i, j, k+1]]
    if lst3[1] not in flag:
        heapq.heappush(q, lst3)
        flag.append(lst3[1])
    print(ans[h])

Submission status: Runtime Error

Solution code:
import heapq
import sys
sys.setrecursionlimit(1000000)
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 = []
ans = []
flag = []
first = [A[0] + B[0] + C[0], [0, 0, 0]]
heapq.heappush(q, first)
flag.append(first[1])
for h in range(k):
    max_ans = heapq.heappop(q)
    ans.append(abs(max_ans[0]))
    i, j, k = max_ans[1]
    if i == len(A)-1:
        i = i-1
    if j == len(B)-1:
        j = j-1
    if k == len(C)-1:
        k = k-1

    lst1 =  [-A[i+1]-B[j]-C[k], [i+1, j, k]]
    if lst1[1] not in flag:
        heapq.heappush(q, lst1)
        flag.append(lst1[1])

    lst2 = [-A[i]-B[j+1]-C[k], [i, j+1, k]]
    if lst2[1] not in flag: 
        heapq.heappush(q, lst2)
        flag.append(lst2[1])

    lst3 = [-A[i]-B[j]-C[k+1], [i, j, k+1]]
    if lst3[1] not in flag:
        heapq.heappush(q, lst3)
        flag.append(lst3[1])
    print(ans[h])

Submission status: Runtime Error

Solution code:
import heapq
sys.setrecursionlimit(1000000)
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 = []
ans = []
flag = []
first = [A[0] + B[0] + C[0], [0, 0, 0]]
heapq.heappush(q, first)
flag.append(first[1])
for h in range(k):
    max_ans = heapq.heappop(q)
    ans.append(abs(max_ans[0]))
    i, j, k = max_ans[1]
    if i == len(A)-1:
        i = i-1
    if j == len(B)-1:
        j = j-1
    if k == len(C)-1:
        k = k-1

    lst1 =  [-A[i+1]-B[j]-C[k], [i+1, j, k]]
    if lst1[1] not in flag:
        heapq.heappush(q, lst1)
        flag.append(lst1[1])

    lst2 = [-A[i]-B[j+1]-C[k], [i, j+1, k]]
    if lst2[1] not in flag: 
        heapq.heappush(q, lst2)
        flag.append(lst2[1])

    lst3 = [-A[i]-B[j]-C[k+1], [i, j, k+1]]
    if lst3[1] not in flag:
        heapq.heappush(q, lst3)
        flag.append(lst3[1])
    print(ans[h])