Problem:


Score: 400 points

Problem Statement

The Patisserie AtCoder sells cakes with number-shaped candles.
There are X, Y and Z kinds of cakes with 1-shaped, 2-shaped and 3-shaped candles, respectively.
Each cake has an integer value called deliciousness, as follows:

The deliciousness of the cakes with 1-shaped candles are A_1, A_2, ..., A_X.

The deliciousness of the cakes with 2-shaped candles are B_1, B_2, ..., B_Y.

The deliciousness of the cakes with 3-shaped candles are C_1, C_2, ..., C_Z.

Takahashi decides to buy three cakes, one for each of the three shapes of the candles, to celebrate ABC 123.

There are X \times Y \times Z such ways to choose three cakes.

We will arrange these X \times Y \times Z ways in descending order of the sum of the deliciousness of the cakes.

Print the sums of the deliciousness of the cakes for the first, second, ..., K-th ways in this list.

Constraints

1 \leq X \leq 1 \ 000

1 \leq Y \leq 1 \ 000

1 \leq Z \leq 1 \ 000

1 \leq K \leq \min(3 \ 000, X \times Y \times Z)

1 \leq A_i \leq 10 \ 000 \ 000 \ 000

1 \leq B_i \leq 10 \ 000 \ 000 \ 000

1 \leq C_i \leq 10 \ 000 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z K
A_1 \ A_2 \ A_3 \ ... \ A_X
B_1 \ B_2 \ B_3 \ ... \ B_Y
C_1 \ C_2 \ C_3 \ ... \ C_Z

Output

Print K lines. The i-th line should contain the i-th value stated in the problem statement.

Sample Input 1

2 2 2 8
4 6
1 5
3 8

Sample Output 1

19
17
15
14
13
12
10
8

There are 2 \times 2 \times 2 = 8 ways to choose three cakes, as shown below in descending order of the sum of the deliciousness of the cakes:

(A_2, B_2, C_2): 6 + 5 + 8 = 19

(A_1, B_2, C_2): 4 + 5 + 8 = 17

(A_2, B_1, C_2): 6 + 1 + 8 = 15

(A_2, B_2, C_1): 6 + 5 + 3 = 14

(A_1, B_1, C_2): 4 + 1 + 8 = 13

(A_1, B_2, C_1): 4 + 5 + 3 = 12

(A_2, B_1, C_1): 6 + 1 + 3 = 10

(A_1, B_1, C_1): 4 + 1 + 3 = 8

Sample Input 2

3 3 3 5
1 10 100
2 20 200
1 10 100

Sample Output 2

400
310
310
301
301

There may be multiple combinations of cakes with the same sum of the deliciousness. For example, in this test case, the sum of A_1, B_3, C_3 and the sum of A_3, B_3, C_1 are both 301.
However, they are different ways of choosing cakes, so 301 occurs twice in the output.

Sample Input 3

10 10 10 20
7467038376 5724769290 292794712 2843504496 3381970101 8402252870 249131806 6310293640 6690322794 6082257488
1873977926 2576529623 1144842195 1379118507 6003234687 4925540914 3902539811 3326692703 484657758 2877436338
4975681328 8974383988 2882263257 7690203955 514305523 6679823484 4263279310 585966808 3752282379 620585736

Sample Output 3

23379871545
22444657051
22302177772
22095691512
21667941469
21366963278
21287912315
21279176669
21160477018
21085311041
21059876163
21017997739
20703329561
20702387965
20590247696
20383761436
20343962175
20254073196
20210218542
20150096547

Note that the input or output may not fit into a 32-bit integer type.



Sample input:
2 2 2 8
4 6
1 5
3 8


Sample output:
19
17
15
14
13
12
10
8

Submission status: Runtime Error

Solution code:
import numpy as np
x,y,z,k = map(int,input().split())
a = np.array(list(map(int,input().split())))
b = np.array(list(map(int,input().split())))
c = np.array(list(map(int,input().split())))
a2,b2,c2 = np.meshgrid(a,b,c)
abc = np.c_[a2.flatten(),b2.flatten(),c2.flatten()]
ans = []
for i in range(x*y*z):
    ans.append(sum(abc[i]))
ans = np.sort(ans)[::-1]
for i in range(k):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import numpy as np
x,y,z,k = map(int,input().split())
a = np.array(list(map(int,input().split())))
b = np.array(list(map(int,input().split())))
c = np.array(list(map(int,input().split())))
ans = []
for i in range(x):
    for j in range(y):
        for l in range(z):
            ans.append(a[i] + b[j] + c[l])
