#K14871. Maximum Balanced Substrings

    ID: 24231 Type: Default 1000ms 256MiB

Maximum Balanced Substrings

Maximum Balanced Substrings

Given a binary string s of length n, partition the string into the maximum number of balanced substrings. A substring is considered balanced if it contains an equal number of 0's and 1's. In other words, if we define the balance of a substring as \(\text{balance} = \#0 - \#1\), then a balanced substring satisfies \(\text{balance} = 0\).

Your task is to determine the maximum possible number of such balanced substrings that can be obtained by scanning the string from left to right.

inputFormat

The input consists of two lines:

  1. The first line contains an integer \(n\) (\(n \ge 1\)) representing the length of the binary string.
  2. The second line contains a binary string \(s\) of length \(n\), consisting only of characters 0 and 1.

outputFormat

Output a single integer — the maximum number of balanced substrings that can be formed from s.

## sample
8
01010101
4