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: Wrong Answer

Solution code:
# D - Cake 123

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)

d = []
heapq.heappush(d, (-(A[0]+B[0]+C[0]), 0, 0, 0)) # heapqにタプルを与えると最初の要素でソートする
ijk_set = set(str(000))

for _ in range(K):
    tmp, i, j, k = heapq.heappop(d)
    print(-tmp)
    if str(i+1)+str(j)+str(k) not in ijk_set and i+1 <= X-1:
        heapq.heappush(d, (-(A[i+1]+B[j]+C[k]), i+1, j, k))
        ijk_set.add(str(i+1)+str(j)+str(k))
    if str(i)+str(j+1)+str(k) not in ijk_set and j+1 <= Y-1:
        heapq.heappush(d, (-(A[i]+B[j+1]+C[k]), i, j+1, k))
        ijk_set.add(str(i)+str(j+1)+str(k))
    if str(i)+str(j)+str(k+1) not in ijk_set and k+1 <= Z-1:
        heapq.heappush(d, (-(A[i]+B[j]+C[k+1]), i, j, k+1))
        ijk_set.add(str(i)+str(j)+str(k+1))

Submission status: Wrong Answer

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 = -1 # 不可能
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:
        up = mid
    else:
        low = mid
A_line = A_max - up
B_line = B_max - up
C_line = C_max - up

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: Wrong Answer

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 = -1 # 不可能
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:
        up = mid
    else:
        low = mid
A = [x for x in A if x >= A.max() - up]
B = [x for x in B if x >= B.max() - up]
C = [x for x in C if x >= C.max() - up]
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: Wrong Answer

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 if (a < X) and (b < Y) and (c < Z)]

while len(R) < K:
    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:
            s = sum([A[a + 1], B[b], C[c]])
            for i in range(len(iS)):
                if iS[i][1] <= s:
                    break
            iS = iS[:i+1] + [((a + 1, b, c), s)] + iS[i+1:]
            D.add((a + 1, b, c))
    if b + 1 < Y:
        if not (a, b + 1, c) in D:
            s = sum([A[a], B[b + 1], C[c]])
            for i in range(len(iS)):
                if iS[i][1] <= s:
                    break
            iS = iS[:i+1] + [((a, b + 1, c), s)] + iS[i+1:]
            D.add((a, b + 1, c))
    if c + 1 < Z:
        if not (a, b, c + 1) in D:
            s = sum([A[a], B[b], C[c + 1]])
            for i in range(len(iS)):
                if iS[i][1] <= s:
                    break
            iS = iS[:i+1] + [((a, b, c + 1), s)] + iS[i+1:]
            D.add((a, b, c + 1))

for r in R:
    print(r)

Submission status: Wrong Answer

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 if (a < X) and (b < Y) and (c < Z)]

while len(R) < K:
    # TLE check
    # 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: Wrong Answer

Solution code:
import itertools


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

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

for r in R:
    print(r)

Submission status: Wrong Answer

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

ans_list = []

x_list.append(-99999999999)
y_list.append(-99999999999)
z_list.append(-99999999999)

l,m,n = 0,0,0

x_list.sort(reverse=True)
y_list.sort(reverse=True)
z_list.sort(reverse=True)

x1 = x_list[0]
y1 = y_list[0]
z1 = z_list[0]

while (l+1)*(m+1)*(n+1) < 10000 and (l<x and m<y and n<z):
    if x1-x_list[l+1] <= y1-y_list[m+1] and x1-x_list[l+1] <= z1-z_list[n+1]:
        l += 1
    elif y1-y_list[m+1] <= x1-x_list[l+1] and y1-y_list[m+1] <= z1-z_list[n+1]:
        m += 1
    elif z1-z_list[n+1] <= x1-x_list[l+1] and z1-z_list[n+1] <= y1-y_list[m+1]:
        n += 1
    else:
        print(ans_list[9999999])
        break
#print(l,m,n)
for p in range(l+1):
    for q in range(m+1):
        for r in range(n+1):
            ans_list.append(x_list[p]+y_list[q]+z_list[r])

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

