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:
nums = list(map(int, input().split()))
X, Y, Z, K = nums[0], nums[1], nums[2], nums[3]
A = [int(a) for a in input().split()]
B = [int(a) for a in input().split()]
C = [int(a) for a in input().split()]

A = sorted(A)[::-1]
B = sorted(B)[::-1]
C = sorted(C)[::-1]

from collections import defaultdict
d = defaultdict(int)

A_i, B_i, C_i = {0}, {0}, {0}
for _ in range(K):
    m = 0
    ma, mb, mc = -1, -1, -1
    for a in A_i:
        for b in B_i:
            for c in C_i:
                if d[(a,b,c)] == 0 and a < len(A) and b < len(B) and c < len(C):
                    if A[a] + B[b] + C[c] > m:
                        m = A[a] + B[b] + C[c]
                        ma, mb, mc = a, b, c
    print(m)
    d[(ma,mb,mc)] = 1
    A_i.add(ma+1)
    B_i.add(mb+1)
    C_i.add(mc+1)

Submission status: Time Limit Exceeded

Solution code:
x, y, z, k = map(int, input().split())
aaa = sorted(map(int, input().split()), reverse=True)
bbb = sorted(map(int, input().split()), reverse=True)
ccc = sorted(map(int, input().split()), reverse=True)
ma, mb, mc = aaa[0], bbb[0], ccc[0]
cakes = [(ma - a, 0) for a in aaa[1:]]
cakes += [(mb - b, 1) for b in bbb[1:]]
cakes += [(mc - c, 2) for c in ccc[1:]]
cakes.sort()

buf = [[ma], [mb], [mc]]
ans = [ma + mb + mc]

for d, i in cakes:
    if i == 0:
        a = aaa[len(buf[i])]
        if len(ans) == k and ans[-1] > a + mb + mc:
            break
        buf[i].append(a)
        ans.extend(a + b + c for b in buf[1] for c in buf[2])
        ans.sort(reverse=True)
        ans = ans[:k]
    elif i == 1:
        b = bbb[len(buf[i])]
        if len(ans) == k and ans[-1] > ma + b + mc:
            break
        buf[i].append(b)
        ans.extend(a + b + c for a in buf[0] for c in buf[2])
        ans.sort(reverse=True)
        ans = ans[:k]
    else:
        c = ccc[len(buf[i])]
        if len(ans) == k and ans[-1] > ma + mb + c:
            break
        buf[i].append(c)
        ans.extend(a + b + c for a in buf[0] for b in buf[1])
        ans.sort(reverse=True)
        ans = ans[:k]

print('\n'.join(map(str, ans)))

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  from itertools import permutations

  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)
  res = []
  for a in A[:K]:
    for b in B[:K]:
      for c in C[:K]:
        res.append(a+b+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=[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 a in A:
    for b in B:
        AB.append(a+b)

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

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

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


for abc in ABC:
    print(abc)

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:
    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())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
ll=[]
for i in range(len(a)):
    for j in range(len(b)):
        for l in range(len(c)):
            ll.append(a[i]+b[j]+c[l])
ll.sort(reverse=True)
for i in range(k):
    print(ll[i])

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
import numpy as np

X, Y, Z, K = map(int, input().split())
Xs = [int(i) for i in input().split()]
Ys = [int(i) for i in input().split()]
Zs = [int(i) for i in input().split()]
lst = np.array([sum(i) for i in product(Xs, Ys, Zs)])
lst = np.sort(lst)
for i in range(1, K + 1):
	print(lst[-i])

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 = LI()
B = LI()
C = LI()

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

points = []
# 10**3 と 10** 6
for i in range(X):
    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())
al = list(map(int,input().split()))
bl = list(map(int,input().split()))
cl = list(map(int,input().split()))
al.sort(reverse=True)
bl.sort(reverse=True)
cl.sort(reverse=True)
ll = []
for a in al:
    for b in bl:
        for c in cl:
            ll.append(a+b+c)
