#AGC040E. [AGC040E] Prefix Suffix Addition

[AGC040E] Prefix Suffix Addition

Score : 14001400 points

Problem Statement

Snuke has a sequence of NN integers x1,x2,,xNx_1,x_2,\cdots,x_N. Initially, all the elements are 00.

He can do the following two kinds of operations any number of times in any order:

  • Operation 11: choose an integer kk (1kN1 \leq k \leq N) and a non-decreasing sequence of kk non-negative integers c1,c2,,ckc_1,c_2,\cdots,c_k. Then, for each ii (1ik1 \leq i \leq k), replace xix_i with xi+cix_i+c_i.
  • Operation 22: choose an integer kk (1kN1 \leq k \leq N) and a non-increasing sequence of kk non-negative integers c1,c2,,ckc_1,c_2,\cdots,c_k. Then, for each ii (1ik1 \leq i \leq k), replace xNk+ix_{N-k+i} with xNk+i+cix_{N-k+i}+c_i.

His objective is to have xi=Aix_i=A_i for all ii. Find the minimum number of operations required to achieve it.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1Ai1091 \leq A_i \leq 10^9
  • 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 minimum number of operations required to achieve Snuke's objective.

5
1 2 1 2 1
3

One way to achieve the objective in three operations is shown below. The objective cannot be achieved in less than three operations.

  • Do Operation 11 and choose k=2,c=(1,2)k=2,c=(1,2). Now we have x=(1,2,0,0,0)x=(1,2,0,0,0).
  • Do Operation 11 and choose k=3,c=(0,0,1)k=3,c=(0,0,1). Now we have x=(1,2,1,0,0)x=(1,2,1,0,0).
  • Do Operation 22 and choose k=2,c=(2,1)k=2,c=(2,1). Now we have x=(1,2,1,2,1)x=(1,2,1,2,1).
5
2 1 2 1 2
2
15
541962451 761940280 182215520 378290929 211514670 802103642 28942109 641621418 380343684 526398645 81993818 14709769 139483158 444795625 40343083
7