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:
from heapq import heapify, heappush, heappop, heappushpop

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

sum12s = []
for A in As:
    for B in Bs:
        sum12s.append(A+B)
sum12s.sort(reverse=True)

PQ = [Cs[0]+sum12 for sum12 in sum12s[:K]]
heapify(PQ)
for C in Cs[1:]:
    for sum12 in sum12s[:K]:
        heappush(PQ, C+sum12)
    for _ in range(K, len(PQ)):
        heappop(PQ)

anss = []
for _ in range(K):
    anss.append(heappop(PQ))
print('\n'.join(map(str, anss[::-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()))
P = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            P.append(A[i]+B[j]+C[k])

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

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = map(int,input().split())
lisa = list(map(int,input().split()))
lisb = list(map(int,input().split()))
lisc = list(map(int,input().split()))
lisa.sort(reverse=True)
lisb.sort(reverse=True)
lisc.sort(reverse=True)
an = []
ans = []
for i in range(x):
    for j in range(y):
        an.append(lisa[i]+lisb[j])
an.sort(reverse=True)
an = an[:min(3001,x*y*z+1)]
for i in range(z):
    for nu in an:
        ans.append(lisc[i]+nu)
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import product

def main():
    input = sys.stdin.readline
    X, Y, Z, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))

    A = sorted(A, key=lambda x: -x)
    B = sorted(B, key=lambda x: -x)
    C = sorted(C, key=lambda x: -x)

    n = 1
    while n^3 < K:
        n += 1

    A = A[:n]
    B = B[:n]
    C = C[:n]

    delicious = []
    for x in product(A, B, C):
        delicious.append(sum(x))

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
d1 = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
k = list()
for num1 in a:
    for num2 in b:
        for num3 in c:
            k.append(num1+num2+num3)
k.sort(reverse=True)
for n in range(0, d1[3]):
    print(k[n])

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

p = []
for i in range(x):
    for j in range(y):
        for h in range(z):
            p.append(a[i]+b[j]+c[h])
p.sort()
for i in range(len(p)-1,len(p)-k-1, -1):
    print(p[i])

Submission status: Time Limit Exceeded

Solution code:
import itertools
x,y,z,k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
l = list(itertools.product(a, b, c))
m = list(map(lambda x: sum(x), l))
m2 = sorted(m, reverse=True)
for i in range(k):
  print(m2[i])

Submission status: Time Limit Exceeded

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

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

    ab = [aa + bb for aa in a for bb in b]
    ab.sort(reverse=True)
    ab = ab[:3000]

    abc = [aabb + cc for aabb in ab for cc in c]
    abc.sort(reverse=True)

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


main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import product

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

ret = [sum(x) for x in product(A, B, C)]
ret.sort(reverse=True)
for r in ret[0:K]:
    print(r)

Submission status: Time Limit Exceeded

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

A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))

AB_list = [0]*(X*Y)
for i in range(X):
    for j in range(Y):
        AB_list[i*Y+j] = A_list[i] + B_list[j]
AB_list_s = sorted(AB_list)
if len(AB_list_s) > 3000:
    AB_list_s = AB_list_s[-3000:]
W = len(AB_list_s)
ABC_list = [0]*(W*Z)
for i in range(W):
    for j in range(Z):
        ABC_list[i*Z+j] = AB_list_s[i] + C_list[j]
ABC_list_s = sorted(ABC_list)
for i in range(K):
    print(ABC_list_s[-(i+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()))
li=[]
for h in a:
    for i in b:
        for j in c:li+=[h+i+j]
li=sorted(li)[::-1]
for i in range(k):
    print(li[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()))

sum_ab = [0] * (X * Y)

count = 0
for i, a in enumerate(A):
    for j, b in enumerate(B):
        sum_ab[count] = a + b
        count += 1

sum_ab.sort(reverse = True)

sum_abc = [0] * (min(X * Y, 3000) * Z)

count = 0
for i, ab in enumerate(sum_ab[0:min(3000, X * Y)]):
    for j, c in enumerate(C):
        sum_abc[count] = ab + c
        count += 1

sum_abc.sort(reverse = True)

for temp in sum_abc[0:K]:
    print(temp)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8-*-

def f (A, B, K):
    r = []
    for i in A:
        for j in B:
            r.append(i + j)
    r.sort()
    r.reverse()
    return r[:K]


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(" ")))
    D = f(A, B, K)
    E = f(D, C, K)

    for i in E:
        print(i)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,w=map(int,input().split())
lx=list(map(int,input().split()))
ly=list(map(int,input().split()))
lz=list(map(int,input().split()))
l=[]
for i in range(x):
  for j in range(y):
    for k in range(z):
      l.append(lx[i]+ly[j]+lz[k])
