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

oisisa=[]
for i in range(x):
    for j in range(y):
        for k in range(z):
            val=a[i]+b[j]+c[k]
            oisisa.append(val)
oisisa.sort(reverse=True)
for i in range(K):
    print(oisisa[i])

Submission status: Time Limit Exceeded

Solution code:
import itertools
x, y, z, k = map(int, input().split())
al = list(map(int,input().split()))
bl = list(map(int,input().split()))
cl = list(map(int,input().split()))
sum_list = [sum(i) for i in itertools.product(al, bl, cl)]
sum_list.sort(reverse=True)
for i in sum_list[:k]:
  print(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()))
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:
# encoding:utf-8
import copy
import numpy as np
import random
import bisect #bisect_left　これで二部探索の大小検索が行える

x,y,z,k = map(int,input().split())
A = [i for i in map(int,input().split())]
B = [i for i in map(int,input().split())]
C =[i for i in map(int,input().split())]


select = k

# A.sort()
# A.reverse()
# B.sort()
# B.reverse()
C.sort()
C.reverse()
ck = C[0:select]

ab = []
for i in A:
    for j in B:
        ab.append(i+j)

ab.sort()
ab.reverse()
ab_sel = ab[0:select]

anss = []
for i in ab_sel:
    for j in C:
        anss.append(i+j)

anss.sort()
anss.reverse()

for i in range(k):
    print(anss[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=1)
b.sort(reverse=1)
c.sort(reverse=1)
d=[]
for i in a:
  for j in b:
    d.append(i+j)
d.sort(reverse=1)
ans=[]
if len(d)>k:
  for i in d[:k]:
    for j in c:
      ans.append(i+j)
else:
  for i in d:
    for j in c:
      ans.append(i+j)
ans.sort(reverse=1)
if len(ans)>k:
  for i in ans[:k]:
    print(i)
else:
  for i in ans:
    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()))

def cal(a, b, c):
    return A[a] + B[b] + C[c]

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

choice=[]
for i in range(K):
    if i==0:
        choice.append([0, 0, 0])
    else:
        koho = []
        for j in choice:
            a = j[0]
            b = j[1]
            c = j[2]

            if a+1<X:
                koho.append([a+1, b, c])
            if b+1<Y:
                koho.append([a, b+1, c])
            if c+1<Z:
                koho.append([a, b, c+1])


        saidai = 0
        koho2 = []
        for l in koho:
            if l in choice:
                continue
            else:
                if cal(*l)>saidai:
                    koho2 = l
                    saidai=cal(*l)
        choice.append(koho2)

    print(cal(*choice[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 = []
for p in a:
    for q in b:
        for r in c:
            ans.append(p+q+r)
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
def int_raw():
    return int(input())


def ss_raw():
    return raw_input().split()


def ints_raw():
    return list(map(int, ss_raw()))


def diff_array(array):
    return [array[i + 1] - array[i] for i in xrange(len(array) - 1)]


X, Y, Z, K = ints_raw()
A = ints_raw()
sorted_A = sorted(A, reverse=True)
B = ints_raw()
sorted_B = sorted(B, reverse=True)
C = ints_raw()
sorted_C = sorted(C, reverse=True)

current_A = 0
current_B = 0
current_C = 0


def diff(array, index):
    if len(array) <= index:
        return 10000000000
    return array[0] - array[index]


count = 0
while count < 3000:
    count += 1
    a = diff(sorted_A, current_A + 1)
    b = diff(sorted_B, current_B + 1)
    c = diff(sorted_C, current_C + 1)
    if a <= b and a <= c and current_A < X - 1:
        current_A += 1
        continue
    if b <= c and b <= a and current_B < Y - 1:
        current_B += 1
        continue
    if c <= a and c <= b and current_C < Z - 1:
        current_C += 1
        continue

result = []
for i in xrange(current_A + 1):
    for j in xrange(current_B + 1):
        for k in xrange(current_C + 1):
            result.append(sorted_A[i] + sorted_B[j] + sorted_C[k])

sorted_result = sorted(result, reverse=True)
for i in xrange(K):
    print(sorted_result[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()))
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 j in range(min(3000,x*y)):
        ans.append(lisc[i]+an[j])
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 16 20:52:46 2019

@author: Owner
"""


import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy
import bisect

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())

    
all(nstr.count(c) for c in '753')

# 複数配列を並び替え
ABT = zip(A, B, totAB)
result = 0
# itemgetterには何番目の配列をキーにしたいか渡します
sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)

# 2進数のbit判定
(x >> i) & 1

# dp最小化問題
dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break
#２分探索
li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    

"""

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

A = [0]*X
A[:] = map(int, input().split())
B = [0]*Y
B[:] = map(int, input().split())
C = [0]*Z
C[:] = map(int, input().split())

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
INF = 10**12
ABC = [0]*K
for x in range(X):
    for y in range(Y):
        for z in range(Z):
            ABC[-1] = max(ABC[-1], A[x]+B[y]+C[z])    
            ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = input().strip().split(' ')
x,y,z,k = [int(x), int(y),int(z),int(k)]
aList = list(map(int, input().strip().split(' ')))
bList = list(map(int, input().strip().split(' ')))
cList = list(map(int, input().strip().split(' ')))
ansList = []
for i in range(len(aList)):
        for j in range(len(bList)):
                for n in range(len(cList)):
                        y = aList[i] + bList[j] + cList[n]
                        ansList.append(y)
ansList.sort(reverse = True)

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
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()))

v = []

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


v = sorted(v, reverse=True)

for vv in v[0:k]:
    print(vv)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import itertools

X, Y, Z, K = tuple([int(x) for x in str(input()).split(' ')])
A = np.array(sorted([int(x) for x in str(input()).split(' ')], reverse=True)[:min(X, K)])
B = np.array(sorted([int(x) for x in str(input()).split(' ')], reverse=True)[:min(Y, K)])
C = np.array(sorted([int(x) for x in str(input()).split(' ')], reverse=True)[:min(Z, K)])

out = sorted([sum(x) for x in itertools.product(A, B, C)], reverse=True)

for x in out[:K]:
    print(x)

Submission status: Time Limit Exceeded

Solution code:
X, Y, Z, K = list(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()))

