import scala.collection.mutable.HashMap
import scala.collection.mutable.ArrayBuffer

object Main extends App {
  val inputs: Seq[String] = scala.io.Source.stdin.getLines().toList
  val d  = inputs(0).toInt // 365
  val ci = inputs(1).split(" ").map(_.toInt)
  val sdi = HashMap.empty[Int, Seq[Int]]
  // 日付ごとに、満足度の配列を持つ
  inputs.splitAt(2)._2.zipWithIndex.foreach { case (line, index) =>
    sdi.update(index, line.split(" ").map(_.toInt))
  }

  solve(d, ci, sdi)

  // 1. 単純にでかい数を持っているものを抽出
  // 2. 減少分と上昇分を考慮して最大になる物を選ぶ
  def solve(d: Int, ci: Array[Int], sdi: HashMap[Int, Seq[Int]]): Unit = {
    val last = ArrayBuffer.fill(26)(1)
    var score = 0
    val range = Range(0, 26, 1)

    (0 until d).foreach { day =>
      val todayValues = sdi(day)
      val max = todayValues.max
      // println(s"max value = $max")
      // TODO: 同じ数値のものが複数ある可能性を考慮
      val maxIndexes = range.map { i =>
        if (max == todayValues(i)) {
          i
        } else {
          -1
        }
      }.filter(v => v != -1)

      val minCiOfTopValue = maxIndexes.map(v => ci(v)).min
      val minCiOfTopValueIndex = range.filter(i => maxIndexes.exists(_ == i) && ci(i) == minCiOfTopValue).min
      val i = minCiOfTopValueIndex + 1

      // println(s"selected: $i")

      last.update(i, d)
      score = score + max - range.map(i => ci(i) * (day - last(i)) ).sum
      // println(s"score: $score")
    }

  }

}