Problem: 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.

Submission attempts (in order):
X, Y, Z, K = list(map(int, input().split()))
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))

A_list.sort(reverse=True)
B_list.sort(reverse=True)
C_list.sort(reverse=True)

class Value():
  def __init__(self, A_list, B_list, C_list, a_idx, b_idx, c_idx):
    self.A = A_list[a_idx]
    self.B = B_list[b_idx]
    self.C = C_list[c_idx]
    self.a_idx = a_idx
    self.b_idx = b_idx
    self.c_idx = c_idx
    self.value = self.A + self.B + self.C
  def __eq__(self, other):
    return self.a_idx == other.a_idx and self.b_idx == other.b_idx and self.c_idx == other.c_idx
  def __gt__(self, other):
    return self.value > other.value
  def __ge__(self, other):
    return self.value >= other.value
    
que = []
que.append(Value(A_list, B_list, C_list, 0, 0, 0))

for _ in range(K):
  if que:
    v = que.pop(0)
    print(v.value)
  new_v_list = []
  if v.a_idx < X - 1:
    new_v = Value(A_list, B_list, C_list, v.a_idx + 1, v.b_idx, v.c_idx)
    if not new_v in que: 
      que.append(new_v)
  if v.b_idx < Y - 1:
    new_v = Value(A_list, B_list, C_list, v.a_idx, v.b_idx + 1, v.c_idx)
    if not new_v in que:
      que.append(new_v)
  if v.c_idx < Z - 1:
    new_v = Value(A_list, B_list, C_list, v.a_idx, v.b_idx, v.c_idx + 1)
    if not new_v in que:
      que.append(new_v)
  que.sort(key=lambda x: x.value, reverse=True)
  que = que[:len(que)//2+1]

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

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

from heapq import heapify, heappop, heappush, heappushpop
 
 
class PriorityQueue:
    def __init__(self, heap):
        '''
        heap ... list
        '''
        self.heap = heap
        heapify(self.heap)
 
    def push(self, item):
        heappush(self.heap, item)
 
    def pop(self):
        return heappop(self.heap)
 
    def pushpop(self, item):
        return heappushpop(self.heap, item)
 
    def __call__(self):
        return self.heap
 
 
heap = []  
 
q = PriorityQueue(heap)
q.push((-(A[0] + B[0] + C[0]), 0, 0, 0))
 
considered = set()
ans = []
for k_th in range(1, K+1):
    heap_max, i, j, k = q.pop()
    ans.append(-heap_max)
    for di, dj, dk in zip([1, 0, 0], [0, 1, 0], [0, 0, 1]):
        i_new, j_new, k_new = i + di, j + dj, k + dk
        if i_new >= X or j_new >= Y or k_new >= Z:
            continue
        if (i_new, j_new, k_new) in considered:
            continue
        considered.add((i_new, j_new, k_new))
        q.push((-(A[i_new] + B[j_new] + C[k_new]), i_new, j_new, k_new))
 
 
print(*ans, sep='\n')


Verdicts: Wrong Answer

Accepted