No.1765 While Shining
Problem Statement
There is a grid of $N$ cells arranged in a horizontal row. The $i$-th cell from the left is called cell $i$. Initially, cell $i$ is shining if $A_i=1$, and is not shining if $A_i=0$.
You will now place a robot on one of the cells. The placed robot repeatedly performs the following sequence of actions.
- If the current cell is cell $N$ or is not shining, stop operating.
- Move to the cell $1$ to the right, then invert the "shining" / "not shining" state of all cells.
For each cell, find the number of times the robot moves when it is placed on that cell, and output the sum of these values.
Input
$N$ $A_1$ $A_2$ $\dots$ $A_N$
- All inputs are integers.
- $1 \le N \le 2 \times 10^5$
- $0 \le A_i \le 1$
Output
Output the sum of the number of times the robot moves, followed by a newline.
Sample
Sample 1
Input
5 1 1 1 0 1
Output
4
If the robot is placed on cell $1$ or cell $2$, then immediately after it moves $1$ time, the current cell changes to the not shining state, so it stops at the cell $1$ to the right.
If the robot is placed on cell $3$, then immediately after each move, the current cell changes to the shining state, so it reaches cell $5$.
Sample 2
Input
8 0 1 0 1 1 0 0 1
Output
6
Sample 3
Input
15 0 0 0 1 1 0 0 0 1 0 1 0 0 1 1
Output
10
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。