ll.sort(reverse=True)
for i in range(K):
    print(ll[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 answer():
    l = []
    for va in A:
        for vb in B:
            for vc in C:
                l.append(va + vb + vc)
    l.sort(reverse=True)
    for i in range(K):
        print(l[i])

def main():
    answer()

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

for a in A:
    for b in B:
        for c in C:
            n = a + b + c
            idx = bisect.bisect_right(nums, n)
            if idx == 0 and len(nums) > K:
                break
            else:
                nums.insert(idx, n)
                if len(nums) > K:
                    nums.pop(0)
        if a + b + C[0] < nums[0]:
            break
    if a + B[0] + C[0] < nums[0]:
        break

for n in nums[::-1]:
    print(n)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,n=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)
#print(a,b,c)
ge=[]
for i in range(0,min(x,500)):
  for j in range(0,min(y,500)):
    for k in range(0,min(z,500)):
      ge.append(a[i]+b[j]+c[k])
      #print(ge)

ge.sort(reverse=True)
for i in range(n):
  print(ge[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(' ')))

out = []
for a in A:
    for b in B:
        for c in C:
            out.append(a+b+c)
out.sort()
[print(o) for o in out[::-1][:K]]

Submission status: Time Limit Exceeded

Solution code:
XYZK=list(map(int, input().split()))
A=list(map(int, input().split()))
B=list(map(int, input().split()))
C=list(map(int, input().split()))

#全パターン作ってソートしてみる
SC=[]
for i in range(XYZK[0]):
  for j in range(XYZK[1]):
    for k in range(XYZK[2]):
      SC.append(A[i]+B[j]+C[k])

SC.sort(reverse=True)

for i in range(XYZK[3]):
  print(int(SC[i]))

Submission status: Time Limit Exceeded

Solution code:
# map(int, input().split)
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 = []
for i in range(0, X):
    for j in range(0, Y):
        for k in range(0, Z):    
            res.append(A[i] + B[j] + C[k])

ress = sorted(res, key=lambda x:-x)
for i in range(0, K):
    print(ress[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]), m[2])
for i in range(0, 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()))

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

sum_count = []

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

sum_count.sort(reverse=True)

for i in range(K):
    print(sum_count[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 a in A:
    for b in B:
        for c in C:
            l.append(a + b + c)
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()))
d = []
for i in range(len(a)):
    for j in range(len(b)):
        for l in range(len(c)):
            d.append(a[i]+b[j]+c[l])
d.sort()
for i in range(1,k+1):
    print(d[-i])

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 = LI()
B = LI()
C = LI()

# iが決まれば、j,kのおおきさは分かる
BC = []
for j in range(Y):
    for k in range(Z):
        BC.append(B[j] + C[k])

points = []
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 heapq
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 = heapq.nlargest(k, m)
for i in range(k):
  print(m2[i])

Submission status: Time Limit Exceeded

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

info = [list(map(int,input().split())) for i in range(3)]
info[0]=sorted(info[0],reverse=True)
info[1]=sorted(info[1],reverse=True)
info[2]=sorted(info[2],reverse=True)
li = []
ans = []
li_append = li.append 
ans_append = ans.append

for i in info[0]:
  for j in info[1]:
    li_append(i+j)
li = sorted(li,reverse=True)

for i in li[:3000]:
  for j in info[2]:
    ans_append(i+j)
ans = sorted(ans,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()))
temp_sum = []
for i in range(X):
	for j in range(Y):
		for k in range(Z):
			temp_sum.append(A[i] + B[j] + C[k])

def merge_sort(arr):
    if len(arr) <= 1:
        return arr

    mid = len(arr) // 2
    # ここで分割を行う
    left = arr[:mid]
    right = arr[mid:]

    # 再帰的に分割を行う
    left = merge_sort(left)
    right = merge_sort(right)

    # returnが返ってきたら、結合を行い、結合したものを次に渡す
    return merge(left, right)


