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:
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())
import sys


A,B,C,K=sep()
a=lis()
b=lis()
c=lis()
a.sort()
b.sort()
c.sort()
s=set()
ans=[]
from collections import deque
def bfs(i,j,k):
    s=set()
    queue=deque([])
    queue.append((i,j,k))
    s.add((i,j,k))
    while queue:
        # print(queue, "queue")
        t=queue.popleft()
        i,j,k=t
        ans.append(a[t[0]]+ b[t[1]] + c[t[2]])
        if len(ans)>100000:
            return
        if i-1>=0:
            if (i-1,j,k) not in s:
                queue.append((i-1,j,k))
                s.add((i-1, j, k))
        if j-1>=0:
            if (i , j-1, k) not in s:
                queue.append((i,j-1,k))
                s.add((i, j-1, k))
        if k-1>=0:
            if (i , j, k-1) not in s:
                queue.append((i,j,k-1))
                s.add((i, j, k-1))
    # print(s)




bfs(A-1,B-1,C-1)
ans.sort(reverse=True)
# print(ans)
for i in range(K):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())
import sys


A,B,C,K=sep()
a=lis()
b=lis()
c=lis()
a.sort()
b.sort()
c.sort()
s=set()
ans=[]
from collections import deque
def bfs(i,j,k):
    s=set()
    queue=deque([])
    queue.append((i,j,k))
    s.add((i,j,k))
    while queue:
        # print(queue, "queue")
        t=queue.popleft()
        i,j,k=t
        ans.append(a[t[0]]+ b[t[1]] + c[t[2]])
        if len(ans)>30000:
            return
        if i-1>=0:
            if (i-1,j,k) not in s:
                queue.append((i-1,j,k))
                s.add((i-1, j, k))
        if j-1>=0:
            if (i , j-1, k) not in s:
                queue.append((i,j-1,k))
                s.add((i, j-1, k))
        if k-1>=0:
            if (i , j, k-1) not in s:
                queue.append((i,j,k-1))
                s.add((i, j, k-1))
    # print(s)




bfs(A-1,B-1,C-1)
ans.sort(reverse=True)
# print(ans)
for i in range(K):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())
import sys
sys.setrecursionlimit(10**6)

A,B,C,K=sep()
a=lis()
b=lis()
c=lis()
a.sort()
b.sort()
c.sort()
s=set()
ans=[]
def dfs(i,j,k):
    if i<0 or j<0 or k<0:
        return
    if (i,j,k) in s:
        return
    if len(ans)>300000:
        return
    ans.append(a[i]+b[j]+c[k])
    s.add((i,j,k))
    dfs(i-1,j,k)
    dfs(i,j-1,k)
    dfs(i,j,k-1)

dfs(A-1,B-1,C-1)
ans.sort(reverse=True)
for i in range(K):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())
import sys
sys.setrecursionlimit(10**6)

A,B,C,k=sep()
a=lis()
b=lis()
c=lis()
a.sort()
b.sort()
c.sort()
s=set()
ans=[]
def dfs(i,j,k):
    if i<0 or j<0 or k<0:
        return
    if (i,j,k) in s:
        return
    if len(ans)>100000:
        return
    ans.append(a[i]+b[j]+c[k])
    s.add((i,j,k))
    dfs(i-1,j,k)
    dfs(i,j-1,k)
    dfs(i,j,k-1)

dfs(A-1,B-1,C-1)
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())
import sys
sys.setrecursionlimit(10**6)

A,B,C,k=sep()
a=lis()
b=lis()
c=lis()
a.sort()
b.sort()
c.sort()
s=set()
ans=[]
def dfs(i,j,k):
    if i<0 or j<0 or k<0:
        return
    if (i,j,k) in s:
        return
    if len(ans)>10000:
        return
    ans.append(a[i]+b[j]+c[k])
    s.add((i,j,k))
    dfs(i-1,j,k)
    dfs(i,j-1,k)
    dfs(i,j,k-1)

