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:
from sys import stdin
import math
import numpy as np
# N = int(stdin.readline().rstrip().split()[0])
# A = [int(x) for x in stdin.readline().rstrip().split()]
# N, X = [int(x) for x in stdin.readline().rstrip().split()]
# S = [int(x) for x in stdin.readline().rstrip().split()]
X, Y, Z, K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]
C = [int(x) for x in stdin.readline().rstrip().split()]
K = min([K, X*Y*Z])
A.sort()
B.sort()
C.sort()
A.reverse()
B.reverse()
C.reverse()
A.append(-10**9)
B.append(-10**9)
C.append(-10**9)
A.append(-10)
B.append(-10)
C.append(-10)

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

Submission status: Wrong Answer

Solution code:
# D
x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
a_l = []
a_max = a[0]
for i in range(len(a)):
    a_l.append(a[i] - a_max)
b_l = []
b_max = b[0]
for i in range(len(b)):
    b_l.append(b[i] - b_max)
c_l = []
c_max = c[0]
for i in range(len(c)):
    c_l.append(c[i] - c_max)
a_count = 1
b_count = 1
c_count = 1
while a_count * b_count * c_count < k:
    l = []
    if a_count < len(a):
        l.append(a_l[a_count])
    if b_count < len(b):
        l.append(b_l[b_count])
    if c_count < len(c):
        l.append(c_l[c_count])
    l.sort(reverse=True)
    for m in l:
        if a_count < len(a) and m == a_l[a_count]:
            a_count += 1
            break
        elif b_count < len(b) and m == b_l[b_count]:
            b_count += 1
            break
        elif c_count < len(c) and m == c_l[c_count]:
            c_count += 1
            break
a = a[:a_count+1]
b = b[:b_count+1]
c = c[:c_count+1]

ans = []
for i in a:
    for j in b:
        for l in c:
            ans.append(i+j+l)
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

"""
最大、最大2なら、あとはなにかけあわせても最大しかない
ABCの最大2個なので、プラス残りのlen()を足す。
もし超えたら、その組み合わせで順番にもとめていく
"""
X, Y, Z, K = LI()
A = sorted(LI(), reverse=True)
B = sorted(LI(), reverse=True)
C = sorted(LI(), reverse=True)


#  ABC = A+B+C
#  ABC = sorted(ABC, reverse=True)
#  #  print(ABC)
INF = 10**18
a,b,c = [INF],[INF],[INF]
#  abc = 0
count = 0
while a[-1] > B[0] or a[-1] > C[0] or len(a) <= min(50, X):
    a.append(A[count])
    count += 1
    if count >= len(A):
        break


count = 0
while b[-1] > A[0] or b[-1] > C[0] or len(b) <= min(50, Y):
    b.append(B[count])
    count += 1
    if count >= len(B):
        break

count = 0
while c[-1] > B[0] or c[-1] > A[0] or len(c) <= min(50, Z):
    c.append(C[count])
    count += 1
    if count >= len(C):
        break
a = a[1:]
b = b[1:]
c = c[1:]

#  while a[-1] > b[0] and a[-1] > c[0] and b[-1] > a[0] and b[-1] > c[0] and c[-1] > a[0] and c[-1] > b[0]:

#  while not(len(a) >= min(100, X) and len(b) >= min(100, Y) and len(c) >= min(100, Z)):
#      if A:
#          if ABC[abc]in A:
#              a.append(ABC[abc])
#              A.remove(ABC[abc])
#      if B:
#          if ABC[abc]in B:
#              b.append(ABC[abc])
#              B.remove(ABC[abc])
#      if C:
#          if ABC[abc]in C:
#              c.append(ABC[abc])
#              C.remove(ABC[abc])
#      abc+=1
#  #  for abc in range(min(200, X+Y+Z)):

#  #      if ABC[abc]in A:
#  #          a.append(ABC[abc])
#  #          A.remove(ABC[abc])
#  #      if ABC[abc]in B:
#  #          b.append(ABC[abc])
#  #          B.remove(ABC[abc])
#  #      if ABC[abc]in C:
#  #          c.append(ABC[abc])
#  #          C.remove(ABC[abc])
#  #  print(a,b,c)

points = []
for i in a:
    for j in b:
        for k in c:
            points.append(i+j+k)
#  print(points)


#  #  A = A[:min(250, X)]
#  #  B = B[:min(250, Y)]
#  #  C = C[:min(250, Z)]
#  #  # Aの中で最大の数字足すBの最大、Cの最大は全体の中で最大
#  #  # 3000個大きい順に求めれば良い