ans = np.sort(ans)[::-1]
for i in range(k):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import numpy as np
x,y,z,k = map(int,input().split())
a = np.array(list(map(int,input().split())))
b = np.array(list(map(int,input().split())))
c = np.array(list(map(int,input().split())))
a2,b2,c2 = np.meshgrid(a,b,c)
abc = np.c_[a2.flatten(),b2.flatten(),c2.flatten()]
ans = np.zeros(x*y*z,dtype=int)
for i in range(x*y*z):
    ans[i] = sum(abc[i])
ans = np.sort(ans)[::-1]
for i in range(k):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import numpy as np
x,y,z,k = map(int,input().split())
a = np.array(list(map(int, input().split())))
b = np.array(list(map(int, input().split())))
c = np.array(list(map(int, input().split())))
ans = []
for i in range(x):
    for j in range(y):
        for l in range(z):
            ans.append(a[i] + b[j] + c[l])
ans.sort(reverse = True)
for i in range(k):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    x, y, z, w = ns()
    a = na()
    b = na()
    c = na()

    a.sort(reverse=True)
    b.sort(reverse=True)
    c.sort(reverse=True)

    ans = []
    lim = 4000
    for i in range(x):
        if x > lim:
            break
        for j in range(y):
            if x * y > lim:
                break
            for k in range(z):
                if x * y * z > lim:
                    break
                ans.append(a[i] + b[j] + c[k])

    ans.sort(reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    x, y, z, w = ns()
    a = na()
    b = na()
    c = na()

    a.sort(reverse=True)
    b.sort(reverse=True)
    c.sort(reverse=True)

    ans = []
    lim = 3333
    for i in range(x):
        if x > lim:
            break
        for j in range(y):
            if x * y > lim:
                break
            for k in range(z):
                if x * y * z > lim:
                    break
                ans.append(a[i] + b[j] + c[k])
                
    ans.sort(reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    x, y, z, w = ns()
    a = na()
    b = na()
    c = na()

    a.sort(reverse=True)
    b.sort(reverse=True)
    c.sort(reverse=True)

    ans = []
    for i in range(x):
        if x > 3000:
            break
        for j in range(y):
            if x * y > 3000:
                break
            for k in range(z):
                if x * y * z > 3000:
                    break
                ans.append(a[i] + b[j] + c[k])
    ans.sort(reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    x, y, z, w = ns()
    size = [[0, x], [1, y], [2, z]]
    size.sort(key=lambda x: x[1])
    d = []
    for _ in range(3):
        tmp = na()
        tmp.sort(reverse=True)
        d.append(tmp)

    d2 = []
    for i, s in size:
        d2.append(d[i])

    lima = min(size[0][1], 15)
    limb = min(size[1][1], int((3375 / lima) ** (1 / 2)) + 1)
    limc = min(size[2][1], int((3375 / lima / limb) ** (1 / 2)) + 2)

    ans = []
    for i in range(lima):
        for j in range(limb):
            for k in range(limc):
                ans.append(d2[0][i] + d2[1][j] + d2[2][k])
    ans.sort(reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    x, y, z, w = ns()
    size = [[0, x], [1, y], [2, z]]
    size.sort(key=lambda x: x[1])
    d = []
    for _ in range(3):
        tmp = na()
        tmp.sort(reverse=True)
        d.append(tmp)

    d2 = []
    for i, s in size:
        d2.append(d[i])

    lima = min(size[0][1], 15)
    limb = min(size[1][1], int(math.sqrt(3000 / lima)) + 1)
    limc = min(size[2][1], int(math.sqrt(3000 / lima / limb)) + 1)

    ans = []
    for i in range(lima):
        for j in range(limb):
            for k in range(limc):
                ans.append(d2[0][i] + d2[1][j] + d2[2][k])
    ans.sort(reverse=True)

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


if __name__ == '__main__':
    main()

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

for i in A:
    for j in B:
        for k in C:
            s.append(i+j+k)

s = sorted(s, reverse=True)

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

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

for i in A:
	for j in B:
		for k in C:
			s.append(i+j+k)

s = sorted(s, reverse=True)

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

Submission status: Runtime Error

Solution code:
import numpy as np
x,y,z,k = map(int,input().split())
a = np.array(list(map(int, input().split())))
b = np.array(list(map(int, input().split())))
c = np.array(list(map(int, input().split())))
aa,bb,cc = np.meshgrid(a,b,c)
aaa = aa.flatten()
bbb = bb.flatten()
ccc = cc.flatten()
abc = np.c_[aaa,bbb,ccc]
ans = np.sum(abc,axis=1)
ans = list(ans)
ans.sort(reverse = True)
for i in range(k):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import numpy as np
x,y,z,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
a = np.array(a)
b = np.array(b)
c = np.array(c)
aa,bb,cc = np.meshgrid(a,b,c)
abc = np.c_[aa.flatten(),bb.flatten(),cc.flatten()]
ans = np.sum(abc,axis=1)
ans = list(ans)
ans.sort(reverse = True)
for i in range(k):
    print(ans[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()))

if X*Y*Z <= 1:
  D = sorted([A[i]+B[j]+C[k] for i in range(X) for j in range(Y) for k in range(Z)],reverse=True)
  for i in range(K):
    print(D[i])
  
else:
  A = sorted(A)
  D = sorted([B[i]+C[j] for i in range(Y) for j in range(Z)])

  thr = K
  s = A[-1]+D[max(0,len(D)-thr)]
  prev_idx=0
  selected = [A[-1] + D[i] for i in range(max(0,len(D)-thr),len(D))]
  
  for i in range(1,X):
    r = s - A[-1-i]
    if r<=0:break
    idx = bisect_left(D,r,prev_idx,len(D)-1)
    if idx == len(D):break
    selected.extend([A[-1-i] + D[j] for j in range(idx,len(D))])
    prec_idx = idx

  selected = sorted(selected,reverse=True)

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

Submission status: Runtime Error

Solution code:
X, Y, Z, K  = list(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())))

l = []
for a in A:
    for b in B:
        l.append(a+b)
l = sorted(l, reverse = True)[:K]

ans = []
for c in C:
    for k in l:
        ans.append(c + k)
        
ans = sorted(ans, reverse = True)
print(*ans[:K], seq="\n")

Submission status: Runtime Error

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

a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

hq = [[-a[0]-b[0]-c[0], 0, 0, 0]]
heapq.heapify(hq)
# visited = [[[0] * z for yy in range(y)] for xx in range(x)]
visited = np.zeros((x, y, z), dtype=int)

for i in range(k):
    ii, ia, ib, ic = heapq.heappop(hq)
    print(-ii)
    na = min(ia+1, x-1)
    nb = min(ib+1, y-1)
    nc = min(ic+1, z-1)
    if visited[na][ib][ic] == 0:
        heapq.heappush(hq, [-a[na] -b[ib] -c[ic], na, ib, ic])
        visited[na][ib][ic] = 1
    if visited[ia][nb][ic] == 0:
        heapq.heappush(hq, [-a[ia] -b[nb] -c[ic], ia, nb, ic])
        visited[ia][nb][ic] = 1
    if visited[ia][ib][nc] == 0:
        heapq.heappush(hq, [-a[ia] -b[ib] -c[nc], ia, ib, nc])
        visited[ia][ib][nc] = 1

Submission status: Runtime Error

Solution code:
import heapq
import numpy as np

x, y, z, k =  map(int,input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

hq = [[-a[0]-b[0]-c[0], 0, 0, 0]]
heapq.heapify(hq)
# visited = [[[0] * z for yy in range(y)] for xx in range(x)]
visited = np.zeros((x, y, z), dtype=int)

for i in range(k):
    ii, ia, ib, ic = heapq.heappop(hq)
    print(-ii)
    na = min(ia+1, x-1)
    nb = min(ib+1, y-1)
    nc = min(ic+1, z-1)
    if visited[na][ib][ic] == 0:
        heapq.heappush(hq, [-a[na] -b[ib] -c[ic], na, ib, ic])
        visited[na][ib][ic] = 1
    if visited[ia][nb][ic] == 0:
        heapq.heappush(hq, [-a[ia] -b[nb] -c[ic], ia, nb, ic])
        visited[ia][nb][ic] = 1
    if visited[ia][ib][nc] == 0:
        heapq.heappush(hq, [-a[ia] -b[ib] -c[nc], ia, ib, nc])
        visited[ia][ib][nc] = 1

Submission status: Runtime Error

Solution code:
from itertools import product
from heapq import *

X,Y,Z,K,*f = map(int, open(0).read().split())
A = f[:X]
B = f[X:X+Y]
C = f[X+Y:]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
AB = [sum(x) * (-1) for x in product(A,B)]
heapify(AB)
AB_picked = []
for i in range(K):
    AB_picked.append(heappop(AB)*(-1))
ABC = [sum(x) * (-1) for x in product(AB_picked,C)]
heapify(ABC)
for i in range(K):
    print(heappop(ABC)*(-1))

Submission status: Runtime Error

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############
X, Y, Z, K = getNM()
A = getList()
B = getList()
C = getList()
A.sort(reverse = True)
B.sort(reverse = True)
C.sort(reverse = True)

alta = []
for i in range(X):
    for j in range(Y):
        alta.append(A[i] + B[j])

alta.sort(reverse = True)
alta = alta[:K]
ans = []
for i in range(K):
    for j in range(Z):
        ans.append(alta[i] + C[j])
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()))

AB = [0]*X*Y
i = 0
for a in A:
    for b in B:
        AB[i] = a + b
        i += 1
AB.sort(reverse=True)

ans = [0]*min(K, len(AB)*Z)
i, j = 0, 0
for i in range(min(K, len(AB))):
    for c in C:
        ans[j] = AB[i] + c
        j += 1
ans.sort(reverse=True)

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

Submission status: Runtime Error

Solution code:
#!/usr/bin/python3
# import bisect
# from collections import Counter, deque, OrderedDict, defaultdict
# from copy import copy, deepcopy # pythonのみ．copyは1次元，deepcopyは多次元．
# from functools import reduce
from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import statistics # Pythonのみ
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    X,Y,Z,K=map(int,input().split())
    v=[sorted(list(map(int,input().split())), reverse=True) for _ in range(3)]

    q=[]
    checked=[[[False]*Z for _ in range(Y)] for _ in range(X)]
    heappush(q,(-(v[0][0]+v[1][0]+v[2][0]),0,0,0))
    checked[0][0][0]=True
    for _ in range(K):
        M=heappop(q)
        print(-M[0])
        x,y,z=M[1]+1,M[2],M[3]
        if (not checked[x][y][z]) and x<X and y<Y and z<Z:
            heappush(q,(-(v[0][x]+v[1][y]+v[2][z]),x,y,z))
            checked[x][y][z]=True


        x,y,z=M[1],M[2]+1,M[3]
        if (not checked[x][y][z]) and x<X and y<Y and z<Z:
            heappush(q,(-(v[0][x]+v[1][y]+v[2][z]),x,y,z))
            checked[x][y][z]=True


        x,y,z=M[1],M[2],M[3]+1
        if (not checked[x][y][z]) and x<X and y<Y and z<Z:
            heappush(q,(-(v[0][x]+v[1][y]+v[2][z]),x,y,z))
            checked[x][y][z]=True
      



    
resolve()

Submission status: Runtime Error

Solution code:
import sys
#import numpy as np
import math
#from fractions import Fraction
#import itertools
from collections import deque
#import heapq
from fractions  import gcd

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=[0]*(x*y)
c.sort(reverse=True)
for i in range(x):
    for j in range(y):
        ab[y*i+j]=a[i]+b[j]
ab.sort(reverse=True)
d=ab[:k]
ans=[0]*(k*z)
for i in range(k):
    for j in range(z):
        ans[z*i+j]=d[i]+c[j]
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 = [0]*(X*Y*Z)
c = 0

from decimal import *

for x in range(X):
  for y in range(Y):
    for z in range(Z):
      l[c] = Decimal(A[x]+B[y]+C[z])
      c += 1

ans = sorted(l,reverse = True)

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

l = [0]*(X*Y*Z)
c = 0

for x in range(X):
  for y in range(Y):
    for z in range(Z):
      l[c] = A[x]+B[y]+C[z]
      c += 1

ans = sorted(l,reverse = True)

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

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
#N = int(input())
#A = list(map(int,input().split()))
#S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
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)
ca = 1
cb = 1
cc = 1
flaga = 0
flagb = 0
flagc = 0
while ca*cb*cc < 106307:
    if flaga == 1:
        a = 0
    else:
        a = A[ca]
    if flagb == 1:
        b = 0
    else:
        b = B[cb]
    if flagc == 1:
        c = 0
    else:
        c = C[cc]
    if flaga*flagb*flagc == 1:
        break
    if a >= b and a >= c:
        ca += 1
        if ca == X:
            flaga = 1
    elif b >= a and b >= c:
        cb += 1
        if cb == Y:
            flagb = 1
    else:
        cc += 1
        if cc == Z:
            flagc = 1
