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: Time Limit Exceeded

Solution code:
x, y, z, k = map(int, input().split())
ax = list(map(int, input().split()))
by = list(map(int, input().split()))
cz = list(map(int, input().split()))

ax.sort(reverse=True)
by.sort(reverse=True)
cz.sort(reverse=True)

ans = []
for a in ax[:min(x, 200)]:
    for b in by[:(min(y, 200))]:
        for c in cz[:min(z, 200)]:
            ans.append(a + b + c)

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

Submission status: Time Limit Exceeded

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]

ABC = []
for c in C:
    for ab in AB:
        ABC.append(ab + c)
ABC = sorted(ABC, reverse=True)[:k]

for abc in ABC:
    print(abc)

Submission status: Time Limit Exceeded

Solution code:
def binsearch(l, r, fn):
    while r - l > 1:
        m = (l + r) // 2
        if fn(m):
            l = m
        else:
            r = m
    return l

def main():
    X, Y, Z, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = sorted(map(int, input().split()), reverse=True)
    AB = sorted((a + b for a in A for b in B), reverse=True)
    def helper(x):
        r = 0
        t = len(C) - 1
        for ab in AB:
            while t >= 0 and C[t] + ab < x:
                t -= 1
            r += t + 1
        return r >= K
    l = binsearch(AB[-1] + C[-1], AB[0] + C[0] + 1, helper)
    r = []
    for ab in AB:
        for c in C:
            if ab + c < l:
                break
            r.append(ab + c)
            if len(r) == K:
                break
        if len(r) >= K:
            break

    r.sort(reverse=True)
    print('\n'.join(str(i) for i in r))

main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
X, Y, Z, K, *L = map(int, open(0).read().split())
A = L[:X]
B = L[X:X+Y]
C = L[X+Y:]
ab = []
for i in range(X):
  for j in range(Y):
    ab += [A[i]+B[j]]
ab.sort()
ab = np.array(ab)
c = np.array(C)
l = -1
r = 3*10**10+1
M = X*Y*Z
for k in range(K):
  l = -1
  M = X*Y*Z
  while l+1<r:
    n = (l+r)//2
    d = n-c
    m = np.searchsorted(ab,d).sum()
    if M-m>k:
      l = n
    else:
      r = n
  print(l)
  r = l+1

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
X, Y, Z, K, *L = map(int, open(0).read().split())
A = L[:X]
B = L[X:X+Y]
C = L[X+Y:]
ab = []
for i in range(X):
  for j in range(Y):
    ab += [A[i]+B[j]]
ab.sort()
ab = np.array(ab)
c = np.array(C)
l = -1
r = 10**30+1
M = X*Y*Z
for k in range(K):
  l = -1
  r = 10**30+1
  M = X*Y*Z
  while l+1<r:
    n = (l+r)//2
    d = n-c
    m = np.searchsorted(ab,d).sum()
    if M-m>k:
      l = n
    else:
      r = n
  print(l)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappop, heappush
from itertools import product

X, Y, Z, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
A.sort(reverse=True)

D = [i+j for j in C for i in B]
D.sort(reverse=True)

q = []
for i, j in product(A, D):
    heappush(q, -(i+j))

for _ in range(K):
    print(-heappop(q))

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
A.sort(reverse=True)

D = [i+j for j in C for i in B]
D.sort(reverse=True)
E = [i+j for j in D[:K] for i in A[:K]]
E.sort(reverse=True)

print(*E[:K], sep="\n")

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
from heapq import heappush, heappop

X, Y, Z, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

# x以上のものがK個あるならOK
def f(x):
    cnt = 0
    for i in A:
        for j in B:
            for k in C:
                if i+j+k<x:break
                cnt += 1
                if cnt>=K:
                    return True
    return False

def get_boarder():
    l, r = 0, A[0]+B[0]+C[0]
    while r-l>1:
        m = (l+r)//2
        if f(m):
            l = m
        else:
            r = m
    return l

s = []
for i in B:
    for j in C:
        s.append(i+j)
s.sort()
A.sort()
boarder = get_boarder()
q = []

for i in A:
    if boarder-i<0:continue
    pos = bisect_left(s, boarder-i)
    for j in s[pos:]:
        heappush(q, -(i+j))

for _ in range(K):
    print(-heappop(q))

Submission status: Time Limit Exceeded

Solution code:
import bisect

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)

YZ = Y*Z
BC = []
for b in B:
  for c in C:
    BC.append(b+c)

