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 operator import itemgetter

K = int(input().split()[-1])

A = sorted([int(e) for e in input().split()], reverse=True)
B = sorted([int(e) for e in input().split()], reverse=True)
C = sorted([int(e) for e in input().split()], reverse=True)


def get_next_iter(i, _list):
    if i < len(_list) - 1:
        return i + 1
    else:
        return None


def get_value(ia, ib, ic):
    return A[ia] + B[ib] + C[ic]


def get_next(ia, ib, ic):
    ia_n = get_next_iter(ia, A)
    ib_n = get_next_iter(ib, B)
    ic_n = get_next_iter(ic, C)

    s = []
    if ia_n is not None:
        s.append((ia_n, ib, ic, get_value(ia_n, ib, ic)))
    if ib_n is not None:
        s.append((ia, ib_n, ic, get_value(ia, ib_n, ic)))
    if ic_n is not None:
        s.append((ia, ib, ic_n, get_value(ia, ib, ic_n)))

    return s


values = [(0, 0, 0, get_value(0, 0, 0))]
candidate = set(get_next(0, 0, 0))

for k in range(K - 1):
    vs = sorted(candidate, reverse=True, key=itemgetter(3))
    v = vs[0]
    values.append(v)
    candidate.remove(v)
    for each in get_next(v[0], v[1], v[2]):
        candidate.add(each)

for each in values:
    print(each[3])

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

K = int(input().split()[-1])

A = sorted([int(e) for e in input().split()], reverse=True)
B = sorted([int(e) for e in input().split()], reverse=True)
C = sorted([int(e) for e in input().split()], reverse=True)


def get_next_iter(i, _list):
    if i < len(_list) - 1:
        return i + 1
    else:
        return i


def get_value(ia, ib, ic):
    return A[ia] + B[ib] + C[ic]


def get_next(ia, ib, ic):
    ia_n = get_next_iter(ia, A)
    ib_n = get_next_iter(ib, B)
    ic_n = get_next_iter(ic, C)

    return [(ia_n, ib, ic, get_value(ia_n, ib, ic)),
            (ia, ib_n, ic, get_value(ia, ib_n, ic)),
            (ia, ib, ic_n, get_value(ia, ib, ic_n))]


values = [(0, 0, 0, get_value(0, 0, 0))]
candidate = set(get_next(0, 0, 0))

for k in range(K - 1):
    vs = sorted(candidate, reverse=True, key=itemgetter(3))
    v = vs[0]
    values.append(v)
    candidate.remove(v)
    for each in get_next(v[0], v[1], v[2]):
        candidate.add(each)

for each in values:
    print(each[3])

Submission status: Wrong Answer

Solution code:
import bisect

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

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

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

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

Submission status: Wrong Answer

Solution code:
X,Y,Z,K=[int(i) for i in input().split()]
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
C=[int(i) for i in input().split()]
A.sort(key=lambda x:-x)
B.sort(key=lambda y:-y)
C.sort(key=lambda z:-z)
pri=[]
def dp(l):
  return A[l[0]]+B[l[1]]+C[l[2]]

def priq(a,b,c):
  if [a,b,c] not in pri:

    for i in range(len(pri)-1,-1,-1):
      if dp(pri[i])<dp([a,b,c]):
        pri.insert(i+1,[a,b,c])
        print(pri)
        return
    pri.insert(0,[a,b,c])

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

Submission status: 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)

ans = []
for i in range(len(A)):
    for j in range(len(B)):
        if (i+1) * (j+1) > K:
            continue
        for k in range(len(C)):
            if (i + 1) *(j + 1)*(k + 1) > K:
                continue
            ans.append(A[i]+B[j]+C[k])

for a in sorted(ans,reverse=True):
    print(a)

Submission status: Wrong Answer

Solution code:
X,Y,Z,K=map(int,input().split())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
C=[int(i) for i in input().split()]
A_B=[0]*(X*Y)
count=0
for i in range(X):
    for j in range(Y):
        A_B[count]=A[i]+B[j]
        count+=1
count=0
A_B.sort()
K1=min(K,X*Y)
A_B_C=[0]*(K1*Z)
for i in range(K1):
    for j in range(Z):
        A_B_C[count]=A_B[i]+C[j]
        count+=1
