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

Submission status: Runtime Error

Solution code:
x,y,z = map(int,input().split())
k =int(input())
A=list(map(int,int(input().split())))
B=list(map(int,int(input().split())))
C=list(map(int,int(input().split())))
aa = []
for a in range(x):
    for b in range(y):
        for c in range(z):
            aa.append(A[a]+B[b]+C[c])
aa.sort(reverse=True)
for i in aa:
    print(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)
i = 0
j = 0
k = 0
while i+j+k < min(3000,X*Y*Z):
  ans = A[i]+B[j]+C[k]
  deltaA = 10**16
  deltaB = 10**16
  deltaC = 10**16
  if i+j+k == min(3000,X*Y*Z)-1:
    break
  if i+1<X:
    deltaA=A[i]-A[i+1]
  if j+1<Y:
    deltaB=B[j]-B[j+1]
  if k+1<Z:
    deltaC=C[k]-C[k+1]
  L = 10**16
  index = -1
  w = [deltaA,deltaB,deltaC]
  for mm in range(3):
    if w[i] < L:
      L = w[mm]
      index = mm
  if index==0:
    i+=1
  elif index==1:
    j+=1
  else:
    k+=1
I=i+1
J=j+1
M=k+1
answers =[]
for i in range(I):
  for j in range(J):
    for k in range(M):
      answers.append(A[i]+B[j]+C[k])
answers.sort(reverse=True)
for i in range(min(3000,X*Y*Z)):
  print(answers[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()))
L[i+j+k for i in A for j in B for k in C]
L.sort()
L.reverse()
for i in L:
  print(i)

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

K = min(K, X*Y*Z)
ls = sorted(dp.reshape(-1), reverse=True)
for ans in ls[:K]:
    print(ans)

Submission status: Runtime Error

Solution code:
import bisect

x,y,z,k = map(int, input().split())
aa = sorted(list(map(int, input().split())))[::-1]
bb = sorted(list(map(int, input().split())))[::-1]
cc = sorted(list(map(int, input().split())))[::-1]

ia,ib,ic = 0,0,0
aa_ = [aa[ia]]
bb_ = [bb[ib]]
cc_ = [cc[ic]]
while len(aa_)*len(bb_)*len(cc_) <=k:
    if ia<x and aa[ia] == min(aa[ia],bb[ib],cc[ic]):
        ia +=1
        aa_ += [aa[ia]]
    if ib<y and bb[ib] == min(aa[ia],bb[ib],cc[ic]):
        ib +=1
        bb_ += [bb[ib]]
    else:
        ic +=1
        cc_ += [cc[ic]]

score = []
for a in aa_:
    for b in bb_:
        for c in cc_:
            bisect.insort_right(score, a+b+c)
for s in score[::-1][:k]:
    print(s)

Submission status: Runtime Error

