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;

string s;

pair<int, int> inc(pair<int, int> u, pair<int, int> goodRange) {
    if (u.first == u.second && u.second == goodRange.second) return {1e9, -1e9};
    u.first++;
    u.second++;
    if (u.second > goodRange.second) u.second -= 2;
    return u;
}

pair<int, int> dec(pair<int, int> u, pair<int, int> goodRange) {
    if (u.first == u.second && u.first == goodRange.first) return {1e9, -1e9};
    u.first--;
    u.second--;
    if (u.first < goodRange.first) u.first += 2;
    return u;
}

int eval(int lowest, int delta) {
    pair<int, int> goodRange(lowest, lowest + delta);
    pair<int, int> curPos(0, 0);
    for (char u : s) {
        if (u == '1') {
            curPos = inc(curPos, goodRange);
            if (curPos.first > curPos.second) {
                return 1;
            }
        } else if (u == '0') {
            curPos = dec(curPos, goodRange);
            if (curPos.first > curPos.second) {
                return -1;
            }
        } else {
            pair<int, int> curPos1 = inc(curPos, goodRange);
            pair<int, int> curPos2 = dec(curPos, goodRange);
            curPos.first = min(curPos1.first, curPos2.first);
            curPos.second = max(curPos1.second, curPos2.second);
        }
        // cout << "char " << u << endl;
        // cout << curPos.first << ' ' << curPos.second << endl;
        // cout << goodRange.first << ' ' << goodRange.second << endl;
    }
    return 0;
}

bool check(int delta) {
    if (delta == 1) {
        return eval(-1, 1) == 0 || eval(0, 1) == 0;
    } 
    // for (int i = -delta; i <= 0; i++) {
    //     cout << eval(i, delta) << endl;
    // }
    // return 0;

    //even
    int low = -delta;
    int high = 0;
    if (low & 1) low++;
    if (high & 1) high--;
    while (high - low > 2) {
        int mid = low + high >> 1;
        if (mid & 1) mid++;
        int val = eval(mid, delta);
        if (val == 0) return 1;
        if (val == 1) high = mid;
        else low = mid;
    }
    if (eval(low, delta) == 0 ||  eval(high, delta) == 0) return 1;

    //odd
    low = -delta;
    high = 0;
    if (low % 2 == 0) low++;
    if (high % 2 == 0) high--;
    while (high - low > 2) {
        int mid = low + high >> 1;
        if (mid % 2 == 0) mid++;
        int val = eval(mid, delta);
        if (val == 0) return 1;
        if (val == 1) high = mid;
        else low = mid;
    }
    if (eval(low, delta) == 0 ||  eval(high, delta) == 0) return 1;
    return 0;
}

void solve(int test) {
    cin >> s;


    int low = 0, high = s.size();

    int minVal=  0, maxVal = 0;
    int cur = 0;
    for (char u : s) {
        if (u == '?') {
            minVal = maxVal = 0;
            cur = 0;
        } else if (u == '1') {
            cur++;
            maxVal = max(maxVal, cur);
        } else {
            cur--;
            minVal = min(minVal, cur);
        }
        low = max(low, maxVal - minVal - 1);
    }

    while (high - low > 1) {
        int mid = low + high >> 1;
        if (check(mid)) high = mid;
        else low = mid;
    }
    cout << high << '\n';
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int tc;
    // cin >> tc;
    tc = 1;
    for (int test = 1; test <= tc; test++) {
        solve(test);
    }
    return 0;
}