#  #  # iが決まれば、j,kのおおきさは分かる
#  #  BC = []
#  #  # 1000*1000 で10**6の計算量
#  #  #  for j in range(len(B)):
#  #  #      for k in range(len(C)):
#  #  #          BC.append(B[j] + C[k])

#  #  points = []
#  #  # 10**3 と 10** 6
#  #  #  for i in range(len(A)):
#  #  #      points += [bc + A[i] for bc in BC]
#  #  for i in range(len(A)):
#  #      #  for bc in BC:
#  #      #      points.append(A[i]+bc)
#  #      for j in range(len(B)):
#  #          for k in range(len(C)):
#  #              points.append(A[i] + B[j] + C[k])
for i in sorted(points, reverse=True)[:K]:
    print(i)

Submission status: Wrong Answer

Solution code:
def classify(a,b,c,T):
    if T[a][1]!=T[b][1] and T[b][1]!=T[c][1] and T[c][1]!=T[a][1]:
        return 1
    else:
        return 0
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()))
T=[]
for i in range(X):
    T.append([A[i],1])
for i in range(Y):
    T.append([B[i],2])
for i in range(Z):
    T.append([C[i],3])
T.sort(key=lambda x:x[0],reverse=True)
i = 0
j=0
a=0
b=1
c=2
ans=[]
while i < 300*K and j<=X*Y*Z:
    j+=1
    if classify(a,b,c,T):
        ans.append(T[a][0]+T[b][0]+T[c][0])
        i+=1
    if (c-b)==1 and c!=(X+Y+Z-1):
        c+=1
        b=a+1
    elif (c-b)==1 and c==(X+Y+Z-1) and a!=(X+Y+Z-3):
        a+=1
        b=a+1
        c=a+2
    else:
        b+=1
ans.sort(reverse=True)
for i in ans[:K]:
    print(i)

Submission status: Wrong Answer

Solution code:
def classify(a,b,c,T):
    if T[a][1]!=T[b][1] and T[b][1]!=T[c][1] and T[c][1]!=T[a][1]:
        return 1
    else:
        return 0
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()))
T=[]
for i in range(X):
    T.append([A[i],1])
for i in range(Y):
    T.append([B[i],2])
for i in range(Z):
    T.append([C[i],3])
T.sort(key=lambda x:x[0],reverse=True)
i = 0
j=0
a=0
b=1
c=2
ans=[]
while i < 1000*K and j<=X*Y*Z:
    j+=1
    if classify(a,b,c,T):
        ans.append(T[a][0]+T[b][0]+T[c][0])
        i+=1
    if (c-b)==1 and c!=(X+Y+Z-1):
        c+=1
        b=a+1
    elif (c-b)==1 and c==(X+Y+Z-1) and a!=(X+Y+Z-3):
        a+=1
        b=a+1
        c=a+2
    else:
        b+=1
ans.sort(reverse=True)
for i in ans[:K]:
    print(i)

Submission status: Wrong Answer

Solution code:
# D
x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
a_l = []
a_max = a[0]
for i in range(len(a)):
    a_l.append(a[i] - a_max)
b_l = []
b_max = b[0]
for i in range(len(b)):
    b_l.append(b[i] - b_max)
c_l = []
c_max = c[0]
for i in range(len(c)):
    c_l.append(c[i] - c_max)
a_count = 0
b_count = 0
c_count = 0
while a_count * b_count * c_count < k:
    l = []
    if a_count < len(a):
        l.append(a_l[a_count])
    if b_count < len(b):
        l.append(b_l[b_count])
    if c_count < len(c):
        l.append(c_l[c_count])
    l.sort(reverse=True)
    for m in l:
        if a_count < len(a) and m == a_l[a_count]:
            a_count += 1
            break
        elif b_count < len(b) and m == b_l[b_count]:
            b_count += 1
            break
        elif c_count < len(c) and m == c_l[c_count]:
            c_count += 1
            break
a = a[:a_count+1]
b = b[:b_count+1]
c = c[:c_count+1]

ans = []
for i in a:
    for j in b:
        for l in c:
            ans.append(i+j+l)
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
import bisect

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)
sum_AB = []
k = 0
for x in range(X):
    for y in range(Y):
        sum_AB.append(A[x]+B[y])