A_B_C.sort()
for i in range(K):
    print(A_B_C[-i-1])

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)
ans = [A[i]+B[j]+C[k] for k in range(Z) for j in range(Y) for i in range(X) if (i+1)*(j+1)*(k+1) <= K]
ans.sort(reverse=True)
print(len(ans))
for i in range(K):
    print(ans[i])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import math
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
x,y,z,p = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
"""
if x*y*z <= 3*10**6:
    abc = []
    for i in range(x):
        for j in range(y):
            for k in range(z):
                abc.append(a[i]+b[j]+c[k])
    abc.sort()
    abc = abc[::-1]
    for i in range(p):
        print(abc[i])  
    exit()
"""
ab = []
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort()
c = c[::-1]
ab = ab[::-1]
ab.append(-2**62)
ans = []
miru = [0]*z
for i in range(z):
    if i + 1 == z:
        d = -2**60
    else:
        d = c[i+1]+ab[0]
    xx = ans[-1] if ans else 0
    flag = 0
    for j in range(i+1):
        while ab[miru[j]] + c[j] >= d and (len(ans)<= p or ab[miru[j]] + c[j] >= xx):
             ans.append(ab[miru[j]] + c[j])
             miru[j]+=1
             flag = 1
        
    if flag:
        ans.sort(key = lambda x:-x)
        while len(ans) > p:
            ans.pop()
ans.sort()
ans = ans[::-1]
for i in range(p):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
import sys
import math
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
x,y,z,p = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
"""
if x*y*z <= 3*10**6:
    abc = []
    for i in range(x):
        for j in range(y):
            for k in range(z):
                abc.append(a[i]+b[j]+c[k])
    abc.sort()
    abc = abc[::-1]
    for i in range(p):
        print(abc[i])  
    exit()
"""
ab = []
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort()
c = c[::-1]
ab = ab[::-1]
ab.append(-2**62)
ans = []
miru = [0]*z
for i in range(z):
    if i + 1 == z:
        d = -2**60
    else:
        d = c[i+1]+ab[0]
    xx = ans[-1] if ans else 0
    for j in range(i+1):
        while ab[miru[j]] + c[j] >= d and (len(ans)<= p or ab[miru[j]] + c[j] >= xx):
             ans.append(ab[miru[j]] + c[j])
             miru[j]+=1
             flag = 1
        
    if flag:
        ans.sort(key = lambda x:-x)
        while len(ans) > p:
            ans.pop()
ans.sort()
ans = ans[::-1]
for i in range(p):
    print(ans[i])

Submission status: Wrong Answer

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

A.sort()
B.sort()
sublist = []
for i in range(max(X-K-1, 0), X):
    for j in range(max(Y - K - 1, 0), Y):
        if (X-i)*(Y-j) <= K:
            sublist.append(A[i]+B[j])

sublist.sort()
print(sublist)
subanslist = sublist[-K::1]
sbsize = len(subanslist)
anslist = []
C.sort()

for num, i in enumerate(subanslist):
    for j in range(max(Z - K - 1, 0), Z):
        if (sbsize-num)*(Z-j) <= K:
            anslist.append(i+C[j])

anslist.sort()
ans = anslist[:-K-1:-1]
print(*ans, sep="\n")

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,Y,Z,K = map(int,input().split())
A = [i for i in map(int,input().split())]
B = [i for i in map(int,input().split())]
C = [i for i in map(int,input().split())]
ABC = []
for i in range(X):
    for j in range(Y):
        if (i+1) * (j+1) > K:
            break
        for k in range(Z):
            if (i+1) * (j+1) * (k+1) > K:
                break
            ABC.append(A[i] +  B[j] + C[k])
for i in range(K):
    print(ABC[i])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
input = sys.stdin.readline

# bisect_left(lists, 3)
# bisect_right(lists, 3)

def main():
	X, Y, Z, K = map(int, input().split(" "))
	As = []
	Bs = []
	Cs = []
	As = input().split()
	As = [int(s) for s in As]
	Bs = input().split()
	Bs = [int(s) for s in Bs]
	Cs = input().split()
	Cs = [int(s) for s in Cs]

	As.sort(reverse=True)
	Bs.sort(reverse=True)
	Cs.sort(reverse=True)

	As = As[:20]
	Bs = Bs[:20]
	Cs = Cs[:20]

	ans = []
	for i, val in enumerate(As):
		for j, vol in enumerate(Bs):
			for k, ogi in enumerate(Cs):
				ans.append(val+vol+ogi)

	ans.sort(reverse=True)
	for i in range(K):
		print(ans[i])
	
if __name__ == "__main__":
	main()

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

h=[]
for a in A:
    for b in B:
        for c in C:
            n=a+b+c
            heapq.heappush(h,-n)

for i in range(K):
    print(-i)

Submission status: Wrong Answer

Solution code:
import heapq
l = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

