Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S))
#define TV(K, L, N, M, R, S)                                                   \
  vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S)))
#define pint pair<lli, lli>
#define paf(L, R) pair<L, R>
#define mod 1000000007
#define MAX 51000000
#define ALL(a) a.begin(), a.end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int main() {
  lli A, B, C, D, E, F, N, M, K, L, X, Y, Z, H, W, sum = 0, num = 0, flag = 0;
  string S, T;
  cin >> N >> K >> S;
  Vec(P, lli, 1, 0);
  if (S[0] == '1')
    flag = 1;
  else {
    P.pb(0);
    num++;
  }
  rep(i, 0, N) {
    if (flag) {
      if (S[i] == '1')
        P[num]++;
      else {
        flag = 0;
        num++;
        P.pb(0);
        P[num]++;
      }
    } else {
      if (S[i] == '0')
        P[num]++;
      else {
        flag = 1;
        num++;
        P.pb(0);
        P[num]++;
      }
    }
  }
  M = P.size();
  Vec(Q, lli, M, 0);
  Q[0] = P[0];
  rep(i, 1, M) Q[i] = P[i] + Q[i - 1];
  if (2 * K < M)
    sum = Q[2 * K];
  else
    sum = Q[M - 1];
  int l = 1;
  int r = 2 * K + 2;
  while (l < r && r < M) {
    sum = max(sum, Q[r] - Q[l]);
    // cout << l << " " << r << endl;
    l += 2;
    r += 2;
    if (r > M - 1)
      r = M - 1;
    // Out(sum);
  }
  sum = max(sum, Q[M - 1] - Q[M - 1 - 2 * K]);
  Out(sum);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S))
#define TV(K, L, N, M, R, S)                                                   \
  vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S)))
#define pint pair<lli, lli>
#define paf(L, R) pair<L, R>
#define mod 1000000007
#define MAX 51000000
#define ALL(a) a.begin(), a.end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int main() {
  lli A, B, C, D, E, F, N, M, K, L, X, Y, Z, H, W, sum = 0, num = 0, flag = 0;
  string S, T;
  cin >> N >> K >> S;
  Vec(P, lli, 1, 0);
  if (S[0] == '1')
    flag = 1;
  else {
    P.pb(0);
    num++;
  }
  rep(i, 0, N) {
    if (flag) {
      if (S[i] == '1')
        P[num]++;
      else {
        flag = 0;
        num++;
        P.pb(0);
        P[num]++;
      }
    } else {
      if (S[i] == '0')
        P[num]++;
      else {
        flag = 1;
        num++;
        P.pb(0);
        P[num]++;
      }
    }
  }
  M = P.size();
  Vec(Q, lli, M, 0);
  Q[0] = P[0];
  rep(i, 1, M) Q[i] = P[i] + Q[i - 1];
  if (2 * K < M)
    sum = Q[2 * K];
  else
    sum = Q[M - 1];
  int l = 1;
  int r = 2 * K + 2;
  while (l < r && r < M) {
    sum = max(sum, Q[r] - Q[l]);
    // cout << l << " " << r << endl;
    l += 2;
    r += 2;
    if (r > M - 1)
      r = M - 1;
    // Out(sum);
  }
  if (2 * K < M)
    sum = max(sum, Q[M - 1] - Q[M - 1 - 2 * K]);
  Out(sum);
}