l=sorted(l)
for i in range(w):
  print(l[-1-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()))
a.sort()
a.reverse()
b.sort()
b.reverse()
c.sort()
c.reverse()
ans=[]
if x*y*z<=3000:
  p=x
  q=y
  r=z
else:
  p=min(x,3000)
  q=min(y,3000)
  r=min(z,3000)
for i in range(p):
  for j in range(q):
    for h in range(r):
      ans.append(a[i]+b[j]+c[h])
ans.sort()
ans.reverse()
for i in range(k):
  print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
x, y, z, k = map(int, input().split())
X = [int(i) for i in input().split()]
Y = [int(i) for i in input().split()]
Z = [int(i) for i in input().split()]

count = []

for i in X:
    for j in Y:
        for l in Z:
            count.append(i+j+l)

count.sort(reverse=1)

for c in range(k):
    print(count[c])

Submission status: Time Limit Exceeded

Solution code:
import itertools

X, Y, Z, K = [int(i) for i in 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()])

A = A[::-1][:K]
B = B[::-1][:K]
C = C[::-1][:K]


result = sorted([
  a+b+c for a, b, c in itertools.product(A, B, C)])
result = result[::-1]

for r in result[:K]:
  print(r)

Submission status: Time Limit Exceeded

Solution code:
x, y, z, kk = list(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):
    for k in range(z):
      d.append(a[i]+b[j]+c[k])
d.sort(reverse=True)

for i, e in enumerate(d):
  if i>=kk:
    break
  print(e)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,w=map(int,input().split())
lx=list(map(int,input().split()))
ly=list(map(int,input().split()))
lz=list(map(int,input().split()))
l=[]
for i in range(x):
  for j in range(y):
    for k in range(z):
      l.append(lx[i]+ly[j]+lz[k])
l=sorted(l)
for i in range(w):
  print(l[-1-i])

Submission status: Time Limit Exceeded

Solution code:

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

A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

AB = []

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import itertools
import sys
import math
from functools import lru_cache

# 1整数
# n = int(input())

# 空白区切り2変数
from queue import Queue
from operator import mul
from functools import reduce
from queue import Queue
from operator import mul
from functools import reduce
from functools import lru_cache

input = sys.stdin.readline

# N個整数
x, y, z, k = list(map(int, input().split()))

# 1整数
# n = int(input())

# 空白区切りリスト整数
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ll = [0] * 1000001
count = 0
for i in a:
    for j in b:
        ll[count] = i + j
        count += 1
ll.sort(reverse=True)

ll = ll[0:3000]
c.sort(reverse=True)

lll = [0] * 3000001
count = 0
for i in ll:
    for j in c:
        lll[count] = i + j
        count += 1
lll.sort(reverse=True)

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

# 空白区切り文字列
# t, d = input().split()

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K=[int(i) for i in input().split(" ")]
A=[int(i) for i in input().split(" ")]
B=[int(i) for i in input().split(" ")]
C=[int(i) for i in input().split(" ")]

delicious=[]

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

for i in range(min(K,len(A))):
    for j in range(min(K,len(B))):
        for k in range(min(K,len(C))):
            delicious.append(A[i]+B[j]+C[k])

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

Submission status: Time Limit Exceeded

Solution code:
x, y, z, n = map(int, input().split())
f = []
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))
a.reverse()
b.reverse()
c.reverse()
for i in range(x) :
  for j in range(y) :
    for k in range(z) :
      f.append(a[i]+b[j]+c[k])
f = sorted(f)
f.reverse()
for i in range(n) :
  print(f[i])

Submission status: Time Limit Exceeded

Solution code:
import itertools

X, Y, Z, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]

result = sorted([
  a+b+c for a, b, c in itertools.product(A, B, C)])
result = result[::-1]

