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

Q = [[0,0,0]]
i,j,k = Q[0]
P = [A[i]+B[j]+C[k]]

def next(P,Q, cnt):
    if len(P)<0 or cnt==N:
        return
    p = P.pop(0)
    print(p)
    i,j,k = Q.pop(0)
    if i+1<x:
        P.append(A[i+1] + B[j] + C[k])
        Q.append([i+1,j,k])
    if j+1<y:
        P.append(A[i] + B[j+1] + C[k])
        Q.append([i,j+1,k])
    if k+1<z:
        P.append(A[i] + B[j] + C[k+1])
        Q.append([i,j,k+1])
    ids = sorted(range(len(P)), key=lambda k:P[k], reverse=True)
    tmp = [Q[id] for id in ids]
    Q = tmp
    P.sort(reverse=True)
    next(P, Q, cnt+1)
        

next(P, Q, 0)

Submission status: Runtime Error

Solution code:
# D
x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
a_l = []
a_max = a[0]
for i in range(len(a)):
    a_l.append(a[i] - a_max)
b_l = []
b_max = b[0]
for i in range(len(b)):
    b_l.append(b[i] - b_max)
c_l = []
c_max = c[0]
for i in range(len(c)):
    c_l.append(c[i] - c_max)
a_count = 0
b_count = 0
c_count = 0
while a_count * b_count * c_count < k:
    l = [a_l[a_count], b_l[b_count], c_l[c_count]]
    l.sort(reverse=True)
    for m in l:
        if m == a_l[a_count] and a_count < len(a):
            a_count += 1
        elif m == b_l[b_count] and b_count < len(b):
            b_count += 1
        elif m == b_l[b_count] and b_count < len(b):
            c_count += 1
    print(a_count, b_count, c_count)
a = a[:a_count+1]
b = b[:b_count+1]
c = c[:c_count+1]
print(a, b, c)
ans = []
for i in a:
    for j in b:
        for l in c:
            ans.append(i+j+l)
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: 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())
l=[0]*3000
r=[0]*3000
def merge_sort(array):
    if len(array) <= 1:
        return array
    cnt=len(array)//2
    left=array[:cnt]
    right=array[cnt:]
    left = merge_sort(left)
    right = merge_sort(right)
    return merge(left, right)


def merge(left, right):
    array = [0]*3000
    l,r,ind=0,0,0
    if right[0]<left[-1]:
        return left
    while l < len(left) and r < len(right):
        if left[l] >= right[r]:
            array[ind]=left[l]
            l+=1
            ind+=1
        else:
            array[ind]=right[r]
            r+=1
            ind+=1
        if ind>=3000:
            return array
    if l < len(left):
        return array[:ind]+left[l:]
    if r < len(right):
        return array[:ind]+right[r:]

a=merge_sort(a)
b=merge_sort(b)
c=merge_sort(c)
for i in range(y):
    l=merge(l,list(map(lambda w:w+b[i],a)))
for i in range(z):
    r=merge(r,list(map(lambda w:w+c[i],l)))
for i in range(k):
    print(r[i])

Submission status: Runtime Error

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

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

a_nums.sort(reverse=True)
b_nums.sort(reverse=True)
c_nums.sort(reverse=True)

p, q, r = 0, 0, 0
now = a_nums[p] + b_nums[q] + c_nums[r]

while (p+1) * (q+1) * (r+1) < k:
    if a_nums[p+1] >= max(b_nums[q+1], c_nums[r+1]):
        p += 1
    elif b_nums[q + 1] >= c_nums[r + 1]:
        q += 1
    else:
        r += 1

a_nums = a_nums[:p]
b_nums = b_nums[:q]
c_nums = c_nums[:r]

sums.sort(reverse=True)

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

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

Submission status: Runtime Error

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

