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: Runtime Error

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


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


X, Y, Z, K = read_ints()
A = read_ints()
B = read_ints()
C = read_ints()
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)


import numpy as np
dp = np.zeros((X, Y, Z), dtype='int64')
dp[0, 0, 0] = A[0] + B[0] + C[0]

from itertools import product
for a, b, c in product(range(X), range(Y), range(Z)):
    if a + 1 != X:
        dp[a+1, b, c] = dp[a, b, c] - A[a]+A[a+1]
    if b + 1 != Y:
        dp[a, b+1, c] = dp[a, b, c] - B[b]+B[b+1]
    if c + 1 != Z:
        dp[a, b, c + 1] = dp[a, b, c] - C[c] + C[c + 1]

for ans in np.sort(dp.reshape(-1))[::-1][:K]:
    print(ans)

Submission status: Runtime Error

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, int(K / 2))):
  for j in range(min(Y, int(K / 2))):
    for k in range(min(Z, int(K / 2))):
      ans.append(A[i] + B[j] + C[k])

ans.sort(reverse = True)

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

Submission status: Runtime Error

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

for i in range(x):
    for j in range(y):
        for k in range(z):
            result.append(a[i] + b[j] + c[k])
            if k > pos:
                break
for x in range(pos):
    print(result[x]

Submission status: Runtime Error

Solution code:
import numpy as np
import sys

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

A = np.array(sorted(list(map(int,input().split()))))[::-1]
B = np.array(sorted(list(map(int,input().split()))))[::-1]
C = np.array(sorted(list(map(int,input().split()))))[::-1]

zenbu = np.zeros(X*Y*Z,dtype=np.float64)

for x in range(X):
    for y in range(Y):
        for z in range(Z):
            zenbu[x*Y*Z+y*Z+z] = A[x]+B[y]+C[z]

ichibu = np.partition(-zenbu, K-1)[:K]
ichibu.sort()
result = -ichibu.astype(np.int)
for r in result:
    print(r)

Submission status: Runtime Error

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

x,y,z,k = [int(i) for i in input().split(' ')]
#print(x,y,z,k)
    
xi = [int(i) for i in input().split()]
yi = [int(i) for i in input().split()]
zi = [int(i) for i in input().split()]

xi.sort(reverse=True)
yi.sort(reverse=True)
zi.sort(reverse=True)

xi = xi[:k//2]
yi = yi[:k//2]
zi = zi[:k//2]

xs = sum(xi)
ys = sum(yi)
zs = sum(zi)

nlist = []
for n in list(itertools.product(xi, yi, zi)):
    nlist.append(n[0] + n[1] + n[2])

nlist.sort(reverse=True)

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

Submission status: Runtime Error

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

ans = []

AA = sorted(A, key=lambda a: -a)
BB = sorted(B, key=lambda a: -a)
CC = sorted(C, key=lambda a: -a)
for a in AA:
    for b in BB:
        for c in CC:
            if len(ans) > 1000_000:
                for an in sorted(ans, key=lambda p: -p)[:K]:
                    print(an)
                    exit()
            ans.append(a+b+c)

for an in sorted(ans, key=lambda p: -p)[:K]:
    print(an)

Submission status: Runtime Error

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

sa = 1
sb = 1
sc = 1

al = [a[0]]
bl = [b[0]]
cl = [c[0]]

inf = 10**20

while True:

    a_ = a[0] - a[sa] if sa < x else inf
    b_ = b[0] - b[sb] if sb < y else inf
    c_ = c[0] - c[sc] if sc < z else inf
    m = min([a_,b_,c_])
    if a_ == m:
        al.append(a[sa])
        sa += 1
    if b_ == m:
        bl.append(b[sb])
        sb += 1
    if c_ == m:
        cl.append(c[sc])
        sc += 1

    if len(al) * len(bl) * len(cl) > k:
        break


da = list(map(lambda x:a[0]-x, a))
db = list(map(lambda x:b[0]-x, b))
dc = list(map(lambda x:c[0]-x, c))
d_ = al[-1] + bl[-1] + cl[-1]
for i in range(sa,x):
    if da[i] < d_:
        al.append(a[i])

for i in range(sb,y):
    if db[i] < d_:
        bl.append(b[i])

for i in range(sc,z):
    if dc[i] < d_:
        cl.append(c[i])

res = []
for a_ in al:
    for b_ in bl:
        for c_ in cl:
            res.append(a_+b_+c_)


for x in sorted(res, reverse=True)[:k]:
    print(x)

Submission status: Runtime Error

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)
C.sort()
C.reverse()
ab_total_list.sort()
ab_total_list.reverse()

z = 0
min_total = 0
if K > len(ab_total_list):
    min_total = ab_total_list[-1]
else:
    min_total = ab_total_list[K]

for i in ab_total_list:
    for j in C:
        total = i + j
        if z > K+2 and min_total > total:
            break
        total_list.append(i+j)
    min_total = total_list[-1]

total_list.sort()
total_list.reverse()
for i in range(0, K):
    print(total_list[i])

Submission status: Runtime Error

Solution code:
import numpy as np
import sys

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

A = np.array(sorted(list(map(int,input().split()))))[::-1]
B = np.array(sorted(list(map(int,input().split()))))[::-1]
C = np.array(sorted(list(map(int,input().split()))))[::-1]

zenbu = np.zeros(X*Y*Z)

for x in range(X):
    for y in range(Y):
        for z in range(Z):
            zenbu[x*Y*Z+y*Z+z] = A[x]+B[y]+C[z]

ichibu = np.partition(-zenbu, K-1)[:K]
ichibu.sort()
result = -ichibu.astype(np.int)
for r in result:
    print(r)

Submission status: Runtime Error

Solution code:
import heapq

def main():
	x, y, z, k = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = list(map(int, input().split()))
	d = [0]*x*y*z
	for i in range(x):
		for j in range(y):
			for l in range(z):
				d[i*y*z+j*z+l] = a[i]+b[j]+c[l]
	d.sort(reverse=True)
	for i in range(k):
		print(d[i])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

d = np.zeros((X, Y, Z))
for i in range(X):
  for j in range(Y):
    for k in range(Z):
      d[i, j, k] = A[i] + B[j] + C[k]

d = np.sort(d.flatten())[::-1]
for i in range(K):
  print(int(d[i]))

Submission status: Runtime Error

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

ans = []

AA = sorted(A, key=lambda a: -a)
BB = sorted(B, key=lambda a: -a)
CC = sorted(C, key=lambda a: -a)
for a in AA:
    for b in BB:
        for c in CC:
            if len(ans) > 1000_000:
                break
            ans.append(a+b+c)

print('\n'.join(list(map(str, sorted(ans, key=lambda p: -p)[:K]))))

Submission status: Runtime Error

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()))
print(A, B, C)

d = np.zeros((X, Y, Z))
for i in range(X):
  for j in range(Y):
    for k in range(Z):
      d[i, j, k] = A[i] + B[j] + C[k]

d = np.sort(d.flatten())[::-1]
for i in range(K):
  print(int(d[i]))

Submission status: Runtime Error

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

if x>50:
  a.sort()
  a.reverse()
  a=a[0:50]

if y>50:
  b.sort()
  b.reverse()
  b=b[0:50]

if z>50:
  c.sort()
  c.reverse()
  c=c[0:50]

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

ansList.sort()
ansList.reverse()

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

Submission status: Runtime Error

Solution code:

xyzk = input().split('\n')[0].split()
a = input().split('\n')[1].split()
b = input().split('\n')[2].split()
c = input().split('\n')[3].split()
for i in range(0,len(a)):
    a[i] = int(a[i])
for i in range(0,len(b)):
    b[i] = int(b[i])
for i in range(0,len(c)):
    c[i] = int(c[i])
for i in range(0,len(xyzk)):
    xyzk[i] = int(xyzk[i])
p=[]
for i in a:
    for j in b:
        for k in c:
            p.append(i+j+k)
p = sorted(p,reverse=True)
print(p[0:xyzk[3]])

Submission status: Runtime Error

Solution code:
import numpy as np
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()]