h=[]
for a in A:
    for b in B:
        for c in C:
            n=a+b+c
            heapq.heappush(h,-n)

for i in h:
    print(-i)

Submission status: Wrong Answer

Solution code:
import heapq
l = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

h=[]
for a in A:
    for b in B:
        for c in C:
            n=a+b+c
            heapq.heappush(h,n)
h.reverse()

for i in h:
    print(i)

Submission status: Wrong Answer

Solution code:
x,y,z,k = map(int,input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]

a.sort(reverse=True),b.sort(reverse=True),c.sort(reverse=True)
abc = []
i,j,l = 0,0,0
for i in range(x):
  if (i+1)*(j+1)*(l+1)>k:
        break
  for j in range(y):
    if (i+1)*(j+1)*(l+1)>k:
        break
    for l in range(z):
      if (i+1)*(j+1)*(l+1)>k:
        break
      abc.append(a[i]+b[j]+c[l])
abc.sort(reverse=True)
abc = abc[0:k]
for i in abc:
  print(i)

Submission status: Wrong Answer

Solution code:
l = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

lst=[]
for a in A:
    for b in B:
        for c in C:
            n=a+b+c
            lst.append(n)
lst.sort(reverse = True)

for i in lst:
    print(i)

Submission status: Wrong Answer

Solution code:
Anum, Bnum, Cnum, 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)

iA = 0
iB = 0
iC = 0
while((iA + 1) * (iB + 1) * (iC + 1) < K):
    if(iA == Anum - 1):
        tryA = -(A[iA] + B[iB] + C[iC])
    else:
        tryA = A[iA + 1] - A[iA]

    if(iB == Bnum - 1):
        tryB = -(A[iA] + B[iB] + C[iC])
    else:
        tryB = B[iB + 1] - B[iB]

    if(iC == Cnum - 1):
        tryC = -(A[iA] + B[iB] + C[iC])
    else:
        tryC = C[iC + 1] - C[iC]

    if(tryA >= max(tryB, tryC)):
        iA += 1
    elif(tryB >= tryC):
        iB += 1
    else:
        iC += 1

    if(iA == Anum - 1) and (iB == Bnum - 1) and (iC == Cnum - 1):
        break

iA = min(iA + 5, Anum)
iB = min(iB + 5, Bnum)
iC = min(iC + 5, Cnum)

yum = []
for ia in range(iA):
    for ib in range(iB):
        for ic in range(iC):
            yum.append(A[ia] + B[ib] + C[ic])
yum.sort(reverse=True)

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

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(key=lambda x:-x)
b.sort(key=lambda x:-x)
c.sort(key=lambda x:-x)
queue=[(a[0]+b[0]+c[0],0,0,0)]
beauty=[0 for _ in range(k)]
index=0
while index<k:
    queue.sort(key=lambda x:-x[0])
    # print(queue)
    beauty[index],indexa,indexb,indexc=queue[0]
    index+=1
    queue.pop(0)
    for da,db,dc in [(1,0,0),(0,1,0),(0,0,1)]:
        # print(indexa+da,indexb+db,indexc+dc)
        if indexa+da<x and indexb+db<y and indexc+dc<z:
            queue.append((a[indexa+da]+b[indexb+db]+c[indexc+dc],indexa+da,indexb+db,indexc+dc))
    queue=list(set(queue))
for i in range(k):
    print(beauty[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()))

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

e = [i + j for i in a[:500] for j in b[:500]]
e.sort(reverse=True)
e = e[:k]
ans = [i + j for i in e for j in c[:500]]
ans.sort(reverse=True)

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

Submission status: Wrong Answer

Solution code:
Anum, Bnum, Cnum, 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)

iA = 0
iB = 0
iC = 0
while((iA + 1) * (iB + 1) * (iC + 1) < K):
    nextA = A[min(iA + 1, Anum - 1)] + B[iB] + C[iC]
    if(iA == Anum - 1): nextA = 0
    nextB = A[iA] + B[min(iB + 1, Bnum - 1)] + C[iC]
    if(iB == Bnum - 1): nextB = 0
    nextC = A[iA] + B[iB] + C[min(iC + 1, Cnum - 1)]
    if(iC == Cnum - 1): nextC = 0

    yummax = max(nextA, nextB, nextC)
    if(yummax == nextA):
        iA += 1
    if(yummax == nextB):
        iB += 1
    if(yummax == nextC):
        iC += 1
    if(iA == Anum - 1) and (iB == Bnum - 1) and (iC == Cnum - 1):
        break

