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())
candy = [[],[],[]]
for i in range(3):
    temp = list(map(int, input().split()))
    candy[i] = temp

for i in range(3):
    candy[i] = sorted(candy[i], reverse=True)
i, j, k = 0, 0, 0
heap = [(-(candy[0][i] + candy[1][j] + candy[2][k]), i, j, k)]
cnt = 0
visit = {(i, j, k)}
while cnt < K:
    s, ii, jj, kk = heapq.heappop(heap)
    print(-s)
    cnt += 1
    if ii < len(candy[0]) - 1 and (ii+1, jj, kk) not in visit:
        heapq.heappush(heap, (-(candy[0][ii + 1] + candy[1][jj] + candy[2][kk]), ii + 1, jj, kk))
        visit.add((ii+1, jj, kk))
    if jj < len(candy[1]) - 1 and (ii, jj+1, kk) not in visit:
        heapq.heappush(heap, (-(candy[0][ii] + candy[1][jj + 1] + candy[2][kk]), ii, jj + 1, kk))
        visit.add((ii, jj+1, kk))
    if kk < len(candy[2]) - 1 and (ii, jj, kk+1) not in visit:
        heapq.heappush(heap, (-(candy[0][ii] + candy[1][jj] + candy[2][kk + 1]), ii, jj, kk + 1))
        visit.add((ii, jj, kk+1))

Submission status: Runtime Error

Solution code:
x, y, z, k = map(int, input().split())
alis = sorted(list(map(int, input().split())))[::-1]
blis = sorted(list(map(int, input().split())))[::-1]
clis = sorted(list(map(int, input().split())))[::-1]
alis.append(-max(alis))
blis.append(-max(blis))
clis.append(-max(clis))

def search_next(a, b, c):
    cans = [(alis[a] - alis[a + 1]), (blis[b] - blis[b + 1]), (clis[c] - clis[c + 1])]
    nexts = [a, b, c]
    nexts[cans.index(min(cans))] += 1
    return nexts
    
a, b, c = 0, 0, 0
for i in range(min(k), x * y * z):
    print(alis[a] + blis[b] + clis[c])
    a, b, c = search_next(a, b, c)

Submission status: Runtime Error

Solution code:
from sys import stdin
import math
import numpy as np
# N = int(stdin.readline().rstrip().split()[0])
# A = [int(x) for x in stdin.readline().rstrip().split()]
# N, X = [int(x) for x in stdin.readline().rstrip().split()]
# S = [int(x) for x in stdin.readline().rstrip().split()]
X, Y, Z, K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]
C = [int(x) for x in stdin.readline().rstrip().split()]

A.sort()
B.sort()
C.sort()
A.reverse()
B.reverse()
C.reverse()
A.append(-1)
B.append(-1)
C.append(-1)
A.append(-10)
B.append(-10)
C.append(-10)

Max = A[0]+B[0]+C[0]
a, b, c = 0, 0, 0
memo = {}
memo[(a,b,c)] = Max
for i in range(K):
  key, Max = max(memo.items(), key=lambda x: x[1])
  print(Max)
  del memo[key]
  a,b,c=key
  memo[(a+1,b,c)] = A[a+1]+B[b]+C[c]
  memo[(a,b+1,c)] = A[a]+B[b+1]+C[c]
  memo[(a,b,c+1)] = A[a]+B[b]+C[c+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()))

l_XYZ = [X, Y, Z]
l_XYZ.sort()
l_ABC = []
l_ABC += A
l_ABC += B
l_ABC += C
l_ABC.sort(reverse=True)
v_limit = l_ABC[l_XYZ[0] + l_XYZ[1] + 1]

def answer():
    l = []
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    for va in A:
        if va < v_limit:
            break
        for vb in B:
            if vb < v_limit:
                break
            for vc in C:
                if vc < v_limit:
                    break
                l.append(va + vb + vc)

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

def main():
    answer()

if __name__ == '__main__':
    main()

