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

ma,mb,mc = A[0],B[0],C[0]
sA = [a-ma for a in A] + [-10**15]
sB = [b-mb for b in B] + [-10**15]
sC = [c-mc for c in C] + [-10**15]

ia,ib,ic = 0,0,0
while min(10**7,(X)*(Y)*(Z)) > (ia+1)*(ib+1)*(ic+1):
    m = max(sA[ia+1],sB[ib+1],sC[ic+1])
    if ia < (X-1) and sA[ia+1] == m:
        ia = min(ia+1,X-1)
    if ib < (Y-1) and sB[ib+1] == m:
        ib = min(ib+1,Y-1)
    if ic < (Z-1) and sC[ic+1] == m:
        ic = min(ic+1,Z-1)
ans = []
for a in A[:ia+1]:
    for b in B[:ib+1]:
        for c in C[:ic+1]:
            ans.append(a+b+c)
ans.sort(reverse=True)
for out in ans[:K]:
    print(out)

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

AB=[]

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

AB=sorted(AB,reverse=True)[:K:]

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

ABC=sorted(ABC,reverse=True)[:K:]


for abc in ABC:
    print(abc)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

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)

L_a = []
L_b = []
L_c = []
for i in range(X) :
    L_a.append(A[0] - A[i])

for i in range(Y) :
    L_b.append(B[0] - B[i])

for i in range(Z) :
    L_c.append(C[0] - C[i])

L = []

P = list(product(range(max(X,Y,Z)), repeat=3))
su = A[0]+B[0]+C[0]
for i in range(len(P)) :
    l = su - (L_a[P[i][0]] + L_b[P[i][1]] + L_c[P[i][2]])
    L.append(l)
L = sorted(L,reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
x, y, z, k = [int(x) for x in 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()]

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

ans = []
for i in A:
    for j in B:
        for l in C:
            ans.append(i+j+l)
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_total_list = []
total_list = []

for i in A:
    for j in B:
        ab_total_list.append(i+j)
for i in ab_total_list:
    for j in C:
        total_list.append(i+j)
total_list.sort()
total_list.reverse()
for i in range(0, K):
    print(total_list[i])

Submission status: Time Limit Exceeded

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

D = []
for a, b, c in itertools.product(A, B, C):
    d = a + b + c
    D.append(d)
    
D.sort(reverse = True)
for d in D[:K]:
    print(d)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

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

a = input().split()
b = input().split()
c = input().split()

a_int = [int(i) for i in a]
b_int = [int(i) for i in b]
c_int = [int(i) for i in c]

total = []

for x in range(len(a)):
    for y in range(len(b)):
        for z in range(len(c)):
            su = a_int[x] + b_int[y] + c_int[z]
            total.append(su)

total.sort(reverse = True)

for i in range(K):
    print(total[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(' ')))
taste = []
for i in range(x):
    for j in range(y):
        for l in range(z):
            taste.append(a[i] + b[j] + c[l])
taste.sort()
for i in range(k):
    print(taste[-1-i])

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  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()))
  res = [a+b+c for a in A for b in B for c in C]
  res.sort(reverse = True)
  print("\n".join(list(map(str,res[:K]))))

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K= map(int,input().split())
A = [int(j) for j in input().split()]
B = [int(j) for j in input().split()]
C = [int(j) for j in input().split()]
lists =[]
if X > K:
    A.sort(reverse=True)
if Y > K:
    B.sort(reverse=True)
if Z > K:
    C.sort(reverse=True)

for a in A[0:K]:
    for b in B[0:K]:
        for c in C[0:K]:
            lists.append(a+b+c)
lists.sort(reverse=True)
for i in lists[0:K]:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = [int(s) for s in input().split(' ')]
a = [int(s) for s in input().split()]
b = [int(s) for s in input().split()]
c = [int(s) for s in input().split()]

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


ans = sorted(ans,reverse = True)
for i in range(k):
    print(ans[i],'\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()))

ansList=[]
for m in range(x):
  for n in range(y):
    for o in range(z):
      ansList.append(a[m]+b[n]+c[o])

ansList.sort()
ansList.reverse()

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding:utf-8 -*-


def solve():
    """ TLE """
    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(" ")))

    ans = []

    for x in range(X):
        for y in range(Y):
            ab = A[x] + B[y]
            for z in range(Z):
                abc = ab + C[z]
                if len(ans) < K-1:
                    ans.append(abc)
                    continue
                elif len(ans) == K-1:
                    ans.append(abc)
                    ans.sort(reverse=True)
                    continue
                
                if abc > ans[-1]:
                    ans[-1] = abc
                    ans.sort(reverse=True)
                    continue

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


