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:
x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ab = []
for i in range(x):
  for j in range(y):
    ab.append(a[i] + b[j])
ab = sorted(ab, reverse=True)

abc = []
for i in range(min(k, len(ab))):
  for j in range(min(z, -(-k // (i + 1)) + 1)):
    abc.append(ab[i] + c[j])
abc = sorted(abc, reverse=True)

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

Submission status: Wrong Answer

Solution code:
# ヒープに要素を追加、一番でかいのを取り出すという操作を最大3000回やるやり方
# これもなかなか早い
# ヒープは追加も要素の取り出しもO(log n)で住むので、
# 計算オーダーはO(K log n)で済む(nはヒープの要素数)らしいが
# not in があるのでO(n K log n)では？
# pythonのヒープは使い方に癖があるのでこの機会に習得しよう
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


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

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


from heapq import heapify, heappop, heappush, heappushpop


class PriorityQueue:
    def __init__(self, heap):
        '''
        heap ... list
        '''
        self.heap = heap
        heapify(self.heap)

    def push(self, item):
        print(self.heap, item)
        heappush(self.heap, item)

    def pop(self):
        return heappop(self.heap)

    def pushpop(self, item):
        return heappushpop(self.heap, item)

    def __call__(self):
        return self.heap


heap = []  # ヒープといっても順序を工夫したただのリスト

q = PriorityQueue(heap)
q.push((-(A[0] + B[0] + C[0]), 0, 0, 0))

ans = []
for k_th in range(1, K+1):
    heap_max, i, j, k = q.pop()
    # print(heap_max)
    ans.append(-heap_max)
    for di, dj, dk in zip([1, 0, 0], [0, 1, 0], [0, 0, 1]):
        i_new, j_new, k_new = i + di, j + dj, k + dk
        if i_new >= X or j_new >= Y or k_new >= Z:
            continue
        tmp = (-(A[i_new] + B[j_new] + C[k_new]), i_new, j_new, k_new)
        if tmp in q():  # ここが計算量がおおい
            continue
        q.push(tmp)


print(*ans, 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()))

ab = []
for i in range(x):
  for j in range(y):
    ab.append(a[i] + b[j])
ab = sorted(ab, reverse=True)

abc = []
for i in range(min(k, len(ab))):
  for j in range(min(z, -(-k // (i + 1)))):
    abc.append(ab[i] + c[j])
abc = sorted(abc, reverse=True)

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

Submission status: Wrong Answer

Solution code:
# ヒープに要素を追加、一番でかいのを取り出すという操作を最大3000回やるやり方
# 圧倒的爆速だった
# ヒープは追加も要素の取り出しもO(log n)で住むので、
# 計算オーダーはO(K log n)で済む(nはヒープの要素数)
# pythonのヒープは使い方に癖があるのでこの機会に習得しよう
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


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

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

heap = []  # ヒープといっても順序を工夫したただのリスト

from heapq import heapify, heappop, heappush, heappushpop
heappush(heap, (-(A[0] + B[0] + C[0]), 0, 0, 0))

ans = []
for k_th in range(1, K+1):
    heap_max, i, j, k = heappop(heap)
    # print(heap_max)
    ans.append(-heap_max)
    for di, dj, dk in zip([1, 0, 0], [0, 1, 0], [0, 0, 1]):
        i_new, j_new, k_new = i + di, j + dj, k + dk
        if i_new >= X or j_new >= Y or k_new >= Z:
            continue
        tmp = (-(A[i_new] + B[j_new] + C[k_new]), i_new, j_new, k_new)
        if tmp in heap:  # ここが計算量がおおい
            continue
        heappush(heap, tmp)
        print(heap)


print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:

K = [int(n) for n in input().rstrip().split()][3]

A = sorted([int(n) for n in input().rstrip().split()],reverse=True)
B = sorted([int(n) for n in input().rstrip().split()],reverse=True)
C = sorted([int(n) for n in input().rstrip().split()],reverse=True)

List = []

for n in A:
    for m in B:
        List.append(n+m)

List2 =[]

for k in List[:K]:
    for i in C:
        List2.append(k + i)


for i in sorted(List2[:K],reverse=True):
    print(i)

Submission status: Wrong Answer

Solution code:
def solve(p,q):
  return sorted(i+j for i in p for j in q)[:k]
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()))
print(*solve(solve(a,b),c)[::-1],sep='\n')

Submission status: Wrong Answer

Solution code:
[XYZK,A,B,C] = [input() for i in range(4)]
[X,Y,Z,K] = [int(i) for i in XYZK.split()]
A = [int(i) for i in A.split()]
B = [int(i) for i in B.split()]
C = [int(i) for i in C.split()]
Ans = [0 for i in range(K)]
for a in A:
    for b in B:
        for c in C:
            abc = a+b+c
            if min(Ans) < a + b + c:
                Ans[Ans.index(min(Ans))] = a+b+c
Ans = sorted(Ans,reverse=True)
print(Ans)

Submission status: Wrong Answer

Solution code:
x,y,z,k = [int(num) for num in input().split()]
a_cake,b_cake,c_cake = [sorted([int(num) for num in input().split()],reverse=True)[:k] for _ in range(3)]

sum_cake=list()
for i,a in enumerate(a_cake):
    for j,b in enumerate(b_cake):
        if(i*j>k):
            break
        for n,c in enumerate(c_cake):
            if(i*j*n>k):
                break
            sum_cake.append(a+b+c)


sum_cake.sort(reverse=True)

for i in sum_cake:
    print(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()))

data=[]
N=0
for i in range(0,X):
  
    for j in range(0,Y):
    
      for k in range(0,Z):
        if N>K: break
        data.append(int(A[i]+B[j]+C[k]))
        N=N+1
        
    
data.sort()
data.reverse()

for l in range(K):
  print(data[l])

Submission status: Wrong Answer

Solution code:
x,y,z,k = [int(num) for num in input().split()]
#入力して降順ソートしてｋ個分にスライス
a_cake,b_cake,c_cake = [sorted([int(num) for num in input().split()],reverse=True)[:k] for _ in range(3)]

#リストの最初の値(max値)との差分をリスト化
diff_list = [0]
for cake in [a_cake,b_cake,c_cake]:
        #スライスして0を取り除いておく
        diff_list.extend(list(map(lambda x:cake[0]-x,cake))[1:k])

diff_list.sort()
cake_max = a_cake[0]+b_cake[0]+c_cake[0]
for diff in diff_list[:k]:
    print(cake_max-diff)

Submission status: Wrong Answer

Solution code:
x,y,z,k = [int(num) for num in input().split()]
#入力して降順ソートしてｋ個分にスライス
a_cake,b_cake,c_cake = [sorted([int(num) for num in input().split()],reverse=True)[:k] for _ in range(3)]

#リストの最初の値(max値)との差分をリスト化
diff_list = [0]
for cake in [a_cake,b_cake,c_cake]:
        #スライスして0を取り除いておく
        diff_list.extend(list(map(lambda x:cake[0]-x,cake))[1:k])

diff_list.sort()
cake_max = a_cake[0]+b_cake[0]+c_cake[0]
for diff in diff_list:
    print(cake_max-diff)

Submission status: Wrong Answer

Solution code:
x,y,z,k = [int(num) for num in input().split()]
#入力して降順ソートしてｋ個分にスライス
a_cake,b_cake,c_cake = [sorted([int(num) for num in input().split()],reverse=True)[:k] for _ in range(3)]

#リストの最初の値(max値)との差分をリスト化
diff_list = list()
for cake in [a_cake,b_cake,c_cake]:
        diff_list.extend(list(map(lambda x:cake[0]-x,cake)))

diff_list.sort()
cake_max = a_cake[0]+b_cake[0]+c_cake[0]
for diff in diff_list[:k]:
    print(cake_max-diff)

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)

Q = []
Q.append([A[0]+B[0]+C[0], 0, 0, 0])

def search():
    global Q
    q = Q.pop()
    a = q[1]
    b = q[2]
    c = q[3]
    if a+1 < x and [A[a+1]+B[b]+C[c], a+1, b, c] not in Q:
        Q.append([A[a+1]+B[b]+C[c], a+1, b, c])
    if b+1 < y and [A[a]+B[b+1]+C[c], a, b+1, c] not in Q:
        Q.append([A[a]+B[b+1]+C[c], a, b+1, c])
    if c+1 < z and [A[a]+B[b]+C[c+1], a, b, c+1] not in Q:
        Q.append([A[a]+B[b]+C[c+1], a, b, c+1])
    Q.sort()
    return q[0]

for _ in range(k):
    print(search())

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)

