
object Main {

  def main(args: Array[String]): Unit = {

    val N = readLine()
    val array = readLine().split(" ").map(_.toInt)

    println(answer(array))
    

  }

  def answer( array: Array[Int]): Int = {
    (for {
      i <- 2 to array.length
    } yield {
      array.sliding(i).map(second(_)).sum
    }).sum

    
  }


  def second(array: Array[Int]) = {
    val n = array.foldLeft((0,0))( (t,n) => {
      if(n > t._2) (t._2, n)
      else if(n > t._1) (n, t._2)
      else t
    })._1

    n
  }

}