n = gets.to_i
ff = []
n.times do
  ff << gets.split.map(&:to_i)
end

pp = []
n.times do
  pp << gets.split.map(&:to_i)
end

flg = false
ans = 0
pp.each.with_index do |_p,i|
  cnt = ff[i].count(1)
  use = pp[i][0, cnt+1]
  max = use.max
  if use.count(max) == 1
    if use.index(max) != 0
      flg = true
    end
  else
    flg = true
  end
  ans += max
end

if !flg
  ans = -Float::INFINITY
  pp.each.with_index do |_p,i|
    cnt = ff[i].count(1)
    use = pp[i][1, cnt]
    max = use.max
    ans = [ans, max].max
  end
end

puts ans
