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: Wrong Answer

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)

a.append(-10**11)
b.append(-10**11)
c.append(-10**11)

#k番目に美味しい組み合わせ(a+b+c)
#kは3000以下

a_cnt=1
b_cnt=1
c_cnt=1

ai=0
bi=0
ci=0

#print(a,b,c)

while a_cnt*b_cnt*c_cnt<k:
    #print(ai,bi,ci)
    if a[ai]-a[ai+1]<=b[bi]-b[bi+1] and a[ai]-a[ai+1]<=c[ci]-c[ci+1]:
        ai+=1
        a_cnt+=1
    elif c[ci]-c[ci+1]<=b[bi]-b[bi+1] and c[ci]-c[ci+1]<=a[ai]-a[ai+1]:
        ci+=1
        c_cnt+=1
    elif b[bi]-b[bi+1]<=c[ci]-c[ci+1] and b[bi]-b[bi+1]<=a[ai]-a[ai+1]:
        bi+=1
        b_cnt+=1
    else:
        print("error")

#print(a_cnt,b_cnt,c_cnt)

result=[]
for i in range(a_cnt+1):
    for j in range(b_cnt+1):
        for l in range(c_cnt+1):
            result.append(a[i]+b[j]+c[l])
    
result.sort(reverse=True)

cnt=0
for item in result:
    print(item)
    cnt+=1

    if cnt>=k:
        exit()

Submission status: Wrong Answer

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)

a.append(-10**11)
b.append(-10**11)
c.append(-10**11)

#k番目に美味しい組み合わせ(a+b+c)
#kは3000以下

a_cnt=1
b_cnt=1
c_cnt=1

ai=0
bi=0
ci=0

#print(a,b,c)

while a_cnt*b_cnt*c_cnt<k:
    print(ai,bi,ci)
    if a[ai]-a[ai+1]<=b[bi]-b[bi+1] and a[ai]-a[ai+1]<=c[ci]-c[ci+1]:
        ai+=1
        a_cnt+=1
    elif c[ci]-c[ci+1]<=b[bi]-b[bi+1] and c[ci]-c[ci+1]<=a[ai]-a[ai+1]:
        ci+=1
        c_cnt+=1
    elif b[bi]-b[bi+1]<=c[ci]-c[ci+1] and b[bi]-b[bi+1]<=a[ai]-a[ai+1]:
        bi+=1
        b_cnt+=1
    else:
        print("error")

#print(a_cnt,b_cnt,c_cnt)

result=[]
for i in range(a_cnt+1):
    for j in range(b_cnt+1):
        for l in range(c_cnt+1):
            result.append(a[i]+b[j]+c[l])
    
result.sort(reverse=True)

cnt=0
for item in result:
    print(item)
    cnt+=1

    if cnt>=k:
        exit()

Submission status: Wrong Answer

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


def calc(p):
    cnt = 0
    for a in A:
        for b in B:
            for c in C:
                if a + b + c < p:
                    break
                cnt += 1
                if cnt >= K:
                    return True
    return False


end = A[0] + B[0] + C[0]
start = 0
while end - start > 1:
    mid = (end + start)//2
    if calc(mid):
        start = mid
    else:
        end = mid

D = []

for a in A:
    for b in B:
        for c in C:
            if a + b + c < start:
                break
            D.append(a + b + c)
D.sort(reverse=True)
for d in D:
    print(d)

Submission status: Wrong Answer

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 = 0
cb = 0
cc = 0
flaga = 0
flagb = 0
flagc = 0
while ca*cb*cc < 100000:
    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: Wrong Answer

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 = 0
cb = 0
cc = 0
flaga = 0
flagb = 0
flagc = 0
while ca*cb*cc < 3000:
    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: Wrong Answer

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 = []
for a in A:
    for b in B:
        ab.append(a+b)
ab.sort(reverse=True)
print(ab)
abc = []
for i in ab:
    for c in C:
        abc.append(i+c)
abc.sort(reverse=True)
for i in range(K):
    print(abc[i])

Submission status: Wrong Answer

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

def MI():
    return map(int,input().split())

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

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def SI():
    return input()

def SMI():
    return input().split()

def SRI():
    return list(input().split())

def SCI(n):
    return [ input() for _ in range(n) ]

def SLI(n):
    return [ list(input().split()) for _ in range(n) ]

x,y,z,k = MI()
a = RI()
b = RI()
c = RI()

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

M = a[0] + b[0] + c[0]

ls = []
for i in range(x):
    for j in range(y):
        ab = a[i] + b[j]
        ls.append(ab)