A_list.sort(reverse=True)
B_list.sort(reverse=True)
C_list.sort(reverse=True)

class Value():
  def __init__(self, A_list, B_list, C_list, a_idx, b_idx, c_idx):
    self.A = A_list[a_idx]
    self.B = B_list[b_idx]
    self.C = C_list[c_idx]
    self.a_idx = a_idx
    self.b_idx = b_idx
    self.c_idx = c_idx
    self.value = self.A + self.B + self.C
  def __eq__(self, other):
    return self.a_idx == other.a_idx and self.b_idx == other.b_idx and self.c_idx == other.c_idx
  def __gt__(self, other):
    return self.value > other.value
  def __ge__(self, other):
    return self.value >= other.value
    
que = []
que.append(Value(A_list, B_list, C_list, 0, 0, 0))
current_value = 10000000000 * 3
for _ in range(K):
  v = que.pop(que.index(max(que)))
  print(v.value)
  if v.a_idx < X -1:
    new_v = Value(A_list, B_list, C_list, v.a_idx + 1, v.b_idx, v.c_idx)
    if not new_v in que:
      que.append(new_v)
  if v.b_idx < Y -1:
    new_v = Value(A_list, B_list, C_list, v.a_idx, v.b_idx + 1, v.c_idx)
    if not new_v in que:
      que.append(new_v)
  if v.c_idx < Z -1:
    new_v = Value(A_list, B_list, C_list, v.a_idx, v.b_idx, v.c_idx + 1)
    if not new_v in que:
      que.append(new_v)
  #que.sort(key=lambda x: x.value, reverse=True)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = map(int, raw_input().split())
a = map(int, raw_input().split())
b = map(int, raw_input().split())
c = map(int, raw_input().split())
s = []
a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
if(x > 54 & y > 54 & z > 54):
	ax = a[0] - a[54]
	bx = b[0] - b[54]
	cx = c[0] - c[54]
	for n in range(x):
		if(a[0] - a[n] > bx + cx):
			x = n
			break;
	for n in range(y):
		if(b[0] - b[n] > ax + cx):
			y = n
			break;
	for n in range(z):
		if(c[0] - c[n] > bx + ax):
			z = n
			break;
for ka in range(x):
    for kb in range(y):
		for kc in range(z):
			s.append(a[ka]+b[kb]+c[kc])
s.sort(reverse = True)
for n in range(k):
	print(s[n])

Submission status: Time Limit Exceeded

Solution code:
import heapq
x,y,z,k=list(map(int,input().split()))
arr=list(map(int,input().split()))
brr=list(map(int,input().split()))
crr=list(map(int,input().split()))
h=[]
for a in arr:
    for b in brr:
        for c in crr:
            heapq.heappush(h,(a+b+c)*(-1))
