#C2121. Turn All Lamps On
Turn All Lamps On
Turn All Lamps On
You are given a row of N lamps, each either on or off. The state of each lamp is represented by 1 (on) or 0 (off). You are allowed to perform the following operation:
Operation: Select any two consecutive lamps and switch their states (i.e. if a lamp is 0 it becomes 1, and if it is 1 it becomes 0).
Your goal is to turn all the lamps on. In other words, after applying a sequence of operations, each lamp should have a state of 1. Formally, if the lamp states are represented as a sequence \(A_1, A_2, \dots, A_N\), you need to achieve \(A_i = 1\) for all \(1 \le i \le N\).
Although the operation toggles exactly two consecutive lamps, the minimum number of operations required is equal to the number of lamps that are initially off (i.e. equal to 0). This is because by carefully choosing the operations, each off lamp can be corrected individually. For example, if a lamp is off, it's guaranteed that it can be switched to on with one operation. Based on this strategy, if there are \(k\) lamps off, the answer is \(k\).
Note: It is assumed that the optimal sequence of operations exists such that the number of operations is exactly the count of off lamps.
inputFormat
The input is given via standard input and consists of two lines:
- The first line contains an integer \(N\) denoting the number of lamps.
- The second line contains \(N\) space-separated integers \(A_1, A_2, \dots, A_N\) where each \(A_i\) is either 0 (lamp is off) or 1 (lamp is on).
outputFormat
Output a single integer representing the minimum number of operations required to turn all lamps on.
## sample5
1 0 0 1 0
3
</p>