Submission status: 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)

sum1 = []
sum2 = []
sum3 = []
for i in range(X):
    for j in range(Y):
        for k in range(10):
            sum1.append(A[i] + B[j] + C[k])
for i in range(X):
    for j in range(10):
        for k in range(Y):
            sum2.append(A[i] + B[j] + C[k])
for i in range(10):
    for j in range(Y):
        for k in range(Z):
            sum3.append(A[i] + B[j] + C[k])
sum1.sort(reverse=True)
sum2.sort(reverse=True)
sum3.sort(reverse=True)
if sum1<=sum2 and sum3<=sum2:
    for i in range(K):
        print(sum2[i])
elif sum2<=sum1 and sum3 <= sum1:
    for i in range(K):
        print(sum1[i])
else:
    for i in range(K):
        print(sum3[i])

Submission status: Runtime Error

Solution code:
import math
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.sort()
B.sort()
C.sort()
m = A[-1]+B[-1]+C[-1]
ans = []
Ad = [0]+[A[-1]-A[i] for i in range(X-1)]
Bd = [0]+[B[-1]-B[i] for i in range(X-1)]

for i in range(min(X,math.ceil(math.sqrt(3000)))):
    for j in range(min(Y,math.ceil(math.sqrt(3000)))):
        for l in range(min(Z,math.ceil(math.sqrt(3000)))):
            if Ad[i]!=0 or Bd[j]!=0 or Cd[l]!=0:
                ans.append(m-Ad[i]-Bd[j]-Cd[l])
ans.sort()

for i in range(-1,-K,-1):
    print(ans[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()
A.reverse()
B.sort()
B.reverse()
C.sort()
C.reverse()

if X * Y * Z > 3000:
    X = 3000
    Y = 3000
    Z = 3000

ans_list = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            sum = A[i]+B[j]+C[k]
            ans_list.append(sum)        
ans_list.sort()
ans_list.reverse()
for i in range(K):
    print(ans_list[i])

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 a_count < len(a) and m == a_l[a_count]:
            a_count += 1
        elif b_count < len(b) and m == b_l[b_count]:
            b_count += 1
        elif c_count < len(c) and m == c_l[c_count]:
            c_count += 1
a = a[:a_count+1]
b = b[:b_count+1]
c = c[:c_count+1]

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:
a = []
b = []
c = []
l = 300
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 = a
d.extend(b)
d.extend(c)
d.sort()

a2 = []
b2 = []
c2 = []
po = min(len(a)+len(b)+len(c),3000)
for i in range(len(a)):
  if d[po] >= a[i]:
    a2.append(a[i])
for i in range(len(b)):
  if d[po] >= b[i]:
    b2.append(b[i])
for i in range(len(c)):
  if d[po] >= c[i]:
    c2.append(c[i])

oisi = []
for i in range(len(a2)):
  for j in range(len(b2)):
    for m in range(len(c2)):
      oisi.append(a2[i]+b2[j]+c2[m])

oisi.sort()
oisi.reverse()
for i in range(k):
  print(oisi[i])

Submission status: Runtime Error

Solution code:
import math
X,Y,Z,K =3,3,3,5

A = [1,10,100]
B = [2,20,200]
C = [1,10,100]

A.sort()
B.sort()
C.sort()
m = A[-1]+B[-1]+C[-1]
ans = []
Ad = [0]+[A[-1]-A[i] for i in range(X-1)]
Bd = [0]+[B[-1]-B[i] for i in range(X-1)]

for i in range(min(X,math.ceil(math.sqrt(3000)))):
    for j in range(min(Y,math.ceil(math.sqrt(3000)))):
        for l in range(min(Z,math.ceil(math.sqrt(3000)))):
            if Ad[i]!=0 or Bd[j]!=0 or Cd[l]!=0:
                ans.append(m-Ad[i]-Bd[j]-Cd[l])
ans.sort()

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

Submission status: Runtime Error

Solution code:
from itertools import chain
def func1(A,B,C):
    s = A[0]+B[0]+C[0]
    X = func3(A)
    Y = func3(B)
    Z = func3(C)
    return sorted(chain(X,Y,Z,func2(X,Y,b), func2(Y,Z,b), func2(Z,X,b)))
        
import numpy as np
def diff(W):
    W = np.array(W)
    return W[:-1]-W[1:]
def func2(X,Y,b):
    for x in X:
        for y in Y:
            a = x+y
            if a>b:
                break
            yield x+y
def func3(W,s):
    w = 0
    for i in W:
        w += i
        if w >s:
            break
        yield w

*_,k = 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)

def func(base, Ad,Bd,Cd):
    yield base
    while True:
        for d in func1(Ad, Bd, Cd):
            yield base - d
        a,*Ad = Ad
        b,*Bd = Bd
        c,*Cd = Cd
        base -= (a+b+c)
for i,t in enumerate(func(A[0]+B[0]+C[0], diff(A), diff(B), diff(C)))

Submission status: Runtime Error

Solution code:


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

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


total = [0]*k
count = 0


def sum_all(a, b, change):
	for i in range(len(change)):
		try:
			total[count] = change[i]
			count += 1
		except IndexError:
			sort_total = sorted(total)
			for i in total:
				print(i)

			quit()

i, j, k = x-1, y-1, z-1
start = 0
while i > 0 or j > 0 or k > 0:
	possible = sorted([A[i], B[j], C[k]])
	now = possible[start]
	
	if A[i] == now:
		sum_all(B[j], C[k], A[:i+1])
	elif B[i] == now:
		sum_all(A[i], C[k], B[:j+1])
	else:
		sum_all(A[i], B[j], C[:k+1])

	start += 1
	if start == 2:
		i -= 1
		j -= 1
		k -= 1

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 = 1, 1, 1
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)]:
    for j in b[:min(len(b)+1,e+2)]:
        for k in c[:min(len(c)+1,e+2)]:
            ls.append(i+j+k)