yum = []
for ia in range(min(iA + 3, Anum)):
    for ib in range(min(iB + 4, Bnum)):
        for ic in range(min(iC + 5, Cnum)):
            yum.append(A[ia] + B[ib] + C[ic])
yum.sort(reverse=True)

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

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)

e = [i + j for i in a[:200] for j in b[:200]]
e.sort(reverse=True)
e = e[:k]
ans = [i + j for i in e for j in c[:200]]
ans.sort(reverse=True)

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

Submission status: Wrong Answer

Solution code:
Anum, Bnum, Cnum, 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)

iA = 0
iB = 0
iC = 0
while((iA + 1) * (iB + 1) * (iC + 1) < K):
    nextA = A[min(iA + 1, Anum - 1)] + B[iB] + C[iC]
    if(iA == Anum - 1): nextA = 0
    nextB = A[iA] + B[min(iB + 1, Bnum - 1)] + C[iC]
    if(iB == Bnum - 1): nextB = 0
    nextC = A[iA] + B[iB] + C[min(iC + 1, Cnum - 1)]
    if(iC == Cnum - 1): nextC = 0

    yummax = max(nextA, nextB, nextC)
    if(yummax == nextA):
        iA += 1
    if(yummax == nextB):
        iB += 1
    if(yummax == nextC):
        iC += 1
    if(iA == Anum - 1) and (iB == Bnum - 1) and (iC == Cnum - 1):
        break

yum = []
for ia in range(min(iA + 3, Anum)):
    for ib in range(min(iB + 4, Bnum)):
        for ic in range(min(iC + 5, Cnum)):
            print("ia={},ib={},ic={},yum={}".format(ia,ib,ic,A[ia]+B[ib]+C[ic]))
            yum.append(A[ia] + B[ib] + C[ic])
yum.sort(reverse=True)

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

Submission status: Wrong Answer

Solution code:
def d_cake_123_binary_search(X, Y, Z, K, A, B, C):
    # editionalの解法4 O(K^2log(max(P))
    # 参考: https://atcoder.jp/contests/abc123/submissions/4871511
    import bisect
    ab = sorted([e1 + e2 for e2 in B for e1 in A])  # A, Bの要素は全部調べる

    # rejected 以上の値は、美味しさの合計がそれ以上である個数がK個未満である
    accepted, rejected = -1, 10**11
    while abs(accepted - rejected) > 1:
        mid = (accepted + rejected) // 2
        count = sum([len(ab) - bisect.bisect_left(ab, mid - e) for e in C])
        if count >= K:
            accepted = mid
        else:
            rejected = mid

    ans = []
    for e in C:
        idx = bisect.bisect_left(ab, accepted - e)
        for i in range(idx, len(ab)):
            ans.append(e + ab[i])
    ans.sort(reverse=True)
    return '\n'.join(map(str, ans))

X, Y, Z, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
print(d_cake_123_binary_search(X, Y, Z, K, A, B, C))

Submission status: Wrong Answer

Solution code:
def d_cake_123_binary_search(X, Y, Z, K, A, B, C):
    # editionalの解法4 O(K^2log(max(P))
    # 参考: https://atcoder.jp/contests/abc123/submissions/4871511
    import bisect
    ab = sorted([e1 + e2 for e2 in B for e1 in A])  # A, Bの要素は全部調べる

    # rejected 以上の値は、美味しさの合計がそれ以上である個数がK個未満である
    accepted, rejected = -1, 10**11
    while abs(accepted - rejected) > 1:
        mid = (accepted + rejected) // 2
        count = sum([len(ab) - bisect.bisect_left(ab, mid - e) for e in C])
        if count >= K:
            accepted = mid
        else:
            rejected = mid

    ans = []
    for e in C:
        idx = bisect.bisect_left(ab, accepted - e)
        for i in range(idx, len(ab)):
            ans.append(e + ab[i])
    ans.sort(reverse=True)
    return ' '.join(map(str, ans))

X, Y, Z, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
print(d_cake_123_binary_search(X, Y, Z, K, A, B, C))

Submission status: Wrong Answer

Solution code:
def d_cake_123_binary_search(X, Y, Z, K, A, B, C):
    # editionalの解法4 O(K^2log(max(P))
    # 参考: https://atcoder.jp/contests/abc123/submissions/4871511
    import bisect
    a, b, c = sorted(A), sorted(B), sorted(C)
    ab = sorted([e1 + e2 for e2 in b for e1 in a])  # A, Bの要素は全部調べる

    # rejected 以上の値は、美味しさの合計がそれ以上である個数がK個未満である
    accepted, rejected = -1, 10**11
    while abs(accepted - rejected) > 1:
        mid = (accepted + rejected) // 2
        count = sum([len(ab) - bisect.bisect_left(ab, mid - e) for e in c])
        if count >= K:
            accepted = mid
        else:
            rejected = mid

    ans = []
    for e in c:
        idx = bisect.bisect_left(ab, accepted - e)
        for i in range(idx, len(ab)):
            ans.append(e + ab[i])
    ans.sort(reverse=True)
    return ' '.join(map(str, ans))

