object Main extends App {
	val num = io.StdIn.readLine().toInt
	val P = io.StdIn.readLine().split(" ").map(_.toInt)
	val second = {
		for {
			l <- 0 until num - 1
			r <- l + 1 until num
		} yield {
			val slice = P.slice(l, r + 1)
			slice.filterNot(_ == slice.max).max
		}
	}
	println(second.sum)
}