atcoder#ARC119C. [ARC119C] ARC Wrecker 2

[ARC119C] ARC Wrecker 2

Score: 500500 points

Problem Statement

There are NN buildings along the AtCoder Street, numbered 11 through NN from west to east. Initially, Buildings 1,2,,N1, 2, \ldots, N have the heights of A1,A2,,ANA_1, A_2, \dots, A_N, respectively.

Takahashi, the president of ARC Wrecker, Inc., plans to choose integers ll and rr (1l<rN)(1 \leq l \lt r \leq N) and make the heights of Buildings l,l+1,,rl, l+1, \dots, 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 xx (lxr1)(l \leq x \leq r-1) and increase the heights of Buildings xx and x+1x+1 by 11 each.
  • Set an integer xx (lxr1)(l \leq x \leq r-1) and decrease the heights of Buildings xx and x+1x+1 by 11 each. This operation can only be done when both of those buildings have heights of 11 or greater.

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

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

Constraints

  • 2N3000002 \leq N \leq 300000
  • 1Ai1091 \leq A_i \leq 10^9 (1iN)(1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \cdots ANA_N

Output

Print the answer.

5
5 8 8 6 6
3

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

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

  • Decrease the heights of Buildings 44 and 55 by 11 each, six times in a row.
  • Decrease the heights of Buildings 22 and 33 by 11 each, eight times in a row.

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

7
12 8 11 3 3 13 2
3

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

For example, for (l,r)=(3,7)(l, r) = (3, 7), the following figure shows one possible solution.

10
8 6 3 9 5 4 7 2 1 10
1

Takahashi can realize his plan for (l,r)=(3,8)(l, r) = (3, 8) only.

14
630551244 683685976 249199599 863395255 667330388 617766025 564631293 614195656 944865979 277535591 390222868 527065404 136842536 971731491
8