for _ in range(k):
    print(heapq.heappop(h)*(-1))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
X, Y, Z, K = map(int, input().split(" "))
A = list(map(int, input().split(" ")))
B = list(map(int, input().split(" ")))
C = list(map(int, input().split(" ")))

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

Total = []
for a in A:
  for b in B:
    for c in C:
      Total.append(a+b+c)
Total_sorted = sorted(Total, reverse=True)
for i in range(K):
  print(Total_sorted[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()))
import itertools
combi=list(itertools.product(a,b,c))
ans=[0]*len(combi)
for i in range(len(combi)):
    ans[i]= sum(combi[i])
ans.sort(reverse=True)
for x in ans[:k]:
    print(x)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k=map(int,input().split())
a=tuple(map(int,input().split()))
b=tuple(map(int,input().split()))
c=tuple(map(int,input().split()))
 
def f(p,q):
    return [i+j for j in q for i in p]
 
if x+y<y+z:
    for i in sorted(f(c,f(a,b)))[::-1][:k]:
        print(i)
elif x+z<y+z:
    for i in sorted(f(b,f(a,c)))[::-1][:k]:
        print(i)
else:
    for i in sorted(f(a,f(b,c)))[::-1][: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)
ans = []

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

ans.sort(reverse = True)

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

list_ = [a+b+c for (a,b,c) in itertools.product(A, B, C)]
list_.sort(reverse=True)
for N in list_[:K]:
  print(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(' ')))
taste = []
for i in set(range(x - k if x - k > 0 else 0, x)):
    for j in set(range(y - k if y - k > 0 else 0, y)):
        for l in set(range(z - k if z - k > 0 else 0, 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:
from itertools import product
import sys
input = sys.stdin.readline

X, Y, Z, K = map(int, input().split())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]
C = [int(_) for _ in input().split()]
A = sorted(A)[::-1]
B = sorted(B)[::-1]
C = sorted(C)[::-1]

cnt = 0
Ax = 0
Bx = 0
Cx = 0
while cnt <= K:
    if Ax < X-1:
        a = A[Ax]-A[Ax+1]
    else:
        a = float('inf')
    if Bx < Y-1:
        b = B[Bx]-B[Bx+1]
    else:
        b = float('inf')
    if Cx < Z-1:
        c = C[Cx]-C[Cx+1]
    else:
        c = float('inf')
    if a <= b and a <= c:
        Ax += 1
    elif b <= a and b <= c:
        Bx += 1
    else:
        Cx += 1
    cnt += 1

List = []
for i, j, k in product(A[:Ax+1], B[:Bx+1], C[:Cx+1]):
    List.append(i+j+k)

List = sorted(List)[::-1]

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

Submission status: Time Limit Exceeded

Solution code:
INF = -10**11
x, y, z, k = [int(item) for item in input().split()]
a = sorted([int(item) for item in input().split()], reverse=True) + [INF]
b = sorted([int(item) for item in input().split()], reverse=True) + [INF]
c = sorted([int(item) for item in input().split()], reverse=True) + [INF]

num = x + y + z
ac, bc, cc = 0, 0, 0
ad = a[0] - a[1]
bd = b[0] - b[1]
cd = c[0] - c[1]
kadd = min(60000, x*y*z)
while (ac+1) * (bc+1) * (cc+1) < kadd:
    if ad <= bd and ad <= cd:
        ac += 1
        ad = a[ac] - a[ac+1]
    elif bd <= ad and bd <= cd:
        bc += 1
        bd = b[bc] - b[bc+1]
    elif cd <= ad and cd <= bd:
        cc += 1
        cd = c[cc] - c[cc+1]

for i in range(50):
    if ac+1 < x:
        ac += 1
    if bc+1 < y:
        bc += 1
    if cc+1 < z:
        cc += 1

vals = []
for ai in a[:ac+1]:
    for bi in b[:bc+1]:
        for ci in c[:cc+1]:
            vals.append(ai+bi+ci)
vals.sort(reverse=True)
for i in range(k):
    print(vals[i])

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
import itertools
x,y,z,k = map(int,input().split())

ls = []

for i in range(3):
    s = sorted(list(map(int,input().split())),reverse=True)
    ls.append(s)
#print (ls)
ptr = [1] * 3


cr = min(3000,k)
while(ptr[0]*ptr[1]*ptr[2]<cr):
    max_p = 0
    for j in range(3):
        p = 1
        for i in range(3):
            if j != i:
                p *= ls[i][0]
            else:
                if len(ls[i]) > ptr[i]:
                    p *= ls[i][ptr[i]]
                else:
                    p = 0
        #print (j,i,p)
        if max_p < p:
            max_p = p
            argmax_p = j
    ptr[argmax_p] += 1
    #print (ptr)
#print (ptr)
#print (ls)
product = list(itertools.product(*ls))
sm = sorted([sum(p) for p  in product],reverse=True)
for i in range(k):
    print (sm[i])

Submission status: Time Limit Exceeded

Solution code:
x, y, z, k = map(int, input().split())
a_s = list(map(int, input().split()))
bs = list(map(int, input().split()))
cs = list(map(int, input().split()))

list = [a_s[l] + bs[m] + cs[n] for l in range(x) for m in range(y) for n in range(z)]
list.sort(reverse=True)
for i in range(k):
    print(list[i])

Submission status: Time Limit Exceeded

Solution code:
import itertools
x, y, z, k = map(int, input().split())
al = list(map(int,input().split()))
bl = list(map(int,input().split()))
cl = list(map(int,input().split()))
sum_list = []
for i in list(itertools.product(al, bl, cl)):
  sum_list.append(sum(i))
sum_list.sort(reverse=True)
for i in sum_list[:k]:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 16 20:52:46 2019

@author: Owner
"""


import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy
import bisect

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())

    
all(nstr.count(c) for c in '753')

# 複数配列を並び替え
ABT = zip(A, B, totAB)
result = 0
# itemgetterには何番目の配列をキーにしたいか渡します
sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)

# 2進数のbit判定
(x >> i) & 1

# dp最小化問題
dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break
#２分探索
li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    

"""

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

A = [0]*X
A[:] = map(int, input().split())
B = [0]*Y
B[:] = map(int, input().split())
C = [0]*Z
C[:] = map(int, input().split())

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
INF = 10**12
ABC = [0]*K
for x in range(X):
    for y in range(Y):
        for z in range(Z):
            tmp_min = min(ABC)
            id_min = find_index(ABC, tmp_min,default=False) 
            ABC[id_min] = max(tmp_min, A[x]+B[y]+C[z])    
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])

