Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;

int main()
{
    ll n, k;
    string str;
    cin >> n >> k >> str;
    vector<ll> ary;
#if 1
    int now = 1; //今見ている数
    int cnt = 0; //nowがいくつ並んでいるか
    for (int i = 0; i < n; i++)
    {
        if (str[i] == (char)('0' + now)) cnt++;
        else {
            ary.push_back(cnt);
            now = 1 - now; //0と1を切り替える時の計算 now ^= 1;
            cnt = 1; //新しいのをカウントし始める
        }
    }
    if (cnt != 0) ary.push_back(cnt);

    //1-0-1-0-1-0-1って感じの配列が欲しい
    //1-0-1-0みたいに0で終わってたら、適当に１つ足す
    if (ary.size() % 2 == 0) ary.push_back(0);
    if (str[0] == '0') {
        ary.push_back(0);
    }
#else
    char prev = 0;
    for (ll i = 0; (i < n); i++) {
        char curr = str[i];
        if (prev != curr) {
            prev = curr;
            ary.push_back(1);
        } else {
            ary[ary.size()-1]++;
        }
    }
    if (str[n-1] == '0') ary.push_back(0);
#endif

#if 1
        vector<int> sum(ary.size() + 1);
        for (int i = 0; i < ary.size(); i++)
        {
                sum[i + 1] = sum[i] + ary[i];
        }
#else
    vector<ll> sum(ary.size()+1, 0);
    for (ll i = 0; (i < ary.size()); ++i) {
        sum[i+1] = sum[i] + ary[i];
    }
#endif

#if 0
        int Add = 2 * k + 1;
        int maxv = 0;
        //1-0-1...の1から始めるので、偶数番目だけ見る
        for (int i = 0; i < ary.size(); i += 2)
        {
                //次のleft, rightを計算する [left, right)
                int left = i;
                int right = min(i + Add, (int)ary.size());
                int tmp = sum[right] - sum[left];

                maxv = max(tmp, maxv);
        }
#else
    ll maxv = 0;
    for(ll i = 1; (i < ary.size()); i+=2) {
        ll left = i - 1;
        ll right = min((ll)(sum.size()-1), i+(k-1)*2+1+1);
        maxv = max(maxv, sum[right] - sum[left]);
    }
#endif
    cout << maxv << endl;
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:78:19: error: no matching function for call to 'max(ll&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type)'
   78 |         maxv = max(maxv, sum[right] - sum[left]);
      |                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:78:19: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'})
   78 |         maxv = max(maxv, sum[right] - sum[left]);
      |                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:78:19: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   78 |         maxv = max(maxv, sum[right] - sum[left]);
      |                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~