dfs(A-1,B-1,C-1)
ans.sort(reverse=True)
for i in range(k):
    print(ans[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()
b.sort()
c.sort()
a.reverse()
b.reverse()
c.reverse()

l,r=0,3*10**10+1
while r-l!=1:
  t=(l+r)//2
  cnt=0
  for i in a:
    for j in b:
      ll,rr=-1,z
      while rr-ll!=1:
        tt=(ll+rr)//2
        if i+j+c[tt]>=t:
          ll=tt
        else:
          rr=tt
      cnt+=rr
  if cnt>=k:
    l=t
  else:
    r=t

ll=[]
for i in a:
  for j in b:
    for k in c:
      if i+j+k<l:
        break
      ll.append(i+j+k)
ll.sort()
ll.reverse()
print("\n".join(map(str,ll)))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

x, y, z, K = map(int, input().split())
a = sorted(list(map(int, input().split())))[::-1]
b = sorted(list(map(int, input().split())))[::-1]
c = sorted(list(map(int, input().split())))[::-1]

d = defaultdict(bool)
d[(0, 0, 0)] = True
q = [(0, 0, 0)]
ans = [a[0] + b[0] + c[0]]
for i, j, k in q:
    if i + 1 < x and not d[(i + 1, j, k)]:
        ans.append(a[i + 1] + b[j] + c[k])
        q.append((i + 1, j, k))
        d[(i + 1, j, k)] = True
    if j + 1 < y and not d[(i, j + 1, k)]:
        ans.append(a[i] + b[j + 1] + c[k])
        q.append((i, j + 1, k))
        d[(i, j + 1, k)] = True
    if k + 1 < z and not d[(i, j, k + 1)]:
        ans.append(a[i] + b[j] + c[k + 1])
        q.append((i, j, k + 1))
        d[(i, j, k + 1)] = True
    if len(ans) > K * 2:
        break

ans = sorted(ans)[::-1]
for i in range(K):
    print(ans[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()))
D = []
for i in range(X):
    for j in range(Y):
        D.append(A[i]+B[j])
D = sorted(list(D),reverse=True)
C = sorted(C,reverse=True)
E = []
k = 0
cnt = 0
while cnt<K:
    if k<len(D) and k<len(C):
        for i in range(k):
            E.append(D[i]+C[k])
            cnt += 1
        for j in range(k):
            E.append(C[j]+D[k])
            cnt += 1
        E.append(C[k]+D[k])
        cnt += 1
    elif k>=len(D) and k<len(C):
        for i in range(len(D)):
            E.append(D[i]+C[k])
            cnt += 1
    elif k<len(D) and k>=len(C):
        for j in range(len(C)):
            E.append(C[j]+D[k])
            cnt += 1
    k += 1
E = sorted(E,reverse=True)
for i in range(K):
    print(E[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()))
D = set()
for i in range(X):
    for j in range(Y):
        D.add(A[i]+B[j])
D = sorted(list(D),reverse=True)
C = sorted(C,reverse=True)
E = []
k = 0
cnt = 0
while cnt<K:
    if k<len(D) and k<len(C):
        for i in range(k):
            E.append(D[i]+C[k])
            cnt += 1
        for j in range(k):
            E.append(C[j]+D[k])
            cnt += 1
        E.append(C[k]+D[k])
        cnt += 1
    elif k>=len(D) and k<len(C):
        for i in range(len(D)):
            E.append(D[i]+C[k])
            cnt += 1
    elif k<len(D) and k>=len(C):
        for j in range(len(C)):
            E.append(C[j]+D[k])
            cnt += 1
    k += 1
E = sorted(E,reverse=True)
for i in range(K):
    print(E[i])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter


import sys
# sys.setrecursionlimit(10**6)
# readline = sys.stdin.buffer.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(X, Y, Z, K, A, B, C):
    q = []

    for a in A:
        for b in B:
            if len(q) <= K:
                heapq.heappush(q, a+b)
            if len(q) > K:
                heapq.heappushpop(q, a+b)

    qq = []
    for c in C:
        for d in q:
            if len(qq) <= K:
                heapq.heappush(qq, d+c)
            if len(qq) > K:
                heapq.heappushpop(qq, d+c)

    qq.sort(reverse=True)
    return qq

def main():
    X, Y, Z, K = read_int_n()
    A = read_int_n()
    B = read_int_n()
    C = read_int_n()
    print(*slv(X, Y, Z, K, A, B, C), sep='\n')


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+Bの上位K個
tmp = []
for a in A:
    for b in B:
        tmp.append(a + b)
tmp = sorted(tmp)[:K]

ans = []
for ab in tmp:
    for c in C:
        ans.append(ab + c)
ans.sort(reverse=True)

for i in range(K):
    print(ans[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()))
tmp = []
for a in A:
    for b in B:
        tmp.append(a + b)
tmp = tmp[:K]

ans = []
for ab in tmp:
    for c in C:
        ans.append(ab + c)
ans.sort(reverse=True)
for i in range(K):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
from collections import deque
X, Y, Z, K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)

A.append(-int(1e+15))
B.append(-int(1e+15))
C.append(-int(1e+15))

Aq = deque(A)
Bq = deque(B)
Ar = [Aq.popleft()]
Br = [Bq.popleft()]
maximum = min((len(A)-1) + (len(B)-1), 3000)
preA = Ar[0]
preB = Br[0]

while len(Ar) + len(Br) < maximum:
    if  Ar[0] - Aq[0] > Br[0] - Bq[0]:
        Br.append(Bq.popleft())
        preB = Br[-1]
    else:
        Ar.append(Aq.popleft())
        preA = Ar[-1]

AB = [Ar[i] + Br[j] for i in range(len(Ar)) for j in range(len(Br))]
AB = sorted(AB, reverse=True)

AB.append(-int(1e+15))

ABq = deque(AB)
Cq = deque(C)
ABr = [ABq.popleft()]
Cr = [Cq.popleft()]
maximum = min((len(AB)-1) + (len(C)-1), 3000)
preAB = ABr[0]
preC = Cr[0]
while len(ABr) + len(Cr) < maximum:
    if ABr[0] - ABq[0] > Cr[0] - Cq[0]:
        Cr.append(Cq.popleft())
        preC = Cr[-1]
    else:
        ABr.append(ABq.popleft())
        preAB = ABr[-1]

ABC = [ABr[i] + Cr[j] for i in range(len(ABr)) for j in range(len(Cr))]
ABC = sorted(ABC, reverse=True)

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

Submission status: Wrong Answer

Solution code:
from collections import deque
X, Y, Z, K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)