d = []
for aa in range(ca):
    for bb in range(cb):
        for ccc in range(cc):
            d.append(A[aa]+B[bb]+C[ccc])
d.sort(reverse = True)
for i in range(K):
    print(d[i])

Submission status: Runtime Error

Solution code:
from sys import stdin
import heapq
def main():
    #入力
    readline=stdin.readline
    x,y,z,k=map(int,readline().split())
    a=list(map(int,readline().split()))
    a.sort(reverse=True)
    b=list(map(int,readline().split()))
    b.sort(reverse=True)
    c=list(map(int,readline().split()))
    c.sort(reverse=True)

    s={(0,0,0)}
    h=[[-(a[0]+b[0]+c[0]),(0,0,0)]]
    heapq.heapify(h)
    for _ in range(k):
        li=heapq.heappop(h)
        n=-li[0]
        co=li[1]
        print(n)

        if co[0]+1<x:
            co_1=(co[0]+1,co[1],co[2])
            n_1=a[co_1[0]]+b[co_1[1]]+c[co_1[2]]
            n_1=-n_1
        if co[1]+1<y:
            co_2=(co[0],co[1]+1,co[2])
            n_2=a[co_2[0]]+b[co_2[1]]+c[co_2[2]]
            n_2=-n_2
        if co[2]+1<z:
            co_3=(co[0],co[1],co[2]+1)
            n_3=a[co_3[0]]+b[co_3[1]]+c[co_3[2]]
            n_3=-n_3

        if co_1 not in s:
            heapq.heappush(h,[n_1,co_1])
            s.add(co_1)
        if co_2 not in s:
            heapq.heappush(h,[n_2,co_2])
            s.add(co_2)
        if co_3 not in s:
            heapq.heappush(h,[n_3,co_3])
            s.add(co_3)

