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:
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc123_d
import itertools
import math

# input
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 = range(1000)
# B = range(1000)
# C = range(1000)
# getans
arr = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if i*j*k > K:
                break
            arr.append(A[i]+B[j]+C[k])
arr = sorted(arr, reverse=True)[:K]
# output
ans = '\n'.join(map(str, arr))
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
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)

h = []
heapq.heappush(h, (A[0] + B[0] + C[0], 0, 0, 0))

ans = []
for _ in range(K):
    p, i, j, k = heapq.heappop(h)
    ans.append(p)
    if i + 1 < X and j < Y and k < Z and not (A[i+1] + B[j] + C[k], i + 1, j, k) in h:
        heapq.heappush(h, (A[i + 1] + B[j] + C[k], i + 1, j, k))
    if i < X and j + 1 < Y and k < Z and not (A[i] + B[j+1] + C[k], i, j+1, k) in h:
        heapq.heappush(h, (A[i] + B[j+1] + C[k], i, j+1, k))
    if i < X and j < Y and k + 1 < Z and not (A[i] + B[j] + C[k+1], i, j, k+1) in h:
        heapq.heappush(h, (A[i] + B[j] + C[k+1], i, j, k+1))

for a in ans:
    print(a)

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 =[]
ABC = []
for i in range(X):
    for j in range(Y):
        ab = A[i] + B[j]
        AB.append(ab)
AB.sort(reverse = True)
m = min(K, X*Y)
if m == K:
    for i in AB:
        ABC.append(i + C[0])
elif m == X*Y:
    for i in range(m):
         for j in range(Z):
            abc = AB[i] + C[j]
            ABC.append(abc)
ABC.sort(reverse = True)
for i in range(K):
    print(ABC[i])

Submission status: Wrong Answer

Solution code:
X,Y,Z,K = map(int,input().split())
A = map(int,input().split())
B = map(int,input().split())
C = map(int,input().split())
AB = []
for a in A:
    for b in B:
        AB.append(a+b)
AB.sort(reverse=True)
ABC = []
for i in range(min(K,len(AB))):
    for c in C:
        ABC.append(AB[i]+c)
ABC.sort(reverse=True)
for i in range(min(K,len(ABC))):
    print(ABC[i])

Submission status: Wrong Answer

Solution code:
# 全探索に終了条件を追加することで計算量を削減させるやり方
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


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

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

ABC = []
from itertools import product
for a, b, c in product(range(X), range(Y), range(Z)):
    # a,b,cはA,B,Cの上から何番目に大きいか
    if (a + 1) * (b + 1) * (c + 1) > K:
        break
    ABC.append(A[a] + B[b] + C[c])


ABC.sort(reverse=True)
ABC = ABC[:K]
print(*ABC, sep='\n')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

def append2(m, cand_a, cand_b, cand_c):
    key = "{}-{}-{}".format(cand_a, cand_b, cand_c)
    key2 = "{}-{}-{}".format(cand_a-1, cand_b, cand_c)
    key3 = "{}-{}-{}".format(cand_a, cand_b-1, cand_c)
    key4 = "{}-{}-{}".format(cand_a, cand_b, cand_c-1)
    if not key in m \
            and cand_a < len(A) \
            and cand_b < len(B) \
            and cand_c < len(C) \
            and not key2 in m \
            and not key3 in m \
            and not key4 in m :
#       print("Appended {}".format(key))
        m[key] = A[cand_a] + B[cand_b] + C[cand_c]
    return m

def append(keys_of_max_val, m):
#   print(keys_of_max_val)
    for k in keys_of_max_val:
        a, b, c = map(int, k.split("-"))
        m = append2(m, a+1, b, c)
        m = append2(m, a, b+1, c)
        m = append2(m, a, b, c+1)
    return m

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)

a = 0
b = 0
c = 0

n_print = 0
#print(A, B, C)
memo = {}
max_k = "{}-{}-{}".format(a, b, c)
memo[max_k] = A[a] + B[b] + C[c]
keys_of_max_val = [max_k]
while True:
    summ = memo[max_k]
    print(summ)

    memo.pop(max_k)
#   print(memo)
#   print("Pop {}".format(max_k))

#   print(memo.items())
#   print(a, b, c)
#   print("")

    n_print += 1
    if n_print == K:
        break

    memo = append(keys_of_max_val, memo)
