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 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(min(x, 15)):
        for j in range(min(y, 15)):
            for k in range(min(z, 15)):
                ans.append(a[i] + b[j] + c[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

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    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)
    MAX = 200

    res = []
    for i in range(min(len(A), MAX)):
        for j in range(min(len(B), MAX)):
            for k in range(min(len(C), MAX)):
                res.append(A[i] + B[j] + C[k])
    res.sort(reverse=True)
    for i in range(K):
        print(res[i])


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

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

if X*Y*Z <= 5*10**4:
  D = sorted([A[i]+B[j]+C[k] for i in range(X) for j in range(Y) for k in range(Z)],reverse=True)
  for i in range(K):
    print(D[i])
  
else:
  A = sorted(A)
  D = sorted([B[i]+C[j] for i in range(Y) for j in range(Z)])

  thr = K
  s = A[-1]+D[max(0,len(D)-thr)]
  prev_idx=0
  selected = [A[-1] + D[i] for i in range(len(D))]
  for i in range(1,X):
    r = s - A[-1-i]
    if r<=0:break
    idx = bisect_left(D,r,prev_idx,len(D)-1)
    if idx == len(D):break
    selected.extend([A[-1-i] + D[j] for j in range(idx,len(D))])
    prec_idx = idx

  selected = sorted(selected,reverse=True)
  print(len(selected))

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

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

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

if X*Y*Z <= 5*10**4:
  D = sorted([A[i]+B[j]+C[k] for i in range(X) for j in range(Y) for k in range(Z)],reverse=True)
  for i in range(K):
    print(D[i])
  
else:
  A = sorted(A)
  D = sorted([B[i]+C[j] for i in range(Y) for j in range(Z)])

  thr = K//2
  s = A[-1]+D[max(0,len(D)-thr)]
  prev_idx=0
  selected = [A[-1] + D[i] for i in range(len(D))]
  for i in range(1,X):
    r = s - A[-1-i]
    if r<=0:break
    idx = bisect_left(D,r,prev_idx,len(D)-1)
    if idx == len(D):break
    selected.extend([A[-1-i] + D[j] for j in range(idx,len(D))])
    prec_idx = idx

  selected = sorted(selected,reverse=True)
  print(len(selected))

  for i in range(K):
    print(selected[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 = sorted(AB,reverse=True)[:K]
C = sorted(C,reverse=True)
ABC = []

for c in C:
    for ab in AB:
        ABC.append(c + ab)
print(*ABC[:K],sep="\n")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

def main():
    X, Y, Z, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)

    q = []
    heappush(q, (-(A[0]+B[0]+C[0]),0,0,0))
    count = 0
    used = set()

    while count < K:
        num, s,t,u = heappop(q)
        print(-num)
        used.add((s,t,u))

        if s+1 < X and (s+1,t,u) not in used:
            heappush(q, (-(A[s+1]+B[t]+C[u]),s+1,t,u))
            used.add((s+1,t,u))
        if t+1 < Y and (s,t+1,u) not in used:
            heappush(q, (-(A[s]+B[t+1]+C[u]),s,t+1,u))
            used.add((s,t+1,u))
        if u+1 < Z and (s+1,t+1,u) not in used:
            heappush(q, (-(A[s]+B[t]+C[u+1]),s,t,u+1))
            used.add((s,t,u+1))
        count += 1

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import heapq
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    X, Y, Z, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)

    def bfs():
        used = set()
        used.add((0, 0, 0))

        q = []
        heapq.heapify(q)
        heapq.heappush(q, (A[0] + B[0] + C[0], 0, 0, 0))
        res = []
        while q:
            v, i, j, k = heapq.heappop(q)
            res.append(v)

            if len(res) == K:
                return res

            for di, dj, dk in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
                ni = i + di
                nj = j + dj
                nk = k + dk

                if ni >= X or nj >= Y or nk >= Z:
                    continue

                if (ni, nj, nk) in used:
                    continue

                used.add((ni, nj, nk))
                heapq.heappush(q, (A[ni] + B[nj] + C[nk], ni, nj, nk))

        return res

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    X, Y, Z, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)

    def bfs():
        used = set()
        used.add((0, 0, 0))

        q = deque([(0, 0, 0)])
        res = [A[0] + B[0] + C[0]]
        while q:
            i, j, k = q.popleft()

            for di, dj, dk in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
                ni = i + di
                nj = j + dj
                nk = k + dk

                if ni >= X or nj >= Y or nk >= Z:
                    continue

                if (ni, nj, nk) in used:
                    continue

                res.append(A[ni] + B[nj] + C[nk])
                used.add((ni, nj, nk))

                if len(res) > K:
                    continue

                q.append((ni, nj, nk))

        return res

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    X, Y, Z, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)

    def bfs():
        used = set()
        used.add((0, 0, 0))

        q = deque([(0, 0, 0)])
        res = [A[0] + B[0] + C[0]]
        while q:
            i, j, k = q.popleft()

            for di, dj, dk in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
                ni = i + di
                nj = j + dj
                nk = k + dk

                if ni >= X or nj >= Y or nk >= Z:
                    continue

                if (ni, nj, nk) in used:
                    continue

                res.append(A[ni] + B[nj] + C[nk])
                used.add((ni, nj, nk))

                if len(res) > K:
                    continue

                q.append((ni, nj, nk))

        print(used)

        return res

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############
X, Y, Z, K = getNM()
A = getList()
B = getList()
C = getList()
A.sort(reverse = True)
B.sort(reverse = True)
C.sort(reverse = True)

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

