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:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

X,Y,Z,K = II()
A = III()
B = III()
C = III()

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

a = []
if X*Y*Z <= 10**5:
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                a.append(A[i]+B[j]+C[k])
    a.sort(reverse=True)
    for i in range(K):
        print(a[i])
else:
    mini = min(X,Y,Z)
    if mini>=60:
        for i in range(60):
            for j in range(60):
                for k in range(60):
                    a.append(A[i]+B[j]+C[k])
        a.sort(reverse=True)
        for i in range(K):
            print(a[i])
    else:
        for i in range(X):
            for j in range(Y):
                for k in range(Z):
                    a.append(A[i]+B[j]+C[k])
        a.sort(reverse=True)
        for i in range(K):
            print(a[i])

Submission status: Wrong Answer

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

X,Y,Z,K = II()
A = III()
B = III()
C = III()

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

a = []
if X*Y*Z <= 10**5:
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                a.append(A[i]+B[j]+C[k])
    a.sort(reverse=True)
    for i in range(K):
        print(a[i])
else:
    mini = min(X,Y,Z)
    if mini>=60:
        for i in range(60):
            for j in range(60):
                for k in range(60):
                    a.append(A[i]+B[j]+C[k])
        a.sort(reverse=True)
        for i in range(K):
            print(a[i])
    elif sum([X<60,Y<60,Z<60])==1:
        val = math.ceil(4000/mini)
        for i in range(min(val,X)):
            for j in range(min(val,Y)):
                for k in range(min(val,Z)):
                    a.append(A[i]+B[j]+C[k])
        a.sort(reverse=True)
        for i in range(K):
            print(a[i])
    else:
        for i in range(X):
            for j in range(Y):
                for k in range(Z):
                    a.append(A[i]+B[j]+C[k])
        a.sort(reverse=True)
        for i in range(K):
            print(a[i])

Submission status: Wrong Answer

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

X,Y,Z,K = II()
A = III()
B = III()
C = III()

a = []
if X*Y*Z <= 10**5:
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                a.append(A[i]+B[j]+C[k])
    a.sort(reverse=True)
    for i in range(K):
        print(a[i])
else:
    mini = min(X,Y,Z)
    if mini>=60:
        for i in range(60):
            for j in range(60):
                for k in range(60):
                    a.append(A[i]+B[j]+C[k])
        a.sort(reverse=True)
        for i in range(K):
            print(a[i])
    elif sum([X<60,Y<60,Z<60])==1:
        val = math.ceil(4000/mini)
        for i in range(min(val,X)):
            for j in range(min(val,Y)):
                for k in range(min(val,Z)):
                    a.append(A[i]+B[j]+C[k])
        a.sort(reverse=True)
        for i in range(K):
            print(a[i])
    else:
        for i in range(X):
            for j in range(Y):
                for k in range(Z):
                    a.append(A[i]+B[j]+C[k])
        a.sort(reverse=True)
        for i in range(K):
            print(a[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()))

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

AB = AB[:K]
C.sort(reverse=True)
ABC = []
i = 0
for c in C:
    if i >= K: break
    for ab in AB:
        ABC.append(ab+c)
        i += 1

ABC.sort(reverse=True)

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

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

AB = AB[:K]
C.sort(reverse=True)
ABC = []
i = 0
for ab in AB:
    if i >= K: break
    for c in C:
        ABC.append(ab+c)
        i += 1

ABC.sort(reverse=True)

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

Submission status: Wrong Answer

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

ans_list = []
for i in range(min(x,k)):
    for j in range(min(y,k-i)):
        ans_list.append(a[i]+b[j])
ans_list.sort(reverse=True)
ans_list_2 = []
for i in range(min(z,k)):
    for j in range(min(k-i, len(ans_list))):
        ans_list_2.append(ans_list[j]+c[i])
ans_list_2.sort(reverse=True)
for i in range(k):
    print(ans_list_2[i])

Submission status: Wrong Answer

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

ans_list = []
for i in range(x):
    for j in range(y):
        ans_list.append(a[i]+b[j])
ans_list.sort(reverse=True)
ans_list_2 = []
print(ans_list)
for i in range(z):
    for j in range(min(k, len(ans_list))):
        ans_list_2.append(ans_list[j]+c[i])
ans_list_2.sort(reverse=True)
for i in range(k):
    print(ans_list_2[i])

Submission status: Wrong Answer