#   print(memo)
#   print("")

#   max_k = max(memo, key=memo.get)
#   a, b, c = map(int, max_k.split("-"))
#   memo.pop(max_k)
    
    max_val = max(memo.values())
    keys_of_max_val = [key for key in memo if memo[key] == max_val]
#   print("keys_of_max_val: {}".format(keys_of_max_val))
#   print(memo)
    max_k = keys_of_max_val[0]
    a, b, c = map(int, max_k.split("-"))

Submission status: Wrong Answer

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

	solve(a, b, c, x, y, z, k)

def solve(a, b, c, x, y, z, k):
	stack = list()
	for p in range(x):
		for q in range(y):
			if (p+1)*(q+1) > k:
				break
			for r in range(z):
				if (p+1)*(q+1)*(r+1) > k:
					break
				stack.append(a[p]+b[q]+c[r])
	stack.sort(reverse=True)
	for i in range(k):
		print(stack[i])

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


C.sort()
C.reverse()
AB =[0]*(X*Y)

for i in range(X):
    for j in range(Y):
        AB[i*Y+j] =  A[i] + B[j]
        
AB.sort()
AB.reverse()
K2 = min(K,X*Y)
AB = AB[0:K2]
ABC = [0] * (K2*Z)
for i in range(K2):
    for j in range(Z):
        ABC[i*Z+j] = AB[i] + C[j]
        
ABC.sort()
ABC = ABC[0:K]
for i in reversed(range(K)):
    print(ABC[i])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

def append(m, cand_a, cand_b, cand_c):
    key = "{}-{}-{}".format(cand_a, cand_b, cand_c)
    key2 = "{}-{}-{}".format(cand_a-1, cand_b, cand_c)
    key3 = "{}-{}-{}".format(cand_a, cand_b-1, cand_c)
    key4 = "{}-{}-{}".format(cand_a, cand_b, cand_c-1)
    if not key in m \
            and cand_a < len(A) \
            and cand_b < len(B) \
            and cand_c < len(C) \
            and not key2 in m \
            and not key3 in m \
            and not key4 in m :
#       print("Appended {}".format(key))
        m[key] = A[cand_a] + B[cand_b] + C[cand_c]
    return m

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)

a = 0
b = 0
c = 0

n_print = 0
#print(A, B, C)
memo = {}
printed = defaultdict(lambda: False)
while True:
    summ = A[a] + B[b] + C[c]
    printed["{}-{}-{}".format(a, b, c)] = True

    print(summ)

#   print(memo.items())
#   print(a, b, c)
#   print("")

    n_print += 1
    if n_print == K:
        break

    memo = append(memo, a+1, b, c)
    memo = append(memo, a, b+1, c)
    memo = append(memo, a, b, c+1)

    max_k = max(memo, key=memo.get)
    a, b, c = max_k.split("-")
    a = int(a)
    b = int(b)
    c = int(c)
        
    memo.pop(max_k)
#   print("Pop {}".format(max_k))

Submission status: Wrong Answer

Solution code:
from heapq import *

an, bn, cn, 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)

ans = []
hp = [[-(a[0] + b[0] + c[0]), 0, 0, 0]]
heapify(hp)
sumi = {(0, 0, 0)}
for _ in range(k):
    hpk = heappop(hp)
    mx = hpk[0]
    ai = hpk[1]
    bi = hpk[2]
    ci = hpk[3]
    if ai + 1 < an and (ai + 1, bi, ci) not in sumi:
        heappush(hp, [mx + a[ai] - a[ai + 1], ai + 1, bi, ci])
        sumi.add((ai + 1, bi, ci))
    if bi + 1 < bn and (ai, bi + 1, ci) not in sumi:
        heappush(hp, [mx + b[bi] - b[bi + 1], ai, bi + 1, ci])
        sumi.add((ai, bi + 1, ci))
    if ci + 1 < cn and (ai, bi, ci + 1) not in sumi:
        heappush(hp, [mx + c[ci] - c[ci + 1], ai, bi, ci + 1])
        sumi.add((ai, bi, ci + 1))
    ans += [-mx]
print(type(sumi))
print(*ans, sep="\n")

Submission status: Wrong Answer