if __name__=="__main__":
    main()

Submission status: Runtime Error

Solution code:
import heapq
x, y, z, k = map(int, input().split())
a = sorted(map(int, input().split()))[::-1]
b = sorted(map(int, input().split()))[::-1]
c = sorted(map(int, input().split()))[::-1]

print(a[0] + b[0] + c[0])
candidates = [(-(a[1] + b[0] + c[0]), 1, 0, 0),
              (-(a[0] + b[1] + c[0]), 0, 1, 0),
              (-(a[0] + b[0] + c[1]), 0, 0, 1)]
heapq.heapify(candidates)
popped = {(1, 0, 0): 1,
          (0, 1, 0): 1,
          (0, 0, 1): 1}

for i in range(1, k):
    value, p, q, r = heapq.heappop(candidates)
    print(-value)
    try:
        popped[(p+1, q, r)]
    except:
        if p+1 < x: 
            heapq.heappush(candidates, (-(a[p+1] + b[q] + c[r]), p+1, q, r))
            popped[(p+1, q, r)] = 1
    try:
        popped[(p, q+1, r)]
    except:
        if q+1 < y: 
            heapq.heappush(candidates, (-(a[p] + b[q+1] + c[r]), p, q+1, r))
            popped[(p, q+1, r)] = 1
    try:
        popped[(p, q, r+1)]
    except:
        if r+1 < z: 
            heapq.heappush(candidates, (-(a[p] + b[q] + c[r+1]), p, q, r+1))
            popped[(p, q, r+1)] = 1