Solution code:
X, Y, Z, K = list(map(int, input().split(" ")))
xs = list(map(int, input().split(" ")))
ys = list(map(int, input().split(" ")))
zs = list(map(int, input().split(" ")))
lst = np.array([x + y + z for x in xs for y in ys for z in zs])
lst_s = np.sort(lst)
for i in range(K):
    print(lst_s[-(i+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()))
a.sort()
b.sort()
c.sort()
d = []
f = []
for i in range(max(0,len(a)-5), len(a)):
    for j in range(max(0,len(b)-5), len(b)):
        for l in range(len(c)):
            if not [a[i],b[j],c[l]] in f:
                e = a[i]+b[j]+c[l]
                d.append(e)
                f.append([a[i],b[j],c[l]])
for i in range(max(0,len(a)-5), len(a)):
    for j in range(len(b)):
        for l in range(max(0,len(c)-5), len(c)):
            if not [a[i],b[j],c[l]] in f:
                e = a[i]+b[j]+c[l]
                d.append(e)
                f.append([a[i],b[j],c[l]])
for i in range(len(a)):
    for j in range(max(0,len(b)-5), len(b)):
        for l in range(max(0,len(c)-5), len(c)):
            if not [a[i],b[j],c[l]] in f:
                e = a[i]+b[j]+c[l]
                d.append(e)
                f.append([a[i],b[j],c[l]])
for i in range(max(0,len(a)- 20), len(a)):
    for j in range(max(0,len(b)-20), len(b)):
        for l in range(max(0,len(c)-20), len(c)):
            if not [a[i],b[j],c[l]] in f:
                e = a[i]+b[j]+c[l]
                d.append(e)
                f.append([a[i],b[j],c[l]])
d.sort()
for i in range(1,k+1):
    print(d[-i])

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

ls = sorted(dp.reshape(-1), reverse=True)
for ans in ls[: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()
b.sort()
c.sort()
d = []
f = []
for i in range(max(0,len(a)-5), len(a)):
    for j in range(max(0,len(b)-5), len(b)):
        for l in range(len(c)):
            if not [a[i],b[j],c[l]] in f:
                e = a[i]+b[j]+c[l]
                d.append(e)
                f.append([a[i],b[j],c[l]])
for i in range(max(0,len(a)-5), len(a)):
    for j in range(len(b)):
        for l in range(max(0,len(c)-5), len(c)):
            if not [a[i],b[j],c[l]] in f:
                e = a[i]+b[j]+c[l]
                d.append(e)
                f.append([a[i],b[j],c[l]])
for i in range(len(a)):
    for j in range(max(0,len(b)-5), len(b)):
        for l in range(max(0,len(c)-5), len(c)):
            if not [a[i],b[j],c[l]] in f:
                e = a[i]+b[j]+c[l]
                d.append(e)
                f.append([a[i],b[j],c[l]])
for i in range(max(0,len(a)-50), len(a)):
    for j in range(max(0,len(b)-50), len(b)):
        for l in range(max(0,len(c)-50), len(c)):
            if not [a[i],b[j],c[l]] in f:
                e = a[i]+b[j]+c[l]
                d.append(e)
                f.append([a[i],b[j],c[l]])
d.sort()
for i in range(1,k+1):
    print(d[-i])

Submission status: Runtime Error

Solution code:
import numpy as np
X, Y, Z, K = list(map(int, input().split(" ")))
xs = np.array(list(map(int, input().split(" "))))
ys = np.array(list(map(int, input().split(" "))))
zs = np.array(list(map(int, input().split(" "))))
xm = xs - np.max(xs)
ym = ys - np.max(ys)
zm = zs - np.max(zs)
if K+2 <= X*Y*Z:
    lim = np.sort(np.hstack((xm, ym, zm)))[-(K+2)]
    xs = xs[xm>=lim]
    ys = ys[ym>=lim]
    zs = zs[zm>=lim]
lst = np.array([x + y + z for x in xs for y in ys for z in zs])
lst_s = np.sort(lst)
for i in range(K):
    print(lst_s[-(i+1)])

Submission status: Runtime Error

Solution code:
import numpy as np
XYZK=list(map(int, input().split()))
LA=list(map(int, input().split()))
LB=list(map(int, input().split()))
LC=list(map(int, input().split()))

#全パターン作ってソートしてみる
#forを使わなければ早いかも
A=np.array(LA).reshape(len(LA),1)
B=np.array(LB).reshape(len(LB),1)
C=np.array(LC).reshape(len(LC),1)

D=(np.dot(A,np.ones([1,B.shape[0]]))+np.dot(np.ones([A.shape[0],1]),B.T)).reshape(A.shape[0]*B.shape[0],1)
E=(np.dot(D,np.ones([1,C.shape[0]]))+np.dot(np.ones([D.shape[0],1]),C.T)).reshape(C.shape[0]*D.shape[0],1)

F=[]
for i in range(E.shape[0]):
  F.append(E[i,0])

del D
del E
F.sort(reverse=True)
for i in range(XYZK[3]):
  print(int(F[i]))

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline



def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [X, Y, Z, K] = [int(input_raw[0]), int(input_raw[1]), int(input_raw[2]), int(input_raw[3])]

    input_raw = read_func().strip().split()
    A = [long(input_raw[i]) for i in range(X)]
    input_raw = read_func().strip().split()
    B = [long(input_raw[i]) for i in range(Y)]
    input_raw = read_func().strip().split()
    C = [long(input_raw[i]) for i in range(Z)]

    count = 0
    for i in range(X):
        for j in range(Z):
            BC[count] = B[i] + C[j]
            count += 1

    A.sort(reverse = True)
    BC.sort(reverse = True)

    for i in range(X):
        for j in range(min(K, len(BC))):
            T.append(A[i] + BC[j])


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

if __name__ == '__main__':
    main()

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()
b.sort()
c.sort()
d = []
f = []
for i in range(max(0,len(a)-5), len(a)):
    for j in range(max(0,len(b)-5), len(b)):
        for l in range(len(c)):
            if not [a[i],b[j],c[l]] in f:
                e = a[i]+b[j]+c[l]
                d.append(e)
                f.append([a[i],b[j],c[l]])
for i in range(max(0,len(a)-5), len(a)):
    for j in range(len(b)):
        for l in range(max(0,len(c)-5), len(c)):
            if not [a[i],b[j],c[l]] in f:
                e = a[i]+b[j]+c[l]
                d.append(e)
                f.append([a[i],b[j],c[l]])
for i in range(len(a)):
    for j in range(max(0,len(b)-5), len(b)):
        for l in range(max(0,len(c)-5), len(c)):
            if not [a[i],b[j],c[l]] in f:
                e = a[i]+b[j]+c[l]
                d.append(e)
                f.append([a[i],b[j],c[l]])
for i in range(max(0,len(a)-90), len(a)):
    for j in range(max(0,len(b)-90), len(b)):
        for l in range(max(0,len(c)-90), len(c)):
            if not [a[i],b[j],c[l]] in f:
                e = a[i]+b[j]+c[l]
                d.append(e)
                f.append([a[i],b[j],c[l]])
d.sort()
for i in range(1,k+1):
    print(d[-i])

Submission status: Runtime Error

Solution code:
x,y,z,k=map(int,input().split());a,b,c=list(map(int,input().split())),list(map(int,input().split())),list(map(int,input().split()));a.sort(reverse=1);b.sort(reverse=1);c.sort(reverse=1);ma=a[0]+b[0]+c[0];d=[[0],[0],[0]]
for i in range(len(a)-1):d[0].append(d[0][-1]+a[i]-a[i+1])
for i in range(len(b)-1):d[1].append(d[1][-1]+b[i]-b[i+1])
for i in range(len(c)-1):d[2].append(d[2][-1]+c[i]-c[i+1])
e=d[0]+d[1]+d[2]
if x*y*z>3000:
  f,i=[0,0,0],0
  while f[0]*f[1]*f[2]<3000:
    for z in range(3):
      if f[z]<len(d[z]):
        while d[z][f[z]]==i:
          f[z]+=1
          if f[z]==len(d[z]):break
    i=min(d[0][f[0]],min(d[1][f[1]],d[2][f[2]]))
  for i in range(3):d[i]=d[i][:f[i]+1]
s=[]
for i in d[0]:
  for j in d[1]:
    for h in d[2]:s.append(i+h+j)
s.sort()
for i in range(k):print(ma-s[i])

Submission status: Runtime Error

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

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

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

input = sys.stdin.readline

# N個整数
x, y, z, kk = 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)
c.sort(reverse=True)


ll = [0] * 1000000
count = 0
for i in range(len(a)):
    for j in range(len(b)):
        if i * j > kk:

            break
        for k in range(len(c)):
            if i * j * k > kk:
                break
            ll[count] = a[i] + b[j] + c[k]
            count += 1
ll.sort(reverse=True)

for i in range(kk):
    print(ll[i])

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

Submission status: Runtime Error

Solution code:
list=input().split(" ")
x=int(list[0])
y=int(list[1])
z=int(list[2])
k=int(list[3])

kari=input().split(" ")
alist=[]
for ax in range(x):
    alist.append(int(kari[ax]))
alist=sorted(alist)

kari=input().split(" ")
blist=[]
for by in range(y):
    blist.append(int(kari[by]))
blist=sorted(blist)

kari=input().split(" ")
clist=[]
for cz in range(z):
    clist.append(int(kari[cz]))
clist=sorted(clist)

nowx=0
nowy=0
nowz=0


ngo=[]

for a in range(min(x,10)):
    for b in range(min(y,10)):
        for c in range(min(z,10)):
            ngo.append(alist[x-a-1]+blist[y-b-1]+clist[z-c-1])
            
            
for a in range(max(x-10,0)):
    for b in range(min(y,10)):
        for c in range(min(z,10)):
            ngo.append(alist[x-a-1+max(x-10)]+blist[y-b-1]+clist[z-c-1])

for a in range(min(x, 10)):
    for b in range(max(y-10,0)):
        for c in range(min(z,10)):
            ngo.append(alist[x-a-1]+blist[y-b-1+max(y-10)]+clist[z-c-1])

for a in range(min(x, 10)):
    for b in range(min(y,10)):
        for c in range(max(z-10, 0)):
            ngo.append(alist[x-a-1]+blist[y-b-1]+clist[z-c-1+max(z-10)])

ngo=sorted(ngo, reverse=True)

for d in range(k):
    print(ngo[d])

Submission status: Runtime Error

Solution code:
x, y, z, k = list(map(int, input().split()))
r = [0] * (x * y * z)

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

k = min(3000, k)

i = 0
for ix in range(x):
    for iy in range(y):
        for iz in range(z):
            r[i] = a[ix] + b[iy] + c[iz]
            i += 1

r.sort(reverse=True)

if i >= k:
    for m in range(k):
        print(r[m])
else:
    for m in range(i):
        print(r[m])

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 = sorted(a)
b = sorted(b)
c = sorted(c)
memory = []
ai = x-1
bi = y-1
ci = z-1
p = 10**11
while k>0:
    ps = []
    abc = []
    for dx in range(-5,5):
        for dy in range(-5,5):
            for dz in range(-5,5):
                if not (ai+dx>=0 and ai+dx<x and bi+dy>=0 and bi+dy<y and ci+dz>=0 and ci+dz<z):
                    continue
                tmp = a[ai+dx]+b[bi+dy]+c[ci+dz]
                if p >= tmp and not (ai+dx,bi+dy,ci+dz) in memory:
                    ps.append(tmp) 
                    abc.append((ai+dx, bi+dy, ci+dz))
    p = max(ps)
    i = ps.index(p)
    memory.append(abc[i])
    print(p)
    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()))

