#P1805. 关灯

    ID: 759 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>高精度递推NOI 导刊线性递推,递推式

关灯

Description

Along a road, there are nn lights in a row; some are on, and some are off.

Dawn is about to break, and you are given a task: turn all the lights off.

However, these lights are rather smart and cannot be turned off easily. Their on/off behavior follows the rules below:

  • In each step, you may turn on or off exactly one light.
  • The light with index 11 can be turned on or off freely.
  • If lights 1,2,,k11, 2, \cdots, k-1 are all off and light kk is on, then you may freely turn on or off light k+1k+1.

Before turning off the lights, please compute the minimum number of steps needed to turn all the lights off.

Input Format

The first line contains an integer nn, the number of lights.

The second line contains nn integers. If the ii-th integer Oi=0O_i = 0, the ii-th light is initially off; if Oi=1O_i = 1, the ii-th light is initially on.

Output Format

Output a single integer on one line, the minimum number of steps required to turn off all the lights.

4
1 0 1 0
6

Hint

[Output Explanation]

  • Initial state: 1010.
  • Step 1: 1110.
  • Step 2: 0110.
  • Step 3: 0100.
  • Step 4: 1100.
  • Step 5: 1000.
  • Step 6: 0000.

Constraints

  • For 40%40\% of the testdata, n30n \le 30.
  • For 70%70\% of the testdata, n300n \le 300.
  • For 100%100\% of the testdata, n1000n \le 1000.

Translated by ChatGPT 5