Submission status: Wrong Answer

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

ans_list = []

x_list.append(-99999999999)
y_list.append(-99999999999)
z_list.append(-99999999999)

l,m,n = 0,0,0

x_list.sort(reverse=True)
y_list.sort(reverse=True)
z_list.sort(reverse=True)

x1 = x_list[0]
y1 = y_list[0]
z1 = z_list[0]

while (l+1)*(m+1)*(n+1) < 6000 and (l<x and m<y and n<z):
    if x1-x_list[l+1] <= y1-y_list[m+1] and x1-x_list[l+1] <= z1-z_list[n+1]:
        l += 1
    elif y1-y_list[m+1] <= x1-x_list[l+1] and y1-y_list[m+1] <= z1-z_list[n+1]:
        m += 1
    elif z1-z_list[n+1] <= x1-x_list[l+1] and z1-z_list[n+1] <= y1-y_list[m+1]:
        n += 1
    else:
        print(ans_list[9999999])
        break
#print(l,m,n)
for p in range(l+1):
    for q in range(m+1):
        for r in range(n+1):
            ans_list.append(x_list[p]+y_list[q]+z_list[r])

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

Submission status: Wrong Answer

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

ans_list = []

x_list.append(-99999999999)
y_list.append(-99999999999)
z_list.append(-99999999999)

l,m,n = 0,0,0

x_list.sort(reverse=True)
y_list.sort(reverse=True)
z_list.sort(reverse=True)

x1 = x_list[0]
y1 = y_list[0]
z1 = z_list[0]

while (l+1)*(m+1)*(n+1) < 6000 and (l<x and m<y and n<z):
    if x1-x_list[l+1] <= y1-y_list[m+1] and x1-x_list[l+1] <= z1-z_list[n+1]:
        l += 1
    elif y1-y_list[m+1] <= x1-x_list[l+1] and y1-y_list[m+1] <= z1-z_list[n+1]:
        m += 1
    elif z1-z_list[n+1] <= x1-x_list[l+1] and z1-z_list[n+1] <= y1-y_list[m+1]:
        n += 1
    else:
        print("error")
        break
#print(l,m,n)
for p in range(l+1):
    for q in range(m+1):
        for r in range(n+1):
            ans_list.append(x_list[p]+y_list[q]+z_list[r])

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

Submission status: Wrong Answer

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

ans_list = []

x_list.append(-9999999999)
y_list.append(-9999999999)
z_list.append(-9999999999)

l,m,n = 0,0,0

x_list.sort(reverse=True)
y_list.sort(reverse=True)
z_list.sort(reverse=True)

x1 = x_list[0]
y1 = y_list[0]
z1 = z_list[0]

while (l+1)*(m+1)*(n+1) < 4000 and (l<x and m<y and n<z):
    if x1-x_list[l+1] <= y1-y_list[m+1] and x1-x_list[l+1] <= z1-z_list[n+1]:
        l += 1
    elif y1-y_list[m+1] <= x1-x_list[l+1] and y1-y_list[m+1] <= z1-z_list[n+1]:
        m += 1
    elif z1-z_list[n+1] <= x1-x_list[l+1] and z1-z_list[n+1] <= y1-y_list[m+1]:
        n += 1
    else:
        print("error")
        break
#print(l,m,n)
for p in range(l+1):
    for q in range(m+1):
        for r in range(n+1):
            ans_list.append(x_list[p]+y_list[q]+z_list[r])

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

Submission status: Wrong Answer

Solution code:
s=list(map(int,input().split()))
t=list(map(int,input().split()))
u=list(map(int,input().split()))
v=list(map(int,input().split()))
list=[0 for i in range(s[0]*s[1]*s[2])]
a=0
for i in range(s[0]):
  for j in range(s[1]):
    for k in range(s[2]):
      if a==s[3]:
        break
      else:
        list[a]=t[i]+u[j]+v[k]
        a+=1
