import java.util.Scanner
import scala.collection.mutable.Stack 

object Main extends App { 
  val sc = new Scanner(System.in)
  val N = sc.nextInt()
  val list = (for (i <- 0 until N) yield sc.nextLong()).toList

  class Histgram(list:List[Long]) {
    val bars:Array[Long] = list.toArray
    var stack = Stack[Int]()

    @scala.annotation.tailrec
    final def solver(i:Int, m:Long):Long = {
      def compute(i:Int, m:Long):Long = {
        if (stack.isEmpty || i < N && bars(stack.top) <= bars(i)) m
        else {
          val t = stack.pop
          val n = bars(t) * (if (stack.isEmpty) i else (i -stack.top -1))

          compute(i, n max m)
        }
      }

      if (i == N) compute(i, m)
      else if (stack.isEmpty || bars(stack.top) <= bars(i)) {
        stack.push(i)
        solver(i+1, m)
      }
      else solver(i, compute(i, m))
    }
  }

  val hst = new Histgram(list)

  println(hst.solver(0, 0))

}