sum_AB.sort(reverse=True) #10**6
sum_count = []
for z in range(Z):
    if z>5:
        break
    for xy in range(X*Y):
        sum_count.append(C[z]+sum_AB[xy])


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

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

"""
最大、最大2なら、あとはなにかけあわせても最大しかない
ABCの最大2個なので、プラス残りのlen()を足す。
もし超えたら、その組み合わせで順番にもとめていく
"""
X, Y, Z, K = LI()
A = sorted(LI(), reverse=True)
B = sorted(LI(), reverse=True)
C = sorted(LI(), reverse=True)


#  ABC = A+B+C
#  ABC = sorted(ABC, reverse=True)
#  #  print(ABC)
INF = 10**18
a,b,c = [INF],[INF],[INF]
#  abc = 0
count = 0
while a[-1] > B[0] or a[-1] > C[0] or len(a) <= min(100, X):
    a.append(A[count])
    count += 1
    if count >= len(A):
        break


count = 0
while b[-1] > A[0] or b[-1] > C[0] or len(b) <= min(100, Y):
    b.append(B[count])
    count += 1
    if count >= len(B):
        break

count = 0
while c[-1] > B[0] or c[-1] > A[0] or len(c) <= min(100, Z):
    c.append(C[count])
    count += 1
    if count >= len(C):
        break
a = a[1:]
b = b[1:]
c = c[1:]

#  while a[-1] > b[0] and a[-1] > c[0] and b[-1] > a[0] and b[-1] > c[0] and c[-1] > a[0] and c[-1] > b[0]:

#  while not(len(a) >= min(100, X) and len(b) >= min(100, Y) and len(c) >= min(100, Z)):
#      if A:
#          if ABC[abc]in A:
#              a.append(ABC[abc])
#              A.remove(ABC[abc])
#      if B:
#          if ABC[abc]in B:
#              b.append(ABC[abc])
#              B.remove(ABC[abc])
#      if C:
#          if ABC[abc]in C:
#              c.append(ABC[abc])
#              C.remove(ABC[abc])
#      abc+=1
#  #  for abc in range(min(200, X+Y+Z)):

#  #      if ABC[abc]in A:
#  #          a.append(ABC[abc])
#  #          A.remove(ABC[abc])
#  #      if ABC[abc]in B:
#  #          b.append(ABC[abc])
#  #          B.remove(ABC[abc])
#  #      if ABC[abc]in C:
#  #          c.append(ABC[abc])
#  #          C.remove(ABC[abc])
#  #  print(a,b,c)

points = []
for i in a:
    for j in b:
        for k in c:
            points.append(i+j+k)
#  print(points)


#  #  A = A[:min(250, X)]
#  #  B = B[:min(250, Y)]
#  #  C = C[:min(250, Z)]
#  #  # Aの中で最大の数字足すBの最大、Cの最大は全体の中で最大
#  #  # 3000個大きい順に求めれば良い

#  #  # iが決まれば、j,kのおおきさは分かる
#  #  BC = []
#  #  # 1000*1000 で10**6の計算量
#  #  #  for j in range(len(B)):
#  #  #      for k in range(len(C)):
#  #  #          BC.append(B[j] + C[k])

#  #  points = []
#  #  # 10**3 と 10** 6
#  #  #  for i in range(len(A)):
#  #  #      points += [bc + A[i] for bc in BC]
#  #  for i in range(len(A)):
#  #      #  for bc in BC:
#  #      #      points.append(A[i]+bc)
#  #      for j in range(len(B)):
#  #          for k in range(len(C)):
#  #              points.append(A[i] + B[j] + C[k])
for i in sorted(points, reverse=True)[:K]:
    print(i)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as ddic
from collections import deque
from itertools import combinations, permutations, product
import bisect
import heapq
import math
import sys

stdout = sys.stdout
rr = lambda: input().strip()
rri = lambda: int(rr())
rrm = lambda: list(map(int, rr().split()))


x, y, z, k = rrm()
tasty1 = rrm()
tasty2 = rrm()
tasty3 = rrm()

ans = []
tasty = tasty1 + tasty2 + tasty3
tasty.sort(reverse=True)

dic1 = {key: True for key in tasty1}
dic2 = {key: True for key in tasty2}
dic3 = {key: True for key in tasty3}