Solution code:
X,Y,Z,K = list(map(int,input().split()))
A = [(sorted(list(map(int,input().split())))[::-1]) for i in range(3)]


def max_K_product(A,B,K):
    Q = []
    ret = []
    consider_dict = {(0,0):A[0]+B[0]}
    considered_lst =[]
    while len(ret)<K and len(considered_lst) != len(A)*len(B):
        m = max(consider_dict.values())
        
        # 探索する座標をリストに入れる
        max_list = [i[0]   for i in consider_dict.items() if i[1]==m ]
        
        # 下右を探索する
        for q in max_list:
            if  q[0]+1 < len(A) and (q[0]+1,q[1]) not in considered_lst:
                consider_dict[(q[0]+1,q[1])] = A[q[0]+1] + B[q[1]] 
            if q[1]+1 < len(B) and (q[0],q[1]+1) not in considered_lst:
                consider_dict[(q[0],q[1]+1)] = A[q[0]] + B[q[1]+1]
              
            considered_lst.append(q)
        
        #print(considered_lst)
        
        ret += [max(consider_dict.values())]*list(consider_dict.values()).count(max(consider_dict.values()))
        for q in max_list:
            consider_dict.pop(q)
                              
    return ret
  
for idx,item in enumerate(max_K_product(max_K_product(A[0],A[1],K),A[2],K)):
    if idx<K:
        print(item)
    else:
        break

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import functools
import operator
import itertools
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)

a = 0
b = 0
c = 0

n_print = 0
#print(A, B, C)
memo = defaultdict(lambda: 0)
while True:
    summ = A[a] + B[b] + C[c]
#   print(memo)
#   print(a, b, c)
    print(summ)
#   print("")
    n_print += 1
    if n_print == K:
        break

    if memo["{}-{}-{}".format(a+1, b, c)] == 0 and a+1 < len(A):
        memo["{}-{}-{}".format(a+1, b, c)] = A[a+1] + B[b] + C[c]
    if memo["{}-{}-{}".format(a, b+1, c)] == 0 and b+1 < len(B):
        memo["{}-{}-{}".format(a, b+1, c)] = A[a] + B[b+1] + C[c]
    if memo["{}-{}-{}".format(a, b, c+1)] == 0 and c+1 < len(C):
        memo["{}-{}-{}".format(a, b, c+1)] = A[a] + B[b] + C[c+1]

    max_k = max(memo, key=memo.get)
    a, b, c = map(int, max_k.split("-"))
    memo[max_k] = 0

Submission status: Wrong Answer

Solution code:
X,Y,Z,K = list(map(int,input().split()))
A = [(sorted(list(map(int,input().split())))[::-1]) for i in range(3)]


def max_K_product(A,B,K):
    Q = []
    ret = []
    consider_dict = {(0,0):A[0]+B[0]}
    considered_lst =[]
    while len(ret)<K and len(considered_lst) != len(A)*len(B):
        m = max(consider_dict.values())
        
        # 探索する座標をリストに入れる
        max_list = [i[0]   for i in consider_dict.items() if i[1]==m ]
        
        # 下右を探索する
        for q in max_list:
            if  q[0]+1 < len(A) and (q[0]+1,q[1]) not in considered_lst:
                consider_dict[(q[0]+1,q[1])] = A[q[0]+1] + B[q[1]] 
            if q[1]+1 < len(B) and (q[0],q[1]+1) not in considered_lst:
                consider_dict[(q[0],q[1]+1)] = A[q[0]] + B[q[1]+1]
              
            considered_lst.append(q)
        
        #print(considered_lst)
        
        ret += [max(consider_dict.values())]*list(consider_dict.values()).count(max(consider_dict.values()))
        for q in max_list:
            consider_dict.pop(q)
                              
    return ret
  