list.sort(reverse=True)
for l in range(s[3]):
  print(list[l])

Submission status: Wrong Answer

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

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

exists = set()
q = [(-(A[0] + B[0] + C[0]), (0, 0, 0))]
for _ in range(K):
    v, i = heappop(q)
    print(-v)
    if i[0] < X - 1:
        ni = (i[0] + 1, i[1], i[2])
        if ni not in exists:
            exists.add(ni)
            nv = A[i[0] + 1] + B[i[1]] + C[i[2]]
            heappush(q, (-nv, ni))
    if i[1] < Y - 1:
        ni = (i[0], i[1] + 1, i[2])
        if ni not in exists:
            exists.add(ni)
            nv = A[i[0]] + B[i[1] + 1] + C[i[2]]
            heappush(q, (-v, ni))
    if i[2] < Z - 1:
        ni = (i[0], i[1], i[2] + 1)
        if ni not in exists:
            exists.add(ni)
            nv = A[i[0]] + B[i[1]] + C[i[2] + 1]
            heappush(q, (-v, ni))

Submission status: Wrong Answer

Solution code:
import heapq

def main():
    X, Y, Z, K = map(int,input().split())
    A = [int(o) for o in input().split()]
    B = [int(o) for o in input().split()]
    C = [int(o) for o in input().split()]
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    A_size = len(A)
    B_size = len(B)
    C_size = len(C)
    heap = []
    heapq.heappush(heap,(-(A[0]+B[0]+C[0]),0,0,0))
    for i in range(K):
        val,a,b,c = heapq.heappop(heap)
        if a < (A_size-1) and (-(A[a+1]+B[b]+C[c]),a+1,b,c) not in heap:
            heapq.heappush(heap,(-(A[a+1]+B[b]+C[c]),a+1,b,c))
        if b < (B_size-1) and (-(A[a]+B[b+1]+C[c]),a,b+1,c) not in heap:
            heapq.heappush(heap,(-(A[a]+B[b+1]+C[c]),a,b+1,c))
        if c < (C_size-1) and (-(A[a]+B[b]+C[c+1]),a,b,c+1):
            heapq.heappush(heap,(-(A[a]+B[b]+C[c+1]),a,b,c+1))
        print(-val)
if __name__ == "__main__" :
    main()

Submission status: Wrong Answer

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,k=pop(li)
    ans.append(-value)
    if i<x-1 and (i+1,j,k) not in used:
        push(li,(-a[i+1]-b[j]-c[k],i+i,j,k))
        used.add((i+1,j,k))
    if j<y-1 and (i,j+1,k) not in used:
        push(li,(-a[i]-b[j+1]-c[k],i,j+1,k))
        used.add((i,j+1,k))
    if k<z-1 and (i,j,k+1) not in used:
        push(li,(-a[i]-b[j]-c[k+1],i,j,k+1))
        used.add((i,j,k+1))
for i in ans:
    print(i)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x, y, z, k = [int(elem) for elem in input().split()]
a_lst = [int(elem) for elem in input().split()]
b_lst = [int(elem) for elem in input().split()]
c_lst = [int(elem) for elem in input().split()]

a_lst = sorted(a_lst, reverse=True)
b_lst = sorted(b_lst, reverse=True)

ab_lst = []
for a in a_lst:
  for b in b_lst:
    ab_lst.append(a + b)

ab_lst = sorted(ab_lst[:k+1000], reverse=True)
    
res = []
for ab in ab_lst[:k]:
  for c in c_lst:
    res.append(ab + c)

res = sorted(res, reverse=True)
for elem in res[:k]:
  print(elem)