Q = []
Q.append([A[0]+B[0]+C[0], 0, 0, 0])

def search():
    global Q
    q = Q[0]
    del Q[0]
    a = q[1]
    b = q[2]
    c = q[3]
    if a+1 < x and [A[a+1]+B[b]+C[c], a+1, b, c] not in Q:
        Q.append([A[a+1]+B[b]+C[c], a+1, b, c])
    if b+1 < y and [A[a]+B[b+1]+C[c], a, b+1, c] not in Q:
        Q.append([A[a]+B[b+1]+C[c], a, b+1, c])
    if c+1 < z and [A[a]+B[b]+C[c+1], a, b, c+1] not in Q:
        Q.append([A[a]+B[b]+C[c+1], a, b, c+1])
    Q.sort(reverse=True)
    return q[0]

for _ in range(k):
    print(search())

Submission status: Wrong Answer

Solution code:
x,y,z,k = [int(num) for num in input().split()]
a_cake,b_cake,c_cake = [sorted([int(num) for num in input().split()],reverse=True) for _ in range(3)]

sum_cake = sorted([a+b+c for a in a_cake for b in b_cake for c in c_cake],reverse=True)[:k]
print(sum_cake)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 10 22:30:07 2019

@author: Yamazaki Kenichi
"""
import heapq
#import random
#X,Y,Z,K = 1000,1000,1000,3000
#A = random.sample(range(10**9),1000)
#B = random.sample(range(10**9),1000)
#C = random.sample(range(10**9),1000)

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 = []
T = [[-1 for i in range(X)] for i in range(Y)]
T[0][0] = 0
heapq.heappush(Q,[-(A[0]+B[0]+C[0]),0,0,0])
for i in range(K-1):
    k = heapq.heappop(Q)
    s1,s2,s3 = k[1],k[2],k[3]
    if s1 + 1 != X and T[s1+1][s2] <= s3:
        heapq.heappush(Q,[-(A[s1+1]+B[s2]+C[s3]),s1+1,s2,s3])
        T[s1+1][s2] += 1
    if s2 + 1 != Y and T[s1][s2+1] <= s3:
        heapq.heappush(Q,[-(A[s1]+B[s2+1]+C[s3]),s1,s2+1,s3])
        T[s1][s2+1] += 1
    if s3 + 1 != Z and T[s1][s2] <= s3 + 1:
        heapq.heappush(Q,[-(A[s1]+B[s2]+C[s3+1]),s1,s2,s3+1])
        T[s1][s2] += 1
    print(-k[0])
k = heapq.heappop(Q)
print(-k[0])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 10 22:30:07 2019

@author: Yamazaki Kenichi
"""
import heapq
import random