for i in max_K_product(max_K_product(A[0],A[1],K),A[2],K):
    print(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=[s+t for s in a for t in b]
ab.sort()
ab=ab[-1:-k-1:-1]
print(ab)
abc=[s+t for s in ab for t in c]
abc.sort()
abc=abc[-1:-k-1:-1]
for i in range(len(abc)):
    print(abc[i])

Submission status: Wrong Answer

Solution code:
lst = list(map(int, input().split()))
a_lst = list(map(int, input().split()))
b_lst = list(map(int, input().split()))
c_lst = list(map(int, input().split()))

k = lst[3]
candidate = []
for n, a in enumerate(sorted(a_lst, key = lambda x: -x)):
    for m, b in enumerate(sorted(b_lst, key = lambda x: -x)):
        for l, c in enumerate(sorted(c_lst, key = lambda x: -x)):
            if (n+1)*(m+1)*(l+1) <= k:
                candidate.append(a+b+c)
for i in sorted(candidate, key = lambda x: -x):
    print(i)

Submission status: Wrong Answer

Solution code:

def main(m):
    x, y, z, k = list(map(int, m[0].split(' ')))
    a = list(reversed(sorted(list(map(int, m[1].split(' '))))))
    b = list(reversed(sorted(list(map(int, m[2].split(' '))))))
    c = list(reversed(sorted(list(map(int, m[3].split(' '))))))
    l = []
    
    for i in range(x):
        for j in range(y):
            l.append(a[i] + b[j])

    n = list(sorted(l))[0: min(x * y, k)]
    h = []

    for i in range(len(n)):
        for j in range(z):
            h.append(n[i] + c[j])
    
    sl = list(reversed(sorted(h)))
    for i in range(k):
        print(sl[i])
                

if __name__ == '__main__':
    try:
        m = []
        for i in range(4):
            m.append(input().strip())
        main(m)
    except EOFError:
        pass

Submission status: Wrong Answer

Solution code:
x,y,z,q=map(int,input().split())
a=sorted(map(int,input().split()),reverse=True)
b=sorted(map(int,input().split()),reverse=True)
c=sorted(map(int,input().split()),reverse=True)
l=[]
for i in range(x):
  for j in range(y):
    if i*j>q:
      break
    for k in range(z):
      if i*j*k>q:
        break
      l+=[a[i]+b[j]+c[k]]
l.sort(reverse=True)
print(w for w in l[:q])

Submission status: Wrong Answer

Solution code:
x,y,z,k=map(int,input().split())
a,b,c=[list(map(int,input().split())) for i in range(3)]
l=[i+j for i in a for j in b]
l.sort(reverse=True)
maxl=[i+j for i in l[:k] for j in c]
maxl.sort(reverse=True)
for a in maxl:
    print(a)

Submission status: Wrong Answer

Solution code:
x,y,z,q=map(int,input().split())
a=sorted(map(int,input().split()),reverse=True)
b=sorted(map(int,input().split()),reverse=True)
c=sorted(map(int,input().split()),reverse=True)
 
def bisect(l,r):
  cnt=(l+r)//2
  if cnt==l:
    return l-1+judge(l)
  if judge(cnt):
    return bisect(cnt+1,r)
  else:
    return bisect(l,cnt)
def judge(thr):
  m=0
  for i in range(x):
    for j in range(y):
      if i*j>q:
        break
      for k in range(z):
        if i*j*k>q or a[i]+b[j]+c[k]<thr:
          break
        m+=1
        if m>=q:
          return True
  return False
def make(thr):
  m=0
  l=[]
  for i in a:
    for j in b:
      for k in c:
        n=i+j+k
        if n<thr:
          break
        m+=1
        l+=[n]
        if m>=q:
          l.sort(reverse=True)
          return l
thr=bisect(0,a[0]+b[0]+c[0]+1)
ans=make(thr)
for w in ans:
  print(w)

Submission status: Wrong Answer

Solution code:
x,y,z,q=map(int,input().split())
a=sorted(map(int,input().split()),reverse=True)
b=sorted(map(int,input().split()),reverse=True)
c=sorted(map(int,input().split()),reverse=True)

def make():
  l=[]
  for i in a:
    for j in b:
      if i*j>q:
        break
      for k in c:
        if i*j*k>q:
          break
        l+=[i+j+k]
  l.sort(reverse=True)
  return l[:q]

ans=make()
for w in ans:
  print(w)

Submission status: Wrong Answer

Solution code:
x,y,z,q=map(int,input().split())
a=sorted(map(int,input().split()),reverse=True)
b=sorted(map(int,input().split()),reverse=True)
c=sorted(map(int,input().split()),reverse=True)
 
def bisect(l,r):
  cnt=(l+r)//2
  if cnt==l:
    return l-1+judge(l)
  if judge(cnt):
    return bisect(cnt+1,r)
  else:
    return bisect(l,cnt)
def judge(thr):
  m=0
  for i in a:
    for j in b:
      if i*j>q:
        break
      for k in c:
        if i+j+k<thr:
          break
        m+=1
        if m>=q:
          return True
  return False
def make(thr):
  m=0
  l=[]
  for i in a:
    for j in b:
      for k in c:
        n=i+j+k
        if n<thr+1:
          break
        m+=1
        l+=[n]
  l.sort(reverse=True)
  return l+[thr]*(q-len(l))
thr=bisect(0,a[0]+b[0]+c[0]+1)
ans=make(thr)
for w in ans:
  print(w)

Submission status: Wrong Answer

Solution code:
x,y,z,q=map(int,input().split())
a=sorted(map(int,input().split()),reverse=True)
b=sorted(map(int,input().split()),reverse=True)
c=sorted(map(int,input().split()),reverse=True)
def bisect(l,r):
  cnt=(l+r)//2
  if cnt==l:
    return l-1+judge(l)
  if judge(cnt):
    return bisect(cnt+1,r)
  else:
    return bisect(l,cnt)
def judge(thr):
  m=0
  for i in a:
    for j in b:
      for k in c:
        if i+j+k>thr:
          m+=1
          if m>=q:
            return True
        else:
          break
  return False
def make(thr):
  m=0
  l=[]
  for i in a:
    for j in b:
      for k in c:
        n=i+j+k
        if n>thr:
          m+=1
          l+=[n]
          if m>=q:
            l.sort(reverse=True)
            return l
        else:
          break
thr=bisect(0,a[0]+b[0]+c[0])
ans=make(thr)
for w in ans:
  print(w)

Submission status: Wrong Answer

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

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

AB = []
for i in range(X):
    for j in range(Y):
        if (i+1) * (j+1) > K: continue
        AB.append(A[i] + B[j])
AB.sort()

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

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

ans = []
for c in C:
    ans += [ab + c for ab in AB[bisect_left(AB, r - c):]]
ans.sort(reverse = True)
for a in ans:
    print(a)
for _ in range(K - len(ans)):
    print(l)

Submission status: Wrong Answer

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

A.sort()
B.sort()
C.sort()

AB = []
for i in range(X):
    for j in range(Y):
        if (i+1) * (j+1) > K: continue
        AB.append(A[i] + B[j])
AB.sort()

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

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

ans = []
for c in C:
    ans += [ab + c for ab in AB[bisect_left(AB, r - c):]]
ans.sort(reverse = True)
for a in ans:
    print(a)
for _ in range(K - len(ans)):
    print(l)

Submission status: Wrong Answer

Solution code:
x,y,z,q=map(int,input().split())
a=sorted(map(int,input().split()),reverse=True)
b=sorted(map(int,input().split()),reverse=True)
c=sorted(map(int,input().split()),reverse=True)

def bisect(l,r):
  cnt=(l+r)//2
  if cnt==l:
    return l-1+judge(l)
  if judge(cnt):
    return bisect(cnt+1,r)
  else:
    return bisect(l,cnt)
def judge(thr):
  m=0
  for i in a:
    for j in b:
      for k in c:
        if i+j+k>thr:
          m+=1
          if m>=q:
            return True
        else:
          break
  return False
def make(thr):
  m=0
  l=[]
  for i in a:
    for j in b:
      for k in c:
        n=i+j+k
        if n>thr:
          m+=1
          l+=[n]
          if m>=q:
            l.sort(reverse=True)
            return l
        else:
          break

thr=bisect(0,a[0]+b[0]+c[0]+1)
ans=make(thr)
for w in ans:
  print(w)

Submission status: Wrong Answer

Solution code:
xyzk_, *nums_ = [input() for _ in range(4)]

*le, k = [int(w) for w in xyzk_.split()]
abc = [sorted((int(n) for n in w.split()), reverse=True) for w in nums_]

p = [0, 0, 0]
pool = [0, sum(abc[i][p[0]] for i in range(3))]
for _ in range(k):
    max_pool = max(pool)
    t = [abc[i][min(p[i] + 1, le[i] - 1)] + abc[(i + 1) % 3]
         [0] + abc[(i + 2) % 3][0] for i in range(3)]
    max_t = max(t)
    max_3 = t.index(max_t)
    if max_pool < max_t:
        p[max_3] = min(p[max_3] + 1, le[max_3] - 1)
        j, k = (max_3 + 1) % 3, (max_3 + 2) % 3
        for n1 in abc[j][:p[j] + 1]:
            for n2 in abc[k][:p[k] + 1]:
                pool.append(n1 + n2 + abc[max_3]
                            [p[max_3]])
    print(pool.pop(pool.index(max(pool))))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

X, Y, Z, K = map(int, input().split(" "))
A = list(map(int, input().split(" ")))
B = list(map(int, input().split(" ")))
C = list(map(int, input().split(" ")))

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

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

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

ans = []
for c in C:
    ans += [ab + c for ab in AB[bisect_left(AB, r - c):]]
ans.sort(reverse = True)
for a in ans:
    print(a)
for _ in range(K - len(ans)):
    print(l)

Submission status: Wrong Answer

Solution code:
x,y,z,q=map(int,input().split())
*a,=map(int,input().split())
*b,=map(int,input().split())
*c,=map(int,input().split())
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
 
class bisect:
  def bisect(self,l,r):
    cnt=(l+r)//2
    if cnt==l:
      return l-1+self.judge(l)
    if self.judge(cnt):
      return self.bisect(cnt+1,r)
    else:
      return self.bisect(l,cnt)
  def judge(self,thr):
    m=0
    for i in a:
      for j in b:
        for k in c:
          if i+j+k>thr:
            m+=1
            if m>=q:
              return True
          else:
            break
    return False

def make(thr):
  m=0
  l=[]
  for i in a:
    for j in b:
      for k in c:
        n=i+j+k
        if n>thr:
          m+=1
          l+=[n]
          if m>=q:
            l.sort(reverse=True)
            return l
        else:
          break

s=bisect()
thr=s.bisect(0,a[0]+b[0]+c[0]+1)
ans=make(thr)
for w in ans:
  print(w)

Submission status: Wrong Answer

Solution code:
import numpy as np
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()))
a = np.array(a)
b = np.array(b)
c = np.array(c)
bi,ci = np.meshgrid(b,c)
bc = (bi + ci).flatten()
bc = np.sort(bc)

