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=list(map(int,input().split()))
K=int(input())
alist=[]
blist=[]
clist=[]
for i in range(x):
  alist[i]=int(input())
for i in range(y):
  blist[i]=int(input())
for i in range(z):
  clist[i]=int(input())
azi=[i+j+k for (i,j,k) in zip(alist,blist,clist)]
print(sorted(azi))

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)
if x*y*z < 1000000:
    ans = []
    for a in A:
        for b in B:
            for c in C:
                ans.append(a+b+c)
    ans.sort(reverse = True)
    for i in range(k):
        print(ans[i])
else:
    A.append(-1e20)
    B.append(-1e20)
    C.append(-1e20)
    cnt=0
    a = 0
    b = 0
    c = 0
    print(A[0]+B[0]+C[0])
    while(cnt<k-1):
        Adiv = A[0]-A[a+1]
        Bdiv = B[0]-B[b+1]
        Cdiv = C[0]-C[c+1]
        if(Adiv <= Bdiv and Adiv <= Cdiv):
            a+=1
            print(A[a]+B[0]+C[0])
        elif(Bdiv <= Adiv and Bdiv <= Cdiv):
            b+=1
            print(A[0]+B[b]+C[0])
        else:
            c+=1
            print(A[0]+B[0]+C[c])
        cnt+=1

Submission status: Runtime Error

Solution code:
x,y,z,k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
if x*y*z < 1e6:
    ans = []
    for a in A:
        for b in B:
            for c in C:
                ans.append(a+b+c)
    ans.sort(reverse = True)
    for i in range(k):
        print(ans[i])
else:
    A.append(-1e20)
    B.append(-1e20)
    C.append(-1e20)
    cnt=0
    a = 0
    b = 0
    c = 0
    print(A[0]+B[0]+C[0])
    while(cnt<k-1):
        Adiv = A[0]-A[a+1]
        Bdiv = B[0]-B[b+1]
        Cdiv = C[0]-C[c+1]
        if(Adiv <= Bdiv and Adiv <= Cdiv):
            a+=1
            print(A[a]+B[0]+C[0])
        elif(Bdiv <= Adiv and Bdiv <= Cdiv):
            b+=1
            print(A[0]+B[b]+C[0])
        else:
            c+=1
            print(A[0]+B[0]+C[c])
        cnt+=1

Submission status: Runtime Error

Solution code:
x,y,z,k = map(int,input().split())
num = []
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)
if x*y*z < 1e6:
    ans = []
    for a in A:
        for b in B:
            for c in C:
                ans.append(a+b+c)
    ans.sort(reverse = True)
    for i in range(k):
        print(ans[i])
else:
    A.append(-1e20)
    B.append(-1e20)
    C.append(-1e20)
    cnt=0
    a,b,c = (0,0,0)
    print(A[0]+B[0]+C[0])
    while(cnt<k-1):
        Adiv = A[0]-A[a+1]
        Bdiv = B[0]-B[b+1]
        Cdiv = C[0]-C[c+1]
        if(Adiv <= Bdiv and Adiv <= Cdiv):
            a+=1
            print(A[a]+B[0]+C[0])
        elif(Bdiv <= Adiv and Bdiv <= Cdiv):
            b+=1
            print(A[0]+B[b]+C[0])
        else:
            c+=1
            print(A[0]+B[0]+C[c])
        cnt+=1

Submission status: Runtime Error

Solution code:
x,y,z,k = map(int,input().split())
num = []
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
for i in A:
    num.append((i,'A'))
for i in B:
    num.append((i,'B'))
for i in C:
    num.append((i,'C'))
num.sort(reverse = True)
A = []
B = []
C = []
po = 0
while(po < x*y*z and len(A)*len(B)*len(C) < 222222):
    if(num[po][1] == 'A'):
        A.append(num[po][0])
    elif (num[po][1] == 'B'):
        B.append(num[po][0])
    else:
        C.append(num[po][0])
    po+=1
ans = []
#print(A,B,C)
for a in A:
    for b in B:
        for c in C:
            ans.append(a+b+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()))
#入力受け取り



AA = []
BB = []
CC = []

for k in range(X):
	maxA = max(A)
	A.remove(maxA)
	AA.append(maxA)
for k in range(Y):
	maxB = max(B)
	B.remove(max(B))
	BB.append(maxB)
for k in range(Z):
	maxC = max(C)
	C.remove(maxC)
	CC.append(maxC)
