def min(a, b) return b if a == nil return a if b == nil [a, b].min end def goodness(a, b, cost) a + b - cost end n = gets.to_i c, v = gets.split.map(&:to_i) ans = nil q = [[1, 0, 0]] n.times do r = [] q.each do |a, b, cost| if a >= n ans = min(ans, cost) else r << [a + a, a, cost + c + v] r << [a + b, b, cost + v] end end q = r.sort_by {|a, b, cost| -goodness(a, b, cost)}[0...100] end p ans