X,Y,Z,K = 1000,1000,1000,3000
A = random.sample(range(10**9),1000)
B = random.sample(range(10**9),1000)
C = random.sample(range(10**9),1000)

#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 = []
T = [[-1 for i in range(X)] for i in range(Y)]
T[0][0] = 0
heapq.heappush(Q,[-(A[0]+B[0]+C[0]),0,0,0])
for i in range(K-1):
    k = heapq.heappop(Q)
    s1,s2,s3 = k[1],k[2],k[3]
    if s1 + 1 != X and T[s1+1][s2] <= s3:
        heapq.heappush(Q,[-(A[s1+1]+B[s2]+C[s3]),s1+1,s2,s3])
        T[s1+1][s2] += 1
    if s2 + 1 != Y and T[s1][s2+1] <= s3:
        heapq.heappush(Q,[-(A[s1]+B[s2+1]+C[s3]),s1,s2+1,s3])
        T[s1][s2+1] += 1
    if s3 + 1 != Z and T[s1][s2] <= s3 + 1:
        heapq.heappush(Q,[-(A[s1]+B[s2]+C[s3+1]),s1,s2,s3+1])
        T[s1][s2] += 1
    print(-k[0])
k = heapq.heappop(Q)
print(-k[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()))

import numpy as np
import itertools
AB = []
ABC = []
for (a, b) in itertools.product(A, B):
  AB.append(a + b)
