Problem p00457 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 200000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define EQ(a, b) (abs((a) - (b)) < EPS)
vi puyopuyo;
int main() {
  while (1) {
    puyopuyo.clear();
    int n;
    cin >> n;
    int ans = n;
    if (n == 0)
      break;
    puyopuyo.pb(-1);
    for (int i = 0; i < n; i++) {
      int k;
      cin >> k;
      puyopuyo.pb(k);
    }
    puyopuyo.pb(-12);
    for (int i = 1; i <= n; i++) {
      int tmp = puyopuyo[i];
      int top = i + 1;
      int under = i;
      int res = n;
      puyopuyo[under] = puyopuyo[top];
      while (puyopuyo[under] == puyopuyo[top]) {
        int remove = 0;
        int tmp2 = puyopuyo[top];
        while (puyopuyo[top] == tmp2) {
          top++;
          remove++;
        }
        while (tmp2 == puyopuyo[under]) {
          under--;
          remove++;
        }
        if (remove >= 4) {
          res -= remove;
        } else
          break;
      }
      ans = min(ans, res);
      top = i;
      under = i - 1;
      res = n;
      puyopuyo[top] = puyopuyo[under];
      while (puyopuyo[top] == puyopuyo[under]) {
        int remove = 0;
        int tmp2 = puyopuyo[top];
        while (puyopuyo[top] == tmp2) {
          top++;
          remove++;
        }
        while (puyopuyo[under] == tmp2) {
          under--;
          remove++;
        }
        if (remove >= 4) {
          res -= remove;
        } else
          break;
      }
      ans = min(ans, res);
      puyopuyo[i] = tmp;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define EQ(a, b) (abs((a) - (b)) < EPS)
vi p;
vi puyo, tmp;
int n;
int ans;
vector<int>::iterator it1, it2;
void init() {
  p.clear();
  for (int i = 0; i < sz(tmp); i++) {
    int cnt = 0;
    int k = tmp[i];
    for (int j = i; j < sz(tmp); j++) {
      if (tmp[j] == k)
        cnt++;
      else
        break;
    }
    p.pb(cnt);
  }
}
bool cal() {
  it2 = p.begin();
  it1 = tmp.begin();
  bool flag = false;
  for (int i = 0; i < sz(p); i++) {
    if (*it2 >= 4) {
      flag = true;
      break;
    }
    it2++;
    it1++;
  }
  return flag;
}
void erase() {
  int r = *it2;
  for (int i = 0; i < r; i++) {
    tmp.erase(it1);
    it1++;
  }
  return;
}
int change(int place, int color) {
  tmp.clear();
  tmp = puyo;
  tmp[place] = color;
  bool f = false;
  while (1) {
    init();
    f = cal();
    if (f) {
      erase();
    } else {
      break;
    }
  }
  return sz(tmp);
}
int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    ans = INF;
    for (int i = 0; i < n; i++) {
      int k;
      cin >> k;
      puyo.pb(k);
    }
    reverse(puyo.begin(), puyo.end());
    for (int i = 0; i < sz(puyo); i++) {
      for (int j = 1; j <= 3; j++) {
        if (puyo[i] == j)
          continue;
        ans = min(ans, change(i, j));
      }
    }
    cout << ans << endl;
  }
  return 0;
}