lenbc = len(bc)
lo = 0
hi = bc[-1] + a[-1] + 1
while abs(lo-hi) > 1:
    mid = (lo+hi) // 2
    r = 0
    for v in a:
        r += lenbc - np.searchsorted(bc,mid-v)
    if r >= k:
        lo = mid
    else:
        hi = mid

r = []
for v in a:
    insid = np.searchsorted(bc,lo-v)
    for vb in range(insid, lenbc):
        r.append(v + bc[vb])
r = sorted(r, reverse=True)
for i in r: print(i)

Submission status: Wrong Answer

Solution code:
import numpy as np
import itertools

X, Y, Z, K = tuple([int(x) for x in str(input()).split(' ')])

A = np.array(sorted([int(x) for x in str(input()).split(' ')], reverse=True))
B = np.array(sorted([int(x) for x in str(input()).split(' ')], reverse=True))
C = np.array(sorted([int(x) for x in str(input()).split(' ')], reverse=True))

tmp = {(0, 0, 0) : A[0] + B[0] + C[0]}
out = []

for i in range(K):
    (a, b, c),  p = max(tmp.items(), key=lambda x : x[1])
    out.append(tmp.pop((a, b, c)))

    keys = []
    if a < X - 1:
        keys.append((a+1, b, c))
    if b < Y - 1:
        keys.append((a, b+1, c))
    if c < Z - 1:
        keys.append((a, b, c+1))
    
    for key in keys:
        p = A[key[0]] + B[key[1]] + C[key[2]]
        tmp[key] = p

