Language: C++

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define all(x) (x).begin(),(x).end()
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
using Graph = vector<vector<int>>;
const double pi=acos(-1);
template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) val += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};
using mint = Fp<MOD>;

mint calc(long long N, long long K) {
    mint res = 1;
    for (long long n = 0; n < K; ++n) {
        res *= (N - n);
        res /= (n + 1);
    }
    return res;
}
int main() {
//cout << fixed << setprecision(12);
ll n,k;
cin>>n>>k;
string s;
cin>>s;
ll a[n]={};
]] b[n]={};
ll aa=0;
ll bb=0;
ll ca=0;
ll cb=0;
rep(i,n){
if(s.at(i)=='1'){
while(s.at(i+ca)=='1'){
ca++;}
i+=ca-1;
a[aa]=ca;
ca=0;
aa++;}
else{
while(s.at(i+cb)=='0'){
cb++;}
i+=cb-1;
b[ab]=cb;
cb=0;
bb++;}}
ca=0;
if(k>=bb){
cout << n << endl;}
  
else{
if(s.at(0)=='0'){
rep(i,k){
ca+=(a[i]+b[i]);}
ca=max(ca,ca-b[0]+a[k]+b[k]);
repl(i,k+1,bb){
ca+=max(ca,ca+b[i]+a[i]-a[i-k]-b[i-k];}
}
if(s.at(0)=='1'){
rep(i,k){
ca+=a[i]+b[i];}
ca+=a[k];
repl(i,k+1,aa){
ca=max(ca,ca-a[i-k-1]-b[i-k-1]+a[i]+b[i-1]);}}
if(s.at(n-1)==s.at(0)){
if(s.at(n-1)=='0'){
rep(i,k){
cb+=(a[bb-2-i]+b[bb-1-i]);}
cb=max(cb,cb-b[bb-1]+a[bb-2-k]+b[bb-1-k]);}
else{
rep(i,k){
cb+=(a[aa-1-i]+b[aa-1-i]);}}
if(s.at(n-1)!=s.at(0)){
rep(i,k){
cb+=(a[aa-1-i]+b[aa-1-i]);}
if(s.at(n-1)=='0'){
cb=max(cb,cb-b[aa-1]+a[aa-1-k]+b[aa-1-k]);}}
ca=max(ca,cb);
cout << ca << endl;}

Compiler output:
a.cc: In function 'int main()':
a.cc:95:1: error: expected primary-expression before ']' token
   95 | ]] b[n]={};
      | ^
a.cc:112:1: error: 'b' was not declared in this scope
  112 | b[ab]=cb;
      | ^
a.cc:112:3: error: 'ab' was not declared in this scope; did you mean 'cb'?
  112 | b[ab]=cb;
      |   ^~
      |   cb
a.cc:122:11: error: 'b' was not declared in this scope
  122 | ca+=(a[i]+b[i]);}
      |           ^
a.cc:123:14: error: 'b' was not declared in this scope; did you mean 'cb'?
  123 | ca=max(ca,ca-b[0]+a[k]+b[k]);
      |              ^
      |              cb
a.cc:129:10: error: 'b' was not declared in this scope
  129 | ca+=a[i]+b[i];}
      |          ^
a.cc:132:23: error: 'b' was not declared in this scope
  132 | ca=max(ca,ca-a[i-k-1]-b[i-k-1]+a[i]+b[i-1]);}}
      |                       ^
a.cc:136:16: error: 'b' was not declared in this scope
  136 | cb+=(a[bb-2-i]+b[bb-1-i]);}
      |                ^
a.cc:137:14: error: 'b' was not declared in this scope; did you mean 'cb'?
  137 | cb=max(cb,cb-b[bb-1]+a[bb-2-k]+b[bb-1-k]);}
      |              ^
      |              cb
a.cc:140:16: error: 'b' was not declared in this scope
  140 | cb+=(a[aa-1-i]+b[aa-1-i]);}}
      |                ^
a.cc:143:16: error: 'b' was not declared in this scope
  143 | cb+=(a[aa-1-i]+b[aa-1-i]);}
      |                ^
a.cc:145:14: error: 'b' was not declared in this scope; did you mean 'cb'?
  145 | cb=max(cb,cb-b[aa-1]+a[aa-1-k]+b[aa-1-k]);}}
      |              ^
      |              cb
a.cc:147:21: error: expected '}' at end of input
  147 | cout << ca << endl;}
      |                     ^
a.cc:119:5: note: to match this '{'
  119 | else{
      |     ^
a.cc:147:21: error: expected '}' at end of input
  147 | cout << ca << endl;}
      |                     ^
a.cc:88:12: note: to match this '{'
   88 | int main() {
      |            ^