if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

X, Y, Z, K = LI()
A = sorted(LI(), reverse=True)
B = sorted(LI(), reverse=True)
C = sorted(LI(), reverse=True)

A = A[:min(300, X)]
B = B[:min(300, Y)]
C = C[:min(300, Z)]
# Aの中で最大の数字足すBの最大、Cの最大は全体の中で最大
# 3000個大きい順に求めれば良い

# iが決まれば、j,kのおおきさは分かる
BC = []
# 1000*1000 で10**6の計算量
for j in range(len(B)):
    for k in range(len(C)):
        BC.append(B[j] + C[k])

points = []
# 10**3 と 10** 6
for i in range(len(A)):
    points += [bc + A[i] for bc in BC]
#  #  for i in range(len(A)):
#  #      for bc in BC:
#  #          points.append(A[i]+bc)
#  #      #  for j in range(len(B)):
#  #      #      for k in range(len(C)):
#  #      #          point = A[i] + B[j] + C[k]
#  #      #          points.append(point)
for i in sorted(points, reverse=True)[:K]:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
import itertools

X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
if X > K:
    A.sort(reverse=True)
    A = A[:K]
B = list(map(int, input().split()))
if Y > K:
    B.sort(reverse=True)
    B = B[:K]
C = list(map(int, input().split()))
if Z > K:
    C.sort(reverse=True)
    C = C[:K]

AB = []
for p in itertools.product(A, B):
    AB.append(sum(p))
AB.sort(reverse=True)
AB = AB[:K]

l = []
for p in itertools.product(AB, C):
    l.append(sum(p))
l.sort(reverse=True)
for i in range(K):
    print(l[i])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import math

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

cnum = []

takadaka = math.ceil(k ** 0.75)

for i in range(min(takadaka,x)):
    for j in range(min(takadaka,y)):
        for l in range(min(takadaka,z)):
            cnum.append(a[i] + b[j] + c[l])

cnum.sort()
cnum.reverse()

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import heapq

def readInts():
  return [int(s) for s in input().split(" ")]

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

  heap = []

  for a in A:
    for b in B:
      for c in C:
        s = a + b + c
        if len(heap) >= K and heap[0] < s:
          heapq.heapreplace(heap, s)
        elif len(heap) < K:
          heapq.heappush(heap, s)

  ans = reversed([heapq.heappop(heap) for _ in range(K)])
  for e in ans:
    print(e)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
xyzk_, a_, b_, c_ = [input() for _ in range(4)]

x, y, z, k = [int(w) for w in xyzk_.split()]
a_nums = [int(n) for n in a_.split()]
b_nums = [int(n) for n in b_.split()]
c_nums = [int(n) for n in c_.split()]

sums = []
for a in a_nums:
    for b in b_nums:
        for c in c_nums:
            sums.append(a + b + c)

sums.sort(reverse=True)

for n in sums[:k]:
    print(n)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

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)

L_a = []
L_b = []
L_c = []
for i in range(X) :
    L_a.append(A[0] - A[i])

for i in range(Y) :
    L_b.append(B[0] - B[i])

for i in range(Z) :
    L_c.append(C[0] - C[i])

L = []

P = list(product(range(max(X,Y,Z)), repeat=3))
su = A[0]+B[0]+C[0]
for i in range(len(P)) :
    try :
        l = su - (L_a[P[i][0]] + L_b[P[i][1]] + L_c[P[i][2]])
        L.append(l)
    except IndexError :
        print("error")
        continue