for r in result[:K]:
  print(r)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k=map(int,input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
ab =[]
for aa in a:
    for bb in b:
        ab.append(aa+bb)
ab.sort()
ab.reverse()
ab = ab[:k]
for x in ab:
    for cc in c:    
        ab.append(x+cc)
ab.sort()
ab.reverse()
for i in range(k):
    print(ab[i])

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k=map(int,input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
ab =[]
for aa in a:
    for bb in b:
        for cc in c:    
            ab.append(aa+bb+cc)
ab.sort()
ab.reverse()
for i in range(k):
    print(ab[i])

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K=[int(i) for i in input().split(" ")]
A=[int(i) for i in input().split(" ")]
B=[int(i) for i in input().split(" ")]
C=[int(i) for i in input().split(" ")]

delicious=[]

for a in A:
    for b in B:
        for c in C:
            delicious.append(a+b+c)

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

Submission status: Time Limit Exceeded

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

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


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


def read_float():
    return float(input())


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


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


def read_str_n():
    return list(map(str, input().split()))


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


def mt(f):
    import time

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

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

    return wrap


@mt
def slv(A, B, C, K):
    q = []
    for a in A:
        for b in B:
            for c in C:
                if len(q) == K:
                    heapq.heappushpop(q, a+b+c)
                else:
                    heapq.heappush(q, a+b+c)
    
    a = [ heapq.heappop(q) for _ in range(K) ]
    for v in reversed(a):
        print(v)
    


def main():
    # N = read_int()
    X, Y, Z, K = read_int_n()
    A = read_int_n()
    B = read_int_n()
    C = read_int_n()
    (slv(A, B, C, K))


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 = list(map(int, input().split()))

sumsofAandB = sorted([a+b for a in A for b in B])
sumsofAll = sorted([ab+c for ab in sumsofAandB for c in C], reverse=True)
for i in range(K):
    print(sumsofAll[i])

Submission status: Time Limit Exceeded

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

A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

AB = []

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

ABC = []

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

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

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
from math import ceil
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 = [x+y for x in b for y in c]
bc.sort()


def count_bigger_than(X):
    res = 0
    for v in a:
        res += len(bc)-bisect_right(bc,X-v-1)
    return res

  

right_bound = 3*10**10 + 1#以上の物の個数がK以下
left_bound = 0 #以上の物の個数がKより大きい

while right_bound - left_bound > 1:
    m = (right_bound + left_bound)//2
    if count_bigger_than(m)>k:
        left_bound = m
    else:
        right_bound = m


V = left_bound

ans_que = []

for v in a:
    for i in range(len(bc)-1,-1,-1):
        if v+bc[i]>=V:
            ans_que.append(v+bc[i])
        else:
            break

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

Submission status: Accepted

Solution code:
import sys
from heapq import heappop, heappush

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    X, Y, Z, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))
    C = list(map(int, readline().split()))

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

    hq = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]

    ans = [0] * K
    added = {(0, 0, 0)}

    for idx in range(K):
        taste, i, j, k = heappop(hq)
        ans[idx] = -taste
        if i < X - 1 and (i + 1, j, k) not in added:
            heappush(hq, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
            added.add((i + 1, j, k))
        if j < Y - 1 and (i, j + 1, k) not in added:
            heappush(hq, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
            added.add((i, j + 1, k))
        if k < Z - 1 and (i, j, k + 1) not in added:
            heappush(hq, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
            added.add((i, j, k + 1))

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
#D - Cake 123
import sys 
from heapq import *
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
X, Y, Z, K = map(int, readline().split())
A = list(map(int,readline().split()))
B = list(map(int,readline().split()))
C = list(map(int,readline().split()))

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

q = [(-A[0]-B[0]-C[0],0,0,0)]
heapify(q)
result = []
used = set()

while True:
    S,i,j,k = heappop(q)
    result.append(-S)
    if len(result) == K:
        break
    if i < X-1 and (i+1,j,k) not in used:
        heappush(q,(-A[i+1]-B[j]-C[k],i+1,j,k))
        used.add((i+1,j,k))
    if j < Y-1 and (i,j+1,k) not in used:
        heappush(q,(-A[i]-B[j+1]-C[k],i,j+1,k))
        used.add((i,j+1,k))
    if k < Z-1 and (i,j,k+1) not in used:
        heappush(q,(-A[i]-B[j]-C[k+1],i,j,k+1))
        used.add((i,j,k+1))

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
#D - Cake 123
import sys 
from itertools import product
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
X, Y, Z, K = map(int, readline().split())
A = list(map(int,readline().split()))
B = list(map(int,readline().split()))
C = list(map(int,readline().split()))

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

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

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

Submission status: Accepted

Solution code:
import sys
from heapq import heappush, heappop

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    X, Y, Z, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))
    C = list(map(int, readline().split()))

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

    hq = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
    ans = [0] * K
    seen = {(0, 0, 0)}

    for idx in range(K):
        s, i, j, k = heappop(hq)
        ans[idx] = -s

        if i < X - 1 and (i + 1, j, k) not in seen:
            heappush(hq, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
            seen.add((i + 1, j, k))
        if j < Y - 1 and (i, j + 1, k) not in seen:
            heappush(hq, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
            seen.add((i, j + 1, k))
        if k < Z - 1 and (i, j, k + 1) not in seen:
            heappush(hq, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
            seen.add((i, j, k + 1))

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    X, Y, Z, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))
    C = list(map(int, readline().split()))

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

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

    ABC = [ab + c for ab in AB[:K] for c in C[:K]]
    ABC.sort(reverse=True)

    print(*ABC[:K], sep='\n')

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
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)

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

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

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)

    # heappush(heap, (-(a[i]+b[j]+c[k]), i, j, k))
    heap = []
    heappush(heap, (-(a[0]+b[0]+c[0]), 0, 0, 0))
    s = set()
    for _ in range(k):
        value, i, j, k = heappop(heap)
        print(-value)
        if (i+1, j, k) not in s and i < x - 1:
            s.add((i+1, j, k))
            heappush(heap, (-(a[i+1]+b[j]+c[k]), i+1, j, k))
        if (i, j+1, k) not in s and j < y - 1:
            s.add((i, j+1, k))
            heappush(heap, (-(a[i]+b[j+1]+c[k]), i, j+1, k))
        if (i, j, k+1) not in s and k < z - 1:
            s.add((i, j, k+1))
            heappush(heap, (-(a[i]+b[j]+c[k+1]), i, j, k+1))