def merge(left, right):
    merged = []
    l_i, r_i = 0, 0

    # ソート済み配列をマージするため、それぞれ左から見ていくだけで良い
    while l_i < len(left) and r_i < len(right):
        # ここで=をつけることで安定性を保っている
        if left[l_i] <= right[r_i]:
            merged.append(left[l_i])
            l_i += 1
        else:
            merged.append(right[r_i])
            r_i += 1

    # 上のwhile文のどちらかがFalseになった場合終了するため、あまりをextendする
    if l_i < len(left):
        merged.extend(left[l_i:])
    if r_i < len(right):
        merged.extend(right[r_i:])
    return merged

ans = merge_sort(temp_sum)

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

data_lists=[]
for a in range(X):
    for b in range(Y):
        for c in range(Z):
            data_lists.append(A[a]+B[b]+C[c])
            
data_lists.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
x,y,z,k = map(int,input().split())
X = list(map(int,input().split()))
Y = list(map(int,input().split()))
Z = list(map(int,input().split()))
L = list(itertools.product(X,Y,Z))
J = sorted([sum(i) for i in L],reverse=True)
for i in range(k):
    print(J[i])

Submission status: Time Limit Exceeded

Solution code:
import heapq
xyzk=list(map(int,input().split()))
x=xyzk[0]
y=xyzk[1]
z=xyzk[2]
k=xyzk[3]
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
n=[]
for i in range(x):
    for j in range(y):
        for l in range(z):
            n.append(a[i]+b[j]+c[l])
m=list(reversed(sorted(n)))
for i in range(k):
    print(m[i])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

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

yammy = []

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

yammy.sort(reverse=True)
for l in range(K):
    print(yammy[l])

Submission status: Time Limit Exceeded

Solution code:
X,Y,Z,K = map(int,input().split())
Alist = list(map(int,input().split()))
Blist = list(map(int,input().split()))
Clist = list(map(int,input().split()))
Alllist = []
for a in Alist:
    for b in Blist:
        for c in Clist:
            Alllist.append(a+b+c)
Alllist.sort(reverse=True)

for i in range(K):
    print(Alllist[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()))
alldata = []
for i in a:
    for j in b:
        for k in c:
            alldata.append(i+j+k)
            
alldata.sort(reverse=True)
for i in range(K):
    print(alldata[i])

Submission status: Time Limit Exceeded

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


ab=[]

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

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

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

for aa in sorted((sum(p) for p in product(a, b, c)), reverse=True)[:K]:
    print(aa)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,n = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))

data = []
for i in a:
    for j in b:
        for k in c:
            data.append(i+j+k)

data.sort(reverse = True)

for i in range(n):
    print(data[i])

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

for a in A:
    for b in B:
        for c in C:
            n = a + b + c
            idx = bisect.bisect_right(nums, n)
            if idx == 0 and len(nums) > K:
                break
            else:
                nums.insert(idx, n)
                if len(nums) > K:
                    nums.pop(0)

for n in nums[::-1]:
    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()))
A.sort()
B.sort()
C.sort()
from heapq import heappush,heappop
ans=[]
data=[(-A[-1]-B[-1]-C[-1],X-1,Y-1,Z-1)]
visited=set()
while len(ans)<K:
    now=heappop(data)
    if (now[1],now[2],now[3]) not in visited:
        ans.append(-1*now[0])
        visited|={(now[1],now[2],now[3])}
    if now[1]>0:
        nex=now[0]+A[now[1]]-A[now[1]-1]
        heappush(data,(nex,now[1]-1,now[2],now[3]))
    if now[2]>0:
        nex=now[0]+B[now[2]]-B[now[2]-1]
        heappush(data,(nex,now[1],now[2]-1,now[3]))
    if now[3]>0:
        nex=now[0]+C[now[3]]-C[now[3]-1]
        heappush(data,(nex,now[1],now[2],now[3]-1))