cakes = np.zeros(x*y*z)
i = 0
for ai in a:
    for bj in b:
        for ck in c:
            cakes[i] = ai + bj + ck
            i += 1
soted_cakes = np.sort(cakes)[::-1]

for cake in soted_cakes[:k] :
    print(int(cake))

Submission status: Runtime Error

Solution code:
n = 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)
a.append(-10000000000)
b.append(-10000000000)
c.append(-10000000000)

l = [0,0,0]
res = []

for i in range(n[3]):
    if a[l[0]] - a[l[0]+1] <= min(b[l[1]] - b[l[1]+1],c[l[2]] - b[l[2]+1]):
        l[0] = l[0] + 1
    elif b[l[1]] - b[l[1]+1] <= c[l[2]] - b[l[2]+1]:
        l[1] = l[1] + 1
    else:
        l[2] = l[2] + 1
    if l[0] * l[1] * l[2] >= n[3]:
        break

for i0 in range(l[0]):
    for i1 in range(l[1]):
        for i2 in range(l[2]):
            res.append(a[i0]+b[i1]+c[i2])

res = sorted(res,reverse=True)

for i in range(n[3]):
    print(res[i])

Submission status: Runtime Error

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)

x = []
for i in range(100):
    for j in range(100):
        for k in range(100):
            x.append(A[i] + B[j] + C[k])
