Dyzio

Time limit1sMemory limit128 MB

Summary
Parse a 0/1 description of recursive halving cuts and output the cut count at which the first shortest piece appears.
Level

Medium4 of 10

Topics
Tree, DFS, Recursion, Implementation
Solved
No attempts yet

Problem

Dyzio is Jasiek's friend and, like Jasiek, loves puzzles. Here is the puzzle Dyzio brought to Jasiek:

Jasiek, here is a rope that has to be cut into smaller pieces. I won't tell you directly how to do it. Instead, look at this sequence of zeros (0) and ones (1). Read it from left to right using these rules:

  • If the description of the piece you are looking at starts with 1, cut that piece exactly in half. Right after this 1 I wrote (using the very same rules) what to do with the left part, and right after that description I wrote what to do with the right part.
  • If the description of the piece is 0, then that single 0 is the whole description of the piece and means "make no cut, keep it whole." In particular, if the entire sequence is a single 0, the rope is left in one piece.
  • You must always finish cutting the left part before you may start on the right part.

The rope starts as a single piece. Each cut splits a piece into two equal halves, so a piece's length depends only on how many times it has been cut. Cutting proceeds in the order the sequence dictates (always the left part before the right part).

Find the minimum number of cuts needed to obtain the (first) shortest piece.

Write a program that:

  • reads the description of how to cut the rope from standard input,
  • computes the minimum number of cuts needed to obtain the (first) shortest piece,
  • prints the result to standard output.

Input

The first line contains an integer nn (1≤n≤200001 \le n \le 20000). The second line contains a single 0/1 word of length nn (a string of zeros and ones with no spaces between them): Dyzio's description of how to cut the rope.

Output

Print a single line containing one integer: the minimum number of cuts needed to obtain the (first) shortest piece.

Examples3

  1. Example 1

    Input
    9
    110011000
    
    Expected output
    4
    
  2. Example 2

    Input
    1
    0
    
    Expected output
    0
    
  3. Example 3

    Input
    3
    100
    
    Expected output
    1