index = 0
count = 0
visited = {}
while index != len(tasty)-1:
    num1 = tasty[index]
    i = index + 1
    j = index + 2
    while i < len(tasty) and j < len(tasty):
        num2 = tasty[i]
        num3 = tasty[j]
        if num1 in dic1 and num2 in dic2 and num3 in dic3:
            if num1 + num2 + num3 not in visited:
                visited[num1 + num2 + num3] = [123]
                ans.append(num1 + num2 + num3)
                count += 1
            else:
                if 123 not in visited[num1 + num2 + num3]:
                    visited[num1 + num2 + num3].append(123)
                    ans.append(num1 + num2 + num3)
                    count += 1
        if num1 in dic1 and num2 in dic3 and num3 in dic2:
            if num1 + num2 + num3 not in visited:
                visited[num1 + num2 + num3] = [132]
                ans.append(num1 + num2 + num3)
                count += 1
            else:
                if 132 not in visited[num1 + num2 + num3]:
                    visited[num1 + num2 + num3].append(132)
                    ans.append(num1 + num2 + num3)
                    count += 1
        if num1 in dic2 and num2 in dic1 and num3 in dic3:
            if num1 + num2 + num3 not in visited:
                visited[num1 + num2 + num3] = [213]
                ans.append(num1 + num2 + num3)
                count += 1
            else:
                if 213 not in visited[num1 + num2 + num3]:
                    visited[num1 + num2 + num3].append(213)
                    ans.append(num1 + num2 + num3)
                    count += 1
        if num1 in dic2 and num2 in dic3 and num3 in dic1:
            if num1 + num2 + num3 not in visited:
                visited[num1 + num2 + num3] = [231]
                ans.append(num1 + num2 + num3)
                count += 1
            else:
                if 231 not in visited[num1 + num2 + num3]:
                    visited[num1 + num2 + num3].append(231)
                    ans.append(num1 + num2 + num3)
                    count += 1
        if num1 in dic3 and num2 in dic1 and num3 in dic2:
            if num1 + num2 + num3 not in visited:
                visited[num1 + num2 + num3] = [312]
                ans.append(num1 + num2 + num3)
                count += 1
            else:
                if 312 not in visited[num1 + num2 + num3]:
                    visited[num1 + num2 + num3].append(312)
                    ans.append(num1 + num2 + num3)
                    count += 1
        if num1 in dic3 and num2 in dic2 and num3 in dic1:
            if num1 + num2 + num3 not in visited:
                visited[num1 + num2 + num3] = [321]
                ans.append(num1 + num2 + num3)
                count += 1
            else:
                if 321 not in visited[num1 + num2 + num3]:
                    visited[num1 + num2 + num3].append(321)
                    ans.append(num1 + num2 + num3)
                    count += 1

        if j == len(tasty)-1:
            i += 1
            j = i + 1
        else:
            j += 1

    index += 1

ans.sort(reverse=True)
ind = 0
while ind != k:
    print(ans[ind])
    ind += 1

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


# globals
inf = float('inf')
X, Y, Z, K = 0, 0, 0, 0
A = []
B = []
C = []
MEM = set()


def set_inputs():
    global X, Y, Z, K, A, B, C
    X, Y, Z, K = get_li()
    A = get_li()
    B = get_li()
    C = get_li()
    return


class Node:
    def __init__(self, ia, ib, ic, parent, leaf=False):
        self.ia = ia
        self.ib = ib
        self.ic = ic
        self.parent = parent
        self._ca = None
        self._cb = None
        self._cc = None
        self.leaf = leaf

    def score(self):
        if self.leaf or (self.ia, self.ib, self.ic) in MEM:
            return -inf
        return A[self.ia] + B[self.ib] + C[self.ic]

    def ca(self):
        if self._ca is None:
            if self.ia < X - 1:
                self._ca = Node(self.ia + 1, self.ib, self.ic, self)
            else:
                self._ca = Node(10000, 10000, 10000, self, leaf=True)
        return self._ca

    def cb(self):
        if self._cb is None:
            if self.ib < Y - 1:
                self._cb = Node(self.ia, self.ib + 1, self.ic, self)
            else:
                self._cb = Node(10000, 10000, 10000, self, leaf=True)
        return self._cb

    def cc(self):
        if self._cc is None:
            if self.ic < Z - 1:
                self._cc = Node(self.ia, self.ib, self.ic + 1, self)
            else:
                self._cc = Node(10000, 10000, 10000, self, leaf=True)
        return self._cc

    def done(self):
        MEM.add((self.ia, self.ib, self.ic))