ls.sort()
for i in range(K):
    print(nA+nB+nC-ls[i])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
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)
D = []
E = []
for b in B:
    for c in C:
        D.append(b+c)
D.sort(reverse=True)[:k]
n = 0
for a in A:
    for d in D:
       E.append(a+d)
E.sort(reverse=True)
for e in E[:k]:
    print(e)

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(min(a+1,X)):
    for j in range(min(b+1,Y)):
        for k in range(min(c+1,Z)):
            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 = 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)

x1=0
y1=0
for l in range(len(li)):
    maxtemp=-1
    for i in range(l):

        
        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:
A,B,C,k = map(int, input().split())
ao = reversed([int(i) for i in input().split()])
bo = reversed([int(i) for i in input().split()])
co = reversed([int(i) for i in input().split()])

a = 0
b = 0
c = 0

for i in k:
  print(ao[a] + bo[b] + bo[c])
  ma = -1
  cand = []
  if a < len(ao) - 1:
    cand.append([0, ao[a+1] + bo[b] + bo[c]])
  if b < len(bo) - 1:
    cand.append([1, ao[a] + bo[b+1] + bo[c]])
  if c < len(co) - 1:
    cand.append([2, ao[a] + bo[b] + bo[c+1]])
  ne = sorted(cand, key=lambda a:-a[1])[0]
  if ne[0] == 0:
    a +=1
  if ne[0] == 1:
    b +=1
  if ne[0] == 2:
    c +=1
  print(ne[1])

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())
candy = [[],[],[]]
for i in range(3):
    temp = list(map(int, input().split()))
    candy[i] = temp

for i in range(3):
    candy[i] = sorted(candy[i], reverse=True)