Submission status: Runtime Error

Solution code:
# D - Cake 123

import heapq
import itertools

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

if x*y*z<3000:
    a = list(int(x) for x in input().split())
    b = list(int(x) for x in input().split())
    c = list(int(x) for x in input().split())

    anslist = []
    ip = list(itertools.product(a, b, c))   
    for i in range(len(ip)):
        heapq.heappush(anslist, sum(ip[i])*-1)

    for i in range(k):
        print(heapq.heappop(anslist)*-1)

else:
    a = list(int(x)*-1 for x in input().split())
    b = list(int(x)*-1 for x in input().split())
    c = list(int(x)*-1 for x in input().split())
    inf = 10**18

    heapq.heapify(a)
    heapq.heapify(b)
    heapq.heapify(c)

    la = [inf]
    lb = [inf]
    lc = [inf]

    while len(la)*len(lb)*len(lc)<3000:
        if a[0]<=b[0]<=c[0]:
            tmp = heapq.heappop(a)
            heapq.heappush(a, inf)
            la.append(tmp*-1)
        elif a[0]>b[0]<=c[0]:
            tmp = heapq.heappop(b)
            heapq.heappush(b, inf)
            lb.append(tmp*-1)
        else:
            tmp = heapq.heappop(b)
            heapq.heappush(c, inf)
            lc.append(tmp*-1)

    anslist = []
    ip = list(itertools.product(la, lb, lc))   
    for i in range(len(ip)):
        heapq.heappush(anslist, sum(ip[i])*-1)

    for i in range(k):
        print(heapq.heappop(anslist)*-1)

