n=gets.to_i
f=[]
n.times do
  f << gets.split.map(&:to_i)
end
q=[]
n.times do
  q << gets.split.map(&:to_i)
end
fl=false
a=0
mi=0
mm=-1/0.0
n.times do |i|
  a+=q[i][0..f[i].count(1)].max
  if !fl&&f[i].include?(1)&&q[i][0]<=q[i][1..f[i].count(1)].max
    fl=true
  end
  if f[i].include?(1)&&mm<q[i][1..f[i].count(1)].max-q[i][0]
    mi=i
    mm=q[i][1..f[i].count(1)].max-q[i][0]
  end
end
if fl
  puts a
else
  a=0
  n.times do |i|
    if i==mi
      a+=mm
    else
      a+=q[i][0]
    end
  end
  puts a
end