x.sort(reverse=True)

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

Submission status: Runtime Error

Solution code:
K = int(input().split()[-1])

A = [int(e) for e in input().split()]
B = [int(e) for e in input().split()]
C = [int(e) for e in input().split()]
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# C = list(map(int, input().split()))


# def get_good(a, v):
#     return max(a)
#     if v is not None:
#         return max(a[a < v])
#     else:
#         max(a)



prev_a = max(A)
prev_b = max(B)
prev_c = max(C)

value = prev_a + prev_b + prev_c

A.remove(max(A))
B.remove(max(B))
C.remove(max(C))

print(value)

for i in range(K - 1):
    a = max(A)
    b = max(B)
    c = max(C)

    sub_a = prev_a - a
    sub_b = prev_b - b
    sub_c = prev_c - c

    if sub_a < sub_b and sub_a < sub_c:
        A.remove(a)
    if sub_b < sub_a and sub_b < sub_c:
        B.remove(b)
    if sub_c < sub_a and sub_c < sub_b:
        B.remove(c)


    value -= min(sub_a, sub_b, sub_c)
    print(value)

Submission status: Runtime Error

Solution code:
n = int(input())
a = int(input())
b = int(input())
c = int(input())
d = int(input())
e = int(input())
time = 0
print(time)

Submission status: Runtime Error

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


from sys import stdin
import numpy as np

import time
fd = open('b.txt')
stdin = fd
start=time.time()

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

# 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: Runtime Error

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 get_good(a, v):
#     return max(a)
#     if v is not None:
#         return max(a[a < v])
#     else:
#         max(a)



prev_a = max(A)
prev_b = max(B)
prev_c = max(C)

value = prev_a + prev_b + prev_c

A.remove(max(A))
B.remove(max(B))
C.remove(max(C))

print(value)

for i in range(K - 1):
    a = max(A)
    b = max(B)
    c = max(C)

    sub_a = prev_a - a
    sub_b = prev_b - b
    sub_c = prev_c - c

    if sub_a < sub_b and sub_a < sub_c:
        A.remove(a)
    if sub_b < sub_a and sub_b < sub_c:
        B.remove(b)
    if sub_c < sub_a and sub_c < sub_b:
        B.remove(c)


    value -= min(sub_a, sub_b, sub_c)
    print(value)

Submission status: Runtime Error

Solution code:
N = int(input())
A = int(input())
B = int(input())
C = int(input())
D = int(input())
E = int(input())

class Transporter():
    def __init__(self, cap_out, temp=0):
        self.temp_num = temp
        self.cap_out = cap_out

    def transport(self):
        if self.temp_num < self.cap_out:
            num = self.temp_num
            self.temp_num = 0
            return num
        else:
            self.temp_num -= self.cap_out
            return self.cap_out

A_ = Transporter(A, temp=N)
B_ = Transporter(B)
C_ = Transporter(C)
D_ = Transporter(D)
E_ = Transporter(E)