Solution code:
X,Y,Z,K = map(int,input().split())
AList = list(map(int,input().split()))
BList = list(map(int,input().split()))
CList = list(map(int,input().split()))
AList.sort(reverse=True)
BList.sort(reverse=True)
CList.sort(reverse=True)
print(AList)
print(BList)
print(CList)
ans = []
for x in range(X):
    for y in range(Y):
        if (x+1)*(y+1) > min(K,X*Y):
            break
        for z in range(Z):
            if (x+1)*(y+1)*(z+1) > min(K,X*Y*Z):
                break
            ans.append( AList[x] + BList[y] + CList[z])

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

Submission status: Wrong Answer

Solution code:
import heapq
x, y, z, k = [ int(v) for v in input().split() ]
x_list = sorted([ int(v) for v in input().split() ], reverse = True)
y_list = sorted([ int(v) for v in input().split() ], reverse = True)
z_list = sorted([ int(v) for v in input().split() ], reverse = True)

cake_list = []

def maling_cake(a,b,c):
    d = x_list[a]+y_list[b]+z_list[c]
    return (-d,a,b,c)

for i in range(k):
    if i == 0:
        print(-maling_cake(0,0,0)[0])
        if x > 1: 
            heapq.heappush(cake_list, maling_cake(1,0,0))
        if y > 1: 
            heapq.heappush(cake_list, maling_cake(0,1,0))
        if z > 1: 
            heapq.heappush(cake_list, maling_cake(0,0,1))
        
    else:
        di, ai, bi, ci = heapq.heappop(cake_list)
        if ai < x - 1:
            heapq.heappush(cake_list, maling_cake(ai+1,bi,ci))
        if bi < y - 1:
            heapq.heappush(cake_list, maling_cake(ai,bi+1,ci))
        if ci < z - 1:
            heapq.heappush(cake_list, maling_cake(ai,bi,ci+1))

        print(-di)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right
import sys
input = sys.stdin.readline


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

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

A1 = [A[0]-a for a in A]
B1 = [B[0]-b for b in B]
C1 = [C[0]-c for c in C]

for ia, a in enumerate(A1):
    ib = bisect_right(B1, a)
    ic = bisect_right(C1, a)

    if (ia)*(ib)*(ic) > K:
        break

if ia == X-1:
    A2 = A1
else:
    A2 = A1[:ia+2]
if ib == Y-1:
    B2 = B1
else:
    B2 = B1[:ib+2]
if ic == Z-1:
    C2 = C1
else:
    C2 = C1[:ic+2]

Q = []
for a in A2:
    for b in B2:
        for c in C2:
            Q.append(a+b+c)
Q.sort()
S = A[0] + B[0] + C[0]
for i in range(K):
    print(S-Q[i])

Submission status: Wrong Answer

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()))
C.sort(reverse=True)
 
L = []
 
for i in range(min(120,len(A))):
    for j in range(min(120,len(B))):
        for k in range(min(120,len(C))):
            tmp = A[i] + B[j] + C[k]
            L.append(tmp)
 
L.sort(reverse=True)
 
for i in range(K):
    print(L[i])

Submission status: Wrong Answer

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

L = []

for i in range(min(100,len(A))):
    for j in range(min(100,len(B))):
        for k in range(min(100,len(C))):
            tmp = A[i] + B[j] + C[k]
            L.append(tmp)

L.sort(reverse=True)

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

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush
from collections import defaultdict

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

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

pq = []
heappush(pq, (-(A[0] + B[0] + C[0]), 0, 0, 0))
visited = defaultdict(bool)