alta = alta[:min(K + 1, X * Y)]
ans = []
for i in range(len(alta)):
    for j in range(Z):
        ans.append(alta[i] + C[j])
ans.sort(reverse = True)

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

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############
X, Y, Z, K = getNM()
A = getList()
B = getList()
C = getList()
A.sort(reverse = True)
B.sort(reverse = True)
C.sort(reverse = True)

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

alta = alta[:min(3001, X * Y)]
ans = []
for i in range(len(alta)):
    for j in range(Z):
        ans.append(alta[i] + C[j])
ans.sort(reverse = True)

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# hello worldと表示する
#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

x,y,z,k=MI()
A=sorted(LI(),reverse=True)
B=sorted(LI(),reverse=True)
C=sorted(LI(),reverse=True)
Q=[[(A[0]+B[0]+C[0])*(-1),0,0,0]]
heapify(Q)
#print(Q)
i=0
ans=[]
#print(heappop(Q))
al=[[[-1]*x for i in range(y)] for j in range(z)]
al[0][0][0]=0
while i<k:
    a,u,v,w=heappop(Q)
    ans.append(-a)
    if u+1<x and al[u+1][v][w]==-1:
        Q.append([-A[u+1]-B[v]-C[w],u+1,v,w])
        al[u+1][v][w]=0
    if v+1<y and al[u][v+1][w]==-1:
        Q.append([-A[u]-B[v+1]-C[w],u,v+1,w])
        al[u][v+1][w]=0
    if w+1<z and al[u][v][w+1]==-1:
        Q.append([-A[u]-B[v]-C[w+1],u,v,w+1])
        al[u][v][w+1]=0
    i+=1
#print(len(ans))
for i in range(k):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# hello worldと表示する
#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

x,y,z,k=MI()
A=sorted(LI(),reverse=True)
B=sorted(LI(),reverse=True)
C=sorted(LI(),reverse=True)
Q=[[(A[0]+B[0]+C[0])*(-1),0,0,0]]
heapify(Q)
#print(Q)
i=0
ans=[]
#print(heappop(Q))
al=[[[-1]*x for i in range(y)] for j in range(z)]
al[0][0][0]=0
while i<k:
    a,u,v,w=heappop(Q)
    ans.append(-a)
    if u+1<x and al[u+1][v][w]==-1:
        Q.append([-A[u+1]-B[v]-C[w],u+1,v,w])
        al[u+1][v][w]=0
    if v+1<y and al[u][v+1][w]==-1:
        Q.append([-A[u]-B[v+1]-C[w],u,v+1,w])
        al[u][v+1][w]=0
    if w+1<z and al[u][v][w+1]==-1:
        Q.append([-A[u]-B[v]-C[w+1],u,v,w+1])
        al[u][v][w+1]=0
    i+=1
#print(len(ans))
for i in ans:
    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()))



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

#AB.sort(reverse=True)
#print(AB)
AB = AB[:K]

