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

#include <algorithm>
#include <cstdio>
using namespace std;
int color[10005], leng, go;
int ens(int h, int yu) {
  color[h] = yu;
  int a = h, b = h;
  bool w = true;
  int s = 0, u = 0, ans = leng;
  while (1) {
    int c = a, d = b;
    while (a >= 1) {
      if (color[a] == color[c]) {
        s++;
        a--;
      } else {
        break;
      }
    }
    while (b <= leng) {
      if (color[b] == color[d]) {
        u++;
        b++;
      } else {
        break;
      }
    }
    if (w) {
      if (s + u >= 5) {
        ans -= (s + u - 1);
        s = 0;
        u = 0;
        w = false;
      }
      if (w) {
        return ans;
        break;
      }
    } else {
      if (s + u >= 4) {
        if (color[c] == color[d]) {
          ans -= (s + u);
          s = 0;
          u = 0;
        } else {
          return ans;
          break;
        }
      } else {
        return ans;
        break;
      }
    }
  }
}
int main() {
  while (1) {
    int num;
    scanf("%d", &leng);
    if (leng == 0)
      break;
    num = 100000;
    for (int i = 1; i <= leng; i++) {
      scanf("%d", &color[i]);
    }
    color[0] = color[2];
    color[leng + 1] = color[leng - 1];
    for (int i = 1; i <= leng; i++) {
      go = color[i];
      num = min(num, ens(i, 1));
      num = min(num, ens(i, 2));
      num = min(num, ens(i, 3));
      color[i] = go;
    }
    printf("%d\\n", num);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
using namespace std;
int up = 0, down = 0, x[1001];
int upp, downn, d = 0, c, length, mi = 1000000;
int lensa(int a, int b) {
  while (1) {
    upp = x[a + 1];
    a++;
    if (upp == b) {
      up++;
    } else {
      break;
    }
  }
  a -= up;
  a--;
  while (1) {
    downn = x[a - 1];
    a--;
    if (downn == b) {
      down++;
    } else {
      break;
    }
  }
  if (up + down + 1 >= 4) {
    d = 1;
    for (int f = 1; f <= up; f++) {
      x[a + f] = 0;
    }
    for (int f = 0; f <= down; f++) {
      x[a - f] = 0;
    }
    length -= up + down + 1;
  }
  while (d == 1) {
    upp = a + 1 + up;
    c = upp;
    downn = a - 1 - down;
    int cc = downn;
    up = 0;
    down = 0;
    while (1) {
      for (int r = 0;; r++) {
        if (x[upp + r] == x[upp]) {
          up++;
        } else if (x[upp + r] != 0) {
          upp = upp + r;
          break;
        }
      }
      break;
    }
    while (1) {
      for (int r = 0;; r++) {
        if (x[downn - r] == x[c]) {
          down++;
        } else if (x[downn - r] != 0) {
          downn = downn - r;
          break;
        }
      }
      break;
    }
    d = 0;
    if (up + down >= 4) {
      d = 1;
      for (int f = 0; f < up; f++) {
        x[c + f] = 0;
      }
      for (int f = 0; f < down; f++) {
        x[d - f] = 0;
      }
      length -= up + down;
    }
  }
  return length;
}
int main() {
  int n, l;
  scanf("%d", &n);
  length = n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &x[i]);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= 3; j++) {
      if (x[i] != j) {
        if (mi > lensa(i, j)) {
          mi = lensa(i, j);
        }
      }
    }
  }
  printf("%d\\n", mi);
  return 0;
}