#print(L)
L = sorted(L,reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

X, Y, Z, K = LI()
A = sorted(LI(), reverse=True)
B = sorted(LI(), reverse=True)
C = sorted(LI(), reverse=True)

A = A[:min(500, X)]
B = B[:min(500, Y)]
C = C[:min(500, Z)]
# Aの中で最大の数字足すBの最大、Cの最大は全体の中で最大
# 3000個大きい順に求めれば良い

# iが決まれば、j,kのおおきさは分かる
BC = []
# 1000*1000 で10**6の計算量
for j in range(len(B)):
    for k in range(len(C)):
        BC.append(B[j] + C[k])

points = []
# 10**3 と 10** 6
for i in range(len(A)):
    points += [bc + A[i] for bc in BC]
#  #  for i in range(len(A)):
#  #      for bc in BC:
#  #          points.append(A[i]+bc)
#  #      #  for j in range(len(B)):
#  #      #      for k in range(len(C)):
#  #      #          point = A[i] + B[j] + C[k]
#  #      #          points.append(point)
for i in sorted(points, reverse=True)[:K]:
    print(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(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
a=b=c=0
L=[[0,0,0]]
for k in range(K):
    maximum=0
    index=-1
    for i,l in enumerate(L):
        if A[l[0]]+B[l[1]]+C[l[2]]>maximum:
            maximum=A[l[0]]+B[l[1]]+C[l[2]]
            index=i
    if len(A)>L[index][0]+1 and [L[index][0]+1,L[index][1],L[index][2]] not in L: 
        L.append([L[index][0]+1,L[index][1],L[index][2]])
    if len(B)>L[index][1]+1 and [L[index][0],L[index][1]+1,L[index][2]] not in L:
        L.append([L[index][0],L[index][1]+1,L[index][2]])
    if len(C)>L[index][2]+1 and [L[index][0],L[index][1],L[index][2]+1] not in L:
        L.append([L[index][0],L[index][1],L[index][2]+1])
    print(A[L[index][0]]+B[L[index][1]]+C[L[index][2]])
    del L[index]

Submission status: Time Limit Exceeded

Solution code:
import itertools
x, y, z, pos = map(int, input().split())
a = map(int, input().split())
b = map(int, input().split())
c = map(int, input().split())
result = []
for i, j, k in itertools.product(a, b, c):
    result.append(i + j + k)

result_sorted = sorted(result, reverse=True)
for x in range(pos):
    print(result_sorted[x])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from heapq import merge
from bisect import bisect
def inpl(): return list(map(int, input().split()))

X, Y, Z, K = inpl()
A = inpl()
B = inpl()
C = sorted(inpl(), reverse=True)

AB = sorted([a+b for a in A for b in B])[-K:]
ans = [-1] * K

for c in C:
    if ans[0] > AB[-1] * c:
        break
    tmp = [ab + c for ab in AB][-K:]
    ans = list(merge(ans, tmp))[-K:]

print(*ans[::-1], sep="\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()))
sum = []
for i in A:
    for j in B:
        for k in C:
            sum.append(i + j + k)
sum.sort(reverse = True)
for i in range(K):
    print(sum[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()))

l = []

for i in a:
    for j in b:
        for k in c:
            l.append(i + j + k)
l.sort(reverse = True)

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import math

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

cnum = []

takadaka = math.ceil(k)

for i in range(min(takadaka,x)):
    for j in range(min(takadaka,y)):
        for l in range(min(takadaka,z)):
            cnum.append(a[i] + b[j] + c[l])

cnum.sort()
cnum.reverse()

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
from copy import copy
X,Y,Z,K = map(int,input().split())
Alist = np.array(list(map(int,input().split())))
Blist = np.array(list(map(int,input().split())))
Clist = np.array(list(map(int,input().split())))
Alllist = []
for a in Alist:
    B_list = Blist+a
    for b in B_list:
        C_list = Clist+b
        Alllist.extend(list(C_list))
Alllist.sort(reverse=True)

for i in range(K):
    print(Alllist[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()))

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

memo = set([((0,0,0), A[0]+B[0]+C[0]),])

'''
def next(next_cand, (a,b,c)):
  if a<len(A)-1:
    next_cand.append(((a+1,b,c), A[a+1]+B[b]+C[c]))
  if b<len(B)-1:
    next_cand.append(((a,b+1,c), A[a]+B[b+1]+C[c]))
  if c<len(C)-1:
    next_cand.append(((a,b,c+1), A[a]+B[b]+C[c+1]))
'''

while len(memo)<K:
  next_cand=set()
  
  for m in memo:
    #print(m)
    a,b,c = m[0]
    if a<len(A)-1:
      next_cand.add(((a+1,b,c), A[a+1]+B[b]+C[c]))
    if b<len(B)-1:
      next_cand.add(((a,b+1,c), A[a]+B[b+1]+C[c]))
    if c<len(C)-1:
      next_cand.add(((a,b,c+1), A[a]+B[b]+C[c+1]))
      
  #print(next_cand)
  x = list(next_cand - (next_cand&memo))
  #print(x)
           
  x.sort(reverse=True, key=lambda x:x[1])
  #print(x)
  memo.add((x[0]))

memo=sorted(list(memo),reverse=True, key=lambda x:x[1])
for m in memo:
  a,b,c = m[0]
  print(A[a] +B[b] +C[c])

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K= map(int,input().split())
A = [int(j) for j in input().split()]
B = [int(j) for j in input().split()]
C = [int(j) for j in input().split()]
lists =[]
for a in A:
    for b in B:
        for c in C:
            lists.append(a+b+c)
lists.sort(reverse=True)
for i in lists[0:K]:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))
rr = lambda N: [ri() for _ in range(N)]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18

X,Y,Z,K=rl()
A=rl()
B=rl()
C=rl()
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
X=[]
for a,b,c in zip(A,B,C):
    X.append([a,0])
    X.append([b,1])
    X.append([c,2])
X.sort(reverse=True)

ca=0
cb=0
cc=0
xl = 0
for i in range(len(X)):
    _,a = X[i]
    if a == 0:
        ca += 1
    elif a == 1:
        cb += 1
    else:
        cc += 1 
    if ca*cb*cc>=K:
        xl=i
Ax=[]
Bx=[]
Cx=[]
for x,a in X[:(xl+1)]:
    if a == 0:
        Ax.append(x)
    elif a == 1:
        Bx.append(x)
    else:
        Cx.append(x)

ans = []
import itertools
for a,b,c in itertools.product(Ax,Bx,Cx):
    ans.append(a+b+c)
ans.sort(reverse=True)
for i in range(K):
    print(ans[i])

Submission status: Time Limit Exceeded

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

an,bn,cn,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)
ans=[0]*k
mn=0
bmx=max(b)
cmx=max(c)
for ak in a:
    if ak+bmx+cmx<mn:break
    for bk in b:
        if ak+bk+cmx<mn:break
        for ck in c:
            abc=ak+bk+ck
            if abc<mn:break
            if abc>mn:
                if len(ans)==k:
                    ans.remove(mn)
                ans+=[abc]
                mn=min(ans)
for x in sorted(ans,reverse=True):
    print(x)

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 i in range(len(A)):
    for j in range(len(B)):
        for k in range(len(C)):
            result.append(A[i]+B[j]+C[k])

result.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

AB = []
for p in itertools.product(A, B):
    AB.append(sum(p))
AB.sort(reverse=True)
AB = AB[:K]

l = []
for p in itertools.product(AB, C):
    l.append(sum(p))
l.sort(reverse=True)
for i in range(K):
    print(l[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()))

ans = []
import itertools
for ai,bi,ci in itertools.product(a,b,c):
  ans.append(ai+bi+ci)

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()))
T1 = []
for a in A:
    for b in B:
        T1.append(a + b)