ans = []
for _ in range(K):
    p, i, j, k = heappop(pq)
    ans.append(-p)
    visited[(i, j, k)] = True
    
    if (i + 1 < X) and (not visited[(i + 1, j, k)]):
        heappush(pq, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
    if (j + 1 < Y) and (not visited[(i, j + 1, k)]):
        heappush(pq, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
    if (k + 1 < Z) and (not visited[(i, j, k + 1)]):
        heappush(pq, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
        
print(*sorted(ans, reverse=True), sep='\n')

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

ans = []
for i in range(X):
    for j in range(Y):
        if (i + 1) * (j + 1) > K:
            break

        for k in range(Z):
            if (i + 1) * (j + 1) * (k + 1) <= K:
                ans.append(A[i] + B[j] + C[k])
            else:
                break

print(*sorted(ans, reverse=True), sep='\n')

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

ans = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i + 1) * (j + 1) * (k + 1) <= K:
                ans.append(A[i] + B[j] + C[k])

print(*sorted(ans, reverse=True), 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)
ans = [a[0]+b[0]+c[0]]
l = 0
for i in range((k-2)//7 +1):
    ans += sorted([a[l+1]+b[l]+c[l],a[l]+b[l+1]+c[l],a[l]+b[l]+c[l+1],a[l+1]+b[l]+c[l+1],a[l+1]+b[l+1]+c[l],a[l]+b[l+1]+c[l+1]],reverse=True)
    ans += [a[l+1]+b[l+1]+c[l+1]]
    l += 1

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

Submission status: Wrong Answer

Solution code:
import itertools
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)
A = A[:15]
B = B[:15]
C = C[:15]
ans = []
for abc in itertools.product(A, B, C):
    ans.append(sum(list(abc)))
ans = sorted(ans,reverse=True)[:K]
print("\n".join(map(str,ans)))

Submission status: Wrong Answer

Solution code:
import itertools
X, Y, Z, K = map(int, input().split())
A = sorted(map(int, input().split()))
B = sorted(map(int, input().split()))
C = sorted(map(int, input().split()))
A = A[:15]
B = B[:15]
C = C[:15]
ans = []
for abc in itertools.product(A, B, C):
    ans.append(sum(list(abc)))
ans = sorted(ans,reverse=True)[:K]
print("\n".join(map(str,ans)))

Submission status: Wrong Answer

Solution code:
import heapq
x,y,z,k = map(int,input().split())
ax = [int(_) for _ in input().split()]
ay = [int(_) for _ in input().split()]
az = [int(_) for _ in input().split()]
ax.sort(reverse=True)
ay.sort(reverse=True)
az.sort(reverse=True)
INF = - 3 *10 ** 10
ax.extend([INF])
ay.extend([INF])
az.extend([INF])
l = [(-(ax[0]+ay[0]+az[0]),0,0,0)]
checked = {(-(ax[0]+ay[0]+az[0]),0,0,0)}
for i in range(k):
    t = heapq.heappop(l)
    print(-t[0])
    if (-(ax[t[1]+1]+ay[t[2]]+az[t[3]]),t[1]+1,t[2],t[3]) not in checked:
        heapq.heappush(l,(-(ax[t[1]+1]+ay[t[2]]+az[t[3]]),t[1]+1,t[2],t[3])) 
        checked.add((-(ax[t[1]+1]+ay[t[2]]+az[t[3]]),t[1]+1,t[2],t[3]))
    if (-(ax[t[1]]+ay[t[2]+1]+az[t[3]]),t[1],t[2]+1,t[3]) not in checked:
        heapq.heappush(l,(-(ax[t[1]]+ay[t[2]+1]+az[t[3]]),t[1],t[2]+1,t[3]))
        checked.add((-(ax[t[1]]+ay[t[2]+1]+az[t[3]]),t[1],t[2]+1,t[3]))
    if (ax[t[1]]+ay[t[2]]+az[t[3]+1],t[1],t[2],t[3]+1) not in checked:
        heapq.heappush(l,(-(ax[t[1]]+ay[t[2]]+az[t[3]+1]),t[1],t[2],t[3]+1))
        checked.add((-(ax[t[1]]+ay[t[2]]+az[t[3]+1]),t[1],t[2],t[3]+1))

Submission status: Wrong Answer

Solution code:
x,y,z,k = map(int,input().split())
ax = [int(_) for _ in input().split()]
ay = [int(_) for _ in input().split()]
az = [int(_) for _ in input().split()]
ax.sort(reverse=True)
ay.sort(reverse=True)
az.sort(reverse=True)
INF = - 3*10 **10
ax.extend([INF]*k)
ay.extend([INF]*k)
az.extend([INF]*k)
l = [(ax[0]+ay[0]+az[0],0,0,0)]
for i in range(k):
    t = l.pop(0)
    print(t[0])
    if (ax[t[1]+1]+ay[t[2]]+az[t[3]],t[1]+1,t[2],t[3]) not in l:
        l.append((ax[t[1]+1]+ay[t[2]]+az[t[3]],t[1]+1,t[2],t[3]))
    if (ax[t[1]]+ay[t[2]+1]+az[t[3]],t[1],t[2]+1,t[3]) not in l:
        l.append((ax[t[1]]+ay[t[2]+1]+az[t[3]],t[1],t[2]+1,t[3]))    
    if (ax[t[1]]+ay[t[2]]+az[t[3]+1],t[1],t[2],t[3]+1) not in l:
        l.append((ax[t[1]]+ay[t[2]]+az[t[3]+1],t[1],t[2],t[3]+1))
    l.sort(reverse=True)

Submission status: Wrong Answer

Solution code:
x,y,z,k = map(int,input().split())
ax = [int(_) for _ in input().split()]
ay = [int(_) for _ in input().split()]
az = [int(_) for _ in input().split()]
ax.sort(reverse=True)
ay.sort(reverse=True)
az.sort(reverse=True)
INF = - 10 **9
ax.extend([INF]*k)
ay.extend([INF]*k)
az.extend([INF]*k)
l = [(ax[0]+ay[0]+az[0],0,0,0)]
for i in range(k):
    t = l.pop(0)
    print(t[0])
    if (ax[t[1]+1]+ay[t[2]]+az[t[3]],t[1]+1,t[2],t[3]) not in l:
        l.append((ax[t[1]+1]+ay[t[2]]+az[t[3]],t[1]+1,t[2],t[3]))
    if (ax[t[1]]+ay[t[2]+1]+az[t[3]],t[1],t[2]+1,t[3]) not in l:
        l.append((ax[t[1]]+ay[t[2]+1]+az[t[3]],t[1],t[2]+1,t[3]))    
    if (ax[t[1]]+ay[t[2]]+az[t[3]+1],t[1],t[2],t[3]+1) not in l:
        l.append((ax[t[1]]+ay[t[2]]+az[t[3]+1],t[1],t[2],t[3]+1))
    l.sort(reverse=True)

Submission status: Wrong Answer

Solution code:
import heapq
a, b, c, d = 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, reverse=True)
bl = sorted(bl, reverse=True)
cl = sorted(cl, reverse=True)
q = [[-(al[0] + bl[0] + cl[0]), 0, 0, 0]]
ans = [[-(al[0] + bl[0] + cl[0]), 0, 0, 0]]
heapq.heapify(q)
heapq.heapify(ans)

#while True:
for i in range(d):
    s, aq, bq, cq = heapq.heappop(q)
    if len(al) > aq+1:
        if [-(al[aq+1] + bl[bq] + cl[cq]), aq+1, bq, cq] not in ans:
            heapq.heappush(q, [-(al[aq+1] + bl[bq] + cl[cq]), aq+1, bq, cq])
            heapq.heappush(ans, [-(al[aq+1] + bl[bq] + cl[cq]), aq+1, bq, cq])
            #q.append([al[aq+1] + bl[bq] + cl[cq], aq+1, bq, cq])
            #ans.append([al[aq+1] + bl[bq] + cl[cq], aq+1, bq, cq])

    if len(bl) > bq+1:
        if [-(al[aq] + bl[bq+1] + cl[cq]), aq, bq+1, cq] not in ans:
            heapq.heappush(q, [-(al[aq] + bl[bq+1] + cl[cq]), aq, bq+1, cq])
            heapq.heappush(ans, [-(al[aq] + bl[bq+1] + cl[cq]), aq, bq+1, cq])
            #q.append([al[aq] + bl[bq+1] + cl[cq], aq, bq+1, cq])
            #ans.append([al[aq] + bl[bq+1] + cl[cq], aq, bq+1, cq])

    if len(cl) > cq+1:
        if [-(al[aq] + bl[bq] + cl[cq+1]), aq, bq, cq+1] not in ans:
            heapq.heappush(q, [-(al[aq] + bl[bq] + cl[cq+1]), aq, bq, cq+1])
            heapq.heappush(ans, [-(al[aq] + bl[bq] + cl[cq+1]), aq, bq, cq+1])
            #q.append([al[aq] + bl[bq] + cl[cq+1], aq, bq, cq+1])
            #ans.append([al[aq] + bl[bq] + cl[cq+1], aq, bq, cq+1])

    #q = sorted(q)

    #if len(ans) > d:
    #   ans = sorted(ans, reverse=True)
    #    if ans[d][0] > q[len(q)-1][0]:
    #        break

ans = sorted(ans, reverse=True)
for i in range(d):
    print(-heapq.heappop(ans)[0])

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()))
AB=[]
for i in A:
    for j in B:
        AB.append(i+j)