ab_total_list = []
total_list = []

A.sort()
A.reverse()
if len(A) > K:
    A = A[0:K+1]

B.sort()
B.reverse()
if len(B) > K:
    B = B[0:K+1]

C.sort()
C.reverse()
if len(C) > K:
    C = C[0:K+1]

for i in A:
    for j in B:
        ab_total_list.append(i+j)

ab_total_list.sort()
ab_total_list.reverse()
if len(ab_total_list) > K:
    ab_total_list = ab_total_list[0:K+1]
    
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
        total_list.append(i+j)

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

Submission status: Runtime Error

Solution code:
import itertools, math

def cal(a):
    return a[0] + a[1] + a[2]

k = list(map(int, input().split()))
index = math.ceil(k[3] / 3)
a = sorted(list(map(int, input().split())), reverse=True)[index]
b = sorted(list(map(int, input().split())), reverse=True)[index]
c = sorted(list(map(int, input().split())), reverse=True)[index]
l = sorted(list(map(cal,itertools.product(a,b,c))), reverse=True)
for i in range(k[3]):
    print(l[i])

Submission status: Runtime Error

Solution code:
import heapq

x, y, z, k = map(int, input().split())
ali = list(reversed(sorted(map(int, input().split()))))
bli = list(reversed(sorted(map(int, input().split()))))
cli = list(reversed(sorted(map(int, input().split()))))