i, j, k = 0, 0, 0
heap = [(-(candy[0][i] + candy[1][j] + candy[2][k]), i, j, k)]
cnt = 0
visit = {(i, j, k)}
while cnt < K:
    print(heap)
    s, ii, jj, kk = heapq.heappop(heap)
    print(-s)
    cnt += 1
    if ii < len(candy[0]) - 1 and (ii+1, jj, kk) not in visit:
        heapq.heappush(heap, (-(candy[0][ii + 1] + candy[1][jj] + candy[2][kk]), ii + 1, jj, kk))
        visit.add((ii+1, jj, kk))
    if jj < len(candy[1]) - 1 and (ii, jj+1, kk) not in visit:
        heapq.heappush(heap, (-(candy[0][ii] + candy[1][jj + 1] + candy[2][kk]), ii, jj + 1, kk))
        visit.add((ii, jj+1, kk))
    if kk < len(candy[2]) - 1 and (ii, jj, kk+1) not in visit:
        heapq.heappush(heap, (-(candy[0][ii] + candy[1][jj] + candy[2][kk + 1]), ii, jj, kk + 1))
        visit.add((ii, jj, kk+1))

Submission status: Runtime Error

Solution code:
#!venv/bin/python
 
import math
import numpy as np
import random
import copy
import statistics 
import bisect
import heapq
from collections import defaultdict 

X, Y, Z, K = [ int(x) for x in input().split() ]
A = sorted([ int(x) for x in input().split() ],reverse=False)
B = sorted([ int(x) for x in input().split() ],reverse=False)
C = sorted([ int(x) for x in input().split() ],reverse=False)

maxa = A[0]
maxb = B[0]
maxc = C[0]
cana = [A.pop()]
canb = [B.pop()]
canc = [C.pop()]
print("K: " + str(K))
i = 0
while True:
    print(i)
    i+=1
    diff_a = 1000000000000
    diff_b = 1000000000000
    diff_c = 1000000000000
    if A:
        diff_a = maxa - A[0]
    if B:
        diff_b = maxb - B[0]
    if C:
        diff_c = maxc - C[0]
    if diff_a <= diff_b and diff_a <= diff_c:
        cana.append(A.pop())
    if diff_b <= diff_a and diff_b <= diff_c:
        canb.append(B.pop())
    if diff_c <= diff_a and diff_c <= diff_b:
        canc.append(C.pop())
    if len(cana) * len(canb)  *len(canc) >= 10 * K:
        break
    if not A and not B and not C:
        break
        
vals = []
for a in cana:
    for b in canb:
        for c in canc:
            vals.append(a + b + c)

print ("\n".join([str(x) for x in sorted(vals,reverse=True)[0:K]]))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
x, y, z, k = map(int, input().split())
a_s = sorted(list(map(int, input().split())), reverse=True)
b_s = sorted(list(map(int, input().split())), reverse=True)
c_s = sorted(list(map(int, input().split())), reverse=True)

a_i = 0
b_i = 0
c_i = 0

deliciouses = []
for i in range(k):
    deliciouses.append(a_s[a_i]+b_s[b_i]+c_s[c_i])
    if a_s[a_i]-a_s[a_i+1]>=b_s[b_i]-b_s[b_i+1] and a_s[a_i]-a_s[a_i+1]>=c_s[c_i]-c_s[c_i+1] and a_i<x-1:
        a_i += 1
        continue
    if b_s[b_i]-b_s[b_i+1]-b_s[b_i+1]>=c_s[c_i]-c_s[c_i+1] and b_i<y-1:
        b_i += 1
        continue
    if c_i<z-1:
        c_i += 1
        continue
    break

for d in deliciouses:
    print(d)

Submission status: Runtime Error

Solution code:
from sys import stdin
import math
import numpy as np
# N = int(stdin.readline().rstrip().split()[0])
# A = [int(x) for x in stdin.readline().rstrip().split()]
# N, X = [int(x) for x in stdin.readline().rstrip().split()]
# S = [int(x) for x in stdin.readline().rstrip().split()]
X, Y, Z, K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]
C = [int(x) for x in stdin.readline().rstrip().split()]

