t = gets.to_i n = gets.to_i $c_list = gets.split.map(&:to_i) $v_list = gets.split.map(&:to_i) $max_manzoku = 0 def manzoku(kore) man = 0 kore.each_with_index do |count, i| m = $v_list[i] count.times do man += m m /= 2 end end return man end def noru_ato(nokori, ima, kore) if ima == n - 1 kore << nokori / $c_list[ima] $max_manzoku = [manzoku(kore), $max_manzoku].max else count = 0 loop do next_nokori = nokori - count * $c_list[ima] if next_nokori <= 0 noru_ato(next_nokori, ima + 1, kore + [count]) count += 1 else break end end end end noru_ato(t, 0, []) puts $max_manzoku