done = {}
count = 0

heap = []


def main(a, b, c):
    global count
    if f'{a, b, c}' in done:
        return
    done[f'{a, b, c}'] = True
    count += 1
    print(ali[a] + bli[b] + cli[c])
    if a + 1 < x:
        heapq.heappush(heap, (-(ali[a + 1] + bli[b] + cli[c]), a + 1, b, c))
    if b + 1 < y:
        heapq.heappush(heap, (-(ali[a] + bli[b + 1] + cli[c]), a, b + 1, c))
    if c + 1 < z:
        heapq.heappush(heap, (-(ali[a] + bli[b] + cli[c + 1]), a, b, c + 1))


main(0, 0, 0)
while count < k:
    s, a, b, c = heapq.heappop(heap)
    main(a, b, c)

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 = sorted(a)
b = sorted(b)
c = sorted(c)
memory = []
ai = x-1
bi = y-1
ci = z-1
p = 10**5
while k>0:
    ps = []
    abc = []
    for dx in range(-5,5):
        for dy in range(-5,5):
            for dz in range(-5,5):
                if not (ai+dx>=0 and ai+dx<x and bi+dy>=0 and bi+dy<y and ci+dz>=0 and ci+dz<z):
                    continue
                tmp = a[ai+dx]+b[bi+dy]+c[ci+dz]
                if p >= tmp and not (ai+dx,bi+dy,ci+dz) in memory:
                    ps.append(tmp) 
                    abc.append((ai+dx, bi+dy, ci+dz))
    p = max(ps)
    i = ps.index(p)
    memory.append(abc[i])
    print(p)
    k -= 1

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]

del A 
del B 
del C

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

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

A.sort()
A.reverse()
if len(A) > K:
    A = A[0:K+1]

B.sort()
B.reverse()
if len(B) > K:
    B = B[0:K+1]

C.sort()
C.reverse()
if len(C) > K:
    C = C[0:K+1]

for i in A:
    for j in B:
        ab_total_list.append(i+j)
        
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
        total_list.append(i+j)

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

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=int)
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,q = 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)
b = sorted(b)
c = sorted(c)



