fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val f = (1..n).map { readLine()!!.replace(" ","").reversed().toInt(2) }
    val p = (1..n).map { readLine()!!.split(" ").map { it.toInt() } }

    (1..1023).map { openJ ->
        f.foldIndexed(0L) { i, acc, openI ->
            val count = Integer.bitCount(openJ and openI)
            return@foldIndexed acc + p[i][count]
        }
    }.max().let { println(it) }
}