ABC = []
for i in range(len(AB)):
    for j in range(Z):
        ABC.append(AB[i] + C[j])
        
#ABC.sort(reverse=True)
ABC = ABC[:K]

for i in ABC:
    print(i)

Submission status: Wrong Answer

Solution code:
def resolve():
    X, Y, Z, K = list(map(int, input().split()))
    A = sorted(list(map(int, input().split())))
    B = sorted(list(map(int, input().split())))
    C = sorted(list(map(int, input().split())))

    ABC = []
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                if ((i+1)*(j+1)*(k+1) > K):
                    break
                ABC.append(A[i]+B[j]+C[k])
    ABC = sorted(ABC, reverse=True)
    [print(ABC[i]) for i in range(K)]

if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

x, y, z, t = map(int, readline().split())
a = sorted(map(int, readline().split()), reverse=True)
b = sorted(map(int, readline().split()), reverse=True)
c = sorted(map(int, readline().split()), reverse=True)

ans = []
for i in range(min(len(a), 20)):
    for j in range(min(len(b), 20)):
        for k in range(min(len(c), 20)):
            val = a[i] + b[j] + c[k]
            ans.append(val)

for i in sorted(ans, reverse=True)[:t]:
    print(i)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

x, y, z, t = map(int, readline().split())
a = sorted(map(int, readline().split()))
b = sorted(map(int, readline().split()))
c = sorted(map(int, readline().split()))

ans = []
for i in range(min(len(a), 20)):
    for j in range(min(len(b), 20)):
        for k in range(min(len(c), 20)):
            val = a[i] + b[j] + c[k]
            ans.append(val)

for i in sorted(ans, reverse=True)[:t]:
    print(i)

Submission status: Wrong Answer

Solution code:


if __name__ == '__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()))

    bc = []
    for bb in b:
        for cc in c:
            bc.append(bb + cc)
    bc.sort()

    from bisect import bisect_left

    def C(x):
        # count "abc >= x"
        ret = 0
        for aa in a:
            idx = bisect_left(bc, x - aa)
            ret += len(bc) - idx
        return ret

    lb = 0
    ub = (10 ** 10) * 4
    while ub - lb > 1:
        mid = (ub + lb) // 2
        if C(mid) > k:
            lb = mid
        else:
            ub = mid

    kth = ub

    l = []
    for aa in a:
        idx = bisect_left(bc, kth - aa)
        for i in range(idx, len(bc)):
            l.append(aa + bc[i])

    l.sort(reverse=True)
    for ll in l:
        print(ll)

Submission status: Wrong Answer

Solution code:


if __name__ == '__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()))

    bc = []
    for bb in b:
        for cc in c:
            bc.append(bb + cc)
    bc.sort()

    from bisect import bisect_left

    def C(x):
        # count "abc >= x"
        ret = 0
        for aa in a:
            idx = bisect_left(bc, x - aa)
            ret += len(bc) - idx
        return ret

    lb = 0
    ub = (10 ** 10) * 3 + 1
    while ub - lb > 1:
        mid = (ub + lb) // 2
        if C(mid) > k:
            lb = mid
        else:
            ub = mid

    kth = ub

    l = []
    for aa in a:
        idx = bisect_left(bc, kth - aa)
        for i in range(idx, len(bc)):
            l.append(aa + bc[i])

    l.sort(reverse=True)
    for ll in l:
        print(ll)

Submission status: Wrong Answer

Solution code:


if __name__ == '__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()

    bc = []
    for bb in b:
        for cc in c:
            bc.append(bb + cc)
    bc.sort()

    from bisect import bisect_left

    def C(x):
        # count "abc >= x"
        ret = 0
        for aa in a:
            idx = bisect_left(bc, x - aa)
            ret += len(bc) - idx
        return ret

    lb = 0
    ub = (10 ** 10) * 3 + 1
    while ub - lb > 1:
        mid = (ub + lb) // 2
        if C(mid) >= k:
            lb = mid
        else:
            ub = mid

    kth = lb

    l = []
    for aa in a:
        idx = bisect_left(bc, kth - aa)
        for i in range(idx, len(bc)):
            l.append(aa + bc[i])

    l.sort(reverse=True)
    for ll in l:
        print(ll)

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