T1.sort(reverse=True)
if len(T1) > 3000: T1 = T1[:3000]
T2 = []
for t1 in T1:
    for c in C:
         T2.append(t1 + c)
T2.sort(reverse=True)
m = min(X * Y * Z, K)
if len(T2) > m: T2 = T2[:m]
for t2 in T2:
    print(t2)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding:utf-8 -*-


def solve():
    """ TLE """
    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(" ")))

    ans = []

    for x in range(X):
        for y in range(Y):
            for z in range(Z):
                abc = A[x]+B[y]+C[z]
                if len(ans) < K-1:
                    ans.append(abc)
                    continue
                elif len(ans) == K-1:
                    ans.append(abc)
                    ans.sort(reverse=True)
                    continue
                
                if abc > ans[-1]:
                    ans[-1] = abc
                    ans.sort(reverse=True)
                    continue

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


if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
x, y, z, k=map(int, input().split())
delicious=[]

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

result=[]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
used_list=[]
l=0
while(len(result)<k):
    for i in range(x):
        for j in range(y):
            if not [i, j, l] in used_list:
                tmp=a[i]+b[j]+c[l]
                result.append(tmp)
                used_list.append([i, j, l])

    for i in range(y):
        for j in range(z):
            if not [l, i, j] in used_list:
                tmp=a[l]+b[i]+c[j]
                result.append(tmp)
                used_list.append([l, i, j])

    for i in range(z):
        for j in range(x):
            if not [j, l, i] in used_list:
                tmp=a[j]+b[l]+c[i]
                result.append(tmp)
                used_list.append([j, l, i])
    l=l+1


result.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))
rr = lambda N: [ri() for _ in range(N)]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18

X,Y,Z,K=rl()
A=rl()
B=rl()
C=rl()
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
X=[]
for a,b,c in zip(A,B,C):
    X.append([a,0])
    X.append([b,1])
    X.append([c,2])
X.sort(reverse=True)