Submission status: Wrong Answer

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 = sorted(A,reverse=True)
B = sorted(B,reverse=True)
C = sorted(C,reverse=True)
D = []
cnt = 0
cur = 0
while cur<min(X,Y,Z):
    if cur<X and cur<Y and cur<Z:
        D.append(A[cur]+B[cur]+C[cur])
        cnt += 1
    if cur<X and cur<Y:
        for i in range(cur+1,X):
            for j in range(cur+1,Y):
                D.append(A[i]+B[j]+C[cur])
                cnt += 1
    if cur<X and cur<Z:
        for i in range(cur+1,X):
            for k in range(cur+1,Z):
                D.append(A[i]+B[cur]+C[k])
                cnt += 1
    if cur<Y and cur<Z:
        for j in range(cur+1,Y):
            for k in range(cur+1,Z):
                D.append(A[cur]+B[j]+C[k])
                cnt += 1
    if cur<X:
        for i in range(cur+1,X):
            D.append(A[i]+B[cur]+C[cur])
            cnt += 1
    if cur<Y:
        for j in range(cur+1,Y):
            D.append(A[cur]+B[j]+C[cur])
            cnt += 1
    if cur<Z:
        for k in range(cur+1,Z):
            D.append(A[cur]+B[cur]+C[k])
            cnt += 1
    if cnt>K:break
    cur += 1
D = sorted(D,reverse=True)
for i in range(K):
    print(D[i])

Submission status: Wrong Answer

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 = sorted(A,reverse=True)
B = sorted(B,reverse=True)
C = sorted(C,reverse=True)
D = []
cnt = 0
cur = 0
while cur<max(X,Y,Z):
    if cur<X and cur<Y and cur<Z:
        D.append(A[cur]+B[cur]+C[cur])
        cnt += 1
    if cur<X and cur<Y:
        for k in range(cur):
            D.append(A[cur]+B[cur]+C[k])
            cnt += 1
    if cur<X and cur<Z:
        for j in range(cur):
            D.append(A[cur]+B[j]+C[cur])
            cnt += 1
    if cur<Y and cur<Z:
        for i in range(cur):
            D.append(A[i]+B[cur]+C[cur])
            cnt += 1
    if cur<X:
        for j in range(cur):
            for k in range(cur):
                D.append(A[cur]+B[j]+C[k])
                cnt += 1
    if cur<Y:
        for i in range(cur):
            for k in range(cur):
                D.append(A[i]+B[cur]+C[k])
                cnt += 1
    if cur<Z:
        for i in range(cur):
            for j in range(cur):
                D.append(A[i]+B[j]+C[cur])
                cnt += 1
    if cnt>1000*K:break
    cur += 1
D = sorted(D,reverse=True)
for i in range(K):
    print(D[i])

Submission status: Wrong Answer

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 = sorted(A,reverse=True)
B = sorted(B,reverse=True)
C = sorted(C,reverse=True)
D = []
cnt = 0
cur = 0
while cur<max(X,Y,Z):
    if cur<X and cur<Y and cur<Z:
        D.append(A[cur]+B[cur]+C[cur])
        cnt += 1
    if cur<X and cur<Y:
        for k in range(cur):
            D.append(A[cur]+B[cur]+C[k])
            cnt += 1
    if cur<X and cur<Z:
        for j in range(cur):
            D.append(A[cur]+B[j]+C[cur])
            cnt += 1
    if cur<Y and cur<Z:
        for i in range(cur):
            D.append(A[i]+B[cur]+C[cur])
            cnt += 1
    if cur<X:
        for j in range(cur):
            for k in range(cur):
                D.append(A[cur]+B[j]+C[k])
                cnt += 1
    if cur<Y:
        for i in range(cur):
            for k in range(cur):
                D.append(A[i]+B[cur]+C[k])
                cnt += 1
    if cur<Z:
        for i in range(cur):
            for j in range(cur):
                D.append(A[i]+B[j]+C[cur])
                cnt += 1
    if cnt>100*K:break
    cur += 1
D = sorted(D,reverse=True)
for i in range(K):
    print(D[i])