ls.sort()

count = 0
count = 0
for i in reversed(range(M+1)):
    for j in range(z):
        t = i - c[j]
        ind = bisect_left(ls,t)
        if ind==len(ls):
            pass
        elif ls[ind]==t:
            if count==k:
                break
            else:
                print(i)
                count += 1
exit()

Submission status: Wrong Answer

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()))
import heapq
ans = []
heapq.heapify(ans)
for a in A:
    for b in B:
        for c in C:
          heapq.heappush(ans,-(a+b+c))
print(ans)
for _ in range(K):
    i = heapq.heappop(ans)
    print(-i)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
x,y,z,k = map(int,readline().split())
a = [int(i) for i in readline().split()]
b = [int(i) for i in readline().split()]
c = [int(i) for i in readline().split()]
from heapq import heappush,heappop,heapify
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

chk = [(-(a[0]+b[0]+c[0]),0,0,0)]
heapify(chk)
C = set((0,0,0))
ans = []
for i in range(k):
  p,q,r,s = heappop(chk)
  ans.append(-p)
  C.add((q,r,s))
  if not (q+1,r,s) in C and q < x-1:
    heappush(chk,(-(a[q+1]+b[r]+c[s]),q+1,r,s))
  if not (q,r+1,s) in C and r < y-1:
    heappush(chk,(-(a[q]+b[r+1]+c[s]),q,r+1,s))
  if not (q,r,s+1) in C and s < z-1:
    heappush(chk,(-(a[q]+b[r]+c[s+1]),q,r,s+1))
    
for i in ans:
  print(i)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
x,y,z,k = map(int,readline().split())
a = [int(i) for i in readline().split()]
b = [int(i) for i in readline().split()]
c = [int(i) for i in readline().split()]
from bisect import bisect_left,insort
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

stop = 0
ans = []
cn = 0
for i in a:
  for j in b:
    for l in c:
      if i+j+l <= stop:
        break
      if cn == k:
        ans.sort()
      if cn >= k:
        if bisect_left(ans,i+j+l) > 0:
          insort(ans,i+j+l)
        else:
          stop = max(stop,i+j+l)
      else:
        ans.append(i+j+l)
        cn += 1
        
ans = ans[::-1]
for i in ans[:k]:
  print(i)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import defaultdict


def main():
    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)
    que = [(- A[0] - B[0] - C[0], 0, 0, 0)]  # multiply -1 to change min to max
    already_inserted = defaultdict(int)
    answer = list()
    for _ in range(K):
        q, i, j, k = heappop(que)
        answer.append(-q)
        if i + 1 < X:
            item_a = (- A[i + 1] - B[j] - C[k], i + 1, j, k)
            if already_inserted[item_a] == 0:
                heappush(que, item_a)
        if j + 1 < Y:
            item_b = (- A[i] - B[j + 1] - C[k], i, j + 1, k)
            if already_inserted[item_b] == 0:
                heappush(que, item_b)
        if k + 1 < Z:
            item_c = (- A[i] - B[j] - C[k + 1], i, j, k + 1)
            if already_inserted[item_c] == 0:
                heappush(que, item_c)
    for ans in answer:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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)