C = np.sort(C)
AB = np.sort(AB)
reversed(C)
reversed(AB)
for(ab, c) in itertools.product(AB[:k], C[:k]):
  ABC.append(ab + c)
ABC = np.sort(ABC)
for i in range(k):
  print(ABC[-i-1])

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

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

sum1 = []

if x*y < k:
	k = x*y

for i in range(k):
	for j in range(z):
		sum1.append(sum0[i] + c[j])
sum1.sort(reverse = True)
for i in range(k):
	print(sum0[i])

Submission status: Wrong Answer

Solution code:
import sys
import itertools

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

a = list(map(int,(input().split())))
b = list(map(int,(input().split())))
c = list(map(int,(input().split())))

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

comb_a = [a[0]]
comb_b = [b[0]]
comb_c = [c[0]]
next = []
sum = []

comb = list(itertools.product(comb_a,comb_b,comb_c))
num_comb = len(comb)

while num_comb < 3000:

    next = []
    for i in a,b,c:
        if len(i) >= 2:
            next.append(i[0] - i[1])
        if len(i) == 1:
            next.append(10000000001)
    if min(next) != 10000000001:
        if len(a) >= 2 and min(next) == a[0] - a[1]:
            comb_a.append(a[1])
            a.pop(0)
        elif len(b) >= 2 and min(next) == b[0] - b[1]:
            comb_b.append(b[1])
            b.pop(0)
        elif len(c) >= 2 and min(next) == c[0] - c[1]:
            comb_c.append(c[1])
            c.pop(0)
    else:
        break
    comb = list(itertools.product(comb_a,comb_b,comb_c))
    num_comb = len(comb)

for i in comb:
    sum.append(i[0]+i[1]+i[2])
sum.sort(reverse=True)
for i in range(k):
    print(sum[i])

Submission status: Wrong Answer

Solution code:
import sys
import itertools

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

a = list(map(int,(input().split())))
b = list(map(int,(input().split())))
c = list(map(int,(input().split())))

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

comb_a = [a[0]]
comb_b = [b[0]]
comb_c = [c[0]]
next = []
sum = []

comb = list(itertools.product(comb_a,comb_b,comb_c))
num_comb = len(comb)

while num_comb <= 3000:

    next = []
    for i in a,b,c:
        if len(i) >= 2:
            next.append(i[0] - i[1])
        if len(i) == 1:
            next.append(10000000001)
    if min(next) != 10000000001:
        if len(a) >= 2 and min(next) == a[0] - a[1]:
            comb_a.append(a[1])
            a.pop(0)
        elif len(b) >= 2 and min(next) == b[0] - b[1]:
            comb_b.append(b[1])
            b.pop(0)
        elif len(c) >= 2 and min(next) == c[0] - c[1]:
            comb_c.append(c[1])
            c.pop(0)
    else:
        break
    comb = list(itertools.product(comb_a,comb_b,comb_c))
    num_comb = len(comb)

for i in comb:
    sum.append(i[0]+i[1]+i[2])
sum.sort(reverse=True)
for i in range(k):
    print(sum[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()));c.sort(reverse=True)
l=[]
for i in range(x):
    for j in range(y):
        l+=[a[i]+b[j]]
l.sort(reverse=True)

ans=[]
for i in range(min(k,x*y)):
    if len(ans)>k:
     	break
    for j in range(min(k,x*y,z)):
        ans+=[l[i]+c[j]]
        if len(ans)>k:
            break
ans.sort(reverse=True)
        
for i in range(k):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
import sys
import itertools

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

a = list(map(int,(input().split())))
b = list(map(int,(input().split())))
c = list(map(int,(input().split())))

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

comb_a = [a[0]]
comb_b = [b[0]]
comb_c = [c[0]]
next = []
sum = []

comb = list(itertools.product(comb_a,comb_b,comb_c))
num_comb = len(comb)

