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;
using ll = long long;
int main() {
ll Z=0;
string s;
cin>>s;
ll rui=0;
ll a=0;
ll f=0;
ll s1=s.size();
ll b[s1+1];
ll c[s1+1];
for (ll i=0;i<s1;i++){
if(s.at(i)=='1'){
a++;}
else{
a--;}
b[i]=a;
c[i]=a;
Z=max(Z,a);}
for (ll i=s1-2;i>=0;i--){
b[i]=max(b[i],b[i+1]);}
for (ll i=0;i<s1;i++){
if(s.at(i)=='1'){
rui++;}
else if(s.at(i)=='0'){
rui--;}
else{
if(rui-c[i-1]+b[i]+2<=Z){
rui++;}
else{
rui--;}}
f=min(f,rui);}
ll Z1=Z+1;
ll f1=0;
rui=0;
for (ll i=0;i<s1;i++){
if(s.at(i)=='1'){
rui++;}
else if(s.at(i)=='0'){
rui--;}
else{
if(rui+b[i]-c[i-1]+2<=Z1){
rui++;}
else{
rui--;}}
f1=min(f1,rui);}
ll ans=min(Z-f,Z1-f1);
if(s.at(0)=='?'){
ans=1;}
cout<<ans<<endl;}