A.sort()
B.sort()
C.sort()
A.reverse()
B.reverse()
C.reverse()
A.append(-1)
B.append(-1)
C.append(-1)

Max = A[0]+B[0]+C[0]
a, b, c = 0, 0, 0
memo = {}
memo[(a,b,c)] = Max
for i in range(K):
  key, Max = max(memo.items(), key=lambda x: x[1])
  print(Max)
  del memo[key]
  a,b,c=key
  memo[(a+1,b,c)] = A[a+1]+B[b]+C[c]
  memo[(a,b+1,c)] = A[a]+B[b+1]+C[c]
  memo[(a,b,c+1)] = A[a]+B[b]+C[c+1]

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:
x, y, z, k = map(int, input().split())
alis = sorted(list(map(int, input().split())))[::-1]
blis = sorted(list(map(int, input().split())))[::-1]
clis = sorted(list(map(int, input().split())))[::-1]
alis.append(-max(alis))
blis.append(-max(blis))
clis.append(-max(clis))

def search_next(a, b, c):
    cans = [(alis[a] - alis[a + 1]), (blis[b] - blis[b + 1]), (clis[c] - clis[c + 1])]
    nexts = [a, b, c]
    nexts[cans.index(min(cans))] += 1
    return nexts
    
a, b, c = 0, 0, 0
for i in range(k):
    print(alis[a] + blis[b] + clis[c])
    a, b, c = search_next(a, b, c)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr  6 21:57:55 2019