A.append(-int(1e+15))
B.append(-int(1e+15))
C.append(-int(1e+15))

Aq = deque(A)
Bq = deque(B)
Ar = [Aq.popleft()]
Br = [Bq.popleft()]
maximum = min((len(A)-1)*(len(B)-1), 3000)
preA = Ar[0]
preB = Br[0]

while len(Ar)*len(Br) < maximum:
    if  preA - Aq[0] > preB - Bq[0]:
        Br.append(Bq.popleft())
        preB = Br[-1]
    else:
        Ar.append(Aq.popleft())
        preA = Ar[-1]

AB = [Ar[i] + Br[j] for i in range(len(Ar)) for j in range(len(Br))]
AB = sorted(AB, reverse=True)

AB.append(-int(1e+15))

ABq = deque(AB)
Cq = deque(C)
ABr = [ABq.popleft()]
Cr = [Cq.popleft()]
maximum = min((len(AB)-1)*(len(C)-1), 3000)
preAB = ABr[0]
preC = Cr[0]
while len(ABr)*len(Cr) < maximum:
    if preAB - ABq[0] > preC - Cq[0]:
        Cr.append(Cq.popleft())
        preC = Cr[-1]
    else:
        ABr.append(ABq.popleft())
        preAB = ABr[-1]

ABC = [ABr[i] + C[j] for i in range(len(ABr)) for j in range(len(Cr))]
ABC = sorted(ABC, reverse=True)

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 29 09:50:56 2020