time = 0
while (A_.temp_num!=0):
    time += 1
    a=A_.transport()
    b=B_.transport()
    c=C_.transport()
    d=D_.transport()
    e=E_.transport()
    B_.temp_num+=a
    C_.temp_num+=b
    D_.temp_num+=c
    E_.temp_num+=d

while (B_.temp_num!=0):
    time += 1
    b=B_.transport()
    c=C_.transport()
    d=D_.transport()
    e=E_.transport()
    C_.temp_num+=b
    D_.temp_num+=c
    E_.temp_num+=d

while (C_.temp_num!=0):
    time += 1
    c=C_.transport()
    d=D_.transport()
    e=E_.transport()
    D_.temp_num+=c
    E_.temp_num+=d

whileD(D_.temp_num!=0):
    time += 1
    d=D_.transport()
    e=E_.transport()
    E_.temp_num+=d

whileD(E_.temp_num!=0):
    time += 1
    E_.transport()
print(time)

Submission status: Runtime Error

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


from sys import stdin
import numpy as np

import time
fd = open('b.txt')
stdin = fd
start=time.time()

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

# 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)
print(ans[:K])
#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: Runtime Error

Solution code:
xyzk = input().split('\n')[0].split()
a = input().split('\n')[1].split()
b = input().split('\n')[2].split()
c = input().split('\n')[3].split()
for i in range(0,len(a)):
    a[i] = int(a[i])
for i in range(0,len(b)):
    b[i] = int(b[i])
for i in range(0,len(c)):
    c[i] = int(c[i])
for i in range(0,len(xyzk)):
    xyzk[i] = int(xyzk[i])
p=[]
for i in a:
    for j in b:
        for k in c:
            p.append(i+j+k)
p = sorted(p,reverse=True)
print(p[0:xyzk[3]])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)


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 = sorted(a)[::-1]
b = sorted(b)[::-1]
c = sorted(c)[::-1]


def search(a, b, c, x, y, z, a_index, b_index, c_index):
    value = -1
    ans = -1
    if(a_index != x-1):
        if(value < a[a_index+1]+b[b_index]+c[c_index]):
            value = a[a_index+1]+b[b_index]+c[c_index]
            ans = 0
    if(b_index != y-1):
        if(value < a[a_index]+b[b_index+1]+c[c_index]):
            value = a[a_index]+b[b_index+1]+c[c_index]
            ans = 1
    if(c_index != z-1):
        if(value < a[a_index]+b[b_index]+c[c_index+1]):
            value = a[a_index]+b[b_index]+c[c_index+1]
            ans = 2

    if(ans == 0): return (a_index+1, b_index, c_index)
    elif(ans == 1): return (a_index, b_index+1, c_index)
    elif(ans == 2): return (a_index, b_index, c_index+1)


a_index = 0
b_index = 0
c_index = 0
pre = a[0] * b[0] * c[0]
while(True):
    a_index, b_index, c_index = search(a, b, c, x, y, z, a_index, b_index, c_index)

    if(pre != a[a_index]+b[b_index]+c[c_index]):
        if((a_index+1)*(b_index+1)*(c_index+1) >= x*y*z): break

    pre = a[a_index]+b[b_index]+c[c_index]


ans = []
for i in range(a_index+1):
    for j in range(b_index+1):
        for l in range(c_index+1):
            ans.append(a[i]+b[j]+c[l])

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

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

Submission status: Runtime Error

Solution code:
xyzk = input().split('\n')[0].split()
a = input().split('\n')[1].split()
b = input().split('\n')[2].split()
c = input().split('\n')[3].split()
for i in range(0,len(a)):
    a[i] = int(a[i])
for i in range(0,len(b)):
    b[i] = int(b[i])
for i in range(0,len(c)):
    c[i] = int(c[i])
for i in range(0,len(xyzk)):
    xyzk[i] = int(xyzk[i])

a=sorted(a,reverse=True)
b=sorted(b,reverse=True)
c =sorted(c,reverse=True)
p=[]
for i in a:
    for j in b:
        for k in c:
            p.append(i+j+k)
p = sorted(p,reverse=True)
print(p[0:xyzk[3]])

