Problem p00457 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 100000000;
int n, x[10001][2], list[4], save[10001], l1 = 0, l2 = 0, ans = INF;
vector<int> v1, v2, v3;
int solve(int a) {
  l1 = 0;
  l2 = 0;
  for (int i = 1; i < v2.size(); i++) {
    if (v2[a + i] == v2[a]) {
      l1++;
    } else
      break;
  }
  for (int i = 1; i < v2.size(); i++) {
    if (v2[a - i] == v2[a]) {
      l2++;
    } else
      break;
  }
  if (l1 + l2 >= 3) {
    v2.erase(v2.begin() + a - l2, v2.begin() + a + l1 + 1);
    solve(a - l2);
  } else {
    return v2.size();
  }
}
int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 1; i <= n; i++) {
      cin >> x[i][0];
      v1.push_back(x[i][0]);
      if (i - list[x[i][0]] == 2) {
        save[i - 1] = 2;
      }
      list[x[i][0]] = i;
      if (x[i][0] == x[i - 1][0] && x[i][0] == x[i - 2][0]) {
        save[i + 1] = 1;
        save[i - 3] = 1;
      }
    }
    for (int i = 1; i <= n; i++) {
      if (save[i] == 1) {
        v2 = v1;
        v2.erase(v2.begin() + i - 1);
        if (x[i + 1][0] == x[i + 2][0] && x[i + 1][0] == x[i + 3][0]) {
          v2.insert(v2.begin() + i - 1, x[i + 1][0]);
        } else if (x[i - 1][0] == x[i - 2][0] && x[i - 1][0] == x[i - 3][0]) {
          v2.insert(v2.begin() + i - 1, x[i - 1][0]);
        }
        ans = min(solve(i - 1), ans);
      } else if (save[i] == 2 && x[i + 1][0] == x[i + 2][0]) {
        v2 = v1;
        v2.erase(v2.begin() + i - 1);
        v2.insert(v2.begin() + i - 1, x[i + 1][0]);
        ans = min(solve(i - 1), ans);
      } else if (save[i] == 2 && x[i - 1][0] == x[i - 2][0]) {
        v2 = v1;
        v2.erase(v2.begin() + i - 1);
        v2.insert(v2.begin() + i - 1, x[i - 1][0]);
        ans = min(solve(i - 1), ans);
      } else
        save[i] = 0;
    }
    cout << ans << endl;
    for (int i = 1; i <= n; i++) {
      x[i][0] = 0;
      save[i] = 0;
    }
    list[0] = 0;
    list[1] = 0;
    list[2] = 0;
    list[3] = 0;
    ans = INF;
    v1 = v3;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 100000000;
int n, x[10001][2], list[4], save[10001], l1 = 0, l2 = 0, ans = INF;
vector<int> v1, v2, v3;
int solve(int a) {
  l1 = 0;
  l2 = 0;
  for (int i = 1; i < v2.size(); i++) {
    if (v2[a + i] == v2[a]) {
      l1++;
    } else
      break;
  }
  for (int i = 1; i < v2.size(); i++) {
    if (v2[a - i] == v2[a]) {
      l2++;
    } else
      break;
  }
  if (l1 + l2 >= 3) {
    v2.erase(v2.begin() + a - l2, v2.begin() + a + l1 + 1);
    solve(a - l2 - 1);
  } else {
    return v2.size();
  }
}
int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 1; i <= n; i++) {
      cin >> x[i][0];
      v1.push_back(x[i][0]);
      if (i - list[x[i][0]] == 2) {
        save[i - 1] = 2;
      }
      list[x[i][0]] = i;
      if (x[i][0] == x[i - 1][0] && x[i][0] == x[i - 2][0]) {
        save[i + 1] = 1;
        save[i - 3] = 1;
      }
    }
    for (int i = 1; i <= n; i++) {
      if (save[i] == 1) {
        v2 = v1;
        v2.erase(v2.begin() + i - 1);
        if (x[i + 1][0] == x[i + 2][0] && x[i + 1][0] == x[i + 3][0]) {
          v2.insert(v2.begin() + i - 1, x[i + 1][0]);
        } else if (x[i - 1][0] == x[i - 2][0] && x[i - 1][0] == x[i - 3][0]) {
          v2.insert(v2.begin() + i - 1, x[i - 1][0]);
        }
        ans = min(solve(i - 1), ans);
      } else if (save[i] == 2 && x[i + 1][0] == x[i + 2][0]) {
        v2 = v1;
        v2.erase(v2.begin() + i - 1);
        v2.insert(v2.begin() + i - 1, x[i + 1][0]);
        ans = min(solve(i - 1), ans);
      } else if (save[i] == 2 && x[i - 1][0] == x[i - 2][0]) {
        v2 = v1;
        v2.erase(v2.begin() + i - 1);
        v2.insert(v2.begin() + i - 1, x[i - 1][0]);
        ans = min(solve(i - 1), ans);
      } else
        save[i] = 0;
    }
    cout << ans << endl;
    for (int i = 1; i <= n; i++) {
      x[i][0] = 0;
      save[i] = 0;
    }
    list[0] = 0;
    list[1] = 0;
    list[2] = 0;
    list[3] = 0;
    ans = INF;
    v1 = v3;
  }
  return 0;
}