@author: naoki
"""

# K <= min(3000, XYZ)
# X, Y, Z <= 1000
X, Y, Z, K = map(int, input().split())

As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
Cs = list(map(int, input().split()))

# if there are only 2cakes, I can use the deque.
K = min(K, X*Y*Z)

ABs = []
for A in As:
    for B in Bs:
        ABs.append(A*B)
        
ABs = sorted(ABs, reverse = True)
ABs = ABs[:K]

ABCs = []

for AB in ABs:
    for C in Cs:
        ABCs.append(AB*C)
        
ABCs = sorted(ABCs, reverse = True)

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

Submission status: Wrong Answer

Solution code:
def binary_search_int(ok, ng, test, k, A, B):
    """
    :param ok: solve(x) = True を必ず満たす点
    :param ng: solve(x) = False を必ず満たす点
    """
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if test(mid, k, A, B):
            ok = mid
        else:
            ng = mid
    return ok

def get_cnt(x, A, B):
    """
    a+b>=x となるようなAの要素とBの要素の組の数 (A, Bはソート済み)
    """
    cnt = 0
    L = len(B)
    for a in A:
        cnt += L-bisect_left(B, x-a)
    return cnt

def get_cnt2(x, A, B):
    """
    バグりやすいが高速
    """
    l, r = bisect_left(B, x-A[-1]), len(A)-2
    cnt = len(B) - l
    while l < len(B) and r>=0:
        if A[r]+B[l] < x:
            l += 1
        else:
            cnt += len(B) - l
            r -= 1
    return cnt

def test(x, k, A, B):
    """
    場合の数が k 個(以下)となるような最大の x を探索する関数
    a+b >= x で get_cnt(x,A,B) は同じ値を返すので、最大の x は A, Bの要素の和そのものに一致する
    """
    return get_cnt2(x,A,B) >= k

from bisect import *

X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
D = []
for a in A:
    for b in B:
        D.append(a+b)
D.sort(reverse=True)
D = D[:K]
D.sort()
Q = []
for k in range(1,K+1):
    Q.append(binary_search_int(1, 10**11,test, k, D, C))
print(*Q, sep="\n")

Submission status: Wrong Answer

Solution code:
from collections import deque
X, Y, Z, K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)

A.append(-int(1e+15))
B.append(-int(1e+15))
C.append(-int(1e+15))

Aq = deque(A)
Bq = deque(B)
Ar = [Aq.popleft()]
Br = [Bq.popleft()]
maximum = min((len(A)-1)*(len(B)-1), 3000)
preA = Ar[0]
preB = Br[0]

while len(Ar)*len(Br) < maximum:
    if  Ar[0] - Aq[0] > Br[0] - Bq[0]:
        Br.append(Bq.popleft())
        preB = Br[-1]
    else:
        Ar.append(Aq.popleft())
        preA = Ar[-1]

AB = [Ar[i] + Br[j] for i in range(len(Ar)) for j in range(len(Br))]
AB = sorted(AB, reverse=True)

AB.append(-int(1e+15))

ABq = deque(AB)
Cq = deque(C)
ABr = [ABq.popleft()]
Cr = [Cq.popleft()]
maximum = min((len(AB)-1)*(len(C)-1), 3000)
preAB = ABr[0]
preC = Cr[0]
while len(ABr)*len(Cr) < maximum:
    if ABr[0] - ABq[0] > Cr[0] - Cq[0]:
        Cr.append(Cq.popleft())
        preC = Cr[-1]
    else:
        ABr.append(ABq.popleft())
        preAB = ABr[-1]

ABC = [ABr[i] + C[j] for i in range(len(ABr)) for j in range(len(Cr))]
ABC = sorted(ABC, reverse=True)

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

Submission status: Wrong Answer

Solution code:
from collections import deque
X, Y, Z, K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)

A.append(-int(1e+15))
B.append(-int(1e+15))
C.append(-int(1e+15))

Aq = deque(A)
Bq = deque(B)
Ar = [Aq.popleft()]
Br = [Bq.popleft()]
maximum = min((len(A)-1)*(len(B)-1), 3000)
preA = Ar[0]
preB = Br[0]

while len(Ar)*len(Br) < maximum:
    if  preA - Aq[0] > preB - Bq[0]:
        Br.append(Bq.popleft())
        preB = Br[-1]
    else:
        Ar.append(Aq.popleft())
        preA = Ar[-1]

AB = [Ar[i] + Br[j] for i in range(len(Ar)) for j in range(len(Br))]
AB = sorted(AB, reverse=True)

AB.append(-int(1e+15))

ABq = deque(AB)
Cq = deque(C)
ABr = [ABq.popleft()]
Cr = [Cq.popleft()]
maximum = min((len(AB)-1)*(len(C)-1), 3000)
preAB = ABr[0]
preC = Cr[0]
while len(ABr)*len(Cr) < maximum:
    if preAB - ABq[0] > preC - Cq[0]:
        Cr.append(Cq.popleft())
        preC = Cr[-1]
    else:
        ABr.append(ABq.popleft())
        preAB = ABr[-1]

ABC = [ABr[i] + C[j] for i in range(len(ABr)) for j in range(len(Cr))]
ABC = sorted(ABC, reverse=True)

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

Submission status: Wrong Answer

Solution code:
# O(klog(k))解法 多分...
import sys
from heapq import heappush,heapify,heappop
input = sys.stdin.readline

def make_kth(A, B, k):
    n, m = len(A), len(B)
    q = [(-A[0] - B[0], 0, 0)]
    r=[]
    for _ in range(min(k, n * m)):
        v, s, t = heappop(q)
        r.append(-v)
        if t + 1 < m:
            heappush(q, (-A[s] - B[t + 1], s, t + 1))
        if t == 0 and s + 1 < n:
            heappush(q, (-A[s + 1] - B[0], s + 1, 0))
    return r

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=1)
    b.sort(reverse=1)
    c.sort(reverse=1)

    r = make_kth(a, b, k)
    r = make_kth(c, r, k)
    print(*r, sep="\n")

Submission status: Wrong Answer

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    x, y, z, w = ns()
    a = na()
    b = na()
    c = na()

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

    ans = []
    for i in range(x):
        for j in range(y):
            ans.append(a[i] + b[j])

    ans.sort(reverse=True)
    ans = ans[:3001]

    res = []
    for ci in c:
        for i in range(len(ans)):
            res.append(ci + ans[i])

    for i in range(w):
        print(res[i])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    x, y, z, w = ns()
    size = [[0, x], [1, y], [2, z]]
    size.sort(key=lambda x: x[1])
    d = []
    for _ in range(3):
        tmp = na()
        tmp.sort(reverse=True)
        d.append(tmp)

    d2 = []
    for i, s in size:
        d2.append(d[i])

    lima = min(size[0][1], 16)
    limb = min(size[1][1], 230)
    limc = min(size[2][1], 3001)

    ans = []
    for i in range(lima):
        for j in range(limb):
            for k in range(limc):
                ans.append(d2[0][i] + d2[1][j] + d2[2][k])
    ans.sort(reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    x, y, z, w = ns()
    size = [[0, x], [1, y], [2, z]]
    size.sort(key=lambda x: x[1])
    d = []
    for _ in range(3):
        tmp = na()
        tmp.sort(reverse=True)
        d.append(tmp)

    d2 = []
    for i, s in size:
        d2.append(d[i])

    lima = min(size[0][1], 16)
    limb = min(size[1][1], 225)
    limc = min(size[2][1], 3000)

    ans = []
    for i in range(lima):
        for j in range(limb):
            for k in range(limc):
                ans.append(d2[0][i] + d2[1][j] + d2[2][k])
    ans.sort(reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    x, y, z, w = ns()
    size = [[0, x], [1, y], [2, z]]
    size.sort(key=lambda x: x[1])
    d = []
    for _ in range(3):
        tmp = na()
        tmp.sort(reverse=True)
        d.append(tmp)

    d2 = []
    for i, s in size:
        d2.append(d[i])

    lim = int(3000 ** (1 / 3))
    lima = min(size[0][1], lim + 1)
    limb = min(size[1][1], int((4000/lima)**(1/2)))
    limc = min(size[2][1], 3000)

    ans = []
    for i in range(lima):
        for j in range(limb):
            for k in range(limc):
                ans.append(d2[0][i] + d2[1][j] + d2[2][k])
    ans.sort(reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    x, y, z, w = ns()
    size = [[0, x], [1, y], [2, z]]
    size.sort(key=lambda x: x[1])
    d = []
    for _ in range(3):
        tmp = na()
        tmp.sort(reverse=True)
        d.append(tmp)

    d2 = []
    for i, s in size:
        d2.append(d[i])

    lim = int(3000 ** (1 / 3))
    lima = min(size[0][1], lim + 1)
    limb = min(size[1][1], int((4000 / (min(size[0][1], lim))) ** (1 / 2)) + 1)
    limc = min(size[2][1], int(3000 / lima / ((3000 / lima) ** (1 / 2))))

    ans = []
    for i in range(lima):
        for j in range(limb):
            for k in range(limc):
                ans.append(d2[0][i] + d2[1][j] + d2[2][k])
    ans.sort(reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    x, y, z, w = ns()
    size = [[0, x], [1, y], [2, z]]
    size.sort(key=lambda x: x[1])
    d = []
    for _ in range(3):
        tmp = na()
        tmp.sort(reverse=True)
        d.append(tmp)

    d2 = []
    for i, s in size:
        d2.append(d[i])

    lim = int(3000 ** (1 / 3)) + 1
    lima = min(size[0][1], lim)
    limb = min(size[1][1], int((3000 / lima) ** (1 / 2)))
    limc = min(size[2][1], int(3000 / lima / limb) + 1)

    ans = []
    for i in range(lima):
        for j in range(limb):
            for k in range(limc):
                ans.append(d2[0][i] + d2[1][j] + d2[2][k])
    ans.sort(reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    x, y, z, w = ns()
    size = [[0, x], [1, y], [2, z]]
    size.sort(key=lambda x: x[1])
    d = []
    for _ in range(3):
        tmp = na()
        tmp.sort(reverse=True)
        d.append(tmp)

    d2 = []
    for i, s in size:
        d2.append(d[i])

    lima = min(size[0][1], 15)
    limb = min(size[1][1], int((3375 / lima) ** (1 / 2)))
    limc = min(size[2][1], int(3375 / lima / limb) + 1)

    ans = []
    for i in range(lima):
        for j in range(limb):
            for k in range(limc):
                ans.append(d2[0][i] + d2[1][j] + d2[2][k])
    ans.sort(reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    x, y, z, w = ns()
    size = [[0, x], [1, y], [2, z]]
    size.sort(key=lambda x: x[1])
    d = []
    for _ in range(3):
        tmp = na()
        tmp.sort(reverse=True)
        d.append(tmp)

    d2 = []
    for i, s in size:
        d2.append(d[i])

    lima = min(size[0][1], 15)
    limb = min(size[1][1], int((3375 / lima) ** (1 / 2)) + 1)
    limc = min(size[2][1], int((3375 / lima / limb)) + 2)

    ans = []
    for i in range(lima):
        for j in range(limb):
            for k in range(limc):
                ans.append(d2[0][i] + d2[1][j] + d2[2][k])
    ans.sort(reverse=True)

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


if __name__ == '__main__':
    main()