#list X から X に大きい順にポイントを格納。



indexlist  = []
for k in range(min(X, K)):
	for l in range(min(Y, K)):
		for m in range(min(Z, K)):
			indexlist.append([k, l, m])
#インデックスを参照することで大きい順にポイントの組を取得できるような「リストのリスト」を作成


dictionary = {}
for P in indexlist:
	p = AA[P[0]] + BB[P[1]] + CC[P[2]]
	dictionary.update({str(P[0]) +" , " + str(P[1]) +" , " + str(P[2]): p})
#indexlist 引数の組を取り出し、対応するポイントを足し算して辞書に格納



Ans = dictionary.values()
for k in range(K):
	print(Ans[-k-1])
	Ans.remove(max(Ans))

Submission status: Runtime Error

Solution code:
x,y,z,k = map(int,input().split())
num = []
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
for i in A:
    num.append((i,'A'))
for i in B:
    num.append((i,'B'))
for i in C:
    num.append((i,'C'))
num.sort(reverse = True)
A = []
B = []
C = []
po = 0
while(len(A)*len(B)*len(C) <= k):
    if(num[po][1] == 'A'):
        A.append(num[po][0])
    elif (num[po][1] == 'B'):
        B.append(num[po][0])
    else:
        C.append(num[po][0])
    po+=1
ans = []
for a in A:
    for b in B:
        for c in C:
            ans.append(a+b+c)
ans.sort(reverse = True)
for i in range(k):
    print(ans[i])

Submission status: Runtime Error

Solution code:
n = int(input())
mi = 1e20
for i in range(5):
    mi = min(mi,int(input()))
print(n//mi + (n%mi!=0) + 4)

Submission status: Runtime Error

Solution code:
import heapq,sys


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+Bでのベスト枠K個求めるO(XYlogK)
    AandB = []
    for i in range(X):
        for j in range(Y):
            heapq.heappush(AandB, A[i] + B[j])
            #要素がK+1個以上なら一々はじく！！！ことでO(logK)
            if len(AandB) > K:
                heapq.heappop(AandB)
    #上のとCでのベスト枠K個を求める(ZKlogK)
    heap_queue = []
    for k in range(Z):
        for h in range(len(AandB)):
            heapq.heappush(heap_queue,C[k] + AandB[h])
            if len(heap_queue) > K:
                heapq.heappop(AandB)
    heap_queue.sort(reverse = True)
    for e in heap_queue:
        print(e)
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import heapq,sys


def main():
    input = sys.stdin.readline
    X, Y, Z, K = map(int, input().split())
    A = sorted([int(i) for i in input().split()], reverse=True)
    B = sorted([int(i) for i in input().split()], reverse=True)
    C = sorted([int(i) for i in input().split()], reverse=True)
    ans = []
    AandB = []
    for i in range(X):
        for j in range(Y):
            heapq.heappush(AandB, A[i] + B[j])
            #要素がK+1個以上なら一々はじく！！！ことでO(logK)
            if len(AandB) > K:
                heapq.heappop(AandB)
    
    heap_queue = []
    for k in range(Z):
        for h in range(len(AandB)):
            heapq.heappush(heap_queue,C[k] + AandB[h])
            if len(heap_queue) > K:
                heapq.heappop(AandB)
    heap_queue.sort(reverse = True)
    for e in heap_queue:
        print(e)
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
X,Y,Z,K = map(int, input().split())
a_list = [int(i) for i in input().split()]
b_list = [int(i) for i in input().split()]
c_list = [int(i) for i in input().split()]
a_list.sort(reverse = True)
b_list.sort(reverse = True)
c_list.sort(reverse = True)
total_list = [0 for _ in range(min(106307,X*Y*Z))]
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1)*(j+1)*(k+1) <= K:
                total_list[i*(Z*Y)+j*Z+k] = (a_list[i] + b_list[j] + c_list[k])
            else:
                break
total_list =  sorted(total_list, reverse = True)
for i in range(K):
    print(total_list[i])

Submission status: Runtime Error

Solution code:
X,Y,Z,K = map(int, input().split())
a_list = [int(i) for i in input().split()]
b_list = [int(i) for i in input().split()]
c_list = [int(i) for i in input().split()]
a_list.sort(reverse = True)
b_list.sort(reverse = True)
c_list.sort(reverse = True)
total_list = [0 for _ in range(min(106307,X*Y*Z))]
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1)*(j+1)*(k+1) <= K:
                total_list[i*(Z*Y)+j*Z+k] = (a_list[i] + b_list[j] + c_list[k])
            else:
                break
