Problem p03074 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef long long int ll;
typedef vector<ll> V;
typedef vector<V> VV;

int main() {
  // Happy hacking!
  ll n, k;
  string s;
  cin >> n >> k;
  cin >> s;

  V blk_size;
  V blk_dist;
  ll t_size = 0;
  ll t_dist = 0;
  bool is_in_blk = false;
  if (s[0] == '0') {
    is_in_blk = false;
    blk_size.push_back(0);
  } else {
    is_in_blk = true;
  }
  for (ll i = 0; i < n; i++) {
    char c = s[i];
    if (is_in_blk) {
      if (c == '0') {
        is_in_blk = false;
        blk_size.push_back(t_size);
        t_dist = 1;
      } else {
        t_size++;
      }
    } else {
      if (c == '0') {
        t_dist++;
      } else {
        is_in_blk = true;
        blk_dist.push_back(t_dist);
        t_size = 1;
      }
    }
  }
  if (is_in_blk) {
    blk_size.push_back(t_size);
    blk_dist.push_back(0);
  } else {
    blk_dist.push_back(t_dist);
  }

  ll score_max = 0;
  // calc initial score
  ll score_prev = 0;
  ll init_idx = 0;
  for (; init_idx < k; init_idx++) {
    if (init_idx >= blk_size.size())
      break;
    score_prev += blk_size[init_idx] + blk_dist[init_idx];
  }
  if (init_idx < blk_size.size()) {
    score_prev += blk_size[init_idx];
  }
  score_max = score_prev;
  for (ll i = 1; i < blk_size.size() - k + 1; i++) {
    ll score = score_prev;
    score -= blk_size[i - 1] + blk_dist[i - 1]; // remove the head block
    score += blk_dist[i + k - 1];               // expand
    if (i + k < blk_size.size()) {
      score += blk_size[i + k];
    }
    if (score > score_max) {
      score_max = score;
    }
    score_prev = score;
  }

  cout << score_max << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef long long int ll;
typedef vector<ll> V;
typedef vector<V> VV;

int main() {
  // Happy hacking!
  ll n, k;
  string s;
  cin >> n >> k;
  cin >> s;

  V blk_size;
  V blk_dist;
  ll t_size = 0;
  ll t_dist = 0;
  bool is_in_blk = false;
  if (s[0] == '0') {
    is_in_blk = false;
    blk_size.push_back(0);
  } else {
    is_in_blk = true;
  }
  for (ll i = 0; i < n; i++) {
    char c = s[i];
    if (is_in_blk) {
      if (c == '0') {
        is_in_blk = false;
        blk_size.push_back(t_size);
        t_dist = 1;
      } else {
        t_size++;
      }
    } else {
      if (c == '0') {
        t_dist++;
      } else {
        is_in_blk = true;
        blk_dist.push_back(t_dist);
        t_size = 1;
      }
    }
  }
  if (is_in_blk) {
    blk_size.push_back(t_size);
    blk_dist.push_back(0);
  } else {
    blk_dist.push_back(t_dist);
  }

  ll score_max = 0;
  // calc initial score
  ll score_prev = 0;
  ll init_idx = 0;
  for (; init_idx < k; init_idx++) {
    if (init_idx >= blk_size.size())
      break;
    score_prev += blk_size[init_idx] + blk_dist[init_idx];
  }
  if (init_idx < blk_size.size()) {
    score_prev += blk_size[init_idx];
  }
  score_max = score_prev;
  for (ll i = 1; i < ((ll)blk_size.size()) - k + 1; i++) {
    ll score = score_prev;
    score -= blk_size[i - 1] + blk_dist[i - 1]; // remove the head block
    score += blk_dist[i + k - 1];               // expand
    if (i + k < blk_size.size()) {
      score += blk_size[i + k];
    }
    if (score > score_max) {
      score_max = score;
    }
    score_prev = score;
  }

  cout << score_max << endl;

  return 0;
}