main()

Submission status: Accepted

Solution code:
import sys
from collections import deque
from functools import lru_cache
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]

def main():
    X, Y, Z, K = read_values()
    A = read_list()
    B = read_list()
    C = read_list()

    AB = sorted([a + b for a in A for b in B], reverse=True)
    C.sort()

    P = []
    for c in C:
        heapq.heappush(P, (-(c + AB[0]), 0))
    
    for _ in range(K):
        s, i = heapq.heappop(P)
        print(-s)
        if i + 1 < len(AB):
            heapq.heappush(P, (s + (AB[i] - AB[i + 1]), i + 1))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
icase=0
if icase==0:
    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)

kk=1

ix=0
iy=0
iz=0
ss=[]
s=a[ix]+b[iy]+c[iz]

xyz=[]
xyz.append((ix+1,iy,iz))
xyz.append((ix,iy+1,iz))
xyz.append((ix,iy,iz+1))

#print(ix,iy,iz,s)
#print(xyz)
ss.append(s)
while kk<k:
    kk+=1
    s1=0
    for ix,iy,iz in xyz:
        s2=a[ix]+b[iy]+c[iz]
        if s2>s1:
            s1=s2
            ixx,iyy,izz=ix,iy,iz
#    print("1:",ixx,iyy,izz,s2)
    xyz.remove((ixx,iyy,izz))
    if ixx<x-1 and ((ixx+1,iyy,izz) not in xyz): 
        xyz.append((ixx+1,iyy,izz))
    if iyy<y-1 and ((ixx,iyy+1,izz) not in xyz): 
        xyz.append((ixx,iyy+1,izz))
    if izz<z-1 and ((ixx,iyy,izz+1) not in xyz): 
        xyz.append((ixx,iyy,izz+1))
#    print(ixx,iyy,izz,s1)
#    print(xyz)
    ss.append(s1)

for kk in range(k):
    print(ss[kk])

Submission status: Accepted

Solution code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()


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)

    L = []

    for i in range(X):
        s =K//(i+1)
        for j in range(min(Y, s + 1)):
            t =A[i] +B[j]
            for k in range(min(Z, s // (j + 1) + 1)):
                L.append(t + C[k])

    L.sort(reverse=True)

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()


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)

    L = []

    for i in range(X):
        for j in range(min(Y, K // (i + 1) + 1)):
            for k in range(min(Z, K // (i + 1) // (j + 1) + 1)):
                L.append(A[i] + B[j] + C[k])

    L.sort(reverse=True)

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import heapq


# \n
def input():
    return sys.stdin.readline().rstrip()


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 = []
    S = set()
    heapq.heapify(Q)
    heapq.heappush(Q, (-A[0] - B[0] - C[0], 0, 0, 0))
    for i in range(K):
        m, a, b, c = heapq.heappop(Q)
        print(-m)
        if (a + 1, b, c) not in S and a + 1 < X:
            heapq.heappush(Q, (-A[a + 1] - B[b] - C[c], a + 1, b, c))
            S.add((a + 1, b, c))
        if (a, b + 1, c) not in S and b + 1 < Y:
            heapq.heappush(Q, (-A[a] - B[b + 1] - C[c], a, b + 1, c))
            S.add((a, b + 1, c))
        if (a, b, c + 1) not in S and c + 1 < Z:
            heapq.heappush(Q, (-A[a ] - B[b] - C[c+1], a, b, c + 1))
            S.add((a, b, c + 1))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import heapq


# \n
def input():
    return sys.stdin.readline().rstrip()


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)

    L=[]

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

    L.sort(reverse=True)

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



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import heapq


# \n
def input():
    return sys.stdin.readline().rstrip()


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)

    a = 0
    b = 0
    c = 0
    k = 1

    L = []

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

    L.sort(reverse=True)
    newL = L[:min(3000, X * Y)]
    XY = len(newL)
    L=[]
    for i in range(XY):
        for j in range(Z):
            L.append(newL[i]+C[j])

    L.sort(reverse=True)

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



if __name__ == "__main__":
    main()