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<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#include<map>
#include<bitset>
#include<set>
#include<assert.h>
#include<chrono>
#include<random>
#include<iostream>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define pll pair<long long,long long>
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define VI vector<int>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline LL read()
{
	LL f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int stack[20];
template<typename T>inline void write(T x)
{
	if(x<0){putchar('-');x=-x;}
    if(!x){putchar('0');return;}
    int top=0;
    while(x)stack[++top]=x%10,x/=10;
    while(top)putchar(stack[top--]+'0');
}
template<typename T>inline void pr1(T x){write(x);putchar(' ');}
template<typename T>inline void pr2(T x){write(x);putchar('\n');}
template<typename T>inline void chkmin(T &x,T y){x=x<y?x:y;}
template<typename T>inline void chkmax(T &x,T y){x=x>y?x:y;}
const int MAXN=1000005;
struct node
{
	int L,R,pos;node(){}
	node(int _L,int _R,int _pos){L=_L;R=_R;pos=_pos;}
};
vector<node> vec,las,one;
char ch[MAXN];
node move2(node u)//左上 
{
	if(u.L-1>=1)return node(u.L-1,u.R-1,u.pos);
	if(u.R-1>=1)return node(1,u.R-1,u.pos);
	else return node(1,1,u.pos+1);
}
node move3(node u)//右下 
{
	if(u.R+1<=u.pos+1)return node(u.L+1,u.R+1,u.pos);
	if(u.L+1<=u.pos+1)return node(u.L+1,u.pos+1,u.pos);
	else return node(u.pos+2,u.pos+2,u.pos+1);
}
pair<node,node> move1(node z)
{
	node u=move2(z),v=move3(z);
	if(u.pos==v.pos)return mp(node(min(u.L,v.L),max(u.R,v.R),u.pos),node(-1,-1,-1));
	else if(z.R-z.L>=1)
	{
		if(u.pos<v.pos)return mp(u,node(-1,-1,-1));
		else return mp(v,node(-1,-1,-1));
	}
	else return mp(u,v);
}
void trans1()
{
	las.clear();
	for(auto p:vec)las.emplace_back(move2(p));
	vec=las;
}
void trans0()
{
	las.clear();
	for(auto p:vec)las.emplace_back(move3(p));
	vec=las;
}
bool cmp(node n1,node n2)
{
	if(n1.L!=n2.L)return n1.pos<n2.pos;
	return n1.pos<n2.pos;
}
void trans2()
{
	las.clear();one.clear();
	for(auto p:vec)
	{
		pair<node,node> T=move1(p);
		if(T.second.L==-1)
		{
			node z=T.first;
			if(z.R-z.L>=1)las.emplace_back(z);
			else one.emplace_back(z);
		}
		else one.emplace_back(T.first),one.emplace_back(T.second);
	}
	sort(one.begin(),one.end(),cmp);
	int lasx=0;
	for(auto p:one)
	{
		if(p.L!=lasx)las.emplace_back(p);
		lasx=p.L;
	}vec=las;
}
int main()
{
//	freopen("b.in","r",stdin);
//	freopen("b.out","w",stdout);
	#ifdef Rose
		double BeginJudgeTime=clock();
	#endif
	scanf("%s",ch+1);int len=strlen(ch+1);
	reverse(ch+1,ch+1+len);
	vec.emplace_back(node(1,1,0));
	for(int i=1;i<=len;i++)
	{
		if(ch[i]=='1')trans1();
		else if(ch[i]=='0')trans0();
		else trans2();
	}
	int ans=1e9;
	for(auto p:vec)chkmin(ans,p.pos);
	pr2(ans);
	#ifdef Rose
		double EndJudgeTime=clock();
		cerr<<"JudgeTime is"<<" ";
		cerr<<(EndJudgeTime-BeginJudgeTime)/CLOCKS_PER_SEC<<endl;
	#endif
	return 0;
}