loop do
  line =gets.chomp
  next if line == ""
  n,m = line.split.map(&:to_i)
  break if (n|m)==0
  ps = []
  n.times do
    ps << gets.to_i
  end
  ps.sort!{|a,b|b-a}
  i =0
  s=0
  c=0
  loop do
    break unless ps[i]
    break if c==4
    if s+ps[i]>m
      i+=1
      next
    end
    s+=ps[i]
    c+=1
  end
  puts s
end