# 2分木探索
# m以下の最大の要素を返す
# 組み込みbsearchはfind-minimam
def bsearch(m, arr)
  j = (0...arr.size).bsearch {|i| arr[i] > m}
  if j.nil?
    arr.last
  elsif j > 0
    arr[j - 1]
  else
    false
  end
end

# 4重ループ O(n^4)
def main1(n,m,a)
  max = 0
  0.upto(n) do |i|
    i.upto(n) do |j|
      j.upto(n) do |k|
        k.upto(n) do |l|
          sum = a[i] + a[j] + a[k] + a[l]
          max = sum if sum <= m && sum > max
        end
      end
    end
  end

  max
end

# 3重ループ O(n^3logn)
# a[l] <= m - (a[i] + a[j] + a[k]) を2分木探索
def main2(n,m,a)
  max = 0
  0.upto(n) do |i|
    i.upto(n) do |j|
      j.upto(n) do |k|
        # k.upto(n) do |l|
        buf = m - (a[i] + a[j] + a[k])
        if buf >= 0
          a4 = bsearch(buf, a)
          sum = a[i] + a[j] + a[k] + a4
          max = sum if max < sum
        end
        # end
      end
    end
  end

  max
end

# 2重ループ O(n^2logn)
# 2要素の和をメモ化する
def main3(n,m,a)
  max = 0
  # 2要素の和を格納する配列
  sum_2 = a.dup
  0.upto(n) do |i|
    i.upto(n) do |j|
      a_2 = a[i] + a[j]
      sum_2 << a_2 if a_2 <= m
    end
  end
  sum_2.sort!

  f = sum_2.size - 1
  0.upto(f) do |i|
    another_sum_2 = bsearch(m - sum_2[i], sum_2)
    sum = sum_2[i] + another_sum_2
    max = sum if max < sum
  end

  max
end

loop do
  # 入力
  n,m = gets.split(" ").map(&:to_i)
  break if n == 0
  a = []
  a[0] = 0
  (1..n).each { |i| a[i] = gets.to_i }
  a.sort!

  # main
  puts main2(n,m,a)
end