BC.sort()
# 2つのリストから1つずつ要素を取り出したときに和がx以上となる組合せ
def count_large_2(L1, L2, x):
  # L1.sort()
  # L2.sort(reverse=True)
  N1, N2 = len(L1), len(L2)
  i = j = 0
  c = 0
  while i<N1 and j<N2:
    if L1[i]+L2[j] >= x:
      c += 1
      j += 1
      if j >= N2:
        c += (N1-i-1) * N2
    else:
      i += 1
      if i>=N1:
        continue
      c += j
  return c


def check(S):
  c = count_large_2(BC, A, S)  
  return c >= K



Sl = 0
Sr = A[0]+B[0]+C[0]+1

while Sr - Sl > 1:
  mid = (Sl + Sr) // 2
  if check(mid):
    Sl = mid
  else:
    Sr = mid


Ans = []
for a in A:
  for b in B:
    if a + b + C[0] < Sr:
      break
    for c in C:
      if a + b + c >= Sr:
        Ans.append(a+b+c)
      else:
        break

Ans.sort(reverse=True)
Ans = Ans + [Sl]*(K-len(Ans))
for ans in Ans:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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)

YZ = Y*Z
BC = []
for b in B:
  for c in C:
    BC.append(b+c)

BC.sort()
# 2つのリストから1つずつ要素を取り出したときに和がx以上となる組合せ
def count_large_2(L1, L2, x):
  L1.sort()
  L2.sort(reverse=True)
  N1, N2 = len(L1), len(L2)
  i = j = 0
  c = 0
  while i<N1 and j<N2:
    if L1[i]+L2[j] >= x:
      c += 1
      j += 1
      if j >= N2:
        c += (N1-i-1) * N2
    else:
      i += 1
      if i>=N1:
        continue
      c += j
  return c


def check(S):
  c = count_large_2(BC, A, S)  
  return c >= K



Sl = 0
Sr = A[0]+B[0]+C[0]+1

while Sr - Sl > 1:
  mid = (Sl + Sr) // 2
  if check(mid):
    Sl = mid
  else:
    Sr = mid


Ans = []
for a in A:
  for b in B:
    if a + b + C[0] < Sr:
      break
    for c in C:
      if a + b + c >= Sr:
        Ans.append(a+b+c)
      else:
        break

Ans.sort(reverse=True)
Ans = Ans + [Sl]*(K-len(Ans))
for ans in Ans:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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)

YZ = Y*Z
BC = []
for b in B:
  for c in C:
    BC.append(b+c)

BC.sort()
# 2つのリストから1つずつ要素を取り出したときに和がx以上となる組合せ
def count_large_2(L1, L2, x):
  L1.sort()
  L2.sort(reverse=True)
  N1, N2 = len(L1), len(L2)
  i = j = 0
  c = 0
  while i<N1 and j<N2:
    if L1[i]+L2[j] >= x:
      c += 1
      j += 1
      if j >= N2:
        c += (N1-i-1) * N2
    else:
      i += 1
      if i>=N1:
        continue
      c += j
  return c


def check(S):
  c = count_large_2(A, BC, S)  
  return c >= K



Sl = 0
Sr = A[0]+B[0]+C[0]+1

while Sr - Sl > 1:
  mid = (Sl + Sr) // 2
  if check(mid):
    Sl = mid
  else:
    Sr = mid


Ans = []
for a in A:
  for b in B:
    if a + b + C[0] < Sr:
      break
    for c in C:
      if a + b + c >= Sr:
        Ans.append(a+b+c)
      else:
        break

Ans.sort(reverse=True)
Ans = Ans + [Sl]*(K-len(Ans))
for ans in Ans:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
# 尺取り法を使って数える

import bisect

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)

YZ = Y*Z
BC = []
for b in B:
  for c in C:
    BC.append(b+c)

BC.sort()
# 2つのリストから1つずつ要素を取り出したときに和がx以上となる組合せ
def count_large_2(L1, L2, x):
  L1.sort()
  L2.sort(reverse=True)
  N1, N2 = len(L1), len(L2)
  i = j = 0
  c = 0
  while i<N1 and j<N2:
    if L1[i]+L2[j] >= x:
      c += 1
      j += 1
      if j >= N2:
        c += (N1-i-1) * N2
    else:
      i += 1
      if i>=N1:
        continue
      c += j
  return c


def check(S):
  c = count_large_2(A, BC, S)  
  return c >= K