#while num_comb < k*2 and num_comb < 3000:
while num_comb < 3000:
    comb = list(itertools.product(comb_a,comb_b,comb_c))
    num_comb = len(comb)

    next = []
    for i in a,b,c:
        if len(i) >= 2:
            next.append(i[0] - i[1])
        if len(i) == 1:
            next.append(10000000001)
    if min(next) != 10000000001:
        if len(a) >= 2 and min(next) == a[0] - a[1]:
            comb_a.append(a[1])
            a.pop(0)
        elif len(b) >= 2 and min(next) == b[0] - b[1]:
            comb_b.append(b[1])
            b.pop(0)
        elif len(c) >= 2 and min(next) == c[0] - c[1]:
            comb_c.append(c[1])
            c.pop(0)
    else:
        break

for i in comb:
    sum.append(i[0]+i[1]+i[2])
sum.sort(reverse=True)
for i in range(k):
    print(sum[i])

Submission status: Wrong Answer

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

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

ab_list = []

for i in range(X):
    for j in range(Y):
        ab_list.append(A[i]+B[j])

ab_list2 = sorted(ab_list[:min(X*Y,K)])
ab_list2.reverse()
abc_list = []

count = 0
count2 = 0

for i in range(min(X*Y,K)):
    for j in range(Z):
        abc_list.append(ab_list[i]+C[j])

abc_list2 = sorted(abc_list[:K])
abc_list2.reverse()

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

Submission status: Wrong Answer

Solution code:
import itertools

x,y,z,k=(int(i) for i in input().split())
#print(x,y,z,k)
a=list((-1)*int(i) for i in input().split())
a.sort()
b=list((-1)*int(i) for i in input().split())
b.sort()
c=list((-1)*int(i) for i in input().split())
c.sort()
#print(a,b,c)

mylist=list(itertools.product(a,b))

mydict={}
for i in mylist:
    mydict[i]=sum(i)

mylist=[]
#print(mydict)
for w, v in sorted(mydict.items(), key=lambda x: x[1]):
    mylist+=[w]
if len(mylist) > 3000:
  mylist=mylist[0:3000]
    
mylist=list(itertools.product(mylist,c))

#print(mylist)
#mylist=[mylist[i][0][0]+mylist[i][0][1]++mylist[i][1] for i in range(k)]
mylist=[mylist[i][0][0]+mylist[i][0][1]++mylist[i][1] for i in range(len(mylist))]
mylist.sort()
#print(mylist)

#def heapsort(iterable):
#    h = []
#    for value in iterable:
#        heapq.heappush(h, value)
#    return [heapq.heappop(h) for i in range(k)]

#    return [heapq.heappop(h) for i in range(len(h))]

#heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
#mylist1=heapsort(mylist)

for i in range(k):
  print((-1)*mylist[i])

Submission status: Wrong Answer

Solution code:
import itertools

x,y,z,k=(int(i) for i in input().split())
#print(x,y,z,k)
a=list((-1)*int(i) for i in input().split())
a.sort()
b=list((-1)*int(i) for i in input().split())
b.sort()
c=list((-1)*int(i) for i in input().split())
c.sort()
#print(a,b,c)
mylist=list(itertools.product(a,b))
kk=min(k,3000,len(mylist))
mydict={}

for i in mylist:
    mydict[i]=sum(i)

mylist=[]
#print(mydict)
for kk, v in sorted(mydict.items(), key=lambda x: x[1]):
    mylist+=[kk]
#print(mylist)

mylist=list(itertools.product(mylist,c))
#mylist=[mylist[i] for i in range(k)]

#print(mylist)
#mylist=[mylist[i][0][0]+mylist[i][0][1]++mylist[i][1] for i in range(k)]
mylist=[mylist[i][0][0]+mylist[i][0][1]++mylist[i][1] for i in range(len(mylist))]
mylist.sort()
#print(mylist)

#def heapsort(iterable):
#    h = []
#    for value in iterable:
#        heapq.heappush(h, value)
#    return [heapq.heappop(h) for i in range(k)]

#    return [heapq.heappop(h) for i in range(len(h))]

#heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
#mylist1=heapsort(mylist)

for i in range(k):
  print((-1)*mylist[i])

Submission status: Wrong Answer