@author: maezawa
"""

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.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

im = 0
jm = 0
km = 0

while im+1<x and jm+1<y and km+1<z:
    if a[im+1] >= b[jm+1] and a[im+1] >= c[km+1] and im + 1 < x:
        im += 1
    elif b[jm+1] >= a[im+1] and b[jm+1] >= c[km+1] and jm + 1 < y:
        jm += 1
    elif c[km+1] >= a[im+1] and c[km+1] >= b[jm+1] and km + 1 < z:
        km += 1
    ncomb = (im)*(jm)*(km)
    if ncomb > k:
        break

ans = []
for i in range(im):
    for j in range(jm):
        for k in range(km):
            ans.append(a[i]+b[j]+c[k])

ans.sort()
for val in ans:
    print(ans)

Submission status: Runtime Error

Solution code:
x, y, z, k = map(int, input().split())
a = []
a.append(list(map(int, input().split())))
a.append(list(map(int, input().split())))
a.append(list(map(int, input().split())))

a[0].sort()
a[1].sort()
a[2].sort()

# da = [a[i] - a[i+1] for i in range(len(a)-1)]
# db = [b[i] - b[i+1] for i in range(len(b)-1)]
# dc = [c[i] - c[i+1] for i in range(len(c)-1)]

ia = [len(a[0])-1, len(a[1])-1, len(a[2])-1]
deli = a[0][ia[0]] + a[1][ia[1]] + a[2][ia[2]]
for i in range(k):
    how_modi = None
    min_delta = 0
    if i != 0:
        for j in range(3):
            if ia[j] == 0:
                continue
            delta = a[j][ia[j]] - a[j][ia[j-1]]
            min_delta = delta
            for l in range(3):
                if l == j:
                    continue
                idx = ia[l]
                while True:
                    if a[l][idx] > delta or idx == len(a[l])-1:
                        break
                    idx += 1
                if min_delta > delta - a[l][idx] + a[l][ia[l]]:
                    if idx == ia[l]:
                        min_delta = delta - a[l][idx] + a[l][ia[l]]
                        how_modi = [j, l, idx]
                    else:
                        min_delta = delta - a[l][idx] + a[l][ia[l]]
                        how_modi = [j]
                else:
                    how_modi = [j]
    deli -= min_delta
    if len(how_modi) == 1:
        ia[how_modi[0]] -= 1
    else:
        ia[how_modi[0]] -= 1
        ia[how_modi[1]] = how_modi[2]
    print(deli)

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 = []
for a in a_nums:
    for b in b_nums:
        for c in c_nums:
            sums.append(a + b + c)

sums.sort(reverse=True)
for n in sums[:k]:
    print(n)

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

Submission status: Runtime Error

Solution code:
import numpy as np

X,Y,Z,K = map(int, input().split())
A = np.sort(np.array(list(map(int, input().split()))))[::-1]
#A = np.sort(np.array(list(map(int, input().split()))))
#print(A)
B = np.sort(np.array(list(map(int, input().split()))))[::-1]
C = np.sort(np.array(list(map(int, input().split()))))[::-1]
As = A.shape[0]
Bs = B.shape[0]
Cs = C.shape[0]
D = np.empty((As,Bs,Cs))
for i in range (As):
    for j in range (Bs):
        for k in range (Cs):
            D[i,j,k] = A[i]+B[j]+C[k]

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

Submission status: Runtime Error

Solution code:
def ii2ss(n):
    ss = []
    for i in range(n):
        ss.append(input())
    return ss

def sp2nn(sp, sep=' '):
    return [int(s) for s in sp.split(sep)]

def ss2nn(ss):
    return [int(s) for s in list(ss)]

def tk(i, j, k):
    return '%d %d %d' % (i, j, k)

def tkr(key):
    return sp2nn(key)

def main(ss):
    X, Y, Z, K = sp2nn(ss[0])
    aa = sp2nn(ss[1])
    bb = sp2nn(ss[2])
    cc = sp2nn(ss[3])
    aa.sort()
    aa.reverse()
    bb.sort()
    bb.reverse()
    cc.sort()
    cc.reverse()
    
    d = {}
    st = set()
    key = tk(0, 0, 0)
    st.put(key)
    d[key] = aa[0] + bb[0] + cc[0]
    for _ in range(K):
        vmax = -1
        vk = ''
        for k, v in d.items():
            if vmax < v:
                vmax = v
                vk = k
        print(vmax)
        d.pop(vk)
        st.put(vk)
        i, j, k = tkr(vk)
        if i + 1 < len(aa):
            key = tk(i+1, j, k)
            if key not in st:
                d[key] = aa[i+1] + bb[j] + cc[k]
        if j + 1 < len(bb):
            key = tk(i, j+1, k)
            if key not in st:
                d[key] = aa[i] + bb[j+1] + cc[k]
        if k + 1 < len(cc):
            key = tk(i, j, k+1)
            if key not in st:
                d[key] = aa[i] + bb[j] + cc[k+1]
        
main(ii2ss(4))

Submission status: Runtime Error

Solution code:
X, Y, Z, K = [int(x) for x in input().split()]

A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
C = [int(x) for x in input().split()]

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

i = 0
j = 0
l = 0

for _ in range(0, K):
    a = A[i]
    b = B[j]
    c = C[k]
    print(a + b +c)
    
    if (A[i+1] if i+1 < X else A[i] > B[j+1] if j + 1 < Y else Y[j]) and (A[i+1] if i +1 < X else A[i] > C[l+1] if l + 1 < Z else C[l]):
        i += 1
    elif ((B[j+1] if j+1 < Y else B[j]) > (A[i+1] if i+1 else A[i])) <(( X and B[j+1] if j+1 < Y else B[j]) > (C[l+1] if l + 1 < Z else C[l])):
        j += 1
    else:
        l += 1

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(k):
    print(dp[i])

Submission status: Runtime Error

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

X,Y,Z,K = (int(i) for i in input().split())
ax = [int(i) for i in input().split()]
by = [int(i) for i in input().split()]
cz = [int(i) for i in input().split()]
ans = [0] * 3000
cnt = 0
for i in ax:
  for j in by:
    for k in cz:
      ans[cnt] = i + j + k
      cnt += 1
ans.sort(reverse=True)
for i in range(K):
  print(ans[i])