Sl = 0
Sr = A[0]+B[0]+C[0]+1

while Sr - Sl > 1:
  mid = (Sl + Sr) // 2
  if check(mid):
    Sl = mid
  else:
    Sr = mid


Ans = []
for a in A:
  for b in B:
    if a + b + C[0] < Sr:
      break
    for c in C:
      if a + b + c >= Sr:
        Ans.append(a+b+c)
      else:
        break

Ans.sort(reverse=True)
Ans = Ans + [Sl]*(K-len(Ans))
for ans in Ans:
  print(ans)

Submission status: Time Limit Exceeded

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=sorted([x+y for x in a for y in b],reverse=True)[:k]
abc=sorted([x+y for x in ab for y in c],reverse=True)[:k]
for i in abc:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
import sys


def solve(k, ax, bx, cx):
    ab = []
    for a in ax:
        for b in bx:
            ab.append(a+b)

    ab.sort(reverse=True)
    abk = ab[:k]

    ans = []
    for c in cx:
        for abi in abk:
            ans.append(c+abi)

    return sorted(ans, reverse=True)[:k]


if __name__ == '__main__':
    _, _, _, k = map(int, input().split())
    ax = list(map(int, sys.stdin.readline().split()))
    bx = list(map(int, sys.stdin.readline().split()))
    cx = list(map(int, sys.stdin.readline().split()))

    ans = solve(k, ax, bx, cx)

    for a in ans:
        print(a)

Submission status: Time Limit Exceeded

Solution code:
import sys


def solve(k, ax, bx, cx):
    ab = sorted([a + b for a in ax for b in bx], reverse=True)[:k]
    abc = sorted([abi + c for abi in ab for c in cx], reverse=True)[:k]

    return abc


if __name__ == '__main__':
    _, _, _, k = map(int, input().split())
    *ax, = map(int, sys.stdin.readline().split())
    *bx, = map(int, sys.stdin.readline().split())
    *cx, = map(int, sys.stdin.readline().split())

    ans = solve(k, ax, bx, cx)

    for a in ans:
        print(a)

Submission status: Time Limit Exceeded

Solution code:
def solve(k,ax,bx,cx):
    ab = []
    for a in ax:
        for b in bx:
            ab.append(a+b)

    ab.sort(reverse=True)
    abk = ab[:k]

    ans = []
    for c in cx:
        for abi in abk:
            ans.append(c+abi)

    return sorted(ans, reverse=True)

if __name__ == '__main__':
    _,_,_,k = map(int, input().split())
    ax = list(map(int,input().split()))
    bx = list(map(int,input().split()))
    cx = list(map(int,input().split()))
    ans = solve(k,ax,bx,cx)
    for a in ans[:k]:
        print(a)

Submission status: Time Limit Exceeded

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

bc = [x + y for y in c for x in b]
bc.sort()
bcl = len(bc)

def bs_l(f, high, low=0):
    l, r = low-1, high+1
    while r - l > 1:
        x = (l + r) // 2
        if f(x): l = x
        else: r = x
    return l

def detect(s):
    cnt = 0
    for x in a:
        t = max(0, s-x)
        cnt += bcl - bisect_left(bc, t)
    return cnt >= k

res = bs_l(detect, 10**10+1)
ans = []
for x in a:
    t = max(0, res-x)
    for i in range(bcl-1, bisect_left(bc, t)-1, -1):
        ans.append(x + bc[i])

ans.sort(reverse=True)
print(*ans[:k], sep="\n")

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

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

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    X, Y, Z, K = map(int, input().split())
    A, B, C = [sorted(map(int, input().split())) for _ in range(3)]

    AB = []
    for i, a in enumerate(A):
        for j, b in enumerate(B):
            AB.append(a + b)
    AB = sorted(AB, reverse=True)[:K]

    ans = []
    for i, ab in enumerate(AB):
        for j, c in enumerate(C):
            ans.append(ab + c)
    ans = sorted(ans, reverse=True)[:K]
    for i in ans:
        print(i)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = sorted(map(int, input().split()), reverse=True)

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

d = sorted(d, reverse=True)
d = d[0:min(k,len(d))]
w = len(d)

e = []
e_append = e.append
for i in range(min(k,z)):
    for j in range(min(k,w)):
        e_append(c[i]+d[j])

e = sorted(e, reverse=True)
for f in e[0:k]:
    print(f)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = map(int, input().split())
