atcoder#ARC136C. [ARC136C] Circular Addition

[ARC136C] Circular Addition

Score : 500500 points

Problem Statement

We have an integer sequence of length NN: x=(x0,x1,,xN1)x=(x_0,x_1,\cdots,x_{N-1}) (note that its index is 00-based). Initially, all elements of xx are 00.

You can repeat the following operation any number of times.

  • Choose integers i,ki,k (0iN10 \leq i \leq N-1, 1kN1 \leq k \leq N). Then, for every jj such that iji+k1i \leq j \leq i+k-1, increase the value of xjmodNx_{j\bmod N} by 11.

You are given an integer sequence of length NN: A=(A0,A1,,AN1)A=(A_0,A_1,\cdots,A_{N-1}). Find the minimum number of operations needed to make xx equal AA.

Constraints

  • 1N2000001 \leq N \leq 200000
  • 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

A0A_0 A1A_1 \cdots AN1A_{N-1}

Output

Print the answer.

4
1 2 1 2
2

We should do the following.

  • Initially, we have x=(0,0,0,0)x=(0,0,0,0).
  • Do the operation with i=1,k=3i=1,k=3, making x=(0,1,1,1)x=(0,1,1,1).
  • Do the operation with i=3,k=3i=3,k=3, making x=(1,2,1,2)x=(1,2,1,2).
5
3 1 4 1 5
7
1
1000000000
1000000000