#print(ab)
ans = []
for i in range(min(k, x*y)):
    for j in range(z):
        ans.append(ab[i] + c[j])
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
import math
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)
l=[]
stat=int(math.sqrt(3*(10**6)//X))
for i in range(X):
    for j in range(min(stat,Y)):
        for k in range(min(stat,Z)):
            l.append(A[i] + B[j] + C[k])
end1=stat
stat2=int(math.sqrt(3*(10**6)//Y))
for i in range(min(stat,X),min(stat+stat2,X)):
    for j in range(Y):
        for k in range(min(stat,Z),min(stat+stat2,Z)):
            l.append(A[i] + B[j] + C[k])
end2=stat+stat2
stat3=int(math.sqrt(3*(10**6)//Z))
for i in range(min(stat+stat2,X),min(stat+stat2+stat3,X)):
    for j in range(min(stat+stat2,Y),min(stat+stat2+stat3,Y)):
        for k in range(Z):
            l.append(A[i] + B[j] + C[k])

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

Submission status: Wrong Answer

Solution code:
import math
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)
l=[]
stat=int(math.sqrt(10**6//X))
for i in range(X):
    for j in range(min(stat,Y)):
        for k in range(min(stat,Z)):
            l.append(A[i] + B[j] + C[k])
end1=stat
stat2=int(math.sqrt(10**6//Y))
for i in range(min(stat,X),min(stat+stat2,X)):
    for j in range(Y):
        for k in range(min(stat,Z),min(stat+stat2,Z)):
            l.append(A[i] + B[j] + C[k])
end2=stat+stat2
stat3=int(math.sqrt(10**6//Z))
for i in range(min(stat+stat2,X),min(stat+stat2+stat3,X)):
    for j in range(min(stat+stat2,Y),min(stat+stat2+stat3,Y)):
        for k in range(Z):
            l.append(A[i] + B[j] + C[k])

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

Submission status: Wrong Answer

Solution code:
# D - Cake 123

import heapq
import itertools
from collections import defaultdict

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

A = list(int(a) for a in input().split())
B = list(int(b) for b in input().split())
C = list(int(c) for c in input().split())

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

hq = []
hqAns = []
d = defaultdict(int)

heapq.heappush(hq, ((A[0]+B[0]+C[0])*-1,(0,0,0)))
d[(0,0,0)] = 1
print(d)
while len(hqAns)<k :
    val, key = heapq.heappop(hq)
    heapq.heappush(hqAns, val)
 
    if d[(key[0]+1, key[1], key[2])]==0 and key[0]+1 < x:
        heapq.heappush(hq, ((A[key[0]+1]+B[key[1]]+C[key[2]])*-1, ((key[0]+1, key[1], key[2]))))
        d[(key[0]+1, key[1], key[2])] = 1

    if d[(key[0], key[1]+1, key[2])]==0 and key[1]+1 < y:
        heapq.heappush(hq, ((A[key[0]]+B[key[1]+1]+C[key[2]])*-1, ((key[0], key[1]+1, key[2]))))
        d[(key[0], key[1]+1, key[2])] = 1
 
    if d[(key[0], key[1], key[2]+1)]==0 and key[2]+1 < z:
        heapq.heappush(hq, ((A[key[0]]+B[key[1]]+C[key[2]+1])*-1, ((key[0], key[1], key[2]+1))))
        d[(key[0], key[1], key[2]+1)] = 1

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

'''
# ans.2 #

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

hq = []
for i in range(x):
    for j in range(y):
        for l in range(z):
            if (i+1)*(j+1)*(l+1)<=k:
                heapq.heappush(hq, (A[i]+B[j]+C[l])*-1)
            else:
                break

for _ in range(k):
    print(heapq.heappop(hq)*-1)

'''

'''

# ans.1 #

hq = []
for i, j in itertools.product(A, B):
    heapq.heappush(hq, (i+j)*-1)

AB = []
while len(hq)>0 and len(AB)<=k:
    AB.append(heapq.heappop(hq)*-1)

hq = []
for i, j in itertools.product(AB, C):
    heapq.heappush(hq, (i+j)*-1)

for _ in range(k):
    print(heapq.heappop(hq)*-1)

'''

Submission status: Wrong Answer

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

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

    la = [0]
    lb = [0]
    lc = [0]

    while len(la)*len(lb)*len(lc)<=3000:
        if a[0]<=b[0]<=c[0]:
            tmp = heapq.heappop(a)
            heapq.heappush(a, 0)
            la.append(tmp*-1)
        elif a[0]>b[0]<=c[0]:
            tmp = heapq.heappop(b)
            heapq.heappush(b, 0)
            lb.append(tmp*-1)
        else:
            tmp = heapq.heappop(c)
            heapq.heappush(c, 0)
            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: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

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

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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

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

ans = []
for a in A:
    j = len(C) - 1
    for i in B:

        while i + C[j] <= l - a:
            j -= 1
            if j < 0: break
        else:
            for k in range(j+1):
                ans.append(a+i+C[k])
            continue
        break

ans.sort(reverse=True)
for _ in range(K-len(ans)):
    ans.append(l)
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

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

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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

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

ans = []
cnt = 0
for a in A:
    j = len(C) - 1
    for i in B:

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

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

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

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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

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

ans = []
cnt = 0
for a in A:
    j = len(C) - 1
    last_cnt = cnt
    for i in B:

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

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

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

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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

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

ans = []
cnt = 0
for a in A:
    j = len(C) - 1
    last_cnt = cnt
    for i in B:

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

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

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

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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

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

ans = []
cnt = 0
for a in A:
    j = len(C) - 1
    last_cnt = cnt
    for i in B:

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

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

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

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

h = A[0] + B[0] + C[0]
l = 0
while h > l+1:
    m = (h+l) // 2

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

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

Submission status: Wrong Answer

Solution code:
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)
l=[]
for i in range(x):
  if i+1>k:
    break
  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
      l.append(a[i]+b[j]+c[k])
l.sort(reverse=True)
for i in l[:k]:
  print(i)

Submission status: Wrong Answer

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()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
Q=[[-A[0]-B[0]-C[0],0,0,0]]
heapq.heapify(Q)
ans=[A[0]+B[0]+C[0]]
ijk={(0,0,0)}
while len(ans)<=K:
  abc,i,j,k=heapq.heappop(Q)
  if i+1<X and  not (i+1,j,k) in ijk:
    ijk.add((i+1,j,k))
    heapq.heappush(Q,[-A[i+1]-B[j]-C[k],i+1,j,k])
  if j+1<Y and  not (i,j+1,k) in ijk:
    ijk.add((i,j+1,k))
    heapq.heappush(Q,[-A[i]-B[j+1]-C[k],i,j+1,k])
  if k+1<Z and  not (i,j,k+1) in ijk:
    ijk.add((i,j,k+1))
    heapq.heappush(Q,[-A[i]-B[j]-C[k+1],i,j,k+1])
  ans.append(-abc)
for i in ans:
  print(i)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
import sys
input=sys.stdin.readline
x,y,z,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
bc=[]
for i in b:
  for j in c:
    bc.append(i+j)
bc.sort()
yz=y*z
ng=10**12
ok=1
while ng-ok>1:
  mid=(ok+ng)//2
  cnt=0
  for j in a:
    cnt+=yz-bisect_left(bc,mid-j)
  #print(cnt)
  if cnt>=k:
    ok=mid
  else:
    ng=mid
ans=[]
#print(ok)
for i in a:
  idx=bisect_left(bc,ok-i)
  for j in range(idx,yz):
    ans.append(i+bc[j])
ans.sort(reverse=True)
#print(len(ans))
for i in ans:
  print(i)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
from bisect import bisect_left, bisect_right
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# AとBは全探索、その後二分探索
X, Y, Z, K = lr()
A = lr(); A.sort()
B = lr(); B.sort()
C = lr(); C.sort()
D = []
for a, b in itertools.product(A, B):
    D.append(a+b)
D.sort()
print(D)
def check(x):
    length = len(D)
    count = 0
    for y in C:
        i = bisect_left(D, x-y)
        count += length - i
    return count >= K

ok = 0; ng = 10 ** 11
while abs(ng-ok) > 1:
    mid = (ok+ng) // 2
    if check(mid):
        ok = mid
    else:
        ng = mid

# ok以上の値
answer = []
for y in C:
    index = bisect_left(D, ok-y)
    for i in range(index, len(D)):
        answer.append(D[i]+y)
answer.sort(reverse=True)
for i in range(K):
    print(answer[i])

# 02

Submission status: Wrong Answer

Solution code:
import heapq
x, y, z, k = map(int, input().split())

abc = []
heapq.heapify(abc)
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, t, u = 0,0,0
abc = [[-a[0], 0], [-b[0], 1], [-c[0], 2]]
heapq.heapify(abc)
while len(abc)>0 and s*t*u<3000:
    n, m = heapq.heappop(abc)
    if m == 0:
        s += 1
        if s < x:
            heapq.heappush(abc, [-a[s], 0])
    elif m == 1:
        t += 1
        if t < y:
            heapq.heappush(abc, [-b[t], 1])
    else:
        u += 1
        if u < z:
            heapq.heappush(abc, [-c[u], 2])
ABC = []
heapq.heapify(ABC)
for i in range(s):
    for j in range(t):
        for ij in range(u):
            heapq.heappush(ABC, -a[i]-b[j]-c[ij])
for _ in range(k):
    print(-heapq.heappop(ABC))

Submission status: Wrong Answer

Solution code:
import heapq

def main():
  X,Y,Z,K=map(int,input().split())
  A=list(reversed(sorted(map(int,input().split()))))
  B=list(reversed(sorted(map(int,input().split()))))
  C=list(reversed(sorted(map(int,input().split()))))
  q=[(-A[0]-B[0]-C[0],0,0,0)]
  queued={(0,0,0)}
  for i in range(K):
    v,s,t,u=heapq.heappop(q)
    print(-v)
    for e,f,g in [(s,t,u+1),(s,t+1,u),(s+1,t,u)]:
      if e>=X or f>=Y or g>=Z:
        continue
      if (e,f,g) in queued:
        continue
      heapq.heappush(q,(-(-A[e]+B[f]+C[g]),e,f,g))
      queued.add((e,f,g))
      
if __name__=="__main__":
  main()