print(total_list)
total_list =  sorted(total_list, reverse = True)
for i in range(K):
    print(total_list[i])

Submission status: Runtime Error

Solution code:
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split())) #list() と [] は違うな
B = list(map(int, input().split()))
C = list(map(int, input().split()))

AB = [[0 for _ in range(Y)] for _ in range(X)]
for i in range(X):
    for j in range(Y):
        AB[i][j] = A[i] + B[j]
AB = sum(AB, [])
AB.sort(reverse=True)
AB = AB[:K]

ans = [[0 for _ in range(Z)] for _ in range(K)]
for i in range(K):
    for j in range(Z):
        ans[i][j] = AB[i] + C[j]
ans = sum(ans,[])
ans.sort(reverse=True)
ans = ans[:K]

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

Submission status: Runtime Error

Solution code:
X, Y, Z, K = map(int, input().split())
A = [map(int, input().split()) for _ in range(X)]
B = [map(int, input().split()) for _ in range(Y)]
C = [map(int, input().split()) for _ in range(Z)]

AB = []
for i in range(X):
    for j in range(Y):
        AB[i][j] = A[i]*B[j]
AB = sum(AB, [])
AB.sort(reversed=True)
AB = AB[:K]

ans = []
for i in range(K):
    for j in range(Z):
        ans[i][j] = AB[i]*C[j]
ans = sum(ans,[])
ans.sort(reversed=True)
ans = ans[:K]

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

Submission status: Runtime Error

Solution code:
def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]
def zeros(n): return [0]*n
# def zeros(n): return np.zeros(n, dtype=np.longlong)
def zeros2(n, m): return [zeros(m) for i in range(n)] # obsoleted zeros((n, m))で代替

INF = 10**18

class Debug():
    def __init__(self):
        self.debug = True
 
    def off(self):
        self.debug = False
 
    def dmp(self, x, cmt=''):
        if self.debug:
            if cmt != '':
                w = cmt + ': ' + str(x)
            else:
                w = str(x)
            print(w)
        return x

def prob():
    d = Debug()
    d.off()
    X, Y, Z, K = getIntList()
    d.dmp((X, Y, Z, K),'X, Y, Z, K')
    A, B, C = zeros(X), zeros(Y), zeros(Z)
    A, B, C = getIntList(), getIntList(), getIntList()
    d.dmp((A, B, C),'A, B, C')
    delic = zeros(5000)
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    d.dmp((A, B, C),'A, B, C')
    a, b, c, k = 0, 0, 0, 0
    delic[k] = A[a]+B[b]+C[c]
    while a+1 < X or b+1 < Y or c+1 < Z:
        #d.dmp((a, b, c, k),'a, b, c, k')
        if a+1 < X:
            difa = A[a]-A[a+1]
        else:
            difa = INF
        if b+1 < Y:
            difb = B[b]-B[b+1]
        else:
            difb = INF
        if c+1 < Z:
            difc = C[c]-C[c+1]
        else:
            difc = INF
        if  difa <= difb and difa <= difc:
            a += 1
            for i in range(b+1):
                for j in range(c+1):
                    k += 1
                    #if k < K:
                    #d.dmp((a,b,c,k),'a, b, c, k')
                    delic[k] = A[a]+B[i]+C[j]
        elif difb <= difa and difb <= difc:
            b += 1
            for i in range(a+1):
                for j in range(c+1):
                    k += 1
                    #if k < K:
                    delic[k] = A[i]+B[b]+C[j]
        else:
            c += 1
            for i in range(a+1):
                for j in range(b+1):
                    k += 1
                    #if k < K:
                    #d.dmp((a, b, c, k),'a, b, c, k')
                    delic[k] = A[i]+B[j]+C[c]
    d.dmp((delic),'delic')
    delic.sort(reverse=True)
    for i in range(K):
        print(delic[i])
    

ans = prob()

Submission status: Runtime Error

Solution code:
input = sys.stdin.readline

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

list_cake_1 = list(map(int, input().split()))
list_cake_2 = list(map(int, input().split()))
list_cake_3 = list(map(int, input().split()))

list_cake_1 = sorted(list_cake_1, reverse=True)
list_cake_2 = sorted(list_cake_2, reverse=True)
list_cake_3 = sorted(list_cake_3, reverse=True)

list_total_sum = []