if  x*y*z <100000
    la = []
    for i in range(x):
        for k in range(y):
            for m in range(z):
                la.append(a[i]+b[k]+c[m])
    la = sorted(la)

    for p in range(q):
        print(la[-p-1])

else:
    la  = []
    for i in range(x):
        for k in range(3000//i, y):
            for m in range(3000//(i*k), z):
                la.append(a[i]+b[k]+c[m])
    la = sorted(la)
    for i in range(q):
        print(la[-i-1])

Submission status: Runtime Error

Solution code:
x,y,z,k=map(int,input().split())
a=list(map(int,input().split())).sort(reverse=True)
b=list(map(int,input().split())).sort(reverse=True)
c=list(map(int,input().split())).sort(reverse=True)
n=0
l=[]
while n=<k:
    for i in range(k//3):
        for j in range(k//3):
            for x in range(k//3):
                l.append(a[i]+b[j]+c[x])
l.sort(reverse=True)
for i in range(k):
    print(l[i])

Submission status: Runtime Error

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

sys.setrecursionlimit(100000)


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


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


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


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


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


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


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


def mt(f):
    import time

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

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

    return wrap


@mt
def slv(A, B, C, K):
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)

    def s(i, j, k):
        return A[i] + B[j] + C[k]
    used = set()
    
    cand = []
    heapq.heappush(-s(0, 0, 0), (0, 0, 0))
    for _ in range(K-1):
        v, i = heapq.heappop(cand)
        print(-v)
        
        ti = (i[0]+1, i[1], i[2])
        if ti[0] < len(A) and ti not in used:
            heapq.heappush(cand, (-s(*ti), ti))
            used.add(ti)
        
        ti = (i[0], i[1]+1, i[2])
        if ti[1] < len(B) and ti not in used:
            heapq.heappush(cand, (-s(*ti), ti))
            used.add(ti)
        
        ti = (i[0], i[1], i[2]+1)
        if ti[2] < len(C) and ti not in used:
            heapq.heappush(cand, (-s(*ti), ti))
            used.add(ti)



def main():
    # for _ in range(1000):
    #     K = 3000
    #     A = [random.randint(1, 10000000000) for _ in range(random.randint(1, 1000))]
    #     B = [random.randint(1, 10000000000) for _ in range(random.randint(1, 1000))]
    #     C = [random.randint(1, 10000000000) for _ in range(random.randint(1, 1000))]
    #     K = min(K, len(A) + len(B) + len(C))
    #     slv(A,B,C,K)
    # N = read_int()
    X, Y, Z, K = read_int_n()
    A = read_int_n()
    B = read_int_n()
    C = read_int_n()
    (slv(A, B, C, K))


if __name__ == '__main__':
    main()

Submission status: 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 = sorted(a)
b = sorted(b)
c = sorted(c)
memory = []
ai = x-1
bi = y-1
ci = z-1
p = 10**5
while k>0:
    ps = []
    abc = []
    for dx in range(-ai,x-ai):
        for dy in range(-bi,y-bi):
            for dz in range(-ci,z-ci):
                if not (ai+dx>=0 and ai+dx<x and bi+dy>=0 and bi+dy<y and ci+dz>=0 and ci+dz<z):
                    continue
                tmp = a[ai+dx]+b[bi+dy]+c[ci+dz]
                if p >= tmp and not (ai+dx,bi+dy,ci+dz) in memory:
                    ps.append(tmp) 
                    abc.append((ai+dx, bi+dy, ci+dz))
    p = max(ps)
    i = ps.index(p)
    memory.append(abc[i])
    print(p)
    k -= 1

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]

del A 
del B 
del C

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

Submission status: Runtime Error

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

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

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

C.sort(reverse=True)
AB.sort(reverse=True)


ABX=[AB[0]-ab for ab in AB]
CX=[C[0]-c for c in C]

ABX.append(float("inf"))
CX.append(float("inf"))

j=0
for i in range(X*Y):
    while j<Z-1 and CX[j]<ABX[i]:
        j+=1

    if (i+1)*(j+1)>K:
        break

print(i,j)

MIN=AB[i]+C[j]
MAX=AB[0]+C[0]-MIN
LIST=[]

for ii in range(X+Z):
    if AB[0]-AB[ii]>MAX:
        break
    for jj in range(Y):
        if AB[ii]+C[jj]>=MIN:
            LIST.append(AB[ii]+C[jj])
        else:
            break

LIST.sort(reverse=True)

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