Submission status: Runtime Error

Solution code:
x, y, z, k = map(int, input().split())
a = sorted(list(map(int, input().split())),reverse=True)
b = sorted(list(map(int, input().split())),reverse=True)
c = sorted(list(map(int, input().split())),reverse=True)
useA = [a.pop(0)]
useB = [b.pop(0)]
useC = [c.pop(0)]
while True:
  if a[0] == b[0] == c[0] == 0:
    break
  if a[0] >= b[0] and a[0] >= c[0]:
    useA.append(a.pop(0))
    if len(a) == 0:
      a.append(0)
  elif b[0] >= a[0] and b[0] >= c[0]:
    useB.append(b.pop(0))
    if len(b) == 0:
      b.append(0)
  elif c[0] >= b[0] and c[0] >= a[0]:
    useC.append(c.pop(0))
    if len(c) == 0:
      c.append(0)
#  print( len(useA) * len(useB) * len(useC))
  if k**2 <= len(useA) * len(useB) * len(useC):
    break
ans = []
#print(useA, useB, useC)
for i in range(len(useA)):
  for j in range(len(useB)):
    for m in range(len(useC)):
      ans.append(useA[i] + useB[j] + useC[m])
ans.sort(reverse = True)
for i in range(k):
  print(ans[i])

Submission status: Runtime Error

Solution code:
xyzk = input().split('\n')[0].split()
a = input().split('\n')[1].split()
b = input().split('\n')[2].split()
c = input().split('\n')[3].split()
for i in range(0,len(a)):
    a[i] = int(a[i])
for i in range(0,len(b)):
    b[i] = int(b[i])
for i in range(0,len(c)):
    c[i] = int(c[i])
for i in range(0,len(xyzk)):
    xyzk[i] = int(xyzk[i])

a=sorted(a,reverse=True)
b=sorted(b,reverse=True)
c =sorted(c,reverse=True)
p=[]
for i in a:
    for j in b:
        for k in c:
            p.append(i+j+k)
p = sorted(p,reversed=True)
print(p[0:xyzk[3]])

Submission status: Runtime Error

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

if x>200:
  a.sort()
  a.reverse()
  a=a[0:200]

if y>200:
  b.sort()
  b.reverse()
  b=b[0:200]

if z>200:
  c.sort()
  c.reverse()
  c=c[0:200]

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: Runtime Error

Solution code:
x,y,z,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
a.reverse()
b.sort()
b.reverse()
c.sort()
c.reverse()
ans=[]
if x*y*z<=3000:
  for i in range(x):
    for j in range(y):
      for h in range(z):
        ans.append(a[i]+b[j]+c[h])
  ans.sort()
  ans.reverse()
  for i in range(k):
    print(ans[i])
  exit()
p=0
q=0
r=0
while (p+1)*(q+1)*(r+1)<k:
  if a[p]==min(a[p],b[q],c[r]) and p+1<len(a):
    p+=1
  elif b[q]==min(a[p],b[q],c[r]) and q+1<len(b):
    q+=1
  elif c[r]==min(a[p],b[q],c[r]) and r+1<len(c):
    p+=1
  else:
    if p+1<len(a):
      p+=1
    if q+1<len(b):
      q+=1
    if r+1<len(c):
      r+=1
for i in range(p+1):
  for j in range(q+1):
    for h in range(r+1):
      ans.append(a[i]+b[j]+c[h])
ans.sort()
ans.reverse()
for i in range(k):
  print(ans[i])

Submission status: Runtime Error

Solution code:
xyzk = input().split('\n')[0].split()
a = input().split('\n')[1].split()
b = input().split('\n')[2].split()
c = input().split('\n')[3].split()
for i in range(0,len(a)):
    a[i] = int(a[i])
for i in range(0,len(b)):
    b[i] = int(b[i])
for i in range(0,len(c)):
    c[i] = int(c[i])
for i in range(0,len(xyzk)):
    xyzk[i] = int(xyzk[i])

a=sorted(a,reverse=True)
b=sorted(b,reverse=True)
c =sorted(c,reverse=True)
p=[]
for i in a:
    for j in b:
        for k in c:
            p.append(i+j+k)
