Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) unique(v.begin(), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for (int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
#define des_priority_queue priority_queue<int, vector<int>, greater<int>>
#define all(v) (v).begin(), (v).end()
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1ll << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  vector<int> A;
  ll num = 0;
  bool flag_a = true;
  rep(i, 0, S.size()) {
    if (flag_a) {
      if (S[i] == '1')
        num++;
      else {
        A.push_back(num);
        num = 1;
        flag_a = false;
      }
    } else {
      if (S[i] == '1') {
        A.push_back(num);
        num = 1;
        flag_a = true;
      } else {
        num++;
      }
    }
  }
  if (num > 0)
    A.push_back(num);
  if (A.size() % 2 == 0)
    A.push_back(0);

  int Add = 2 * K + 1;
  vector<int> sum(A.size() + 1);
  rep(i, 0, sum.size()) sum[i + 1] = sum[i] + A[i];

  ll rsl = 0;
  for (int i = 0; i < A.size(); i += 2) {
    int left = i, right = min(i + Add, (int)A.size());
    ll tmp = sum[right] - sum[left];
    chmax(rsl, tmp);
  }

  cout << rsl << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) unique(v.begin(), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for (int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
#define des_priority_queue priority_queue<int, vector<int>, greater<int>>
#define all(v) (v).begin(), (v).end()
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1ll << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  vector<int> A;
  ll num = 0;
  bool flag_a = true;
  rep(i, 0, S.size()) {
    if (flag_a) {
      if (S[i] == '1')
        num++;
      else {
        A.push_back(num);
        num = 1;
        flag_a = false;
      }
    } else {
      if (S[i] == '1') {
        A.push_back(num);
        num = 1;
        flag_a = true;
      } else {
        num++;
      }
    }
  }
  if (num > 0)
    A.push_back(num);
  if (A.size() % 2 == 0)
    A.push_back(0);

  int Add = 2 * K + 1;
  vector<int> sum(A.size() + 1);
  rep(i, 0, A.size()) sum[i + 1] = sum[i] + A[i];

  ll rsl = 0;
  for (int i = 0; i < A.size(); i += 2) {
    int left = i, right = min(i + Add, (int)A.size());
    ll tmp = sum[right] - sum[left];
    chmax(rsl, tmp);
  }

  cout << rsl << endl;
}