Language: C++

#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 1000006;

int n;
char a[N];

bool stg1()
{
    bool z = true;
    for (int i = 0; i < n; ++i)
    {
        if (i % 2 == 0)
        {
            if (a[i] == '1')
            {
                z = false;
                break;
            }
        }
        else
        {
            if (a[i] == '0')
            {
                z = false;
                break;
            }
        }
    }
    if (z)
        return true;
    z = true;
    for (int i = 0; i < n; ++i)
    {
        if (i % 2 == 1)
        {
            if (a[i] == '1')
            {
                z = false;
                break;
            }
        }
        else
        {
            if (a[i] == '0')
            {
                z = false;
                break;
            }
        }
    }
    return z;
}

void solv0()
{
    int ans = N;
    for (int x = 0; x < (1 << v.size()); ++i)
    {
        for (int i = 0; i < v.size(); ++i)
        {
            if ((x & (1 << i)))
            {
                a[v[i]] = '1';
            }
            else
            {
                a[v[i]] = '0';
            }
        }
        int p = 0;
        int minu = 0, maxu = 0;
        int yans = 0;
        for (int i = 0; i < n; ++i)
        {
            if (a[i] == '0')
                --p;
            else
                ++p;
            yans = max(yans, max(abs(p - minu), abs(p - maxu)));
            minu = min(minu, p);
            maxu = max(maxu, p);
        }
        ans = min(ans, yans);
    }
    printf("%d\n", ans);
}

void solv()
{
    scanf(" %s", a);
    n = strlen(a);
    vector<int> v;
    for (int i = 0; i < n; ++i)
    {
        if (a[i] == '?')
            v.push_back(i);
    }
    if (stg1())
    {
        printf("1\n");
        return;
    }
    if (v.size() < 10)
    {
        solv0();
        return;
    }
    int minu = 0, maxu = 0;
    int ans = 1;
    for (int i = 0; i < n; ++i)
    {
        if (a[i] == '0')
        {
            --minu;
            --maxu;
        }
        else if (a[i] == '1')
        {
            ++minu;
            ++maxu;
        }
        else
        {
            ++minu;
            --maxu;
            minu = min(0, minu);
            maxu = max(0, maxu);
        }
        ans = max(ans, max(abs(minu), abs(maxu)));
    }
    ans = max(ans, 2);
    printf("%d\n", ans);
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    //ios_base::sync_with_stdio(false), cin.tie(0);
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


Compiler output:
a.cc: In function 'void solv0()':
a.cc:66:31: error: 'v' was not declared in this scope
   66 |     for (int x = 0; x < (1 << v.size()); ++i)
      |                               ^
a.cc:66:44: error: 'i' was not declared in this scope
   66 |     for (int x = 0; x < (1 << v.size()); ++i)
      |                                            ^