#C9266. Parking Lot Toggle Operations

    ID: 53340 Type: Default 1000ms 256MiB

Parking Lot Toggle Operations

Parking Lot Toggle Operations

Given a parking lot with M slots, all initially empty, and a desired configuration represented as an array s of length M (where each element is either 0 for empty or 1 for occupied), determine the minimum number of toggle operations required to achieve the target configuration.

In one operation, you may toggle a contiguous segment of slots. Toggling a slot changes its state from 0 to 1 or from 1 to 0. Since all parking slots start at 0, the only operations needed are those that toggle segments to 1. Therefore, the solution reduces to counting the number of contiguous segments of 1's in the target configuration.

For example, if M = 5 and the target configuration is [1, 0, 1, 0, 1], there are three separate segments of 1's, and hence the minimum number of operations required is 3.

inputFormat

The input consists of two lines. The first line contains a single integer M representing the number of parking slots. The second line contains M space-separated integers (each either 0 or 1) representing the desired state for each slot.

outputFormat

Output a single integer which is the minimum number of toggle operations required to obtain the desired parking slot configuration.## sample

5
1 0 1 0 1
3