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;
typedef long long ll;
const int N=1000005,p=998244353,inf=0x3f3f3f3f;
int read(){
	int f=1,g=0;
	char ch=getchar();
	for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-1;
	for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0';
	return f*g;
}
int n,g[N],ans;
char ch[N];
int work(int R){
	int t=0,cur=0;
	for (int i=1;i<=n;i++){
		if (ch[i]!='?') cur+=(ch[i]=='1') ? 1 : -1;
		else cur+=(cur+1+g[i+1]<=R) ? 1 : -1;
		t=min(t,cur);
	}
	return t;
}
int main(){
//	freopen("a.in","r",stdin);
	scanf("%s",ch+1);
	n=strlen(ch+1);
	for (int i=n;i;i--)	g[i]=max(0,g[i+1]+(ch[i]=='1') ? 1 : -1);
	ans=min(g[1]-work(g[1]),g[1]+1-work(g[1]+1));
	printf("%d\n",ans);
	return 0;
}