for x in out:
    print(x)

Submission status: Wrong Answer

Solution code:
xyzk = list(map(int, input().split()))
x = xyzk[0]
y = xyzk[1]
z = xyzk[2]
k = xyzk[3]

a = list(map(int, input().split()))
a.sort(reverse=True)
b = list(map(int, input().split()))
b.sort(reverse=True)
c = list(map(int, input().split()))
c.sort(reverse=True)

queue = []

queue.append([a[0] + b[0] + c[0], 0, 0, 0])
for i in range(k):
    maximum = max(queue)
    print(maximum[0])
    aa = maximum[1]
    bb = maximum[2]
    cc = maximum[3]
    queue.remove(maximum)
    FlagA = False
    FlagB = False
    FlagC = False
    for j in range(len(queue)):
        if queue[j][1] == aa + 1 and queue[j][2] == bb and queue[j][3] == cc:
            FlagA = True
        elif queue[j][1] == aa and queue[j][2] == bb + 1 and queue[j][3] == cc:
            FlagB = True
        elif queue[j][1] == aa and queue[j][2] == bb and queue[j][3] == cc + 1:
            FlagC = True

    if not FlagA and len(a) > aa + 1:
        queue.append([a[aa + 1] + b[bb] + c[cc], aa + 1, bb, cc])
    if not FlagB and len(b) > bb + 1:
        queue.append([a[aa] + b[bb + 1] + c[cc], aa, bb + 1, cc])
    if not FlagC and len(c) > cc + 1:
        queue.append([a[aa] + b[bb] + c[cc + 1], aa, bb, cc + 1])