for i in range(K):
    print(ans[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 = []
for p in itertools.product(A, B, 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()))

'''
考え方
各リストをソートして、前からk個分のみの要素にする
'''

'''
import math

cnt_num = math.ceil(pow(k, 1/3))

A = sorted(A,reverse=True)[:cnt_num]
B = sorted(B,reverse=True)[:cnt_num]
C = sorted(C,reverse=True)[:cnt_num]
'''
A = sorted(A,reverse=True)[:k]
B = sorted(B,reverse=True)[:k]
C = sorted(C,reverse=True)[:k]


ans_list = []
for a in A:
  for b in B:
    for c in C:
      ans_list.append(a+b+c)

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

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 = LI()
B = LI()
C = LI()

points = []
for i in range(len(A)):
    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()))

dp = []
for a in A:
  for b in B:
    for c in C:
      dp.append(a+b+c)

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

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappushpop


def main():
    x, y, z, k = map(int, input().split())
    aa = sorted(map(int, input().split()), reverse=True)
    bb = sorted(map(int, input().split()), reverse=True)
    cc = sorted(map(int, input().split()), reverse=True)

    heap = []
    for a in aa:
        if len(heap) == k and a + bb[0] + cc[0] < heap[0]:
            break
        for b in bb:
            if len(heap) == k and a + b + cc[0] < heap[0]:
                break
            for c in cc:
                v = a + b + c
                if len(heap) < k:
                    heappush(heap, v)
                    continue
                if v < heap[0]:
                    break
                heappushpop(heap, v)

    for v in sorted(heap, reverse=True):
        print(v)


if __name__ == "__main__":
    main()

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
import bisect

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

a.sort(reverse=True)
b.sort(reverse=True)
ll = []
for i in a:
    for j in b:

        if len(ll) > 3001 and ll[0] > i + j:
            break
        else:
            bisect.insort(ll, i + j)

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

lll = [0] * 3000001
for i in ll:
    for j in c:

        if len(lll) > 3001 and lll[0] > i + j:
            break
        else:
            bisect.insort(lll, i + j)

lll.reverse()

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

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

Submission status: Time Limit Exceeded

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

# abc123_d
import itertools

# input
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()))
# getans
arr = itertools.product(A, B, C)
arr = map(sum, arr)
arr = sorted(arr, reverse=True)
# output
for a in arr[:K]:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
x,y,z,k=map(int,input().split())
dx=map(int,input().split())
dy=map(int,input().split())
dz=map(int,input().split())
e=[]
from itertools import product
a=[ex+ey+ez for ex,ey,ez in product(dx,dy,dz)]
for s in sorted(a)[::-1][:k]:
  print(s)

Submission status: Time Limit Exceeded

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

def solve():
    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):
                ans.append(A[x]+B[y]+C[z])
    
    ans.sort(reverse=True)

    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())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
tot = []
for i in range(X):
   for j in range(Y):
      for k in range(Z):
         tot.append(a[i] + b[j] + c[k])
tot.sort()
for i in range(K):
   print(tot[-(i+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 i in range(len(a)):
        for j in range(len(b)):
            v.append(a[i] + b[j])

    v.sort(reverse=True)

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

    ans.sort(reverse=True)

    for i in range(k):
        sys.stdout.write("%d\n" % ans[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()]
flav = [sum(i) for i in itertools.product(A,B,C)]
flav.sort(reverse=True)
for i in range(K):
    print(flav[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()))

list = []

for ix in range(x):
    for iy in range(y):
        for iz in range(z):
            list.append(a[ix] + b[iy] + c[iz])

list.sort(reverse=True)

for i in range(k):
    print(list[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()))
beauty=[0 for _ in range(x*y*z)]
index=0
for i in range(x):
    for j in range(y):
        for l in range(z):
            beauty[index]=a[i]+b[j]+c[l]
            index+=1
beauty.sort(key=lambda x:-x)
# print(beauty)
for i in range(k):
    print(beauty[i])

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 = []
for ka in a:
    for kb in b:
		for kc in c:
			s.append(ka+kb+kc)
for n in range(k):
	print(max(s))
	s.remove(max(s))

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]
    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, = 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 >= left:
        v = S[cur-1]+c
        if v == left and len(ans) >= K:
            break
        ans.append(v)
        cur -= 1
ans.sort(reverse=1)
print(*ans[:K], sep='\n')