def main():
    setrecursionlimit(100000)
    set_inputs()
    # ----------MAIN----------
    A.sort(reverse=True)
    B.sort(reverse=True)
    C.sort(reverse=True)
    root = Node(0, 0, 0, None)
    nas = [Node(1, 0, 0, root) if X > 1 else Node(10000, 10000, 10000, root, leaf=True)]
    nbs = [Node(0, 1, 0, root) if Y > 1 else Node(10000, 10000, 10000, root, leaf=True)]
    ncs = [Node(0, 0, 1, root) if Z > 1 else Node(10000, 10000, 10000, root, leaf=True)]
    n = root
    for k in range(K):
        print(n.score())
        n.done()
        if nas[-1].score() >= nbs[-1].score() and nas[-1].score() >= ncs[-1].score():
            n = nas.pop()
        elif nbs[-1].score() >= ncs[-1].score():
            n = nbs.pop()
        else:
            n = ncs.pop()
        nas.append(n.ca())
        nbs.append(n.cb())
        ncs.append(n.cc())
        nas = list(set(nas))
        nbs = list(set(nbs))
        ncs = list(set(ncs))
        nas.sort(key=lambda x: x.score())
        nbs.sort(key=lambda x: x.score())
        ncs.sort(key=lambda x: x.score())
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a = IR(5)
    k = II()
    if max(a) - min(a) > k:
        print(":(")
    else:
        print("Yay!")
    return

#B
def B():
    a = IR(5)
    b = a[0]
    ans = 0
    for a_ in a:
        if a_ % 10 != 0:
            if b % 10> a_ % 10:
                b = a_
    flg = 0
    for a_ in a:
        if a_ != b or flg:
            ans += math.ceil(a_ / 10) * 10
        else:
            ans += b
            flg = 1
    print(ans)
            
    
    return

#C
def C():
    n = II()
    ai = IR(5)
    print(math.ceil(n / min(ai)) + 4)
        
        
    return

#D
def D():
    x, y, z, K = LI()
    a = LI()
    a.sort(reverse=True)
    b = LI()
    b.sort(reverse = True)
    c = LI()
    c.sort(reverse=True)
    ab = []
    if z > x:
        if y > z:
            bf = a
            a = c
            c = bf
            bf = x
            x = z
            z = bf
        elif x > y:
            bf = b
            b = c
            c = bf
            bf = y
            y = z
            z = bf
    if z > y:
        if y > x:
            bf = a
            a = c
            c = bf
            bf = x
            x = z
            z = bf
        elif x > y:
            bf = b
            b = c
            c = bf
            bf = y
            y = z
            z = bf
        
    for a_ in a:
        for b_ in b:
            ab.append(a_ + b_)
    ab.sort(reverse = True)
    ab = ab[:min(x + y, 3000)]
    ans = []
    for ab_ in ab:
        for c_ in c:
            ans.append(ab_ + c_)
    ans.sort(reverse = True)
    for k in range(K):
        print(ans[k])



        
                    

    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    D()

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

"""
最大、最大2なら、あとはなにかけあわせても最大しかない
ABCの最大2個なので、プラス残りのlen()を足す。
もし超えたら、その組み合わせで順番にもとめていく
"""
X, Y, Z, K = LI()
A = sorted(LI(), reverse=True)
B = sorted(LI(), reverse=True)
C = sorted(LI(), reverse=True)


#  ABC = A+B+C
#  ABC = sorted(ABC, reverse=True)
#  #  print(ABC)
INF = 10**18
a,b,c = [INF],[INF],[INF]
#  abc = 0
count = 0
while a[-1] > B[0] or a[-1] > C[0] or len(a) <= min(200, X):
    a.append(A[count])
    count += 1
    if count >= len(A):
        break


count = 0
while b[-1] > A[0] or b[-1] > C[0] or len(b) <= min(100, Y):
    b.append(B[count])
    count += 1
    if count >= len(B):
        break

count = 0
while c[-1] > B[0] or c[-1] > A[0] or len(c) <= min(100, Z):
    c.append(C[count])
    count += 1
    if count >= len(C):
        break
a = a[1:]
b = b[1:]
c = c[1:]

#  while a[-1] > b[0] and a[-1] > c[0] and b[-1] > a[0] and b[-1] > c[0] and c[-1] > a[0] and c[-1] > b[0]:

#  while not(len(a) >= min(100, X) and len(b) >= min(100, Y) and len(c) >= min(100, Z)):
#      if A:
#          if ABC[abc]in A:
#              a.append(ABC[abc])
#              A.remove(ABC[abc])
#      if B:
#          if ABC[abc]in B:
#              b.append(ABC[abc])
#              B.remove(ABC[abc])
#      if C:
#          if ABC[abc]in C:
#              c.append(ABC[abc])
#              C.remove(ABC[abc])
#      abc+=1
#  #  for abc in range(min(200, X+Y+Z)):

#  #      if ABC[abc]in A:
#  #          a.append(ABC[abc])
#  #          A.remove(ABC[abc])
#  #      if ABC[abc]in B:
#  #          b.append(ABC[abc])
#  #          B.remove(ABC[abc])
#  #      if ABC[abc]in C:
#  #          c.append(ABC[abc])
#  #          C.remove(ABC[abc])
#  #  print(a,b,c)

points = []
for i in a:
    for j in b:
        for k in c:
            points.append(i+j+k)
#  print(points)


#  #  A = A[:min(250, X)]
#  #  B = B[:min(250, Y)]
#  #  C = C[:min(250, Z)]
#  #  # Aの中で最大の数字足すBの最大、Cの最大は全体の中で最大
#  #  # 3000個大きい順に求めれば良い

#  #  # iが決まれば、j,kのおおきさは分かる
#  #  BC = []
#  #  # 1000*1000 で10**6の計算量
#  #  #  for j in range(len(B)):
#  #  #      for k in range(len(C)):
#  #  #          BC.append(B[j] + C[k])

#  #  points = []
#  #  # 10**3 と 10** 6
#  #  #  for i in range(len(A)):
#  #  #      points += [bc + A[i] for bc in BC]
#  #  for i in range(len(A)):
#  #      #  for bc in BC:
#  #      #      points.append(A[i]+bc)
#  #      for j in range(len(B)):
#  #          for k in range(len(C)):
#  #              points.append(A[i] + B[j] + C[k])
for i in sorted(points, reverse=True)[:K]:
    print(i)

Submission status: Wrong Answer

Solution code:
a = []
b = []
c = []
l = 300
x,y,z,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
po = min(len(a)+len(b)+len(c),3000)-1
d = a
d.extend(b)
d.extend(c)
d.sort()
a2 = []
b2 = []
c2 = []
for i in range(len(a)):
  if d[po] >= a[i]:
    a2.append(a[i])
for i in range(len(b)):
  if d[po] >= b[i]:
    b2.append(b[i])
for i in range(len(c)):
  if d[po] >= c[i]:
    c2.append(c[i])

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

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 
import math
import sys
import itertools
import numpy as np


# なんこか入力
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()
b.sort()
c.sort()
#cake = [[0 for i in range(3)] for j in range(3000)]
#cake[0][0]=x-1
#cake[0][1]=y-1
#cake[0][2]=z-1
print(a[x-1]+b[y-1]+c[z-1])
#print(a[cake[0][0]],b[cake[0][1]],c[cake[0][2]])
s = [[x-2,y-1,z-1],[x-1,y-2,z-1],[x-1,y-1,z-2]]
#print(s)

for i in range(1,k):
    ma = 0
    temp = [-1,-1,-1]
    for j in s:
        if ma <= a[j[0]]+b[j[1]]+c[j[2]] :
            temp = j
            ma = a[j[0]]+b[j[1]]+c[j[2]]
#    print(temp)
    print(a[temp[0]]+b[temp[1]]+c[temp[2]])
#    print(a[temp[0]],b[temp[1]],c[temp[2]])
    s.remove(temp)
#    print(s)
#    print(temp)
    f = 0
    if temp[0] > 0:
        for j in s:
            if temp[0]-1 == j[0] and temp[1]== j[1] and temp[2]==j[2]:
                f=1
                break
        if f==0:
            s.append([temp[0]-1,temp[1],temp[2]])
    f = 0
    if temp[1] > 0:
        for j in s:
            if temp[0] == j[0] and temp[1]-1== j[1] and temp[2]==j[2]:
                f=1
                break
        if f==0:
            s.append([temp[0],temp[1]-1,temp[2]])
    f = 0
    if temp[2] > 0:
        for j in s:
            if temp[0] == j[0] and temp[1]== j[1] and temp[2]-1==j[2]:
                f=1
                break
        if f==0:
            s.append([temp[0],temp[1],temp[2]-1])

Submission status: Wrong Answer

Solution code:
import math
X,Y,Z,K = list(map(int,input().split()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

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

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

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