bc = []
for bb in b:
    for cc in c:
        bc.append(bb + cc)
bc.sort()

from bisect import bisect_left, bisect_right

def C(x):
    # count "abc <= x"
    ret = 0
    for aa in a:
        idx = bisect_right(bc, x - aa)
        ret += idx
    return ret

kk = x * y * z - k + 1
lb = 0
ub = (10 ** 10) * 4
while ub - lb > 1:
    mid = (ub + lb) // 2
    if C(mid) >= kk:
        ub = mid
    else:
        lb = mid

kth = ub

l = []
for aa in a:
    idx = bisect_left(bc, kth - aa)
    for i in range(idx, len(bc)):
        l.append(aa + bc[i])

l.sort(reverse=True)
for ll in l:
    print(ll)

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

from bisect import bisect_left, bisect_right

def C(x):
    # count "abc <= x"
    ret = 0
    for aa in a:
        for bb in b:
            idx = bisect_right(c, x - aa - bb)
            ret += idx
    return ret

kk = x * y * z - k + 1
lb = 0
ub = (10 ** 10) * 4
while ub - lb > 1:
    mid = (ub + lb) // 2
    if C(mid) >= kk:
        ub = mid
    else:
        lb = mid

kth = ub
print(-1)

Submission status: Wrong Answer

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

X,Y,Z,K = map(int,readline().split())

A = sorted(list(map(int,readline().split())),reverse = True)
B = sorted(list(map(int,readline().split())),reverse = True)
C = sorted(list(map(int,readline().split())),reverse = True)

# Aからa個、Bからb個、Cからc個選ぶ
# a * b * c >= Kとなること
# これらは、次との差が小さい順に採用する

aind = 0
bind = 0
cind = 0
while (aind + 1) * (bind + 1) * (cind + 1) < K:
  diff = [10 ** 18, 10 ** 18, 10 ** 18]
  if aind + 1 < len(A):
    diff[0] = A[aind] - A[aind + 1]
  if bind + 1 < len(B):
    diff[1] = B[bind] - B[bind + 1]
  if cind + 1 < len(C):
    diff[2] = C[cind] - C[cind + 1]
  if diff[0] <= min(diff[1],diff[2]):
    aind += 1
  if diff[1] <= min(diff[0],diff[2]):
    bind += 1
  if diff[2] <= min(diff[0],diff[1]):
    cind += 1
  
ans = []
for a in range(min(aind + 3,len(A))):
  for b in range(min(bind + 3,len(B))):
    for c in range(min(cind + 3,len(C))):
      ans.append(A[a] + B[b] + C[c])
      
ans = sorted(ans,reverse = True)
for i in range(K):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
from heapq import heappop,heappush
x,y,z,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

A = sorted(A,reverse=True)
B = sorted(B,reverse=True)
C = sorted(C,reverse=True)

def solve(p,add=False):
  cnt = 0
  for i in range(x):
    for j in range(y):
      for k in range(z):
        sum_ = A[i] + B[j] + C[k]
        if sum_ < p:
          break
        cnt += 1
        if add:
          ans.append(sum_) 
        if cnt >= K:
          return True
  return False

l = 0 
r = 3 * 10**10
while l+1 < r: 
  p = (l+r)//2
  if solve(p): # p以上がk個以上存在していたら、条件を厳しく: pを増やす (つまり→) lを増やす
    l = p    
  else:
    r = p
    
ans = []
solve(p-1,True)
for a in sorted(ans,reverse=True):
  print(a)

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)

a.append(-10**11)
a.append(-10**11)
a.append(-10**11)
a.append(-10**11)
b.append(-10**11)
b.append(-10**11)
b.append(-10**11)
b.append(-10**11)
c.append(-10**11)
c.append(-10**11)
c.append(-10**11)
c.append(-10**11)
a.append(-10**11)
a.append(-10**11)
a.append(-10**11)
a.append(-10**11)
b.append(-10**11)
b.append(-10**11)
b.append(-10**11)
b.append(-10**11)
c.append(-10**11)
c.append(-10**11)
c.append(-10**11)
c.append(-10**11)

#k番目に美味しい組み合わせ(a+b+c)
#kは3000以下