for i, cake_1 in enumerate(list_cake_1, 1):
	for j, cake_2 in enumerate(list_cake_2, 1):
		for k, cake_3 in enumerate(list_cake_3, 1):
			if i*j*k > K:
				break
			list_total_sum.append(cake_1 + cake_2 + cake_3)

for element in sorted(list_total_sum, reverse=True)[:K]:
	print(element)

Submission status: Runtime Error

Solution code:
# priority queue like solution
# O(K log K)

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


def score(ia, ib, ic):
    return sum((a[ia], b[ib], c[ic]))


ans = []

init = (0, 0, 0)
comb = set()
comb.add((score(*init), *init))

for _ in range(K):
    t = max(comb)
    s, i, j, k = t
    ans.append(s)
    comb.remove(t)

    opts = [
        (i + 1, j, k),
        (i, j + 1, k),
        (i, j, k + 1),
    ]
    for opt in opts:
        try:
            comb.add((score(*opt), *opt))
        except IndexError:
            pass

for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
import bisect

X,Y,Z,K = list(map(int,input().split()))
AA = list(map(int,input().split()))
BB = list(map(int,input().split()))
CC = list(map(int,input().split()))
MAX = max(max(AA),max(BB),max(CC))

AB = []
for a in AA:
  for b in BB:
    AB.append(a+b)
    
AB.sort()
XY = len(AB)

def check(n):
  cnt = 0
  for c in CC:
    cnt += (XY - bisect.bisect_left(AB,n-c))
  return cnt >= K

OK = 0
NG = MAX*3+100
while NG - OK >1:
  mid = (OK+NG)//2
  if check(mid):
    OK = mid
  else:
    NG = mid
    
    
ans = []
for c in CC:
    n = bisect.bisect_left(AB,OK-c)
    for i in range(n,XY):
      ans.append(c+AB[i])
      