X, Y, Z, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
print(d_cake_123_binary_search(X, Y, Z, K, A, B, C))

Submission status: Wrong Answer

Solution code:
import itertools

X, Y, Z, K = (int(i) for i in input().split())
A = [int(i) for i in input().split()] 
B = [int(i) for i in input().split()] 
C = [int(i) for i in input().split()] 

A = sorted(A)[::-1]
B = sorted(B)[::-1]
C = sorted(C)[::-1]

T = [A[:K], B[:K]]
score = []
for cand in itertools.product(*T):
  score.append(sum(cand))

T = [score, C[:K]]
for cand in itertools.product(*T):
  score.append(sum(cand))

score = sorted(score)[::-1]
for i in range(K):
    print(score[i])

Submission status: Wrong Answer

Solution code:
x, y, z, k = map(int, input().split())
a_array = sorted([int(x) for x in input().split()], reverse=True)
b_array = sorted([int(x) for x in input().split()], reverse=True)
c_array = sorted([int(x) for x in input().split()], reverse=True)


def solver1():
    ab_array = sorted([x + y for x in a_array for y in b_array])[:3000]
    abc_array = sorted([ab + c for ab in ab_array for c in c_array], reverse=True)
    for abc in abc_array[:k]:
        print(abc)


solver1()

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()))
for i in range(X):
  for j in range(Y):
    for k in range(Z):
      print(A[i]+B[j]+C[k])

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)

def f(thres:int)->bool:
    """return (おいしさが thres 以上の組み合わせ) >= K
    """
    global K
    cnt = 0
    for a in range(X):
        for b in range(Y):
            for c in range(Z):
                if A[a]+B[b]+C[c]<thres:
                    break
                cnt += 1
                if cnt >= K:
                    return True
    return False

def binary_search(f, s:int, t:int)->int:
    """f: function(x:int)->bool
    f(s)=False, f(t)=True，f は広義単調増加
    return : [s, t) での f(x)=True なる最小のx
    """
    ok = s-1
    ng = t
    while ng - ok > 1:
        mid = (ok + ng)//2
        if f(mid): ok = mid
        else:      ng = mid
        # print(ng, ok, ok-ng)

    if ok==s-1:
        raise(ValueError("For all x, f(x)=False"))
    return ok

M = A[0]+B[0]+C[0]
m = A[-1]+B[-1]+C[-1]
# print(f(m), f(M))
thres = binary_search(f, m, M+1)-1
# print("thres:",thres)
ans = []
for a in range(X):
    for b in range(Y):
        for c in range(Z):
            if A[a]+B[b]+C[c]<thres:
                break
            ans.append(A[a]+B[b]+C[c])
            if len(ans)==K:
                ans.sort(reverse=True)
                print("\n".join(map(str, ans)))

Submission status: Wrong Answer

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

A = map(int,raw_input().split())
B = map(int,raw_input().split())
C = map(int,raw_input().split())

D = []
for i in xrange(min(len(A),K)):
    for j in xrange(min(len(B),K)):
        D.append(A[i]+B[i])

D.sort(reverse = True)
E = []
for i in xrange(min(len(D),K)):
    for j in xrange(min(len(C),K)):
        E.append(D[i]+C[j])

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

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
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
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])
#print(len(arr))
arr = sorted(arr, reverse=True)[:K]
# output
ans = '\n'.join(map(str, arr))
#print(ans)

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
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
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])
print(len(arr))
arr = sorted(arr, reverse=True)[:K]
# output
ans = '\n'.join(map(str, arr))
#print(ans)

Submission status: Wrong Answer

Solution code:
x, y, z, k = map(int, input().split())
a_array = sorted([int(x) for x in input().split()], reverse=True)
b_array = sorted([int(x) for x in input().split()], reverse=True)
c_array = sorted([int(x) for x in input().split()], reverse=True)


def solver1():
    ab_array = [x + y for x in a_array for y in b_array][:3000]
    abc_array = sorted([ab + c for ab in ab_array for c in c_array], reverse=True)
    for abc in abc_array:
        print(abc)


solver1()

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