Submission status: Wrong Answer

Solution code:
xyzk = list(map(int, input().split()))
x = xyzk[0]
y = xyzk[1]
z = xyzk[2]
k = xyzk[3]

a = list(map(int, input().split()))
a.sort(reverse=True)
b = list(map(int, input().split()))
b.sort(reverse=True)
c = list(map(int, input().split()))
c.sort(reverse=True)

queue = []

queue.append([a[0] + b[0] + c[0], 0, 0, 0])
for i in range(k):
    print(max(queue)[0])
    aa = max(queue)[1]
    bb = max(queue)[2]
    cc = max(queue)[3]
    queue.remove(max(queue))
    FlagA = False
    FlagB = False
    FlagC = False
    """
    for j in range(len(queue)):
        if queue[j][1] == aa + 1 and queue[j][2] == bb and queue[j][3] == cc:
            FlagA = True
        elif queue[j][1] == aa and queue[j][2] == bb + 1 and queue[j][3] == cc:
            FlagB = True
        elif queue[j][1] == aa and queue[j][2] == bb and queue[j][3] == cc + 1:
            FlagC = True
    """

    if not FlagA and len(a) > aa + 1:
        queue.append([a[aa + 1] + b[bb] + c[cc], aa + 1, bb, cc])
    if not FlagB and len(b) > bb + 1:
        queue.append([a[aa] + b[bb + 1] + c[cc], aa, bb + 1, cc])
    if not FlagC and len(c) > cc + 1:
        queue.append([a[aa] + b[bb] + c[cc + 1], aa, bb, cc + 1])

Submission status: Wrong Answer

Solution code:
xyzk = list(map(int, input().split()))
x = xyzk[0]
y = xyzk[1]
z = xyzk[2]
k = xyzk[3]

a = list(map(int, input().split()))
a.sort(reverse=True)
b = list(map(int, input().split()))
b.sort(reverse=True)
c = list(map(int, input().split()))
c.sort(reverse=True)

queue = []

"""
queue.append([a[0] + b[0] + c[0], 0, 0, 0])
for i in range(k):
    print(max(queue)[0])
    aa = max(queue)[1]
    bb = max(queue)[2]
    cc = max(queue)[3]
    queue.remove(max(queue))
    FlagA = False
    FlagB = False
    FlagC = False
    for j in range(len(queue)):
        if queue[j][1] == aa + 1 and queue[j][2] == bb and queue[j][3] == cc:
            FlagA = True
        elif queue[j][1] == aa and queue[j][2] == bb + 1 and queue[j][3] == cc:
            FlagB = True
        elif queue[j][1] == aa and queue[j][2] == bb and queue[j][3] == cc + 1:
            FlagC = True

    if not FlagA and len(a) > aa + 1:
        queue.append([a[aa + 1] + b[bb] + c[cc], aa + 1, bb, cc])
    if not FlagB and len(b) > bb + 1:
        queue.append([a[aa] + b[bb + 1] + c[cc], aa, bb + 1, cc])
    if not FlagC and len(c) > cc + 1:
        queue.append([a[aa] + b[bb] + c[cc + 1], aa, bb, cc + 1])
"""