AB.sort(reverse=True)
AB=AB[:K]
ABC=[]
for i in AB:
    for j in C:
        ABC.append(i+j)

Submission status: Wrong Answer

Solution code:
def main():
    x,y,z,k = map(int,raw_input().split())
    a_taste = map(int,raw_input().split())
    b_taste = map(int,raw_input().split())
    c_taste = map(int,raw_input().split())

    print(a_taste,b_taste,c_taste)
    """simple"""
    sum_taste = []
    for i in a_taste:
        for j in b_taste:
            for l in c_taste:
                sum_taste.append(i + j + l)
    sorted_taste = sorted(sum_taste,reverse = True)
    for i in range(k):
        print(sorted_taste[i])

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

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


kouho = []

for i in range(x):
    for j in range(y):
        for k in range(z):
            if (i+1)*(j+1)*(k+1) > k:
                break
            kouho.append(a[i]+b[j]+c[k])


kouho.sort(reverse=True)

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

Submission status: Wrong Answer

Solution code:
inf = input().split()
X = int(inf[0])
Y = int(inf[1])
Z = int(inf[2])
K = int(inf[3])

A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
ab = []
for a in A:
  for b in B:
    ab +=[a+b]
ab.sort(reverse=True)
C.sort(reverse=True)
state = [ab[0]+i for i in C]
count  = 0
while count<K:
  x = max(state)
  print(x)
  count += 1
  i = state.index(x)
  j = ab.index(x-C[i])
  if j<X*Y-1:
    state[i] = ab[j+1]+C[i]
  else:
    state[i] = (-1)

