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: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int) (x).size())
typedef long long ll;
typedef pair<int,int> pii;
template<class T> void print(T & x){ cout << x; }
template<class T,class U> void print(pair<T,U> & p){cout << "("; print(p.first); cout << ", "; print(p.second); cout << ")"; }
template<class T> void print(vector<T> & v) {
  cout << "{";
  if (sz(v)) print(v[0]);
  for (int i = 1; i < sz(v); i++) cout << ", ", print(v[i]);
  cout << "}\n";
}

string S;
int res = 1;

int solve(int m){
  vector<int> sm(sz(S));
  int cnt = 0;
  for(int i=0; i<sz(S); i++){
    if(S[i] == '1') cnt++;
    else cnt--;
    sm[i] = cnt;
  }
  for(int i=sz(S)-2; i>=0; i--)
    sm[i] = max(sm[i], sm[i+1]);
  int add = 0;
  int minim = sz(S);
  cnt = 0;
  for(int i=0; i<sz(S); i++){
    if(S[i] != '?')
      cnt += (S[i]-'0')*2-1;
    else{
      if(sm[i]+add+2 <= m)
        cnt++, add += 2;
      else
        cnt--;
    }
    minim = min(minim, cnt);
  }
  return m-minim;
}

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> S;
  int z = 0;
  int cnt = 0;
  for(int i=0; i<sz(S); i++){
    if(S[i] == '1') cnt++;
    else cnt--;
    z = max(z, cnt);
  }
  cout << min(solve(z), solve(z+1)) << endl;
}