Problem:
Score : 800 points

Problem Statement

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

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

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

Find the minimum possible unbalancedness of S'.

Constraints

1 \leq |S| \leq 10^6

Each character of S is 0, 1, or ?.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum possible unbalancedness of S'.

Sample Input 1

0??

Sample Output 1

1

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

Sample Input 2

0??0

Sample Output 2

2

Sample Input 3

??00????0??0????0?0??00??1???11?1?1???1?11?111???1

Sample Output 3

4

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
template<typename T>
void fin(T const& t){ std::cout << t << std::endl; exit(0); }

int main() {
  std::cin.tie(0); std::ios::sync_with_stdio(false);
  std::string s; std::cin >> s;
  int da[4], db[4];
  da[1] = db[0] = da[3] = db[3] = -1;
  da[0] = db[1] = 1;
  int a0 = 0, b0 = 0, a1 = -1, b1 = -1, ma = 0, mb = 0;
  for(int i = 0; i < s.size(); ++i) {
    char c = s[i];
    ma += da[c&3]; if(ma < 0) ma = 0;
    mb += db[c&3]; if(mb < 0) mb = 0;
    if(i&1) {
      a0 = std::max(a0, ma);
      b0 = std::max(b0, mb);
    } else {
      a1 = std::max(a1, ma);
      b1 = std::max(b1, mb);
    }
  }
  ma = std::max(a0, a1); if(a0 == a1) ++ma;
  mb = std::max(b0, b1); if(b0 == b1) ++mb;
  fin(std::max(ma, mb));
  return 0;
}