def get_donguri(donguri, from_rate, to_rate)
  rate = from_rate.zip(to_rate)
  rate = rate.reject { |r1, r2| r1 >= r2 }
             .sort { |r1, r2| (1.0 * r1[1]) / r1[0] <=> (1.0 * r2[1]) / r2[0] }
  dp = [0] * (donguri + 1)

  rate.each do |r|
    0.upto(donguri - r[0]) do |i|
      dp[i + r[0]] = dp[i] + r[1]
    end
  end
  0.upto(donguri) do |i|
    dp[i] += donguri - i
  end
  dp.max
end


N = gets.chop.to_i
A_RATE = gets.chop.split.map(&:to_i)
B_RATE = gets.chop.split.map(&:to_i)

n = get_donguri(N, A_RATE, B_RATE)
n = get_donguri(n, B_RATE, A_RATE)

puts n

