import scala.collection.mutable.ArrayBuffer
object Main extends App {
  import scala.io.StdIn._
  val w = readInt
  val a = readLine.trim.split(' ').map(_.toInt)
  val nearestRight: Array[Int] = a.indices.map(i ⇒ if (a(i) != 0) Int.MaxValue else i).toArray.also(arr ⇒ arr.indices.reverse.tail.foreach(i ⇒ arr(i) = math.min(arr(i), arr(i + 1))))
  val nearestLeft: Array[Int] = a.indices.map(i ⇒ if (a(i) != 0) Int.MinValue else i).toArray.also(arr ⇒ arr.indices.tail.foreach(i ⇒ arr(i) = math.max(arr(i), arr(i - 1))))
  val limitFromLeft = MinSegmentTree(a.indices.map(i ⇒ if (a(i) >= 0) Int.MaxValue else a(i).abs + startTimeFromLeft(i)).toArray)
  val limitFromRight = MinSegmentTree(a.indices.map(i ⇒ if (a(i) >= 0) Int.MaxValue else a(i).abs + startTimeFromRight(i)).toArray)
  println(
    a.indices.filter(a(_) > 0).map(i ⇒ math.max(canMoveLeft(i, a(i), nearestLeft, limitFromRight), canMoveRight(i, a(i), nearestRight, limitFromLeft))).sum
  )
  def canMoveRight(position: Int, time: Int, nearestRight: Array[Int], limitFromLeft: MinSegmentTree): Int = {
    if ((0 until w).contains(nearestRight(position)) && startTimeFromLeft(position) <= limitFromLeft.min(position, nearestRight(position))){
      val t = startTimeFromLeft(position)
      val min = limitFromLeft.min(position, nearestRight(position))
      (startTimeFromLeft(position) until math.min(startTimeFromLeft(position) + time, limitFromLeft.min(position, nearestRight(position)))).length
    }else 0
  }
  def canMoveLeft(position: Int, time: Int, nearestLeft: Array[Int], limitFromRight: MinSegmentTree): Int = {
    if ((0 until w).contains(nearestLeft(position)) && startTimeFromRight(position) <= limitFromRight.min(nearestLeft(position), position)){
      (startTimeFromRight(position) until math.min(startTimeFromRight(position) + time, limitFromRight.min(nearestLeft(position), position))).length
    }else 0
  }

  def startTimeFromLeft(to: Int): Int = -to
  def startTimeFromRight(to: Int): Int = to + 1 - w
  object MinSegmentTree {
    def apply(array: Array[Int]): MinSegmentTree = {
      val arr = ArrayBuffer[Array[Int]](array)
      while (arr.last.length != 1){
        arr.append(Array.tabulate(arr.last.length / 2){_ ⇒ Int.MaxValue})
      }
      for (depth ← arr.indices.tail; i ← arr(depth).indices){
        arr(depth)(i) = math.min(arr(depth - 1)(i * 2), arr(depth - 1)(i * 2 + 1))
      }
      new MinSegmentTree(arr.toArray)
    }
  }
  class MinSegmentTree private (private val array: Array[Array[Int]]){
    def min(from: Int, to: Int): Int = {
      var depth = 0
      var left = from
      var right = to
      var min = Int.MaxValue
      while (left <= right){
        if (left % 2 == 1){
          min = math.min(min, array(depth)(left))
          left += 2
        }
        if (right % 2 == 0){
          min = math.min(min, array(depth)(right))
          right -= 2
        }
        left /= 2
        right /= 2
        depth += 1
      }
      min
    }
  }
  implicit class Scope[T](val value: T) extends AnyVal {
    @inline
    def let[A](func: T ⇒ A): A = func(value)
    @inline
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
  }
}


Measured memory_bytes: 100216