ca=0
cb=0
cc=0
xl = 0
for i in range(len(X)):
    _,a = X[i]
    if a == 0:
        ca += 1
    elif a == 1:
        cb += 1
    else:
        cc += 1 
    if ca*cb*cc>=K:
        xl=i
Ax=[]
Bx=[]
Cx=[]
for x,a in X:
    if a == 0:
        Ax.append(x)
    elif a == 1:
        Bx.append(x)
    else:
        Cx.append(x)

ans = []
import itertools
for a,b,c in itertools.product(Ax,Bx,Cx):
    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, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())

S = []
for a in A:
    for b in B:
        S.append(a+b)
S.sort()
C.sort()
T = C

L = X*Y

left = 3; right = max(A)+max(B)+max(C)+1
while left+1 < right:
    mid = (left + right) >> 1
    cur = L
    cnt = 0
    for c in C:
        while cur > 0 and S[cur-1]+c > mid:
            cur -= 1
        cnt += L - cur
    if cnt < K:
        right = mid
    else:
        left = mid
ans = []
for c in C:
    cur = L
    while cur > 0 and S[cur-1]+c >= right:
        v = S[cur-1]+c
        ans.append(v)
        cur -= 1
ans.sort(reverse=1)
while len(ans) < K:
    ans.append(left)
print(*ans[:K], sep='\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()))
li=[]

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

for l in range(k):
    print(li[l])

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)

min_b = min(B)
max_b = max(B)
max_c = max(C)
summs = []
for a in A:
    if len(summs) >= K and a + max_b + max_c < summs[K-1]:
        break
        
    for b in B:
        if len(summs) >= K and a + b + max_c < summs[K-1]:
            break

        for c in C:
            summ = a + b + c
            if len(summs) >= K:
                min_sum = summs[K-1]    
                if summ < min_sum:
                    break
            summs.append(summ)
        summs.sort(reverse=True)

for s in summs[0:K]:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
x, y, z, k = map(int, input().split())
listA = list(map(int, input().split()))
listB = list(map(int, input().split()))
listC = list(map(int, input().split()))

listA.sort()
listB.sort()
listC.sort()
listA.reverse()
listB.reverse()
listC.reverse()

indexA=0
indexB=0
indexC=0
listAL=[listA[0]]
listBL=[listB[0]]
listCL=[listC[0]]
index=1
while index < k:
    caseA=0
    caseB=0
    caseC=0
    if indexA+1 < len(listA):
        caseA = listA[indexA+1]+listB[indexB]+listC[indexC]
    if indexB+1 < len(listB):
        caseB = listA[indexA]+listB[indexB+1]+listC[indexC]
    if indexC+1 < len(listC):
        caseC = listA[indexA]+listB[indexB]+listC[indexC+1]
    if caseA >= max(caseB, caseC) and caseA!=0:
        listAL.append(listA[indexA+1])
        indexA+=1
    elif caseB >= max(caseC, caseA)and caseB != 0:
        listBL.append(listB[indexB+1])
        indexB+=1
    elif caseC >= max(caseA, caseB)and caseC != 0:
        listCL.append(listC[indexC+1])
        indexC+=1
    else:
        break
    index+=1
listAns=[]
Counter=0
index2=0
while index2 < len(listAL):
    index3=0
    while index3 < len(listBL):
        index4=0
        while index4 < len(listCL):
            listAns.append(listAL[index2]+listBL[index3]+listCL[index4])
            index4+=1
        index3+=1
    index2+=1
listAns.sort()
listAns.reverse()

index5=0
while index5 < k:
    print(listAns[index5])
    index5+=1

Submission status: Time Limit Exceeded

Solution code:
import sys

if __name__ == "__main__":
    (X, Y, Z, K) = list(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()))

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

    v.sort(reverse=True)

    ans = []
    for i in range(min(len(v),3000)):
        for c in C:
            ans.append(v[i] + c)

    ans.sort(reverse=True)

    for i in range(K):
        sys.stdout.write("%d\n" % ans[i])

Submission status: Time Limit Exceeded

Solution code:
x, y, z, k = map(int, input().split())
m = []
for _ in range(3):
    m.append(sorted(list(map(int, input().split())), reverse=True))

def sorted_sum(list_a, list_b):
    s = []
    for x in list_a:
        for y in list_b:
            s.append(x + y)
    return sorted(s, reverse=True)

ans = sorted_sum(sorted_sum(m[0], m[1])[0:k], m[2])
for i in range(0, k):
    print(ans[i])