a = sorted(map(int, input().split()), reverse=True)
b = sorted(map(int, input().split()), reverse=True)
c = sorted(map(int, input().split()), reverse=True)

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

d = sorted(d, reverse=True)
d = d[0:min(k,len(d))]
w = len(d)

e = []
e_append = e.append
for i in range(min(k,z)):
    for j in range(min(k,w)):
        e_append(c[i]+d[j])

e = sorted(e, reverse=True)
for f in e[0:k]:
    print(f)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = map(int, input().split())
a = sorted(map(int, input().split()), reverse=True)
b = sorted(map(int, input().split()), reverse=True)
c = sorted(map(int, input().split()), reverse=True)

d = []
for i in range(min(x,int(k*1.4))):
    for j in range(min(y,int(k*1.4))):
        d.append(a[i]+b[j])

d = sorted(d, reverse=True)
d = d[0:min(k,len(d))]
w = len(d)

e = []
for i in range(min(k,z)):
    for j in range(w):
        e.append(c[i]+d[j])

e = sorted(e, reverse=True)
for f in e[0:k]:
    print(f)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = map(int, input().split())
a = sorted(map(int, input().split()), reverse=True)
b = sorted(map(int, input().split()), reverse=True)
c = sorted(map(int, input().split()), reverse=True)

d = []
for i in range(min(x,int(k*1.5))):
    for j in range(y):
        d.append(a[i]+b[j])

d = sorted(d, reverse=True)
d = d[0:min(k,len(d))]
w = len(d)

e = []
for i in range(min(k,z)):
    for j in range(min(k,w)):
        e.append(c[i]+d[j])

e = sorted(e, reverse=True)
for f in e[0:k]:
    print(f)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = map(int, input().split())
a = sorted(map(int, input().split()), reverse=True)
b = sorted(map(int, input().split()), reverse=True)
c = sorted(map(int, input().split()), reverse=True)

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

d = sorted(d, reverse=True)
d = d[0:min(k,len(d))]
w = len(d)

e = []
for i in range(min(k,z)):
    for j in range(min(k,w)):
        e.append(c[i]+d[j])

e = sorted(e, reverse=True)
for f in e[0:k]:
    print(f)

Submission status: Time Limit Exceeded

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

for c in range(z):
    for b in range(y):
        for a in range(x):
            result.append(A[a] + B[b] + C[c])

result.sort()
[print(val) for val in result[-k:][::-1]]

Submission status: Time Limit Exceeded

Solution code:
import heapq
X,Y,Z,K = map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
oishisa_list=[]

for i_A in A:
  for i_B in B:
    for i_C in C:
      oishisa_list.append((-1)*(i_A + i_B + i_C))

#print(oishisa_list)
heapq.heapify(oishisa_list)
#print(oishisa_list)

for _ in range(K):
  temp=heapq.heappop(oishisa_list)
  print((-1)*temp)

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
A_lst = sorted(list(map(int, input().split())))
B_lst = sorted(list(map(int, input().split())))
C_lst = sorted(list(map(int, input().split())))

ans = sorted([ a+b+c for a in A_lst for b in B_lst for c in C_lst ], reverse=True)
for i in range(K):
  print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
# 想定解法1

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)

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

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

ABC = []
for ab in AB:
  for c in C:
    ABC.append(ab+c)

ABC.sort(reverse=True)
ABC = ABC[:K]

for ans in ABC:
  print(ans)

Submission status: Time Limit Exceeded

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)

import heapq
nums=[[-(A[0]+B[0]+C[0]), 0, 0, 0]]
heapq.heapify(nums)

sumi=[[[0]*Z for __ in range(Y)] for _ in range(X)]
for _ in range(K):
  out, a, b, c=heapq.heappop(nums)
  print(-out)
  if a<X-1 and sumi[a+1][b][c]==0:
    heapq.heappush(nums, [-(A[a+1]+B[b]+C[c]), a+1, b, c])
    sumi[a+1][b][c]=1
  if b<Y-1 and sumi[a][b+1][c]==0:
    heapq.heappush(nums, [-(A[a]+B[b+1]+C[c]), a, b+1, c])
    sumi[a][b+1][c]=1
  if c<Z-1 and sumi[a][b][c+1]==0:
    heapq.heappush(nums, [-(A[a]+B[b]+C[c+1]), a, b, c+1])
    sumi[a][b][c+1]=1