#input
import numpy as np
[X,Y,Z,K] = list(map(int,input().split()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

D = []
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

#make list
memo = np.zeros((X,Y,Z))
for i in range(min(K,X)):
    for j in range(min((K//(i+1))+1,Y)):
        for k in range(min(((K//(i+1))+1)//(j+1),Z)):
            if memo[i][j][k] != 0:
                continue
            else:
                memo[i][j][k] = A[i]+B[j]+C[k]
                D.append(memo[i][j][k])
D.sort(reverse=True)
for i in range(K):
    print(int(D[i]))

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.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
nA = A[0]
nB = B[0]
nC = C[0]
a, b, c = [], [], []
for i in A:
    a.append(nA-i)
for i in B:
    b.append(nB-i)
for i in C:
    c.append(nC-i)
ls = []
d, e, f = 0, 0, 0
while d*e*f < K:
    if d+1 == len(a):
        a.append(float('inf'))
        d += 1
    if e+1 == len(b):
        b.append(float('inf'))
        e += 1
    if f+1 == len(c):
        c.append(float('inf'))
        f += 1
    if min(a[d], b[e], c[f]) == a[d]:
        d += 1
    elif min(a[d], b[e], c[f]) == b[e]:
        e += 1
    elif min(a[d], b[e], c[f]) == c[f]:
        f += 1
for i in a[:min(len(a)+1,d+10)]:
    for j in b[:min(len(b)+1,e+10)]:
        for k in c[:min(len(c)+1,e+10)]:
            ls.append(i+j+k)
ls.sort()
for i in range(K):
    print(nA+nB+nC-ls[i])

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.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
SUM=[]
for a in A:
  for b in B:
    for c in C:
      SUM.append(a+b+c)
      if c < A[min(60,X-1)] or c < B[min(60,Y-1)]:
        break
    if b < A[min(60,X-1)] or b < C[min(60,Z-1)]:
      break
  if a < B[min(60,Y-1)] or a < C[min(60,Z-1)]:
      break
SUM.sort(reverse=True)
for i in range(K):
  print(SUM[i])

Submission status: Runtime Error

Solution code:
import sys
def main():
	sys.setrecursionlimit(10**9)
	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:
X,Y,Z,K=map(int,input().split())
la=list(map(int,input().split()))
lb=list(map(int,input().split()))
lc=list(map(int,input().split()))
la=sorted(la,key=lambda x:x,reverse=True)
lb=sorted(lb,key=lambda x:x,reverse=True)
lc=sorted(lc,key=lambda x:x,reverse=True)
la+=[-10000000000000000000000]
lb+=[-10000000000000000000000]
lc+=[-10000000000000000000000]
ai=bi=ci=0
sm=la[ai]+lb[bi]+lc[ci]
f=0
print(sm)
cnt=1
while True:
  if cnt>K-1:
    exit()
  a=la[ai]
  b=lb[bi]
  c=lc[ci]
  if ai<len(la)-1 and a-la[ai+1]<b-lb[bi+1] and a-la[ai+1]<c-lc[ci+1]:
    ai+=1
    a=la[ai]
    bbi=cci=0
    sm=sm-(la[ai-1]-a)
    print(sm)
    cnt+=1
    while True:
      if cnt>K-1:
        exit()
      if bbi>=bi and cci>=ci:
        break
      if bbi<=bi and lb[bbi]-lb[bbi+1]<lc[cci]-lc[cci+1]:
        bbi+=1
        print(sm-(lb[bi]-lb[bbi]))
      else:
        cci+=1
        print(sm-(lc[ci]-lc[cci]))
      cnt+=1
  elif bi<len(lb)-1 and b-lb[bi+1]<c-lc[ci+1] and b-lb[bi+1]<a-la[ai+1]:
    bi+=1
    b=lb[bi]
    cci=aai=0
    sm=sm-(lb[bi-1]-b)
    print(sm)
    cnt+=1
    while True:
      if cnt>K-1:
        exit()
      if cci>=ci and aai>=ai:
        break
      if cci<=ci and lc[cci]-lc[cci+1]<la[aai]-la[aai+1]:
        print(sm-(lc[ci]-lc[cci]))
        cci+=1
        
      else:
        print(sm-(la[ai]-la[aai]))
        aai+=1
        
      cnt+=1
  else:
    ci+=1
    c=lc[ci]
    aai=bbi=0
    sm=sm-(lc[ci-1]-c)
    print(sm)
    cnt+=1
    while True:
      if cnt>K-1:
        exit()
      if aai>=ai and bbi>=bi:
        break
      if aai<=ai and la[aai]-la[aai+1]<lb[bbi]-lb[bbi+1]:
        print(sm-(la[ai]-la[aai]))
        aai+=1
        
      else:
        print(sm-(lb[bi]-lb[bbi]))
        bbi+=1
        
      cnt+=1

Submission status: Runtime Error

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

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

a_nums.sort(reverse=True)
b_nums.sort(reverse=True)
c_nums.sort(reverse=True)

p, q, r = 0, 0, 0
now = a_nums[p] + b_nums[q] + c_nums[r]

for (p+1) * (q+1) * (r+1) < k:
    print(a_nums[p] + b_nums[q] + c_nums[r])
    if a_nums[p+1] >= max(b_nums[q+1], c_nums[r+1]):
        p += 1
    elif b_nums[q + 1] >= c_nums[r + 1]:
        q += 1
    else:
        r += 1

a_nums = a_nums[:p]
b_nums = b_nums[:q]
c_nums = c_nums[:r]

sums.sort(reverse=True)

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

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

Submission status: Runtime Error

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

A = sorted(list(map(int,input().split())))
A.reverse()
B = sorted(list(map(int,input().split())))
B.reverse()
C = sorted(list(map(int,input().split())))
C.reverse()

ia,ib,ic = 0,0,0
_min_ = 10000000000001
for ida,a in enumerate(A) :
    for idb,b in enumerate(B[:int(K/ida+1)]):
        idc = int(K/(ida+1)/(idb+1)) -1
        if  idc < len(C) and a+b+C[idc] < _min_:
            _min_ = a+b+C[idc]
            ia,ib,ic = ida,idb,idc

import itertools
#print(list(itertools.product(A[:s_a], B[:s_b],C[:s_c])))
for idx,value in enumerate(sorted([sum(i) for i in  list(itertools.product(A[:ia], B[:ib],C[:ic]))])[::-1]):
    if idx > K-1:
        break
    print(value)

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(-6,6):
        for dy in range(-6,6):
            for dz in range(-6,6):
                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:
def merge(x,y):
    ans = []
    l = len(x) + len(y)
    for i in range(l):
        if x[0] < y[0]:
            ans.append(y[0])
            if(len(y)<=1):
                ans.extend(x)
                break
            y = y[1:]
        else:
            ans.append(x[0])
            if(len(x)<=1):
                ans.extend(y)
                break
            x = x[1:]
    return ans

def sort(L):
    l = len(L)
    if l <= 1:
        return L
    else:
        b = l // 2
        left = sort(L[0:b])
        right = sort(L[b:])
        return merge(left,right)

k = 1
def sortK(L):
    l = len(L)
    if l <= 1:
        return L
    else:
        b = l // 2
        left = sort(L[0:b])
        right = sort(L[b:])
        return merge(left,right)[0:k]

def find(l,t):
    a = 0
    b = len(l)
    while (b-a) > 0:
        c = (a+b) // 2
        tmp = l[c] 
        if c > t:
            a = c
        else:
            b = c
    return a

X,Y,Z,k=(int(x) for x in input().split())
cake = []
for i in range(3):
    tmp=[]
    for x in input().split():
        tmp.append(int(x))
    cake.append(sort(tmp))
mix = sortK(cake[0].append(cake[1]).append(cake[2]))
y = mix[k]
a = find(cake[0],y)
b = find(cake[1],y)
c = find(cake[2],y)

ans = []
for i in range(a+1):
    for j in range(b+1):
        for k in range(c+1):
            ans.append(cake[0][i] + cake[1][j] + cake[2][k])

hoge = sort(ans)

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

Submission status: Runtime Error

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

A = sorted(list(map(int,input().split())))
A.reverse()
B = sorted(list(map(int,input().split())))
B.reverse()
C = sorted(list(map(int,input().split())))
C.reverse()

ia,ib,ic = 0,0,0
_min_ = 10000000000001
for ida,a in enumerate(A) :
    for idb,b in enumerate(B[:int(K/ida)]):
        idc = int(K/(ida+1)/(idb+1)) -1
        if  idc < len(C) and a+b+C[idc] < _min_:
            _min_ = a+b+C[idc]
            ia,ib,ic = ida,idb,idc

import itertools
#print(list(itertools.product(A[:s_a], B[:s_b],C[:s_c])))
for idx,value in enumerate(sorted([sum(i) for i in  list(itertools.product(A[:ia], B[:ib],C[:ic]))])[::-1]):
    if idx > K-1:
        break
    print(value)

Submission status: Runtime Error

Solution code:
from heapq import heappush, heappop


def solve(X, Y, Z, K, As, Bs, Cs):
    As.sort()
    Bs.sort()
    Cs.sort()
    ADs = [a2 - a1 for a1, a2 in zip(As, As[1:])][::-1]
    BDs = [a2 - a1 for a1, a2 in zip(Bs, Bs[1:])][::-1]
    CDs = [a2 - a1 for a1, a2 in zip(Cs, Cs[1:])][::-1]

    m = As[-1] + Bs[-1] + Cs[-1]
    h = []
    ans = [m]
    heappush(h, (ADs[0], (0, -1, -1)))
    heappush(h, (BDs[0], (-1, 0, -1)))
    heappush(h, (CDs[0], (-1, -1, 0)))
    taken = set()
    for i in range(K - 1):
        p, stacks = heappop(h)
        taken.add(stacks)
        ans.append(m - p)
        if stacks[0] + 1 < len(ADs):
            d = (p + ADs[stacks[0] + 1]), (stacks[0] + 1, stacks[1], stacks[2])
            if d not in h and d not in taken:
                heappush(h, d)
        if stacks[1] + 1 < len(BDs):
            d = (p + BDs[stacks[1] + 1]), (stacks[0], stacks[1] + 1, stacks[2])
            if d not in h and d not in taken:
                heappush(h, d)
        if stacks[2] + 1 < len(CDs):
            d = (p + CDs[stacks[2] + 1]), (stacks[0], stacks[1], stacks[2] + 1)
            if d not in h and d not in taken:
                heappush(h, d)

    return "\n".join(str(a) for a in ans)


if __name__ == "__main__":
    X, Y, Z, K = tuple(map(int, input().split(" ")))
    As = list(map(int, input().split(" ")))
    Bs = list(map(int, input().split(" ")))
    Cs = list(map(int, input().split(" ")))
    print(solve(X, Y, Z, K, As, Bs, Cs))

Submission status: Runtime Error

Solution code:
import sys
from itertools import product

def main():
    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()))

    A = sorted(A, key=lambda x: -x) + [-10**9]
    B = sorted(B, key=lambda x: -x) + [-10**9]
    C = sorted(C, key=lambda x: -x) + [-10**9]

    diffA = [A[i] - A[i+1] for i in range(X)]
    diffB = [B[i] - B[i+1] for i in range(Y)]
    diffC = [C[i] - C[i+1] for i in range(Z)]

    A_ = [A[0]]
    B_ = [B[0]]
    C_ = [C[0]]
    i, j, k = 0, 0, 0
    while (len(A_) * len(B_) * len(C_)) < 10**5:
        a, b, c = diffA[i], diffB[j], diffC[k]

        if min(a, b, c) >= 10**4:
            break

        if a == min(a, b, c):
            A_.append(A[i+1])
            i += 1
        if b == min(a, b, c):
            B_.append(B[j+1])
            j += 1
        if c == min(a, b, c):
            C_.append(C[k+1])
            k += 1

    deli= []
    for a in A_:
        for b in B_:
            for c in C_:
                deli.append(a + b + c)

    deli = sorted(deli, key=lambda x: -x)
    for i in range(K):
        print(deli[i])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

ls = []

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


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

Submission status: Runtime Error

Solution code:
x, y, z, pos = map(int, input().split())
a = map(int, input().split())
b = map(int, input().split())
c = map(int, input().split())
result = []
for i in range(x):
    for j in range(y):
        for k in range(z):
            result.append(a[i] + b[j] + c[k])
result.sort(reverse=True)
for x in range(pos):
    print(result[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 = []

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 j in C:
    z = 0
    for i in ab_total_list:
        z += 1
        total = i + j
        total_list.append(i+j)
        if z > K:
            break

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

Submission status: Runtime Error

Solution code:
x,y,z,k=map(int, input().split())
a=[0]*x
for i in range(x):
    a[i]=int(input())
b=[0]*y
for i in range(y):
    b[i]=int(input())
c=[0]*z
for i in range(z):
    c[i]=int(input())
print(a)

Submission status: Runtime Error

Solution code:
x, y, z, k = list(map(int, input().split()))
a = sorted(list(map(int, input().split())), reverse = True)
b = sorted(list(map(int, input().split())), reverse = True)
c = sorted(list(map(int, input().split())), reverse = True)
d = min(x*y*z, 3000)
dp = [0] * max(x*y*z, 3000)
count = 0
for i in range(x):
    for j in range(y):
        for t in range(z):
            dp[count] = a[i] + b[j] +  c[t]
            count += 1

dp = sorted(dp, reverse = True)

for i in range(d):
    print(dp[i])

Submission status: Runtime Error

Solution code:
from bisect import bisect_left

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 i in range(X):
    for j in range(Y):
        AB.append(A[i] + B[j])
AB.sort()
C.sort()

def n_above(val):
    cnt = 0
    for c in C:
        cnt += len(AB) - bisect_left(AB, val - c)
    return cnt

l = AB[0] + C[0]
r = AB[-1] + C[-1]
while l - r > 1:
    m = (l + r) // 2
    if n_above(m) >= K:
        l = m
    else:
        r = m
if n_above(m) > K:
    border = m - 1
else:
    border = m

ans = []
for c in C:
    ans += [ab + c for ab in AB[bisect_left(AB, border - c):]]
ans.sort(reverse = True)
for i in range(K):
    print(ans[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,j,k) in zip(A,B,C)]
L.sort()
L.reverse()
for i in L:
  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()))
aa=sorted(a,reverse=True)
bb=sorted(b,reverse=True)
cc=sorted(c,reverse=True)
n=0
l=[]
while n<=k:
    for i in range(15):
        for j in range(15):
            for x in range(15):
                ll=l.append(aa[i]+bb[j]+cc[x])
ll.sort(reverse=True)
for i in range(k):
    print(ll[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()


memo = {}

def next_step(i,j,k):
    cand = []
    for dx, dy, dz in ((1,0,0),
                       (0,1,0),
                       (0,0,1),
                       (1,-1,0),
                       (-1,1,0),
                       (0,1,-1),
                       (0,-1,1),
                       (1,0,-1),
                       (-1,0,1),
                       (2,-1,0),
                       (-1,2,0),
                       (0,-1,2),
                       (0,2,-1),
                       (2,0,-1),
                       (-1,0,2),
                       (1,-2,0),
                       (-2,1,0),
                       (0,-2,1),
                       (0,1,-2),
                       (1,0,-2),
                       (-2,0,1),
                       (2,-2,0),
                       (-2,2,0),
                       (0,-2,2),
                       (0,2,-2),
                       (2,0,-2),
                       (-2,0,2)):
        if 0<=i+dx and i+dx<x\
        and 0<=j+dy and j+dy<y\
        and 0<=k+dz and k+dz<z\
        and not (i+dx,j+dy,k+dz) in memo:
            cand.append([-(a[i+dx]+b[j+dy]+c[k+dz]), [i+dx,j+dy,k+dz]])
    cand.sort()
    return cand[0][1]

i = j = k = 0
print(a[i]+b[j]+c[k])
#print(i,j,k)
memo[(i,j,k)] = 0

for _ in range(K-1):
    i,j,k = next_step(i,j,k)
    memo[(i,j,k)] = 0
#    print(i,j,k)
    print(a[i]+b[j]+c[k])

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)

li=[]
dp=[[0 for i in range(3)] for j in range(5)]


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

w=0
for l in range(k):
    maxtemp=0
    for i in range(l+1):

        
        for j in range(z):
            if dp[i][j]==0:
                con=li[i]+c[j]
                
                if con>maxtemp:
                    x1=i
                    y1=j
                    maxtemp=con
                break
    

    dp[x1][y1]=l+1
    
    print(maxtemp)

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)

del LA
del LB
del LC

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)

del D

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

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

Submission status: Runtime Error

Solution code:
x,y,z,k = map(int,input().split())
lisa = list(map(int,input().split()))
lisb = list(map(int,input().split()))
lisc = list(map(int,input().split()))
lisc.sort(reverse=True)
an,ans = [],[]
for num in lisb:for nu in lisa:an.append(num+nu)
an.sort(reverse=True)
an = an[:min(3001,x*y*z+1)]
for num in lisc:for nu in an:ans.append(num+nu)
ans.sort(reverse=True)
for i in range(k):print(ans[i])

Submission status: Runtime Error

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

list = []
i = 0
for a in a_s:
    for b in bs:
        for c in cs:
            list.append(a + b + c)
            i += 1
            if i > k and list[i] < list[k]:
                break
list.sort(reverse=True)
for i in range(k):
    print(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)):
for a in range(X):
    for b in range(Y):
        for c in 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:
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)
d = min(x*y*z, 3000)
dp = [0] * (x*y*z)
count = 0
for i in range(x):
    for j in range(y):
        for t in range(z):
            dp[count] = a[i] + b[j] +  c[t]
            count += 1

dp = sorted(dp, reverse = True)

for i in range(d):
    print(dp[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()))
aa=sorted(a,reverse=True)
bb=sorted(b,reverse=True)
cc=sorted(c,reverse=True)
n=0
l=[]
while n<=k:
    for i in range(100):
        for j in range(100):
            for x in range(100):
                ll=l.append(aa[i]+bb[j]+cc[x])
ll.sort(reverse=True)
for i in range(k):
    print(ll[i])

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;
typedef long long ll;

int main()
{
  ll x,y,z,K;
  cin >> x >> y >> z >> K;

  vector<ll>a(x);
  vector<ll>b(y);
  vector<ll>c(z);  
  
  for( int i=0;i<x;++i)
    cin >> a[i];
  for( int i=0;i<y;++i)
    cin >> b[i];
  for( int i=0;i<z;++i)
    cin >> c[i];

  sort(a.begin(), a.end(), greater<ll>());
  sort(b.begin(), b.end(), greater<ll>());
  sort(c.begin(), c.end(), greater<ll>());

  int i,j,k;
  cout << a[i]+b[j]+c[k] << endl;
  for( int v=1;v<K;++v )
    {
      if( i<x-1 && j<y-1 && k<z-1 )
	{
	  ll s = a[i]+b[j]+c[k+1];
	  ll t = a[i]+b[j+1]+c[k];
	  ll u = a[i+1]+b[j]+c[k];       
	  if( s>t && s>u )
	    k++;
	  else if ( t>s && t>u )
	    j++;
	  else
	    i++;
	}
      else if( i<x-1 && j<y-1 )
	{
	  ll t = a[i+1]+b[j]+c[k];
	  ll u = a[i]+b[j+1]+c[k];       
	  if( t>u )
	    i++;
	  else
	    j++;
	}
      else if( i<x-1 && k<z-1 )
	{
	  ll s = a[i+1]+b[j]+c[k];
	  ll t = a[i]+b[j]+c[k+1];
	  if( s>t )
	    i++;
	  else
	    k++;
	}
      else if( j<y-1 && k<z-1 )
	{
	  ll s = a[i]+b[j+1]+c[k];
	  ll t = a[i]+b[j]+c[k+1];
	  if( s>t )
	    j++;
	  else
	    k++;
	}
      else if( i<x-1 )
	i++;
      else if( j<y-1 )
	j++;
      else
	k++;
      
      cout << a[i]+b[j]+c[k] << endl;	 
    }
  return 0;
}