AtCoder Regular Contest 119 C

Posted Bcoi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AtCoder Regular Contest 119 C相关的知识,希望对你有一定的参考价值。

Problem Statement

There are N buildings along the AtCoder Street, numbered 1 through N from west to east. Initially, Buildings 1,2,,N have the heights of A1,A2,,AN, respectively.

Takahashi, the president of ARC Wrecker, Inc., plans to choose integers l and r (1l<rN) and make the heights of Buildings l,l+1,,r all zero. To do so, he can use the following two kinds of operations any number of times in any order:

  • Set an integer x (lxr1) and increase the heights of Buildings x and x+1 by 1 each.
  • Set an integer x (lxr1) and decrease the heights of Buildings x and x+1 by 1 each. This operation can only be done when both of those buildings have heights of 1 or greater.

Note that the range of x depends on (l,r).

How many choices of (l,r) are there where Takahashi can realize his plan?

Constraints

  • 2N300000
  • 1Ai109 (1iN)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A1 A2  AN

Output

Print the answer.


Sample Input 1 Copy

Copy
5
5 8 8 6 6

Sample Output 1 Copy

Copy
3

Takahashi can realize his plan for (l,r)=(2,3),(4,5),(2,5).

For example, for (l,r)=(2,5), the following sequence of operations make the heights of Buildings 2,3,4,5 all zero.

  • Decrease the heights of Buildings 4 and 5 by 1 each, six times in a row.
  • Decrease the heights of Buildings 2 and 3 by 1 each, eight times in a row.

For the remaining seven choices of (l,r), there is no sequence of operations that can realize his plan.


Sample Input 2 Copy

Copy
7
12 8 11 3 3 13 2

Sample Output 2 Copy

Copy
3

Takahashi can realize his plan for (l,r)=(2,4),(3,7),(4,5).

For example, for html

以上是关于AtCoder Regular Contest 119 C的主要内容,如果未能解决你的问题,请参考以下文章

刷题AtCoder Regular Contest 001

AtCoder Regular Contest 094

[Atcoder Regular Contest 060] Tutorial

AtCoder Regular Contest 103

AtCoder Regular Contest 128

AtCoder Regular Contest 119 C