Problem p03074 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
const int MAX = 700000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
/*第二引数で第一引数を割ったときの切り上げの計算*/
long long int maxtime(long long int x, long long int y) {
  return (x + y - 1) / y;
}
/*最大公約数*/
long long int lcm(long long int number1, long long int number2) {
  long long int m = number1;
  long long int n = number2;

  if (number2 > number1) {
    m = number2;
    n = number1;
  }
  long long int s = -1;
  while (s != 0) {
    s = m % n;
    m = n;
    n = s;
  }
  return m;
}
/*最大公倍数*/
long long int gcd(long long int number1, long long int number2) {
  long long int m = number1;
  long long int n = number2;
  return m / lcm(m, n) * n;
}
int main() {
  int n, k;
  cin >> n >> k;
  string s;
  vector<int> length;
  vector<int> check;
  cin >> s;
  int renzoku = 0;
  char buck = ' ';
  for (int i = 0; i < s.size(); i++) {
    if (i == 0) {
      renzoku = 1;
      buck = s[i];
      continue;
    }
    if (buck == s[i]) {
      renzoku++;
    } else {
      length.push_back(renzoku);
      if (buck == '0') {
        check.push_back(1);
      } else {
        check.push_back(0);
      }
      renzoku = 1;
      buck = s[i];
    }
  }
  length.push_back(renzoku);
  if (buck == '0') {
    check.push_back(1);
  } else {
    check.push_back(0);
  }
  vector<int> a;
  vector<int> b;
  a.push_back(0);
  b.push_back(0);
  for (int i = 0; i < check.size(); i++) {
    int t = a[i] + check[i];
    a.push_back(t);
  }
  for (int i = 0; i < length.size(); i++) {
    int t = b[i] + length[i];
    b.push_back(t);
  }
  if (k >= a[a.size() - 1]) {
    cout << b[b.size() - 1];
    return 0;
  }
  int ans = 0;
  for (int f = k; f <= k + k + 1; f++) {
    for (int i = 0; i < length.size(); i++) {
      if (f + i > a.size() - 1) {
        if (a[a.size() - 1] - a[i] > k) {
          continue;
        }
        ans = max(ans, b[a.size() - 1] - b[i]);
        continue;
      }
      if (a[i + f] - a[i] > k) {
        continue;
      }
      ans = max(ans, b[i + f] - b[i]);
    }
  }
  cout << ans;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
const int MAX = 700000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
/*第二引数で第一引数を割ったときの切り上げの計算*/
long long int maxtime(long long int x, long long int y) {
  return (x + y - 1) / y;
}
/*最大公約数*/
long long int lcm(long long int number1, long long int number2) {
  long long int m = number1;
  long long int n = number2;

  if (number2 > number1) {
    m = number2;
    n = number1;
  }
  long long int s = -1;
  while (s != 0) {
    s = m % n;
    m = n;
    n = s;
  }
  return m;
}
/*最大公倍数*/
long long int gcd(long long int number1, long long int number2) {
  long long int m = number1;
  long long int n = number2;
  return m / lcm(m, n) * n;
}
int main() {
  int n, k;
  cin >> n >> k;
  string s;
  vector<int> length;
  vector<int> check;
  cin >> s;
  int renzoku = 0;
  char buck = ' ';
  for (int i = 0; i < s.size(); i++) {
    if (i == 0) {
      renzoku = 1;
      buck = s[i];
      continue;
    }
    if (buck == s[i]) {
      renzoku++;
    } else {
      length.push_back(renzoku);
      if (buck == '0') {
        check.push_back(1);
      } else {
        check.push_back(0);
      }
      renzoku = 1;
      buck = s[i];
    }
  }
  length.push_back(renzoku);
  if (buck == '0') {
    check.push_back(1);
  } else {
    check.push_back(0);
  }
  vector<int> a;
  vector<int> b;
  a.push_back(0);
  b.push_back(0);
  for (int i = 0; i < check.size(); i++) {
    int t = a[i] + check[i];
    a.push_back(t);
  }
  for (int i = 0; i < length.size(); i++) {
    int t = b[i] + length[i];
    b.push_back(t);
  }
  if (k >= a[a.size() - 1]) {
    cout << b[b.size() - 1];
    return 0;
  }
  int ans = 0;
  int z[3];
  z[0] = k;
  z[1] = k + k;
  z[2] = k + k + 1;
  for (int n = 0; n <= 2; n++) {
    int f = z[n];
    for (int i = 0; i < length.size(); i++) {
      if (f + i > a.size() - 1) {
        if (a[a.size() - 1] - a[i] > k) {
          continue;
        }
        ans = max(ans, b[a.size() - 1] - b[i]);
        continue;
      }
      if (a[i + f] - a[i] > k) {
        continue;
      }
      ans = max(ans, b[i + f] - b[i]);
    }
  }
  cout << ans;
}