Submission status: Wrong Answer

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 = sorted(A,reverse=True)
B = sorted(B,reverse=True)
C = sorted(C,reverse=True)
D = []
cnt = 0
cur = 0
while cur<max(X,Y,Z):
    if cur<X and cur<Y and cur<Z:
        D.append(A[cur]+B[cur]+C[cur])
        cnt += 1
    if cur<X and cur<Y:
        for k in range(cur):
            D.append(A[cur]+B[cur]+C[k])
            cnt += 1
    if cur<X and cur<Z:
        for j in range(cur):
            D.append(A[cur]+B[j]+C[cur])
            cnt += 1
    if cur<Y and cur<Z:
        for i in range(cur):
            D.append(A[i]+B[cur]+C[cur])
            cnt += 1
    if cur<X:
        for j in range(cur):
            for k in range(cur):
                D.append(A[cur]+B[j]+C[k])
                cnt += 1
    if cur<Y:
        for i in range(cur):
            for k in range(cur):
                D.append(A[i]+B[cur]+C[k])
                cnt += 1
    if cur<Z:
        for i in range(cur):
            for j in range(cur):
                D.append(A[i]+B[j]+C[cur])
                cnt += 1
    if cnt>10*K:break
    cur += 1
D = sorted(D,reverse=True)
for i in range(K):
    print(D[i])

Submission status: Wrong Answer

Solution code:
x, y, z, k = [int(_) for _  in input().split()]
a = sorted([int(_) for _  in input().split()])
b = sorted([int(_) for _  in input().split()])
c = sorted([int(_) for _  in input().split()])

while len(a) * len(b) * len(c) > 1000000:
    if len(a) > 1 and a[0] <= b[0] and a[0] <= c[0]:
        a.pop(0)
    elif len(b) > 1 and b[0] <= a[0] and b[0] <= c[0]:
        b.pop(0)
    elif len(c) > 1 and c[0] <= a[0] and c[0] <= b[0]:
        c.pop(0)

answers = sorted([i + j + k for i in a for j in b for k in c], reverse=True)

print(*answers[:k], sep="\n")

Submission status: Wrong Answer

Solution code:
x, y, z, k = [int(_) for _  in input().split()]
a = sorted([int(_) for _  in input().split()])
b = sorted([int(_) for _  in input().split()])
c = sorted([int(_) for _  in input().split()])

while len(a) * len(b) * len(c) > 10000:
    if a[0] <= b[0] and a[0] <= c[0]:
        a.pop(0)
    elif b[0] <= a[0] and b[0] <= c[0]:
        b.pop(0)
    else:
        c.pop(0)

answers = sorted([i + j + k for i in a for j in b for k in c], reverse=True)

print(*answers[:k], sep="\n")

Submission status: Wrong Answer

Solution code:
x, y, z, k = [int(_) for _  in input().split()]
a = sorted([int(_) for _  in input().split()])
b = sorted([int(_) for _  in input().split()])
c = sorted([int(_) for _  in input().split()])

while len(a) * len(b) * len(c) > 10000:
    if a[0] < b[0] and a[0] < c[0]:
        a.pop(0)
    elif b[0] < a[0] and b[0] < c[0]:
        b.pop(0)
    else:
        c.pop(0)

answers = sorted([i + j + k for i in a for j in b for k in c], reverse=True)

print(*answers[:k], sep="\n")

Submission status: Wrong Answer

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

i = 0
j = 0
k = 0
cakes = []
for i, a in enumerate(A):
    if i*k*j > k: break
        
    for j, b in enumerate(B):
        if i*k*j > k: break
            
        for k, c in enumerate(C):
            if i*k*j > k: break
         
            cakes.append(a+b+c)
            
cakes.sort(reverse=True)      
for cake in cakes:
    print(cake)

Submission status: Wrong Answer

Solution code:
class pqueue():
  def __init__(self):
    self.queue = {1: [], 2: []}
  
  def push(self, x):
    if self.queue[1] == []:
      self.queue[1].append(x)
    else:
      if x[0] > self.queue[1][0][0]:
        self.queue[2].append(self.queue[1].pop(0))
        self.queue[1].append(x)
      else:
        self.queue[2].append(x)
      
  def pop(self):
    if self.queue[1] == []:
      return None
    else:
      if self.queue[2] != []:
        m = max(self.queue[2], key=lambda x: x[0])
        self.queue[1].append(m)
        self.queue[2].remove(m)
        return self.queue[1].pop(0)
      else:
        return self.queue[1].pop(0)
    