Solution code:
X,Y,Z,K = map(int,input().split())
A = [int(n) for n in input().split()]
B = [int(n) for n in input().split()]
C = [int(n) for n in input().split()]
A_B = []
for a in A:
    for b in B:
        A_B.append(a+b)
A_B_K = sorted(A_B)[:K]
A_B_C=[]
for ab in A_B_K:
    for c in C:
        A_B_C.append(ab+c)
A_B_C.sort(reverse=True)
for i in range(min(K,len(A_B_C))):
    print(A_B_C[i])

Submission status: Wrong Answer

Solution code:
k = list(map(int,input().split()))
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))

a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
A,B,C = a[:k[3]],b[:k[3]],c[:k[3]]

ab = sorted(list(ax+bx for ax in A for bx in B), reverse=True)[:k[3]]
abc = sorted(list(aby+cy for aby in ab for cy in C), reverse=True)[:k[3]]
for i in abc: print(abc)

Submission status: Wrong Answer

Solution code:
ns = list(map(int, input().split()))
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))
zs = list(map(int, input().split()))
xys = []
ans = []
for x in xs:
	for y in ys:
		xys.append(x+y)
xys.sort()
xys.reverse()
for i in range(min(ns[0]*ns[1], ns[3])):
	for j in range(min(ns[2], ns[3]-i+1)):
		ans.append(xys[i]+zs[j])
ans.sort()
ans.reverse()
for i in range(ns[3]):
	print(ans[i])

Submission status: Wrong Answer

Solution code:
ns = list(map(int, input().split()))
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))
zs = list(map(int, input().split()))
xys = []
ans = []
for x in xs:
	for y in ys:
		xys.append(x+y)
xys.sort()
xys.reverse()
for i in range(min(ns[0]*ns[1], ns[3])):
	for j in range(min(ns[2], ns[3]-i)):
		ans.append(xys[i]+zs[j])
ans.sort()
ans.reverse()
for i in range(ns[3]):
	print(ans[i])

Submission status: Wrong Answer

Solution code:
x,y,z,k = list(map(int, input().split()))
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
l = []
for i in a:
    for j in b:
        for kk in c:
            l.append(i + j + kk)
l.sort(reverse = True)
print(k,'a')
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()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

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

class Combination():
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c
        self.val = A[self.a] + B[self.b] + C[self.c]

    @property
    def param(self):
        return self.a, self.b, self.c

    def __lt__(self, other):
        return self.val < other.val

class Heap():
    nodes = []

    @property
    def size(self):
        return len(self.nodes)

    def pop(self):
        if self.size == 1:
            val = self.nodes[0]
            self.nodes = []
            return val
        if self.size > 1:
            val = self.nodes[0]
            target = self.nodes[-1]
            self.nodes = self.nodes[:-1]
            idx = 0
            size = len(self.nodes)
            while 2*idx+1 < size:
                if 2*idx+2 < size and self.nodes[2*idx+1] < self.nodes[2*idx+2]:
                    target_id = 2 * idx + 2
                else:
                    target_id = 2 * idx + 1
                if self.nodes[target_id] < target:
                    break
                else:
                    self.nodes[idx] = self.nodes[target_id]
                    idx = target_id
            self.nodes[idx] = target
            return val
        return None

    def push(self, node):
        val = node.val
        self.nodes.append(node)
        idx = len(self.nodes) - 1
        while idx > 0:
            target_id = int((idx - 1) / 2)
            if self.nodes[target_id] < self.nodes[idx]:
                self.nodes[idx] = self.nodes[target_id]
                idx = target_id
            else:
                break
        self.nodes[idx] = node

class Leaf():
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

    def add(self, val):
        if self.val == val:
            return False
        elif val < self.val:
            if self.left is None:
                self.left = Leaf(val)
            else:
                return self.left.add(val)
        else:
            if self.right is None:
                self.right = Leaf(val)
            else:
                return self.right.add(val)
        return True

A_off = 10 ** 20
B_off = 10 ** 10

def get_hash(a, b, c):
    return a * A_off + b * B_off + c

heap = Heap()
heap.push(Combination(0, 0, 0))
bt = Leaf(get_hash(0, 0, 0))