Submission status: Time Limit Exceeded

Solution code:
import itertools
import sys

input = sys.stdin.readline

maxn = 10000000000

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.append(-maxn)
B.append(-maxn)
C.append(-maxn)

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

apos, bpos, cpos = 0, 0, 0

while (apos + 1) * (bpos + 1) * (cpos + 1) < min(10 ** 7 - 5 * 10 ** 6, X * Y * Z):
    if (
        apos < len(A) - 2
        and abs(A[apos] - A[apos + 1]) < abs(B[bpos] - B[bpos + 1])
        and abs(A[apos] - A[apos + 1]) < abs(C[cpos] - C[cpos + 1])
    ):
        apos += 1
    elif bpos < len(B) - 2 and B[bpos] - B[bpos + 1] < C[cpos] - C[cpos + 1]:
        bpos += 1
    else:
        cpos += 1

res = []
for a in list(itertools.product(A[: apos + 1], B[: bpos + 1], C[: cpos + 1])):
    res.append(a[0] + a[1] + a[2])

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

Submission status: Time Limit Exceeded

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

Sums = []
for x in Xs:
    for y in Ys:
        for z in Zs:
            Sums.append(x+y+z)
Sums.sort()            
for i in range(1,K+1):
    print(Sums[-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 = [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)

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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()))
n = 0
r = [0] * k
for i in range(x):
  for j in range(y):
    for l in range(z):
      s = a[i] + b[j] + c[l]
      if n >= k:
        idx = np.argmin(r)
        if r[idx] < s:
          r[idx] = s
      else:
        r[n] = s
      n += 1
r.sort()
for i in range(k):
  print(r[-(i+1)])

Submission status: Time Limit Exceeded

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

x1 = list(map(int,input().split(" ")))
y1 = list(map(int,input().split(" ")))
z1 = list(map(int,input().split(" ")))

sm = []

for i in range(x):
    for j in range(y):
        for l in range(z):
            o = int(x1[i]) + int(y1[j]) + int(z1[l])
            sm.append(o)

sm.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k = list(map(int,input().split()))
lis_a = list(map(int,input().split()))
lis_b = list(map(int,input().split()))
lis_c = list(map(int,input().split()))
ans = []
for a in lis_a:
  for b in lis_b:
    for c in lis_c:
      ans.append(a+b+c)
