#P1805. Minimum Steps to Turn Off All Smart Lamps

    ID: 15088 Type: Default 1000ms 256MiB

Minimum Steps to Turn Off All Smart Lamps

Minimum Steps to Turn Off All Smart Lamps

There is a row of (n) smart lamps placed along a road. Each lamp is either on or off. Since dawn is near, you are given the task of turning off all the lamps. However, these lamps are rather clever, and they follow certain rules regarding toggling their states:

  1. In each step, you can toggle (i.e. change the state of) exactly one lamp.
  2. Lamp 1 can be toggled at any time without restrictions.
  3. For any lamp (i) where (2 \le i \le n), you can toggle lamp (i) if and only if the following condition holds: all lamps 1, 2, ..., (i-2) are off and lamp (i-1) is on. In other words, you may toggle lamp (i) if (a_1 = a_2 = \cdots = a_{i-2} = 0) and (a_{i-1} = 1).

Your task is to determine the minimum number of steps required to turn off all the lamps.

(\text{MinSteps}(n, state))

Note that the input configuration is given as (n) followed by (n) numbers (each either 0 or 1) representing the state of each lamp in order. The final (goal) configuration is that all lamps are off (i.e. all zeros).

inputFormat

The first line contains an integer (n) (the number of lamps). The second line contains (n) space-separated integers (a_1, a_2, \ldots, a_n), where each (a_i) is either 0 (off) or 1 (on), representing the initial state of the (i)-th lamp.

outputFormat

Output a single integer: the minimum number of steps required to turn off all the lamps.

sample

2
1 1
2