for _ in range(K):
    comb = heap.pop()
    print(comb.val)
    a, b, c = comb.param
    if a+1 < X and bt.add(get_hash(a+1, b, c)):
        heap.push(Combination(a+1, b, c))
    if b+1 < Y and bt.add(get_hash(a, b+1, c)):
        heap.push(Combination(a, b+1, c))
    if c+1 < Z and bt.add(get_hash(a, b, c+1)):
        heap.push(Combination(a, b, c+1))

Submission status: Wrong Answer

Solution code:
from heapq import *

X, Y, Z, K = [int(i) for i in 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 = sorted([int(x) for x in A], reverse=True)
B = sorted([int(x) for x in B], reverse=True)
C = sorted([int(x) for x in C], reverse=True)

res = []
memo = []
ans = []
i, j, k = 0, 0, 0
while len(ans) < K:
  if [i, j, k] not in memo:
    heappush(res, (-A[i]-B[j]-C[k], i, j, k))
  if i+1 < X and [i+1, j, k] not in memo:
    heappush(res, (-A[i+1]-B[j]-C[k], i+1, j, k))
  if j+1 < Y and [i, j+1, k] not in memo:
    heappush(res, (-A[i]-B[j+1]-C[k], i, j+1, k))
  if k+1 < Z and [i, j, k+1] not in memo:
    heappush(res, (-A[i]-B[j]-C[k+1], i, j, k+1))
  if i+1 < X:
    i += 1
  if j+1 < Y:
    j += 1
  if k+1 < Z:
    k += 1
  e, x, y, z = heappop(res)
  ans.append(-e)

for i in ans:
  print(i)

Submission status: Wrong Answer

Solution code:
X, Y, Z, K = [int(i) for i in 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 = sorted([int(x) for x in A], reverse=True)
B = sorted([int(x) for x in B], reverse=True)
C = sorted([int(x) for x in C], reverse=True)

res = []
memo = []
i, j, k = 0, 0, 0
while len(res) < K:
  if [i, j, k] not in memo:
    res.append(A[i]+B[j]+C[k])
    memo.append([i,j,k])
  if i+1 < X and [i+1, j, k] not in memo:
    res.append(A[i+1]+B[j]+C[k])
    memo.append([i+1, j, k])
  if j+1 < Y and [i, j+1, k] not in memo:
    res.append(A[i]+B[j+1]+C[k])
    memo.append([i, j+1, k])
  if k+1 < Z and [i, j, k+1] not in memo:
    res.append(A[i]+B[j]+C[k+1])
    memo.append([i, j, k+1])
  if i+1 < X:
    i += 1
  if j+1 < Y:
    j += 1
  if k+1 < Z:
    k += 1
res = sorted(res, reverse=True)[:K]
for i in res:
  print(i)

Submission status: Wrong Answer

Solution code:
import itertools
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)
C = C[:k]
AB = list(i+j for i in A for j in B)
AB.sort(reverse=True)
AB = AB[:k]

ABC = list(i+j for i in AB for j in C)
ABC.sort(reverse=True)
for i in ABC:
    print(i)

Submission status: Wrong Answer

Solution code:
import itertools
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()]
C.sort()
C = C[:k]
AB = list(itertools.product(A,B))


for i in range(len(AB)):
    AB[i] = AB[i][0] + AB[i][1]
AB.sort(reverse=True)
AB = AB[:k]

ABC = list(itertools.product(AB,C))
for i in range(len(ABC)):
    ABC[i]=ABC[i][0]+ABC[i][1]
ABC.sort(reverse=True)
for i in ABC:
    print(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()))

res = []
for a in range(X):
    for b in range(Y):
            res.append(A[a] + B[b])
            
res.sort(reverse=True)
result = []
for r in res:
    for c in range(Z):
        result.append(r + C[c])
        if len(result) == K:
            break
            
    if len(result) == K:
        break
            
result.sort(reverse=True)

for r in result:
    print(r)

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

res = []
for a in range(X):
    for b in range(Y):
            res.append(A[a] + B[b])
            
res.sort(reverse=True)
result = []
for r in res:
    for c in range(Z):
        result.append(r + C[c])
        if len(result) == K:
            break
            
result.sort(reverse=True)

for r in result:
    print(r)