n, k, *a = gets.split.map(&:to_i) n.times do a << gets.to_i end max = 0 (0..2**n-1).each do |i| b = a.zip(i.to_s(2).rjust(n,'0').split(//).map(&:to_i)) sum = 0 b.each do |j| sum += j[0] if j[1] == 1 end max = sum if sum > max && sum <= k end p max