Buggy python submission (verdict: Time Limit Exceeded):
while True:
  n,m = map(int,input().split())
  if not n and not m: break
  lst = []
  lst2 = []
  for i in range(n):
    lst.append(int(input()))
  for i in lst:
    for j in lst:
      lst2.append(i + j)
  lst2 = lst2 + lst
  lst2.sort()
  ans = 0
  def search(x, l, r):
    y = len(l)
    if y == 0:
      return r
    elif y == 1:
      if x >= l[0]:
        return l[0]
      else:
        return r
    elif l[y//2] == x:
      return x
    elif l[y//2] > x:
      return search(x, l[:y//2], r)
    elif l[y//2] < x:
      r = l[y//2]
      return search(x, l[y//2 + 1:], r)
  for i in lst2:
    if i <= m:
      ans = max(ans, i + search(m - i, lst2, 0))
    else:
      break
  print(ans)





Fixed submission:
def solve():
  while True:
    n,m = map(int,input().split())
    if not n and not m: break
    
    lst = [int(input()) for _ in range(n)]
    lst.append(0)
    lst = sorted(lst)
    lst2 = [i + j for i in lst for j in lst if i <= j]
    lst2 = sorted(lst2)
    A = []
    index = len(lst2) - 1
    for i in lst2:
      if i + lst2[-1] <= m:
        A.append(i + lst2[-1])
      elif i <= m:
        while True:
          if i + lst2[index] <= m:
            break
          index -= 1
        A.append(i + lst2[index])
      else:
        break
    print(max(A))
solve()

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
  while True:
    n,m = map(int,input().split())
    if not n and not m: break
    lst = []
    lst2 = []
    for i in range(n):
      lst.append(int(input()))
    for i in lst:
      for j in lst:
        lst2.append(i + j)
    lst2 = lst2 + lst
    lst2.sort()
    ans = 0
    def search(x, l, r):
      y = len(l)
      if y == 0:
        return r
      elif y == 1:
        if x >= l[0]:
          return l[0]
        else:
          return r
      elif l[y//2] == x:
        return x
      elif l[y//2] > x:
        return search(x, l[:y//2], r)
      elif l[y//2] < x:
        r = l[y//2]
        return search(x, l[y//2 + 1:], r)
    for i in lst2:
      if i <= m:
        ans = max(ans, i + search(m - i, lst2, 0))
      else:
        break
    print(ans)
solve()




Fixed submission:
def solve():
  while True:
    n,m = map(int,input().split())
    if not n and not m: break
    
    lst = [int(input()) for _ in range(n)]
    lst.append(0)
    lst = sorted(lst)
    lst2 = [i + j for i in lst for j in lst if i <= j]
    lst2 = sorted(lst2)
    A = []
    index = len(lst2) - 1
    for i in lst2:
      if i + lst2[-1] <= m:
        A.append(i + lst2[-1])
      elif i <= m:
        while True:
          if i + lst2[index] <= m:
            break
          index -= 1
        A.append(i + lst2[index])
      else:
        break
    print(max(A))
solve()

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
  while True:
    n,m = map(int,input().split())
    if not n and not m: break
    lst = []
    lst2 = []
    for i in range(n):
      lst.append(int(input()))
    for i in lst:
      for j in lst:
        lst2.append(i + j)
    lst2 = list(set(lst2 + lst))
    lst2.sort()
    ans = 0
    def binary(x, left, right):
      center = (left + right) // 2
      length = right - left + 1
      if length <= 0:
        return -100
      elif length == 1:
        if lst2[center] > x:
          return center - 1
        else:
          return center
      elif length == 2:
        if lst2[center] > x:
          return center - 1
        else:
          return center
      elif lst2[center] == x: return x
      elif lst2[center] < x: return binary(x, center, right)
      elif lst2[center] > x: return binary(x, left, center)
      
    for i in lst2:
      if i <= m:
        ans = max(ans, i + lst2[binary(m - i, 0, len(lst2) - 1)])
      else:
        break
    print(ans)
solve()




Fixed submission:
def solve():
  while True:
    n,m = map(int,input().split())
    if not n and not m: break
    
    lst = [int(input()) for _ in range(n)]
    lst.append(0)
    lst = sorted(lst)
    lst2 = [i + j for i in lst for j in lst if i <= j]
    lst2 = sorted(lst2)
    A = []
    index = len(lst2) - 1
    for i in lst2:
      if i + lst2[-1] <= m:
        A.append(i + lst2[-1])
      elif i <= m:
        while True:
          if i + lst2[index] <= m:
            break
          index -= 1
        A.append(i + lst2[index])
      else:
        break
    print(max(A))
solve()

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
  while True:
    n,m = map(int,input().split())
    if not n and not m: break
    lst = []
    lst2 = []
    for i in range(n):
      x = int(input())
      if x <= m:
        lst.append(x)
    lst2 = [i + j for i in lst for j in lst if i <= j]
    #for i in lst:
    #  for j in lst:
    #    if i + j <= m:
    #      lst2.append(i + j)
    #lst2 = list(set(lst2 + lst))
    lst2.sort()
    def binary(x, left, right):
      center = (left + right) // 2
      length = right - left + 1
      v = lst2[center]
      if length == 1 or length == 2:
        if v > x:
          return center - 1
        else:
          return center
      elif v == x: return x
      elif v < x: return binary(x, center + 1, right)
      elif v > x: return binary(x, left, center - 1)
    A = []
    #ans = 0
    for i in lst2:
      if i + lst2[-1] <= m:
        A.append(i + lst2[-1])
        #ans = max(ans, i + lst2[-1])
      elif i <= m:
        A.append(i + lst2[binary(m - i, 0 , len(lst2) - 1)])
        #ans = max(ans, i + lst2[binary(m - i, 0, len(lst2) - 1)])
      else:
        break
    print(max(A))
    #print(ans)
solve()




Fixed submission:
def solve():
  while True:
    n,m = map(int,input().split())
    if not n and not m: break
    
    lst = [int(input()) for _ in range(n)]
    lst.append(0)
    lst = sorted(lst)
    lst2 = [i + j for i in lst for j in lst if i <= j]
    lst2 = sorted(lst2)
    A = []
    index = len(lst2) - 1
    for i in lst2:
      if i + lst2[-1] <= m:
        A.append(i + lst2[-1])
      elif i <= m:
        while True:
          if i + lst2[index] <= m:
            break
          index -= 1
        A.append(i + lst2[index])
      else:
        break
    print(max(A))
solve()

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
  while True:
    n,m = map(int,input().split())
    if not n and not m: break
    lst = []
    lst2 = []
    lst = [int(input()) for _ in range(n)]
    lst.append(0)
    lst2 = [i + j for i in lst for j in lst if i <= j]
    #for i in lst:
    #  for j in lst:
    #    if i + j <= m:
    #      lst2.append(i + j)
    #lst2 = list(set(lst2 + lst))
    lst2.sort()
    def binary(x, left, right):
      center = (left + right) // 2
      length = right - left + 1
      v = lst2[center]
      if length == 1 or length == 2:
        if v > x:
          return center - 1
        else:
          return center
      elif v == x: return x
      elif v < x: return binary(x, center, right)
      elif v > x: return binary(x, left, center)
    A = []
    #ans = 0
    for i in lst2:
      if i + lst2[-1] <= m:
        A.append(i + lst2[-1])
        #ans = max(ans, i + lst2[-1])
      elif i <= m:
        A.append(i + lst2[binary(m - i, 0 , len(lst2) - 1)])
        #ans = max(ans, i + lst2[binary(m - i, 0, len(lst2) - 1)])
      else:
        break
    print(max(A))
    #print(ans)
solve()




Fixed submission:
def solve():
  while True:
    n,m = map(int,input().split())
    if not n and not m: break
    
    lst = [int(input()) for _ in range(n)]
    lst.append(0)
    lst = sorted(lst)
    lst2 = [i + j for i in lst for j in lst if i <= j]
    lst2 = sorted(lst2)
    A = []
    index = len(lst2) - 1
    for i in lst2:
      if i + lst2[-1] <= m:
        A.append(i + lst2[-1])
      elif i <= m:
        while True:
          if i + lst2[index] <= m:
            break
          index -= 1
        A.append(i + lst2[index])
      else:
        break
    print(max(A))
solve()