Problem p03074 (C++)

Buggy submission (status: Runtime Error):
/*
 * Matheus Oliveira
 * 01:55:36 14/04/2019
 * abc124d.cpp
 */

#include <bits/stdc++.h>
using namespace std;

#define EPS
#define INF
#define MAXN 100100
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define debug(x) printf("%s: %d\n", #x, x);
#define debugarr(x, ini, fim)                                                  \
  printf("%s: ", #x);                                                          \
  for (int i = ini; i <= fim; i++)                                             \
    printf("%d%c", x[i], (i == fim) ? '\n' : ' ');

typedef long long ll;
typedef pair<int, int> pii;

char inp[MAXN];
vector<pii> ilhas0, ilhas1;

int main() {
  int n, k, ans, extra1, actual;
  ans = 0;

  scanf("%d %d", &n, &k);
  getchar();

  scanf("%s", inp);

  int counter0 = 0, counter1 = 0;
  for (int i = 0; i < n; i++) {
    if (inp[i] == '1') {
      if (counter0 > 0) {
        ilhas0.pb(mp(counter0, counter1));
        counter1 = 1;
      } else
        counter1++;
      counter0 = 0;
    } else
      counter0++;
  }

  extra1 = 0;
  if (counter0 > 0) {
    ilhas0.pb(mp(counter0, counter1));
    counter1 = 1;
  } else
    extra1 = counter1;

  //	for(int i = 0; i < ((int) ilhas0.size()); i++) {
  //		printf("%d 0's, %d 1's\n", ilhas0[i].first, ilhas0[i].second);
  //	}

  if (k >= (int)ilhas0.size())
    printf("%d\n", n);
  else {
    int p1, p2;
    p1 = p2 = 0;

    ans = 0;
    actual = ilhas0[p1].first + ilhas0[p1].second;

    if (p1 + 1 < ((int)ilhas0.size()))
      actual += ilhas0[p1 + 1].second;
    else
      actual += extra1;

    while (p1 < ((int)ilhas0.size())) {
      // printf("p1: %d, p2: %d, actual: %d\n", p1, p2, actual);

      if (p2 - p1 + 1 < k) {
        p2++;

        actual += ilhas0[p2].first; // + ilhas0[p1].second;

        if (p2 + 1 < ((int)ilhas0.size()))
          actual += ilhas0[p2 + 1].second;
        else
          actual += extra1;

        ans = max(ans, actual);
      } else {
        ans = max(ans, actual);
        actual -= ilhas0[p1].first + ilhas0[p1].second;
        // if(p1 == ((int) ilhas0.size()) - 1) actual -= extra1;
        p1++;
      }
    }

    printf("%d\n", ans);
  }

  return 0;
}


Fixed submission:
/*
 * Matheus Oliveira
 * 01:55:36 14/04/2019
 * abc124d.cpp
 */

#include <bits/stdc++.h>
using namespace std;

#define EPS
#define INF
#define MAXN 100100
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define debug(x) printf("%s: %d\n", #x, x);
#define debugarr(x, ini, fim)                                                  \
  printf("%s: ", #x);                                                          \
  for (int i = ini; i <= fim; i++)                                             \
    printf("%d%c", x[i], (i == fim) ? '\n' : ' ');

typedef long long ll;
typedef pair<int, int> pii;

char inp[MAXN];
vector<pii> ilhas0, ilhas1;

int main() {
  int n, k, ans, extra1, actual;
  ans = 0;

  scanf("%d %d", &n, &k);
  getchar();

  scanf("%s", inp);

  int counter0 = 0, counter1 = 0;
  for (int i = 0; i < n; i++) {
    if (inp[i] == '1') {
      if (counter0 > 0) {
        ilhas0.pb(mp(counter0, counter1));
        counter1 = 1;
      } else
        counter1++;
      counter0 = 0;
    } else
      counter0++;
  }

  extra1 = 0;
  if (counter0 > 0) {
    ilhas0.pb(mp(counter0, counter1));
    counter1 = 1;
  } else
    extra1 = counter1;

  //	for(int i = 0; i < ((int) ilhas0.size()); i++) {
  //		printf("%d 0's, %d 1's\n", ilhas0[i].first, ilhas0[i].second);
  //	}

  if (k >= (int)ilhas0.size())
    printf("%d\n", n);
  else {
    int p1, p2;
    p1 = p2 = 0;

    ans = 0;
    actual = ilhas0[p1].first + ilhas0[p1].second;

    if (p1 + 1 < ((int)ilhas0.size()))
      actual += ilhas0[p1 + 1].second;
    else
      actual += extra1;

    while (p1 < ((int)ilhas0.size())) {
      // printf("p1: %d, p2: %d, actual: %d\n", p1, p2, actual);

      if (p2 - p1 + 1 < k and p2 < ((int)ilhas0.size())) {
        p2++;

        actual += ilhas0[p2].first; // + ilhas0[p1].second;

        if (p2 + 1 < ((int)ilhas0.size()))
          actual += ilhas0[p2 + 1].second;
        else
          actual += extra1;

        ans = max(ans, actual);
      } else {
        ans = max(ans, actual);
        actual -= ilhas0[p1].first + ilhas0[p1].second;
        // if(p1 == ((int) ilhas0.size()) - 1) actual -= extra1;
        p1++;
      }
    }

    printf("%d\n", ans);
  }

  return 0;
}