Submission status: Runtime Error

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
sys.setrecursionlimit(10**7)
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())

X,Y,Z,K = LI()
A = LI()
B = LI()
C = LI()

if len (C) > len(A) and len(C) > len(B):
    A,C = C,A
elif len(B) > len(A) and len(B) > len(C):
    A,B = B,A

AB = [a + b for a in A for b in B]
AB.sort(reverse=True)
C.sort(reverse=True)
max_abc = AB[0] + C[0]

l = 0
h = max_abc

while h > l+1:
    m = (h+l) // 2

    num = 0
    j = len(C) - 1
    for i in AB:
        while i + C[j] < m:
            j -= 1
            if j < 0: break
        else:
            num += j + 1
            continue
        break
    if num > K:
        l = m
    else:
        h = m

ans = []
j = len(C) - 1
for i in AB:
    while i + C[j] < l:
        j -= 1
        if j < 0: break
    else:
        for k in range(j+1):
            ans.append(i+C[k])
ans.sort(reverse=True)
print (*ans[:K],sep='\n')

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
from heapq import *
from collections import defaultdict
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, reverse = True)
b = sorted(b, reverse = True)
c = sorted(c, reverse = True)

ans = [[-(a[0] + b[0] + c[0]), 1, 1, 1]]
heapify(ans)
log = defaultdict(lambda :0)

d = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
for i in range(k):
    temp, s, t, u = heappop(ans)
    temp = temp * (-1)
    print(temp)
    for ds, dt, du in d:
        ns = s + ds
        nt = t + dt
        nu = u + du
        if not(0 <= ns < x and 0 <= nt < y and 0 <= nu < z):continue
        if log[(ns, nt, nu)]:continue
        heappush(ans, [-(a[ns - 1] + b[nt - 1] + c[nu - 1]), ns, nt, nu])
        log[(ns, nt, nu)] = 1

Submission status: Runtime Error

Solution code:
from heapq import *
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, reverse = True)
b = sorted(b, reverse = True)
c = sorted(c, reverse = True)

ans = heapify([-(a[0] + b[0] + c[0]), 1, 1, 1])
log = [[[0] * len(c) for i in range(len(b))] for j in range(len(a))]
log[0][0][0] = 1

d = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
for i in range(k):
    temp, s, t, u = heappop(ans)
    temp = temp * (-1)
    print(temp)
    for ds, dt, du in d:
        ns = s + ds
        nt = t + dt
        nu = u + du
        if log[ns - 1][nt - 1][nu - 1]:continue
        heappush(ans, [-(a[ns - 1] + b[nt - 1] + c[nu - 1]), ns, nt, nu])
        log[ns - 1][nt - 1][nu - 1] = 1

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    X, Y, Z, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))
    C = list(map(int, readline().split()))
    AB = []
    for i in range(X):
        for j in range(Y):
            AB.append(A[i]+B[j])
    AB.sort(reverse=True)
    ABC = []
    for i in range(K):
        for j in range(Z):
            ABC.append(AB[i] + C[j])
    ABC.sort(reverse=True)
    for i in range(K):
        print(ABC[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()))

A1=[-1*i for i in A]
B1=[-1*i for i in B]
C1=[-1*i for i in C]


list=[]
sum_list=[]
for i in range(X):
    for j in range(Y):
        list.append(A1[i]+B1[j])
list.sort()
for l in range(K):
    for k in range(Z):
        sum_list.append(C1[k]+list[l])
sum_list.sort()
for x in range(K):
    print(-1*sum_list[x])

Submission status: Runtime Error

Solution code:
import heapq

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

A1=[-1*i for i in A]
B1=[-1*i for i in B]
C1=[-1*i for i in C]


list=[]
sum_list=[]
for i in range(X):
    for j in range(Y):
        list.append(A1[i]+B1[j])
list.sort()
for l in range(K):
    for k in range(Z):
        sum_list.append(C1[k]+list[l])
heapq.heapify(sum_list)

for x in range(K):
    print(-1*heapq.heappop(sum_list))