Submission status: Time Limit Exceeded

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=[-A[i] for i in range(X)]
B=[-B[i] for i in range(Y)]
C=[-C[i] for i in range(Z)]
A.sort()
B.sort()
C.sort()

import heapq
nums=[[A[0]+B[0]+C[0], 0, 0, 0]]
heapq.heapify(nums)

sumi=[[[0]*Z for __ in range(Y)] for _ in range(X)]
for _ in range(K):
  out, a, b, c=heapq.heappop(nums)
  print(-out)
  if a<X-1 and sumi[a+1][b][c]==0:
    heapq.heappush(nums, [A[a+1]+B[b]+C[c], a+1, b, c])
    sumi[a+1][b][c]=1
  if b<Y-1 and sumi[a][b+1][c]==0:
    heapq.heappush(nums, [A[a]+B[b+1]+C[c], a, b+1, c])
    sumi[a][b+1][c]=1
  if c<Z-1 and sumi[a][b][c+1]==0:
    heapq.heappush(nums, [A[a]+B[b]+C[c+1], a, b, c+1])
    sumi[a][b][c+1]=1

Submission status: Time Limit Exceeded

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=[-A[i] for i in range(X)]
B=[-B[i] for i in range(Y)]
C=[-C[i] for i in range(Z)]
A.sort()
B.sort()
C.sort()

import heapq
nums=[[A[0]+B[0]+C[0], 0, 0, 0]]
heapq.heapify(nums)

sumi=[[[0]*Z for __ in range(Y)] for _ in range(X)]
sumi[0][0][0]=1
for _ in range(K):
  out, a, b, c=heapq.heappop(nums)
  print(-out)
  if a<X-1 and sumi[a+1][b][c]==0:
    heapq.heappush(nums, [A[a+1]+B[b]+C[c], a+1, b, c])
    sumi[a+1][b][c]=1
  if b<Y-1 and sumi[a][b+1][c]==0:
    heapq.heappush(nums, [A[a]+B[b+1]+C[c], a, b+1, c])
    sumi[a][b+1][c]=1
  if c<Z-1 and sumi[a][b][c+1]==0:
    heapq.heappush(nums, [A[a]+B[b]+C[c+1], a, b, c+1])
    sumi[a][b][c+1]=1

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())

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

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

AB.sort(reverse=True)

ABC = []
for i in range(min(K, len(AB))):
    for j in range(Z):
        ABC.append(AB[i] + C[j])

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
#解法4 二分探索
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 bin_isOK(P):
    cnt=0
    value=0
    for i in range(X):
        for j in range(Y):
            for k in range(Z):
                if A[i]+B[j]+C[k]<P:
                    break
                cnt+=1
                if cnt>=K:
                    return True
    return False


def binary_search():
    ng = A[0]+B[0]+C[0]
    ok = -1

    while abs(ok - ng) >1:
        mid = (ok + ng)//2
        
        if bin_isOK(mid):
            ok = mid
        else:
            ng = mid
    
    return ok

border=binary_search()
ABC=[]

for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if A[i]+B[j]+C[k]>=border:
                ABC.append(A[i]+B[j]+C[k])
            if len(ABC)>=K:
                break

ABC.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
#解法1 ABのK番まで　＋　Cの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()))

AB=[]

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

AB.sort(reverse=True)
C.sort(reverse=True)

ABC=[]

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

ABC.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
#解法1 ABのK番まで　＋　Cの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()))

AB=[]

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

AB.sort(reverse=True)
C.sort(reverse=True)

ABC=[]

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

ABC.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
import sys

x, y, z, k = map(int ,sys.stdin.readline().split())
a, b, c = (sorted(map(int, sys.stdin.readline().split())) for _ in range(3))

def main():
    res = sorted([a[i] + b[j] for i in range(x) for j in range(y)], reverse=True)
    res2 = sorted([res[i] + c[j] for i in range(min(k, x * y)) for j in range(z)], reverse=True)
    return res2[:k]

if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_left as bi_l, bisect_right as bi_r


x, y, z, k = map(int, sys.stdin.readline().split())
a, b, c = (sorted(map(int, sys.stdin.readline().split())) for _ in range(3))

def count_atleast_k(border):
    combs = 0
    for i in range(x):
        for j in range(y):
            combs += z - bi_r(c, border - a[i] - b[j])
    return combs >= k