p = sorted(p)
print(p[0:xyzk[3]],reversed=True)

Submission status: Runtime Error

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)

ABC = np.array([0]*X*Y*Z)
cnt=0 
for x in range(X):
    for y in range(Y):
        for z in range(Z):
            ABC[cnt]=A[x]+B[y]+C[z]
            cnt+=1
np.sort(ABC)[::-1]
for k in range(K):
    print(ABC[k])

Submission status: Runtime Error

Solution code:
xyzk = input().split('\n')[0].split()
a = input().split('\n')[1].split()
b = input().split('\n')[2].split()
c = input().split('\n')[3].split()
a=sorted(a,reverse=True)
b=sorted(b,reverse=True)
c =sorted(c,reverse=True)
p=[]
for i in a:
    for j in b:
        for k in c:
            p.append(i+j+k)
p = sorted(p)
print(p[0:xyzk[3]],reversed=True)

Submission status: Runtime Error

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 - 8 if x - 8 < 0 else 0, x):
    for j in range(y - 8 if x - 8 < 0 else 0, y):
        for l in range(z - 8 if x - 8 < 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: Runtime Error

Solution code:
x, y, z, k = map(int, input().split())
a = sorted(list(map(int, input().split())),reverse=True)
b = sorted(list(map(int, input().split())),reverse=True)
c = sorted(list(map(int, input().split())),reverse=True)
useA = [a.pop(0)]
useB = [b.pop(0)]
useC = [c.pop(0)]
while True:
  if a[0] >= b[0] and a[0] >= c[0]:
    useA.append(a.pop(0))
    if len(a) == 0:
      a.append(0)
  elif b[0] >= a[0] and b[0] >= c[0]:
    useB.append(b.pop(0))
    if len(b) == 0:
      b.append(0)
  elif c[0] >= b[0] and c[0] >= a[0]:
    useC.append(c.pop(0))
    if len(c) == 0:
      c.append(0)
#  print( len(useA) * len(useB) * len(useC))
  if k**2 <= len(useA) * len(useB) * len(useC):
    break
ans = []
#print(useA, useB, useC)
for i in range(len(useA)):
  for j in range(len(useB)):
    for m in range(len(useC)):
      ans.append(useA[i] + useB[j] + useC[m])
ans.sort(reverse = True)
for i in range(k):
  print(ans[i])

Submission status: Runtime Error

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 = sorted(a)[::-1]
b = sorted(b)[::-1]
c = sorted(c)[::-1]


def search(a, b, c, x, y, z, a_index, b_index, c_index):
    value = -1
    ans = -1
    if(a_index != x-1):
        if(value < a[a_index+1]+b[b_index]+c[c_index]):
            value = a[a_index+1]+b[b_index]+c[c_index]
            ans = 0
    if(b_index != y-1):
        if(value < a[a_index]+b[b_index+1]+c[c_index]):
            value = a[a_index]+b[b_index+1]+c[c_index]
            ans = 1
    if(c_index != z-1):
        if(value < a[a_index]+b[b_index]+c[c_index+1]):
            value = a[a_index]+b[b_index]+c[c_index+1]
            ans = 2

    if(ans == 0): return (a_index+1, b_index, c_index)
    elif(ans == 1): return (a_index, b_index+1, c_index)
    elif(ans == 2): return (a_index, b_index, c_index+1)


a_index = 0
b_index = 0
c_index = 0
while(True):
    a_index, b_index, c_index = search(a, b, c, x, y, z, a_index, b_index, c_index)

    if((a_index+1)*(b_index+1)*(c_index+1) >= min(x*y*z, 1e7)): break


ans = []
for i in range(a_index+1):
    for j in range(b_index+1):
        for l in range(c_index+1):
            ans.append(a[i]+b[j]+c[l])

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

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

Submission status: Runtime Error

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


def get_good(a, v):
    if v is not None:
        return max(a[a < v])
    else:
        max(a)


value = 0
a = b = c = None
for i in range(K):
    print(get_good(A, a) + get_good(B, b), get_good(C, c))
    a = get_good(a)
    b = get_good(b)
    c = get_good(c)