Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define maxn 100010
using namespace std;
int n, k;
int m = 1;
string s;
struct t {
  int num, dir;
} a[maxn];

void work1() {
  int ans = -1, sum = 0;
  for (int i = 1; i <= 2 * k + 1; i++)
    sum += a[i].num;
  // cout << sum << endl;
  ans = sum;
  for (int i = 2 * k + 2; i <= m; i += 2) {
    sum += a[i].num + a[i + 1].num - a[i - k * 2 - 1].num - a[i - k * 2].num;
    if (sum > ans)
      ans = sum;
    // cout << sum << endl;
  }
  cout << ans;
}

void work2() {
  int ans = -1, sum = 0;
  for (int i = 1; i <= 2 * k; i++)
    sum += a[i].num;
  // cout << sum << endl;
  ans = sum;

  sum += a[2 * k + 1].num + a[2 * k + 2].num - a[1].num;
  if (sum > ans)
    ans = sum;

  for (int i = 2 * k + 3; i <= m; i += 2) {
    sum += a[i].num + a[i + 1].num - a[i - k * 2 - 1].num - a[i - k * 2].num;
    if (sum > ans)
      ans = sum;
    // cout << sum << endl;
  }
  cout << ans;
}
// 11 2
// 01010110011
void work3() {
  int ans = -1, sum = 0;
  for (int i = 1; i <= 2 * k + 1; i++)
    sum += a[i].num;
  // cout << sum << endl;
  ans = sum;
  for (int i = 2 * k + 2; i < m; i += 2) {
    sum += a[i].num + a[i + 1].num - a[i - k * 2 - 1].num - a[i - k * 2].num;
    if (sum > ans)
      ans = sum;
    // cout << sum << endl;
  }
  sum += a[m].num - a[m - k * 2 - 1].num - a[m - k * 2].num;
  if (sum > ans)
    ans = sum;
  cout << ans;
}
// 15 2
// 111010101100000
void work4() {
  t b[maxn];
  b[1].num = 0;
  b[1].dir = 1;
  for (int i = 1; i <= m; i++) {
    b[i + 1].num = a[i].num;
    b[i + 1].dir = a[i].dir;
  }
  b[m + 2].num = 0;
  b[m + 2].dir = 1;
  for (int i = 1; i <= m + 2; i++) {
    a[i].num = b[i].num;
    a[i].dir = b[i].dir;
  }
  m += 2;
  work1();
}
int main() {
  cin >> n >> k;
  cin >> s;

  a[1].num = 1;
  a[1].dir = s[0] - '0';

  for (int i = 1; i < n; i++)
    if (s[i] - '0' == a[m].dir)
      a[m].num++;
    else {
      a[++m].dir = s[i] - '0';
      a[m].num = 1;
    }

  if (a[1].dir == 1 && a[m].dir == 1)
    work1();
  if (a[1].dir == 0 && a[m].dir == 1)
    work2();
  if (a[1].dir == 1 && a[m].dir == 0)
    work3();
  if (a[1].dir == 0 && a[m].dir == 0)
    work4();
}

Fixed submission:
#include <iostream>
#define maxn 1000000
using namespace std;
int n, k;
int m = 1;
string s;
struct t {
  int num, dir;
} a[maxn];

void work1() {
  int ans = -1, sum = 0;
  for (int i = 1; i <= 2 * k + 1; i++)
    sum += a[i].num;
  // cout << sum << endl;
  ans = sum;
  for (int i = 2 * k + 2; i <= m; i += 2) {
    sum += a[i].num + a[i + 1].num - a[i - k * 2 - 1].num - a[i - k * 2].num;
    if (sum > ans)
      ans = sum;
    // cout << sum << endl;
  }
  cout << ans;
}

void work2() {
  int ans = -1, sum = 0;
  for (int i = 1; i <= 2 * k; i++)
    sum += a[i].num;
  // cout << sum << endl;
  ans = sum;

  sum += a[2 * k + 1].num + a[2 * k + 2].num - a[1].num;
  if (sum > ans)
    ans = sum;

  for (int i = 2 * k + 3; i <= m; i += 2) {
    sum += a[i].num + a[i + 1].num - a[i - k * 2 - 1].num - a[i - k * 2].num;
    if (sum > ans)
      ans = sum;
    // cout << sum << endl;
  }
  cout << ans;
}
// 11 2
// 01010110011
void work3() {
  int ans = -1, sum = 0;
  for (int i = 1; i <= 2 * k + 1; i++)
    sum += a[i].num;
  // cout << sum << endl;
  ans = sum;
  for (int i = 2 * k + 2; i < m; i += 2) {
    sum += a[i].num + a[i + 1].num - a[i - k * 2 - 1].num - a[i - k * 2].num;
    if (sum > ans)
      ans = sum;
    // cout << sum << endl;
  }
  sum += a[m].num - a[m - k * 2 - 1].num - a[m - k * 2].num;
  if (sum > ans)
    ans = sum;
  cout << ans;
}
// 15 2
// 111010101100000
void work4() {
  t b[maxn];
  b[1].num = 0;
  b[1].dir = 1;
  for (int i = 1; i <= m; i++) {
    b[i + 1].num = a[i].num;
    b[i + 1].dir = a[i].dir;
  }
  b[m + 2].num = 0;
  b[m + 2].dir = 1;
  for (int i = 1; i <= m + 2; i++) {
    a[i].num = b[i].num;
    a[i].dir = b[i].dir;
  }
  m += 2;
  work1();
}
int main() {
  cin >> n >> k;
  cin >> s;

  a[1].num = 1;
  a[1].dir = s[0] - '0';

  for (int i = 1; i < n; i++)
    if (s[i] - '0' == a[m].dir)
      a[m].num++;
    else {
      a[++m].dir = s[i] - '0';
      a[m].num = 1;
    }

  if (a[1].dir == 1 && a[m].dir == 1)
    work1();
  if (a[1].dir == 0 && a[m].dir == 1)
    work2();
  if (a[1].dir == 1 && a[m].dir == 0)
    work3();
  if (a[1].dir == 0 && a[m].dir == 0)
    work4();
}