def main():
    hi = 3 * 10 ** 10 + 1
    lo = 2
    while lo + 1 < hi:
        border = (lo + hi) // 2
        if count_atleast_k(border):
            lo = border
        else:
            hi = border

    res = []
    for i in range(x-1, -1, -1):
        if hi - a[i] > b[-1] + c[-1]:
            break
        for j in range(y-1, -1, -1):
            if hi - a[i] - b[j] > c[-1]:
                break
            for k in range(z-1, -1, -1):
                if hi - a[i] - b[j] - c[k] > 0:
                    break
                res.append(a[i] + b[j] + c[k])
    
    return sorted(res, reverse=True)

if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
#AC済み
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()
b.sort()
c.sort()
ab=[-a[i]-b[j] for j in range(y) for i in range(x)]
ab.sort()
ab=ab[:k]
l=len(ab)
abc=[ab[i]-c[j] for j in range(z) for i in range(l)]
abc.sort()
for i in range(k):
    print(-abc[i])

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k=map(int,input().split())
a=sorted([int(i) for i in input().split()])
b=sorted([int(i) for i in input().split()])
c=sorted([int(i) for i in input().split()],reverse=True)
ab=[]
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort(reverse=True)
ab=ab[:k]
l=len(ab)
abc=[]
for i in range(l):
    for j in range(z):
        abc.append(ab[i]+c[j])
abc.sort(reverse=True)
for i in range(k):
    print(abc[i])

Submission status: Time Limit Exceeded

Solution code:
import heapq
x,y,z,k=map(int,input().split())
a=sorted([int(i) for i in input().split()],reverse=True)
b=sorted([int(i) for i in input().split()],reverse=True)
c=sorted([int(i) for i in input().split()],reverse=True)

def solve(p):
    cnt=0
    for i in range(x):
        for j in range(y):
            for k in range(z):
                if a[i]+b[j]+c[k]>=p:
                    cnt+=1
                    if cnt>=k:
                        return True
                else:
                    break
    return False

l,r=0,a[0]+b[0]+c[0]
while r>l+1:
    t=(l+r)//2
    if solve(t):
        r=t
    else:
        l=t
ans=[]
for i in range(x):
    for j in range(y):
        for m in range(z):
            if a[i]+b[j]+c[m]>=r:
                heapq.heappush(ans,-(a[i]+b[j]+c[m]))
            else:
                break
for i in range(k):
    print(-heapq.heappop(ans))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
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=sorted([int(i) for i in input().split()])
ab_max=[]
for i in range(x):
    for j in range(y):
        ab_max.append(a[i]+b[j])
ab_max.sort()
def unti(t_sub):
    global x,y,z,k
    ret=0
    for i in range(x*y):
        h=z-bisect_left(c,t_sub-ab_max[i])
        if h==0 or ret>=k:
            return ret
        else:
            ret+=h
    return ret
l,r=0,ab_max[0]+c[-1]
while r>l+1:
    t=(l+r)//2
    if unti(t)>=k:
        r=t
    else:
        l=t
ans=[]
f=False
for i in range(x*y):
    for j in range(z-1,-1,-1):
        u=ab_max[i]+c[j]
        if u>=r:
            ans.append(u)
        else:
            if j==0:f=True
            break
    if f:
        break
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
AB = [a+b for a in A for b in B]
AB.sort(reverse=True)
AB = AB[:K]
ABC = [ab+c for ab in AB for c in C]
ABC.sort(reverse=True)
ABC = ABC[:K]
for abc in ABC:
    print(abc)

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
*A, = sorted(map(int, input().split()), reverse=True)
*B, = sorted(map(int, input().split()), reverse=True)
*C, = sorted(map(int, input().split()), reverse=True)
AB = [A[x] + B[y] for y in range(Y) for x in range(X)]
AB.sort(reverse=True)
AB = AB[:K]
XY = len(AB)
ABC = [AB[xy] + C[z] for xy in range(XY) for z in range(Z)]
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
*A, = sorted(map(int, input().split()), reverse=True)
*B, = sorted(map(int, input().split()), reverse=True)
*C, = sorted(map(int, input().split()), reverse=True)
AB = [A[x]+B[y] for x in range(X) for y in range(Y)]
AB.sort(reverse=True)
AB = AB[:K]
XY = len(AB)
ABC = [AB[xy]+C[z] for xy in range(XY) for z in range(Z)]
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
AB = [A[x]+B[y] for x in range(X) for y in range(Y)]
AB.sort(reverse=True)
AB = AB[:K]
XY = len(AB)
ABC = [AB[xy]+C[z] for xy in range(XY) for z in range(Z)]
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])