Submission status: Wrong Answer

Solution code:
inf = input().split()
X = int(inf[0])
Y = int(inf[1])
Z = int(inf[2])
K = int(inf[3])

A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
ab = []
for a in A:
  for b in B:
    ab +=[a+b]
ab.sort(reverse=True)
C.sort(reverse=True)
state = [ab[0]+i for i in C]
count  = 0
while count<K:
  x = max(state)
  print(x)
  count += 1
  while x in state:
    i = state.index(x)
    j = ab.index(x-C[i])
    if j<X*Y-1:
      state[i] = ab[j+1]+C[i]

Submission status: Wrong Answer

Solution code:
inf = input().split()
X = int(inf[0])
Y = int(inf[1])
Z = int(inf[2])
K = int(inf[3])

A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
ab = []
for a in A:
  for b in B:
    ab +=[a+b]
ab.sort(reverse=True)
C.sort(reverse=True)
state = [ab[0]+i for i in C]
count  = 0
while count<K:
  x = max(state)
  print(x)
  count += 1
  i = state.index(x)
  j = ab.index(x-C[i])
  if j<X*Y-1:
    state[i] = ab[j+1]+C[i]

Submission status: Wrong Answer

Solution code:
inf = input().split()
X = int(inf[0])
Y = int(inf[1])
Z = int(inf[2])
K = int(inf[3])

A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
ab = []
for a in A:
  for b in B:
    ab +=[a+b]
ab.sort(reverse=True)
C.sort(reverse=True)
state = [ab[0]+i for i in C]
count  = 0
while count<K:
  x = max(state)
  print(x)
  count += 1
  i = state.index(x)
  j = ab.index(x-C[i])
  if j<X*Y-1:
    state[i] = ab[j+1]+C[i]
  else:
    state[i]=0

Submission status: Wrong Answer

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

x, y, z, k = map(int, readline().split())
a = list(map(int, readline().split()))
b = list(map(int, readline().split()))
c = list(map(int, readline().split()))

a.sort(); a.reverse()
b.sort(); b.reverse()
c.sort(); c.reverse()

added = [[0,0,0]]
heap = [[a[0]+b[0]+c[0],0,0,0]]
n = 1

def upheap(heap,n):
	i = n-1
	while i > 0:
		parent = (i-1)//2
		if heap[i][0] > heap[parent][0]:
			tmp = heap[parent]
			heap[parent] = heap[i]
			heap[i] = tmp
		else:
			break
	#print("up", n, heap)

def downheap(heap, n):
	if n <= 1:
		heap.pop()
		n -= 1
	else:
		heap[0] = heap.pop()
		n -= 1
		i = 0
		while True:
			child = 2 * i + 1
			if child > n-1:
				break
			if child < n-1 and heap[child][0] < heap[child+1][0]:
				child += 1
			if heap[i][0] < heap[child][0]:
				tmp = heap[child]
				heap[child] = heap[i]
				heap[i] = tmp
				i = child
			else:
				break
	#print("down", n, heap)
	return n

