#K57867. Minimum Operations to All Ones

    ID: 30516 Type: Default 1000ms 256MiB

Minimum Operations to All Ones

Minimum Operations to All Ones

You are given a binary string (s) of length (n). In one operation, you can select any non-empty substring of (s) and flip all its characters (i.e. change every '0' to '1' and every '1' to '0'). Your task is to determine the minimum number of operations required to convert (s) into a string consisting entirely of '1's.

A crucial observation is that every contiguous segment of zeros can be converted into ones with a single flip. Therefore, the answer is equal to the number of groups of consecutive zeros present in (s).

inputFormat

The input is given via standard input (stdin) and consists of two lines:

  • The first line contains a single integer (n) ((1 \le n \le 10^5)) which represents the length of the binary string.
  • The second line contains the binary string (s) which is a sequence of (n) characters ('0' or '1').

outputFormat

Output a single integer to standard output (stdout): the minimum number of operations required to change all characters in (s) to '1'.## sample

5
11001
1