ans.sort(reverse = True)
for item in ans[:k]:
  print(item)

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=0
b=0
c=0
A=list(A)
B=list(B)
C=list(C)
s=[]
for i in range(len(A)):
    for j in range(len(B)):
        for k in range(len(C)):
            s+=[A[i]+B[j]+C[k]]
s.sort(reverse=True)
for i in range(K):
    print(s[i])
    if i>=3000:
        break

Submission status: Time Limit Exceeded

Solution code:
import itertools
import sys

input = sys.stdin.readline

maxn = 10000000000

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.append(-maxn)
B.append(-maxn)
C.append(-maxn)

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

apos, bpos, cpos = 0, 0, 0

while (apos + 1) * (bpos + 1) * (cpos + 1) < min(10 ** 7, X * Y * Z):
    if (
        apos < len(A) - 2
        and abs(A[apos] - A[apos + 1]) < abs(B[bpos] - B[bpos + 1])
        and abs(A[apos] - A[apos + 1]) < abs(C[cpos] - C[cpos + 1])
    ):
        apos += 1
    elif bpos < len(B) - 2 and B[bpos] - B[bpos + 1] < C[cpos] - C[cpos + 1]:
        bpos += 1
    else:
        cpos += 1

res = []
for a in list(itertools.product(A[: apos + 1], B[: bpos + 1], C[: cpos + 1])):
    res.append(a[0] + a[1] + a[2])

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

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

Al = sorted(A,reverse=True)[:K]
Bl = sorted(B,reverse=True)[:K]
Cl = sorted(C,reverse=True)[:K]

result = []
for a in Al:
    for b in Bl:
        for c in Cl:
            result.append(a+b+c)

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

T = [A, B, C]
cand_list = itertools.product(*T)
score = []
for cand in cand_list:
  score.append(sum(cand))

score = sorted(score)[::-1]
for i in range(K):
	print(score[i])

Submission status: Time Limit Exceeded

Solution code:
import heapq
import itertools

def dif(l):
  return max(l) - min(l)

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)

while x*y*z > k and dif(a) > dif(b) and dif(a) > dif(c):
  a.pop()
  x -= 1

while x*y*z > k and dif(b) > dif(a) and dif(b) > dif(c):
  b.pop()
  y -= 1

while x*y*z > k and dif(c) > dif(b) and dif(c) > dif(a):
  c.pop()
  z -= 1

l=[]
for a1 in a:
    for b1 in b:
        for c1 in c:
            l.append(a1+b1+c1)

m2 = heapq.nlargest(k, l)
for i in range(k):
  print(m2[i])

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as numpy

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

pattern = sorted([sum(l) for l in itertools.product(A, B, C)], reverse=True)

print('\n'.join([str(n) for n in pattern[:K]]))

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K=map(int,input().split())
A=[int(x) for x in input().split()]
A.sort(reverse=True)
B=[int(x) for x in input().split()]
B.sort(reverse=True)
C=[int(x) for x in input().split()]
C.sort(reverse=True)
D=[a+b for a in A for b in B]
D.sort(reverse=True)
D=D[:3000]
E=[ab+c for ab in D for c in C]
E.sort(reverse=True)
for i in range(K):
  print(E[i])

Submission status: Time Limit Exceeded

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


from sys import stdin
import numpy as np



############################################

# read data for n sequences.
input2=(stdin.readline().split())
input3=[]
for a in input2:
    input3.append(int(a))
A=[]
B=[]
C=[]
K=input3[3]
input=stdin.readline().split()
for a in input:
    A.append(int(a))
input=stdin.readline().split()
for a in input:
    B.append(int(a))
input=stdin.readline().split()
for a in input:
    C.append(int(a))

aind=len(A)-1
bind=len(B)-1
cind=len(C)-1

def cal(aind,bind,cind):
    return(A[aind]+B[bind]+C[cind])
ans=[]
for a in A:
    for b in B:
        for c in C:
            ans.append(a+b+c)
ans = sorted(ans, reverse=True)
for a in ans[:K]:
    print(a)
#while(K>0):
#    ans = A[aind] + B[bind] + C[cind]
#    print(ans)
#    amin = np.min([A[aind],B[bind],C[cind]])
#    amin = int(amin)
#    if amin == A[aind] and aind != 0:
#        aind -= 1
#    elif amin == B[bind] and bind != 0:
#        bind -= 1
#    else:
#        cind -= 1
#    K -= 1

Submission status: Time Limit Exceeded

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

arr = sorted([sum(t) for t in itertools.product(A, B, C)], reverse=True)
for i, v in enumerate(arr, 1):
  print(v)
  if i == K:
    sys.exit()