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: Time Limit Exceeded)

#include <bits/stdc++.h>

using namespace std;

string s;
string ss;

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;
}

bool check(int delta) {
    // first: low
    // second : high
    pair<int, int> goodRange(-delta, delta);
    pair<int, int> curPos(0, 0);
    pair<int, int> lastGoodRange = goodRange;
    int swapTime = 0;
    while (1) {
        for (char u : s) {
            if (u == '1') {
                curPos = inc(curPos, goodRange);
            } else if (u == '0') {
                curPos = dec(curPos, goodRange);
            } 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);
            }
            if (curPos.first > curPos.second) {
                if (swapTime) {
                    s.swap(ss);
                }
                return 0;
            }
            goodRange.first = max(goodRange.first, curPos.first - delta);
            goodRange.second = min(goodRange.second, curPos.second + delta);
            // cout << "char " << u << endl;
            // cout << curPos.first << ' ' << curPos.second << endl;
            // cout << goodRange.first << ' ' << goodRange.second << endl;
        }
        if (lastGoodRange == goodRange) break;
        lastGoodRange = goodRange;
        s.swap(ss);
        swapTime ^= 1;
    }
    if (swapTime) s.swap(ss);
    return 1;
}

void solve(int test) {
    cin >> s;
    ss = s;
    reverse(ss.begin(), ss.end());
    for (char &u : ss) {
        if (u == '1') u = '0';
        else if (u == '0') u = '1';
    }

    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;
}