ans.sort(reverse=True)
for YUI in ans:
  print(

Submission status: Runtime Error

Solution code:
import bisect

X,Y,Z,K = list(map(int,input().split()))
AA = list(map(int,input().split()))
BB = list(map(int,input().split()))
CC = list(map(int,input().split()))
MAX = max(max(AA),max(BB),max(CC))

AB = []
for a in AA:
  for b in BB:
    AB.append(a+b)
    
AB.sort()
XY = X*Y

def check(n):
  cnt = 0
  for c in CC:
    cnt += XY - bisect.bisect_left(AB,n-c)
  return cnt >= K

OK = 0
NG = MAX*3+100
while OK-NG >1:
  mid = (OK+NG)//2
  if check(mid):
    OK = mid
  else:
    NG = mid
    
ans = []
for c in CC:
    n = bisect.bisect_left(AB,n-c)
    for i in range(n,XY):
      ans.append(c+AB[i])
      
ans.sort(reverse=Ture)
for YUI in ans:
  print(YUI)

Submission status: Runtime Error

Solution code:
from collections import deque
X,Y,Z,K=[int(i) for i in 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(key=lambda x:-x)
B.sort(key=lambda y:-y)
C.sort(key=lambda z:-z)
pri=deque()
def dp(l):
  return A[l[0]]+B[l[1]]+C[l[2]]

def priq(a,b,c):
  if [a,b,c] not in pri:
    for i in range(len(pri)):      
      if dp(pri[i])>dp([a,b,c]):
        pri.insert(i,[a,b,c])
        return
    pri.append([a,b,c])
pri.append([0,0,0])
for i in range(K):
  x,y,z=pri.pop()
  print(dp([x,y,z]))
  if x<X-1:
    priq(x+1,y,z)
  if y<Y-1:
    priq(x,y+1,z)
  if z<Z-1:
    priq(x,y,z+1)

Submission status: Runtime Error

Solution code:
from collections import deque
X,Y,Z,K=[int(i) for i in 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(key=lambda x:-x)
B.sort(key=lambda y:-y)
C.sort(key=lambda z:-z)
pri=deque()
def dp(l):
  return A[l[0]]+B[l[1]]+C[l[2]]

def priq(a,b,c):
  if [a,b,c] not in pri:
    for i in range(len(pri)-1,-1,-1):
      
      if dp(pri[i])>dp([a,b,c]):
        pri.insert(i+1,[a,b,c])
        return
    pri.append([a,b,c])

pri.append([0,0,0])
for i in range(K):
  x,y,z=pri.popleft()
  print(dp([x,y,z]))
  if x<X-1:
    priq(x+1,y,z)
  if y<Y-1:
    priq(x,y+1,z)
  if z<Z-1:
    priq(x,y,z+1)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>

#define int long long
#define uint unsigned int
#define pii pair<int, int>

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORR(i,a,b) for(int i=(a);i>=(b);--i)
#define REP(i,n) FOR(i,0,n)
#define REPR(i,n) FORR(i,n,0)
#define ALL(c) ((c).begin()), ((c).end())

#define PB(a) push_back(a)
#define EMPB(...) emplace_back(__VA_ARGS__)
#define EMP(...) emplace(__VA_ARGS__)
#define MP(a,b) make_pair(a,b)
#define MT(...) make_tuple(__VA_ARGS__)

#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MINF(a) memset(a,0x3f,sizeof(a))

#define PW(n) (1LL << (n))

using namespace std;

template <class T> inline bool CHMIN(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template <class T> inline bool CHMAX(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> inline void SORT(T& a) { sort(a.begin(), a.end()); }
template <class T> inline void REV(T& a) { reverse(a.begin(), a.end()); }
template <class T> inline void UNI(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }

template <class S, class T> ostream& operator << (ostream& os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")"; return os;
}

const int MOD = 1000000007;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;

/* ---------------------------------------------------------------------------------------------------- */

int sz[3], K;
int a[3][1010];
using P = tuple<int, int, int, int>;
set<P> st;

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  REP(i, 3) cin >> sz[i];
  cin >> K;
  REP(i, 3) REP(j, sz[i]) cin >> a[i][j];
  REP(i, 3) {
    sort(a[i], a[i]+sz[i]);
    reverse(a[i], a[i]+sz[i]);
  }
  priority_queue<P> que;
  que.EMP(a[0][0] + a[1][0] + a[2][0], 0, 0, 0);
  REP(i, K) {
    P p = que.top(); que.pop();
    int v, x, y, z;
    tie(v, x, y, z) = p;
    cout << v << endl;
    if (x+1 < sz[0] && !st.count(MT(v-a[0][x]+a[0][x+1], x+1, y, z))) {
      st.insert(MT(v-a[0][x]+a[0][x+1], x+1, y, z));
      que.EMP(v-a[0][x]+a[0][x+1], x+1, y, z);
    }
    if (y+1 < sz[1] && !st.count(MT(v-a[1][y]+a[1][y+1], x, y+1, z))) {
      st.insert(MT(v-a[1][y]+a[1][y+1], x, y+1, z));
      que.EMP(v-a[1][y]+a[1][y+1], x, y+1, z);
    }
    if (z+1 < sz[2] && !st.count(MT(v-a[2][z]+a[2][z+1], x, y, z+1))) {
      st.insert(MT(v-a[2][z]+a[2][z+1], x, y, z+1));
      que.EMP(v-a[2][z]+a[2][z+1], x, y, z+1);
    }
  }

  return 0;
}

Submission status: Runtime Error

Solution code:
from itertools import product

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)

a1 = []
mXY = min(X, Y)
for i, j in product(A[0:min(mXY, 3000)], B[0:min(mXY, 3000)]):
    a1.append(i + j)
a1.sort(reverse=True)

ans_list = []
for i, j in product(a1[0:min(mXY, 3000)], C[0:min(Z, 3000)]):
    ans_list.append(i + j)
ans_list.sort(reverse=True)

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

Submission status: Runtime Error

Solution code:
from itertools import product

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)

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

a1 = []
mXY = min(X,Y)
for i, j in product(A[0:mXY], B[0:mXY]):
    a1.append(i + j)
a1.sort(reverse=True)

ans_list = []
for i, j in product(a1[0:min(mXY,Z)], C[0:min(mXY,Z)]):
    ans_list.append(i + j)

ans_list.sort(reverse=True)

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

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(reverse=True)
for i in range(K):
    print(ans_list[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)

ABC = []
for i in range(X):
    for j in range(Y):
        if i + 1 + j + 1 > K:
            break
        for k in range(Z):
            if i + 1 + j + 1 + k + 1 > K:
                break
            ABC.append(A[i] + B[j] + C[k])

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

Submission status: Runtime Error

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

list_cake_1 = []
list_cake_2 = []
list_cake_3 = []

while len(list_cake_1) < X:
	element = list(map(int, input().split()))
	list_cake_1.extend(element)

while len(list_cake_2) < Y:
	element = list(map(int, input().split()))
	list_cake_2.extend(element)
	
while len(list_cake_3) < Z:
	element = list(map(int, input().split()))
	list_cake_3.extend(element)

list_cake_1.sort()
list_cake_2.sort()
list_cake_3.sort()
	
list_total_K = []

for i in range(1, X+1):
	for j in range(1, Y+1):
		for k in range(1, Z+1):
			if i*j*k < K:
				continue
			else:
				total_ijk = list_cake_1[i-1]+list_cake_2[j-1]+list_cake_3[k-1]
				list_total_K.append(total_ijk)
				
list_total_K.sort()

for i in range(1, K+1):
	print(list_total_K[-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)

for i in range(X):
    for j in range(Y):
        if i + 1 + j + 1 > K:
            break
        for k in range(Z):
            if i + 1 + j + 1 + k + 1 > K:
                break
            ABC.append(A[i] + B[j] + C[k])

ABC.sort(reverse=True)
for i in range(K):
    print(ABC[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)

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

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)

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(K):
        ABC.append(AB[i] + C[j])

ABC.sort(reverse=True)
for i in range(K):
    print(ABC[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)

AB = []
t = int(K**0.5)+1
for i in range(t):
    for j in range(t):
        AB.append(A[i] + B[j])
AB.sort(reverse=True)

ABC = []
for i in range(t):
    for j in range(t):
        ABC.append(AB[i] + C[j])

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

Submission status: Runtime Error

Solution code:
X,Y,Z,K = (int(i) for i in 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)

print(A[1]+B[1]+C[1])

while count < K-1:
  count = 0
  a = A[0] - A[1]
  b = B[0] - B[1]
  c = C[0] - C[1]
  t = min(a,b,c)
  if t == a:
    A.pop(0)
  if t == b:
    B.pop(0)
  else:
    C.pop(0)
  count += 1

Submission status: Runtime Error

Solution code:
def conKey(i, j, k):
    return str(i)+'-'+str(j)+'-'+str(k)


def getKey(s):
    return map(int, s.split('-'))


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()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

h = []
stack = set([])
ans = []
i, j, k = 0, 0, 0
heapq.heappush(h, (-(A[i]+B[j]+C[k]), conKey(i, j, k)))
stack.add(conKey(i, j, k))
for _ in range(K):
    tmp, key = heapq.heappop(h)
    print(-tmp)
    i, j, k = getKey(key)
    key_0 = [i, j, k]

    for m in range(3):
        key_tmp = key_0.copy()
        key_tmp[m] += 1
        tmp_sum = A[key_tmp[0]] + B[key_tmp[1]] + C[key_tmp[2]]

        str_key = conKey(*key_tmp)
        if str_key not in stack:
            heapq.heappush(h, (-tmp_sum, str_key))
            stack.add(str_key)

Submission status: Runtime Error

Solution code:
list_cake_1 = []
list_cake_2 = []
list_cake_3 = []

while len(list_cake_1) < X:
	element = list(map(int, input().split()))
	list_cake_1.extend(element)

while len(list_cake_2) < Y:
	element = list(map(int, input().split()))
	list_cake_2.extend(element)
	
while len(list_cake_3) < Z:
	element = list(map(int, input().split()))
	list_cake_3.extend(element)

list_total = []

for i in list_cake_1:
	for j in list_cake_2:
		for k in list_cake_3:
			total_ijk = i+j+k
			list_total.append(total_ijk)
				
list_total.sort()

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

Submission status: Runtime Error

Solution code:
import sys
import math
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
x,y,z,p = 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()
if x*y*z <= 3*10**6:
    abc = []
    for i in range(x):
        for j in range(y):
            for k in range(z):
                abc.append(a[i]+b[j]+c[k])
    abc.sort()
    abc = abc[::-1]
    for i in range(p):
        print(abc[i])  
    exit()
ab = []
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort()
c = c[::-1]
ab = ab[::-1]
ab.append(-2**62)
ans = []
miru = [0]*z
for i in range(z):
    if i + 1 == z:
        d = -2**60
    else:
        d = c[i+1]+ab[0]
    for j in range(i+1):
        ans.sort()
        while ab[miru[j]] + c[j] >= d and (len(ans)<= k or ab[miru[j]] + c[j] >= ans[-1]):
             ans.append(ab[miru[j]] + c[j])
             miru[j]+=1
    if len(ans) >= k:
        break
ans.sort()
ans = ans[::-1]
for i in range(p):
    print(ans[i])