atcoder#AGC045B. [AGC045B] 01 Unbalanced

[AGC045B] 01 Unbalanced

Score : 800800 points

Problem Statement

Given is a string SS, where each character is 0, 1, or ?.

Consider making a string SS' by replacing each occurrence of ? with 0 or 1 (we can choose the character for each ? independently). Let us define the unbalancedness of SS' as follows:

  • (The unbalancedness of SS') =max{= \max \{ The absolute difference between the number of occurrences of 0 and 1 between the ll-th and rr-th character of SS (inclusive) : 1lrS}:\ 1 \leq l \leq r \leq |S|\}

Find the minimum possible unbalancedness of SS'.

Constraints

  • 1S1061 \leq |S| \leq 10^6
  • Each character of SS is 0, 1, or ?.

Input

Input is given from Standard Input in the following format:

SS

Output

Print the minimum possible unbalancedness of SS'.

0??
1

We can make S=S'= 010 and achieve the minimum possible unbalancedness of 11.

0??0
2
??00????0??0????0?0??00??1???11?1?1???1?11?111???1
4