Problem p03580 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
#define ALL(x) (x).begin(), (x).end()
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define mp make_pair
#define eb emplace_back
#define REP(i, a) for (int i = 0; i < (a); i++)
#define INIT                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);

std::vector<int> v[201010];
int dp[201010];
int main() {
  INIT;
  int n;
  string s;
  cin >> n >> s;
  s = "000" + s + "000";
  n += 6;
  REP(i, n - 3) {
    if (s.substr(i, 3) != "101")
      continue;
    int l = i - 1;
    while (s[l] == '1')
      --l;
    ++l;
    int r = i + 3;
    while (s[r] == '1')
      ++r;
    v[l].emplace_back(i + 3);
    v[l + 1].emplace_back(i + 3);
    v[i].emplace_back(i + 3);
    v[i].emplace_back(r - 1);
    v[i].emplace_back(r);
  }
  REP(i, n - 1) {
    dp[i + 1] = max(dp[i + 1], dp[i]);
    for (int x = 0; x < v[i].size(); x++) {
      dp[v[i][x]] = max(dp[v[i][x]], dp[i] + v[i][x] - i - 2);
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
#define ALL(x) (x).begin(), (x).end()
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define mp make_pair
#define eb emplace_back
#define REP(i, a) for (int i = 0; i < (a); i++)
#define INIT                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);

std::vector<int> v[501010];
int dp[501010];
int main() {
  INIT;
  int n;
  string s;
  cin >> n >> s;
  s = "000" + s + "000";
  n += 6;
  REP(i, n - 3) {
    if (s.substr(i, 3) != "101")
      continue;
    int l = i - 1;
    while (s[l] == '1')
      --l;
    ++l;
    int r = i + 3;
    while (s[r] == '1')
      ++r;
    v[l].emplace_back(i + 3);
    v[l + 1].emplace_back(i + 3);
    v[i].emplace_back(i + 3);
    v[i].emplace_back(r - 1);
    v[i].emplace_back(r);
  }
  REP(i, n - 1) {
    dp[i + 1] = max(dp[i + 1], dp[i]);
    for (int x = 0; x < v[i].size(); x++) {
      dp[v[i][x]] = max(dp[v[i][x]], dp[i] + v[i][x] - i - 2);
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}