a_cnt=1
b_cnt=1
c_cnt=1

ai=0
bi=0
ci=0

#print(a,b,c)

while a_cnt*b_cnt*c_cnt<k:
    #print(ai,bi,ci)
    if a[ai]-a[ai+1]<=b[bi]-b[bi+1] and a[ai]-a[ai+1]<=c[ci]-c[ci+1]:
        ai+=1
        a_cnt+=1
    elif c[ci]-c[ci+1]<=b[bi]-b[bi+1] and c[ci]-c[ci+1]<=a[ai]-a[ai+1]:
        ci+=1
        c_cnt+=1
    elif b[bi]-b[bi+1]<=c[ci]-c[ci+1] and b[bi]-b[bi+1]<=a[ai]-a[ai+1]:
        bi+=1
        b_cnt+=1
    else:
        print("error")

#print(a_cnt,b_cnt,c_cnt)

result=[]
for i in range(a_cnt+8):
    for j in range(b_cnt+8):
        for l in range(c_cnt+8):
            result.append(a[i]+b[j]+c[l])
    
result.sort(reverse=True)

cnt=0
for item in result:
    print(item)
    cnt+=1

    if cnt>=k:
        exit()

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)

a.append(-10**11)
a.append(-10**11)
a.append(-10**11)
a.append(-10**11)
b.append(-10**11)
b.append(-10**11)
b.append(-10**11)
b.append(-10**11)
c.append(-10**11)
c.append(-10**11)
c.append(-10**11)
c.append(-10**11)

#k番目に美味しい組み合わせ(a+b+c)
#kは3000以下

a_cnt=1
b_cnt=1
c_cnt=1

ai=0
bi=0
ci=0

#print(a,b,c)

while a_cnt*b_cnt*c_cnt<k:
    #print(ai,bi,ci)
    if a[ai]-a[ai+1]<=b[bi]-b[bi+1] and a[ai]-a[ai+1]<=c[ci]-c[ci+1]:
        ai+=1
        a_cnt+=1
    elif c[ci]-c[ci+1]<=b[bi]-b[bi+1] and c[ci]-c[ci+1]<=a[ai]-a[ai+1]:
        ci+=1
        c_cnt+=1
    elif b[bi]-b[bi+1]<=c[ci]-c[ci+1] and b[bi]-b[bi+1]<=a[ai]-a[ai+1]:
        bi+=1
        b_cnt+=1
    else:
        print("error")

#print(a_cnt,b_cnt,c_cnt)

result=[]
for i in range(a_cnt+4):
    for j in range(b_cnt+4):
        for l in range(c_cnt+4):
            result.append(a[i]+b[j]+c[l])
    
result.sort(reverse=True)

cnt=0
for item in result:
    print(item)
    cnt+=1

    if cnt>=k:
        exit()

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)

a.append(-10**11)
a.append(-10**11)
b.append(-10**11)
b.append(-10**11)
c.append(-10**11)
c.append(-10**11)

#k番目に美味しい組み合わせ(a+b+c)
#kは3000以下

a_cnt=1
b_cnt=1
c_cnt=1

ai=0
bi=0
ci=0

#print(a,b,c)

while a_cnt*b_cnt*c_cnt<k:
    #print(ai,bi,ci)
    if a[ai]-a[ai+1]<=b[bi]-b[bi+1] and a[ai]-a[ai+1]<=c[ci]-c[ci+1]:
        ai+=1
        a_cnt+=1
    elif c[ci]-c[ci+1]<=b[bi]-b[bi+1] and c[ci]-c[ci+1]<=a[ai]-a[ai+1]:
        ci+=1
        c_cnt+=1
    elif b[bi]-b[bi+1]<=c[ci]-c[ci+1] and b[bi]-b[bi+1]<=a[ai]-a[ai+1]:
        bi+=1
        b_cnt+=1
    else:
        print("error")

#print(a_cnt,b_cnt,c_cnt)

result=[]
for i in range(a_cnt+2):
    for j in range(b_cnt+2):
        for l in range(c_cnt+2):
            result.append(a[i]+b[j]+c[l])
    
result.sort(reverse=True)

cnt=0
for item in result:
    print(item)
    cnt+=1

    if cnt>=k:
        exit()