import java.util.Scanner

object Main extends App {
  val sc = new Scanner(System.in)

  val n = sc.nextInt()
  val d = Array.ofDim[Long](n, n)
  val a = d.clone
  for(i <- 0 until n)
    for(j <- 0 until n)
        if(i == j)
          d(i)(j) = 0
        else
          d(i)(j) = 1e18.toLong

  for(i <- 0 until n)
    a(i) = sc.nextInt()

  // to d()()
  def warshall_floyd() =
    for(k <- 0 until v; i <- 0 until v; j <- 0 until v){
      d(i)(j) = Math.min(d(i)(j), d(i)(k) + d(k)(j))
    }

  
  // process
  var ans = 0
  for(x <- 0 until n-1)
    for(y <- x until n)
      ans += a(x)(y)

  warshall_floyd()

  var flag = true
  for(x <- 0 until n-1){
    for(y <- x until n){
      val now = a(x)(y)
      if(now > d(x)(y))
        flag = false
      else if(now == d(x)(y)){
        ans -= now
      }
    }
  }


  if(flag)
    println(ans)
  else
    println(-1)
}
