B - Count 1's Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

0,1 からなる長さ N の整数列 A=(A_1,A_2,\cdots,A_N) が与えられます.

あなたはこれから,次の操作をちょうど 1 回行います.

  • A連続する部分列を選び,そこに含まれる要素を flip する.つまり,0 ならば 1 に変え,1 ならば 0 に変える. なお,ここで選ぶ部分列は空であることも許され,その場合 A の要素は全く変化しない.

あなたのスコアは,A に含まれる 1 の個数です. あなたが取るスコアとしてあり得る値が何通りあるか求めてください.

制約

  • 1 \leq N \leq 3 \times 10^5
  • 0 \leq A_i \leq 1
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる.

N
A_1 A_2 \cdots A_N

出力

答えを出力せよ.


入力例 1

4
0 1 1 0

出力例 1

4

スコアとしてあり得る値は,0,1,2,34 通りです. 例えば,A2 番目から 4 番目までの要素を flip すると,A=(0,0,0,1) となり,スコアは 1 になります.


入力例 2

5
0 0 0 0 0

出力例 2

6

入力例 3

6
0 1 0 1 0 1

出力例 3

3

Score : 400 points

Problem Statement

You are given an integer sequence of length N consisting of 0 and 1: A=(A_1,A_2,\cdots,A_N).

You will do the operation below exactly once.

  • Choose a contiguous subsequence of A and flip the elements in it: convert 0 to 1 and vice versa. Here, you may choose an empty subsequence, in which case the elements of A do not change.

Your score will be the number of 1's in A. How many values are there that your score can take?

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 0 \leq A_i \leq 1
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answer.


Sample Input 1

4
0 1 1 0

Sample Output 1

4

There are four possible values for your score: 0, 1, 2, 3. For example, if you flip the 2-nd through 4-th elements of A, you will get A=(0,0,0,1), for a score of 1.


Sample Input 2

5
0 0 0 0 0

Sample Output 2

6

Sample Input 3

6
0 1 0 1 0 1

Sample Output 3

3