P = pqueue()
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)

P.push([a[0]+b[0]+c[0], 0, 0, 0])
for cnt in range(k):
  p = P.pop()
  if p[1]+1 < x:
    A = [a[p[1]+1]+b[p[2]]+c[p[3]], p[1]+1, p[2], p[3]]
    if not A in P.queue[1] and not A in P.queue[2]:
      P.push(A)
  if p[2]+1 < y:
    B = [a[p[1]]+b[p[2]+1]+c[p[3]], p[1], p[2]+1, p[3]]
    if not B in P.queue[1] and not B in P.queue[2]:
      P.push(B)
  if p[3]+1 < z:
    C = [a[p[1]]+b[p[2]]+c[p[3]+1], p[1], p[2], p[3]+1]
    if not C in P.queue[1] and not C in P.queue[2]:
      P.push(C)
  print(p[0])

Submission status: Wrong Answer

Solution code:

Submission status: Wrong Answer

Solution code:
import heapq
x, y, z, w = 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(w):
    max_ans = heapq.heappop(q)
    ans.append(abs(max_ans[0]))
    i, j, k = max_ans[1]

    if i == x-1:
        lst1 =  [-A[i]-B[j]-C[k], [i, j, k]]
    else:
        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])

    if j == y-1:
        lst2 = [-A[i]-B[j]-C[k], [i, j, k]]
    else:
        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])

    if k == z-1:
        lst3 = [-A[i]-B[j]-C[k], [i, j, k]]
    else:
        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])
    print(q)

Submission status: Wrong Answer

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


def d4(p):
    co = 0
    for i in range(min(k, x)):
        for j in range(min(k, y)):
            for l in range(min(k, z)):
                if A[i] + B[j] + C[l] < p:
                    break
                else:
                    D.append(A[i] + B[j] + C[l])
                    co += 1
                if co >= k:
                    return True
    return False

ri = A[0]+B[0]+C[0] + 1
le = 0
mid = (le + ri) // 2

while ri - le > 1:
    D = []
    d4bool = d4(mid)
    if d4bool:
        le = mid
    else:
        ri = mid
    mid = (le + ri) // 2
    #print(le,ri)


D.sort(reverse=1)

while len(D) < k:
    D.append(le)

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

Submission status: Wrong Answer

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

import heapq

def main(): 

    X,Y,Z,K = map(int,input().split())
    
    A=list(map(int,input().split()))
    B=list(map(int,input().split()))
    C=list(map(int,input().split()))
    """
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    
    q = []
    heapq.heapify(q)
    q.append([A[0]+B[0]+C[0], 0, 0, 0])
    a=0
    b=0
    c=0
    q.append([A[a-1]+B[b]+C[c], a-1, b, c])
    q.append([A[a]+B[b-1]+C[c], a, b-1, c])
    q.append([A[a]+B[b]+C[c-1], a, b, c-1])
    for i in range(K):
        ans=q.pop()
        ans
        print(ans[0])
    """
    AB = []
    for i in range(X):
        for j in range(Y):
            AB.append(A[i]+B[j])
    AB.sort(reverse=True)
    ABC=[]
    for i in range(min(K,len(AB))):
        for j in range(Z):
            ABC.append(AB[i]+C[j])
    ABC.sort(reverse=True)
    for i in range(K):
        print(ABC[i],"time")


        


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

points = []
for i in range(x):
  for j in range(y):
    for k in range(z):
      s = a[i]+b[j]+c[k]
      points += [s]
points.sort(reverse=True)  

for x in range(len(points)):
  num = points[x]
  print(num)

Submission status: Wrong Answer

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())) 
e=[]

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

for i in range(1,x+1):
    for j in range(1,y+1):
        e.append(a[-i]+b[-j])
e.sort()
print(e)