for _ in range(k):
	#print(n, heap)
	v, pa, pb, pc = map(int, heap[0])
	print(v)
	n = downheap(heap, n)
	if pa+1 < x and not [pa+1, pb, pc] in added:
		added.append([pa+1, pb, pc])
		n += 1
		heap.append([a[pa+1] + b[pb] + c[pc], pa+1, pb, pc])
		upheap(heap, n)
	if pb+1 < y and not [pa, pb+1, pc] in added:
		added.append([pa, pb+1, pc])
		n += 1
		heap.append([a[pa] + b[pb+1] + c[pc], pa, pb+1, pc])
		upheap(heap, n)
	if pc+1 < z and not [pa, pb, pc+1] in added:
		added.append([pa, pb, pc+1])
		n += 1
		heap.append([a[pa] + b[pb] + c[pc+1], pa, pb, pc+1])
		upheap(heap, n)

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=True)
B.sort(reverse=True)
C.sort(reverse=True)
D = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if i + j + k > K:
                break
            D.append(A[i] + B[j] + C[k])
D.sort(reverse=True)
for ans in D:
    print(ans)

Submission status: Wrong Answer

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

e = []

for va in a:
    for vb in b:
        heapq.heappush(e, -(va+vb))

e = e[:k]

d = []
while e:
    ve = -heapq.heappop(e)
    for vc in c:
        heapq.heappush(d, -(ve+vc))

for i in range(k):
    print(-heapq.heappop(d))

Submission status: Wrong Answer

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)
c = c[:k]

e = []

cnt = 0
for va in a:
    if cnt >= k:
        break
    for vb in b:
        e.append(va+vb)
        cnt+=1

e.sort(reverse=True)

d = []
cnt2 = 0
for ve in e:
    if cnt2 >= k:
        break
    for vc in c:
        d.append(ve+vc)
        cnt2 += 1

d.sort(reverse=True)
d = d[:k]

for vd in d:
    print(vd)

Submission status: Wrong Answer

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)
c = c[:k]

e = []

cnt = 0
for va in a:
    if cnt >= k:
        break
    for vb in b:
        e.append(va+vb)
        cnt+=1

e.sort(reverse=True)

d = []
cnt2 = 0
for ve in e:
    if cnt2 >= k:
        break
    for vc in c:
        d.append(ve+vc)
        cnt2 += 1

d.sort(reverse=True)

for vd in d:
    print(vd)

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, key = lambda x:-x)
B = sorted(B, key = lambda x:-x)
C = sorted(C, key = lambda x:-x)

AB = [0] * (X * Y)

index = 0
for i in range(len(A)):
    for j in range(len(B)):
        AB[index] = A[i] + B[j]
        index += 1

AB = sorted(AB, key = lambda x:-x)
AB = AB[0:3010]

ABC = [0] * (3010 * Z)
index = 0

for i in AB:
    for j in range(len(C)):
        if j > 3:
            break
        ABC[index] = i + C[j]
        index += 1

ABC = sorted(ABC, key = lambda x:-x)
for i in range(0,K):
    print(ABC[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, key = lambda x:-x)
B = sorted(B, key = lambda x:-x)
C = sorted(C, key = lambda x:-x)

AB = [0] * (X * Y)

index = 0
for i in range(len(A)):
    for j in range(len(B)):
        AB[index] = A[i] + B[j]
        index += 1

AB = sorted(AB, key = lambda x:-x)

ABC = [0] * min(3050, X * Y * Z)
index = 0

for i in AB:
    if index >= 3000:
        break
    for j in C:
        ABC[index] = i + j
        index += 1

ABC = sorted(ABC, key = lambda x:-x)
for i in range(0,K):
    print(ABC[i])

Submission status: Wrong Answer

Solution code:
def smaller(x, y):
	if x < y: return x
	return y

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)


aplusb = []
for i in range(0, X):
	for j in range(0, Y):
		aplusb.append(A[i]+B[i])
aplusb.sort(reverse = True)
aplusb = aplusb[:smaller(X*Y, K)]

cakes = []
for i in range(0, Z):
	for j in range(0, smaller(X*Y, K)):
		cakes.append(C[i]+aplusb[j])
cakes.sort(reverse = True)
for i in range(0, K):
	print(cakes[i])

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

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


sums = []
if X * Y * Z > 3000:
    for i in range(X):
        sums.append(B[0]+C[0]+A[i])
        
    for i in range(Y):
        sums.append(B[i]+C[0]+A[0])
    
    for i in range(Z):
        sums.append(B[0]+C[i]+A[0])
else:
    for a in range(X):
        for b in range(Y):
            for c in range(Z):
                sums.append(A[a]+B[b]+C[c])
            

list.sort(sums,reverse = True)

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