
object Main extends App {
  import scala.io.StdIn._
  val n = readInt()
  val fs = for (_ <- 1 to n) yield {readLine().split(' ').map(_ == "1")}
  val ps = for (_ <- 1 to n) yield {readLine().split(' ').map(_.toInt)}
  val shops = fs.zip(ps).map{case (b, i) => Shop(b, i)}
  case class Shop(isOpen:Seq[Boolean], profit:Seq[Int]){
    def joisinosShopOpen(joisinoIsOpen:Boolean):Shop = {
      if (joisinoIsOpen && isOpen.head) {
        Shop(isOpen.tail, profit.tail)
      }else{
        Shop(isOpen.tail, profit)
      }
    }
  }
  def calMaxProfit(otherShops:Seq[Shop], isOpenAtLeastOnce:Boolean = false):Int = {
    otherShops.head match{
      case Shop(Nil, _) => if (isOpenAtLeastOnce) otherShops.map(_.profit.head).sum else Int.MinValue
      case _ => List(true, false).map(b => calMaxProfit(otherShops.map(_.joisinosShopOpen(b)), isOpenAtLeastOnce | b)).max
    }
  }
  println(calMaxProfit(shops))
}
