Language: C++

#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
#define next asdasdas

using namespace std;

   
#define REP( i, n ) \  
    for ( int i = 0; i < (n); i++ )   
   
const int MAXN = 200031;   
   
int N, K, sol;   
int value, kk;   
int l, r, pos;   
int C[MAXN];   
int next[MAXN];   
int last[MAXN];   
struct _comp {   
    bool operator () ( const int &i, const int &j ) {   
        return C[i] != C[j] ? C[i] < C[j] : i < j;   
    }   
};   
set< int, _comp > S;   

vector <int> cost(vector <int> s) {
  S.clear();
  int N = (int) s.size();
  N++;
  sol = 0;
  for (int i = 0; i < N; i++) next[i] = last[i] = 0;
    REP( i, N ) {   
        if ( i ) {   
          C[i] = !s[i - 1];
            S.insert( i );   
            next[i] = i + 1;   
            last[i] = i - 1;   
        }   
    }   
    next[N - 1] = last[1] = 0; // none   
    vector <int> ans = {0};
    for (int i = 1; i <= N / 2; i++) {
   
        pos = *S.begin();   
        l = last[pos];   
        r = next[pos];   
   
        S.erase( pos );   
        if ( l ) S.erase( l );   
        if ( r ) S.erase( r );   
   
        sol += C[pos];   
        C[pos] = C[l] + C[r] - C[pos];   
        if ( l && r ) {   
            S.insert( pos );   
            last[pos] = last[l];   
            next[pos] = next[r];   
        } else   
            pos = 0;   
   
        if ( last[l] ) next[ last[l] ] = pos;   
        if ( next[r] ) last[ next[r] ] = pos;   
        ans.push_back(i - sol);
    }   
    return ans;
   
}
   
int main() {   
#ifdef iq
  freopen("a.in", "r", stdin);
#endif
  string t;
  cin >> t;
  vector <int> ret;
  for (int i= 0; i < (int) t.size(); i++) {
    ret.push_back(t[i] - '0');
  }
  int pep = ret.back();
  ret.pop_back();
  auto r = cost(ret);
  long long ans = 0;
  int n = (int) t.size();
  for (int k = 1; k <= n; k++) {
    int val = 0;
    if (k % 2 == 0) {
      val = r[k / 2];
    } else {
      val = max(r[(k + 1) / 2], r[k / 2] + pep);
    }
    ans += val;
  }
  if ((int) t.size() % 2 == 0)
    cout << ans << endl;
  else
    assert(0);
}


Compiler output:
a.cc:29:21: warning: backslash and newline separated by space
   29 | #define REP( i, n ) \
In file included from /usr/include/c++/14/set:62,
                 from a.cc:8:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = _comp; _Alloc = std::allocator<int>; _Const_Link_type = const std::_Rb_tree_node<int>*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = _comp; _Alloc = std::allocator<int>; key_type = int]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = const int&; _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = _comp; _Alloc = std::allocator<int>]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:514:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Allocator>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = int; _Compare = _comp; _Alloc = std::allocator<int>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Allocator>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<int, int, std::_Identity<int>, _comp, std::allocator<int> >::const_iterator; typename __gnu_cxx::__alloc_traits<_Allocator>::rebind<_Key>::other = std::allocator<int>; typename __gnu_cxx::__alloc_traits<_Allocator>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<int>, int>::rebind<int>; typename _Allocator::value_type = int; value_type = int]'
  514 |           _M_t._M_insert_unique(__x);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:56:21:   required from here
   56 |             S.insert( i );
      |             ~~~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const _comp&, const int&, const int&>' evaluates to false