object Main extends App {
	val N = scala.io.StdIn.readInt

	val F = Array.ofDim[Int](N,10)
	val P1 = new Array[Int](N)
	val P2 = new Array[Int](N)

	var flag = true

	for(i<-0 until N){
		val temp = scala.io.StdIn.readLine.split(" ").map(_.toInt)
		F(i) = temp
	}

	for(i<-0 until N){
		val temp = scala.io.StdIn.readLine.split(" ").map(_.toInt)
		P1(i) = temp(0)
		P2(i) = temp.dropRight(10 - F(i).sum).drop(1).max
		if(P1(i) > P2(i)) flag = false
	}

	if(!flag){
		val t = P1.indexOf(P1.max)
		P2(t) = P1(t)
	}

	println(P2.sum)

	
}