alexjercan/bugnet huggingface dataset:
Problem p00454 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int col[3300][3300];
int h, w, k, x_1, x_2, y_1, y_2, cnt, V, W;
queue<int> Q;
#define INF 1000000007
void A(int B, int C) {
  if (col[B][C] == 0) {
    Q.push(B * 10000 + C);
    col[B][C] = cnt;
  }
}
int main() {
  while (true) {
    memset(col, 0, sizeof(col));
    cin >> h >> w;
    if (h == 0 && w == 0) {
      break;
    }
    cin >> k;
    for (int i = 0; i < k; i++) {
      cin >> x_1 >> y_1 >> x_2 >> y_2;
      for (int j = y_1; j < y_2; j++) {
        for (int k = x_1; k < x_2; k++) {
          col[j][k] = INF;
        }
      }
    }
    cnt = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (col[i][j] == 0) {
          col[i][j] = 1;
          Q.push(i * 10000 + j);
          cnt++;
          while (!Q.empty()) {
            V = Q.front() / 10000;
            W = Q.front() % 10000;
            if (V != 0) {
              A(V - 1, W);
            }
            if (W != 0) {
              A(V, W - 1);
            }
            if (V != h - 1) {
              A(V + 1, W);
            }
            if (W != w - 1) {
              A(V, W + 1);
            }
            Q.pop();
          }
        }
      }
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int col[3300][3300];
int h, w, k, x1, x2, y1, y2, cnt;
queue<int> Q;
#define INF 1000000007
void A(int B, int C) {
  if (col[B][C] == 0) {
    Q.push(B * 10000 + C);
    col[B][C] = cnt;
  }
}
int main() {
  while (true) {
    memset(col, 0, sizeof(col));
    cin >> h >> w;
    if (h == 0 && w == 0) {
      break;
    }
    cin >> k;
    for (int i = 0; i < k; i++) {
      cin >> x1 >> y1 >> x2 >> y2;
      for (int j = y1; j < y2; j++) {
        for (int k = x1; k < x2; k++) {
          col[j][k] = INF;
        }
      }
    }
    cnt = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (col[i][j] == 0) {
          Q.push(i * 10000 + j);
          cnt++;
          while (!Q.empty()) {
            if (i != 0) {
              A(i - 1, j);
            }
            if (j != 0) {
              A(i, j - 1);
            }
            if (i != h - 1) {
              A(i + 1, j);
            }
            if (j != w - 1) {
              A(i, j + 1);
            }
          }
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p00454 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>

#pragma warning(disable : 4996)

using namespace std;

int H, W, N, X1, Y1, X2, Y2;
vector<vector<bool>> M;

const vector<int> dx = {0, 1, 0, -1};
const vector<int> dy = {-1, 0, 1, 0};

int main() {
  while (true) {
    scanf("%d", &W);
    scanf("%d", &H);

    if (H == 0 && W == 0) {
      break;
    }

    scanf("%d", &N);

    M = vector<vector<bool>>(H, vector<bool>(W, false));

    for (int i = 0; i < N; i++) {
      scanf("%d", &X1);
      scanf("%d", &Y1);
      scanf("%d", &X2);
      scanf("%d", &Y2);

      for (int j = Y1; j < Y2; j++) {
        for (int k = X1; k < X2; k++) {
          M[j][k] = true;
        }
      }
    }

    int ret = 0;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (!M[i][j]) {
          M[i][j] = true;

          queue<pair<int, int>> que;
          que.push(make_pair(j, i));

          while (!que.empty()) {
            int rx = que.front().first;
            int ry = que.front().second;
            que.pop();

            for (int dir = 0; dir < 4; dir++) {
              if (0 <= rx + dx[dir] && rx + dx[dir] < W && 0 <= ry + dy[dir] &&
                  ry + dy[dir] < H) {
                if (!M[ry + dy[dir]][rx + dx[dir]]) {
                  M[ry + dy[dir]][rx + dx[dir]] = true;

                  que.push(make_pair(rx + dx[dir], ry + dy[dir]));
                }
              }
            }
          }

          ret++;
        }
      }
    }

    printf("%d\\n", ret);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int H, W, N, X1, Y1, X2, Y2;
vector<vector<bool>> M;

const vector<int> dx = {0, 1, 0, -1};
const vector<int> dy = {-1, 0, 1, 0};

void rec(int x, int y) {
  M[y][x] = true;

  for (int i = 0; i < 4; i++) {
    if (0 <= x + dx[i] && x + dx[i] < W && 0 <= y + dy[i] && y + dy[i] < H) {
      if (!M[y + dy[i]][x + dx[i]]) {
        rec(x + dx[i], y + dy[i]);
      }
    }
  }
}

int main() {
  while (true) {
    scanf("%d", &W);
    scanf("%d", &H);

    if (H == 0 && W == 0) {
      break;
    }

    scanf("%d", &N);

    M = vector<vector<bool>>(H, vector<bool>(W, false));

    for (int i = 0; i < N; i++) {
      scanf("%d", &X1);
      scanf("%d", &Y1);
      scanf("%d", &X2);
      scanf("%d", &Y2);

      for (int j = Y1; j < Y2; j++) {
        for (int k = X1; k < X2; k++) {
          M[j][k] = true;
        }
      }
    }

    int ret = 0;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (!M[i][j]) {
          rec(j, i);
          ret++;
        }
      }
    }

    printf("%d\\n", ret);
  }

  return 0;
}
Problem p00455 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int sh, sm, ss, eh, em, es;
  int h, m, s;
  for (int i = 0; i < 3; i++) {
    cin >> sh >> sm >> ss >> eh >> em >> es;
    if (es < ss) {
      em = em - 1;
      es = es + 60;
    }
    s = es - ss;
    if (em < sm) {
      eh = eh - 1;
      em = em + 60;
    }
    m = em - sm;
    h = eh - sh;
    cout << h << " " << m << " " << s << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int h[3] = {0};
  int m[3] = {0};
  int s[3] = {0};
  int H[3] = {0};
  int M[3] = {0};
  int S[3] = {0};
  int j, f, b;
  for (int i = 0; i < 4; i++) {
    cin >> h[i] >> m[i] >> s[i] >> H[i] >> M[i] >> S[i];
    j = (3600 * H[i] + 60 * M[i] + S[i] - 3600 * h[i] - 60 * m[i] - s[i]) /
        3600;
    f = ((3600 * H[i] + 60 * M[i] + S[i] - 3600 * h[i] - 60 * m[i] - s[i]) -
         j * 3600) /
        60;
    b = (3600 * H[i] + 60 * M[i] + S[i] - 3600 * h[i] - 60 * m[i] - s[i]) -
        j * 3600 - f * 60;
    cout << j << " " << f << " " << b << endl;
  }
  return 0;
}
Problem p00455 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < s; i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<int, int> pii;
#define MAX 99999999
typedef struct {
  int first;
  int secound;
  int third;
} pi;
vector<pi> v(3);
vector<pi> ans(3);
int main() {
  rep(i, 3) {
    cin >> v[i].first >> v[i].secound >> v[i].third;
    int temp = v[i].first * 3600 + v[i].secound * 60 + v[i].third;
    cin >> v[i].first >> v[i].secound >> v[i].third;
    int hoge = v[i].first * 3600 + v[i].secound * 60 + v[i].third;
    hoge -= temp;
    ans[i].first = hoge / 3600;
    hoge = hoge % 3600;
    ans[i].secound = hoge / 60;
    hoge = hoge % 60;
    ans[i].third = hoge;
  }
  rep(i, 3) cout << ans[i].first << " " << ans[i].secound << " " << ans[i].third
                 << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < s; i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<int, int> pii;
#define MAX 99999999
typedef struct {
  int first;
  int secound;
  int third;
} pi;
vector<pi> v(3);
vector<pi> ans(3);
int main() {
  while (1) {
    rep(i, 3) {
      cin >> v[i].first >> v[i].secound >> v[i].third;
      int temp = v[i].first * 3600 + v[i].secound * 60 + v[i].third;
      cin >> v[i].first >> v[i].secound >> v[i].third;
      int hoge = v[i].first * 3600 + v[i].secound * 60 + v[i].third;
      hoge -= temp;
      ans[i].first = hoge / 3600;
      hoge = hoge % 3600;
      ans[i].secound = hoge / 60;
      hoge = hoge % 60;
      ans[i].third = hoge;
    }
    rep(i, 3) cout << ans[i].first << " " << ans[i].secound << " "
                   << ans[i].third << endl;
  }
}
Problem p00456 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

int main() {

  int W[10];
  int K[10];

  int i = 0;
  while (i < 10) {
    cin >> W[i];
    i++;
  }

  while (i < 20) {
    cin >> K[i - 10];
    i++;
  }

  sort(W, W + 10);
  sort(K, K + 10);

  cout << W[7] + W[8] + W[9] << " " << K[7] + K[8] + K[9] << endl;

  return 0;
}


Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {

  int W[10], K[10];

  int i = 0;
  while (i < 10) {
    cin >> W[i];
    i++;
  }

  while (i < 20) {
    cin >> K[i];
    i++;
  }

  sort(W, W + 10);
  sort(K, K + 10);

  cout << W[7] + W[8] + W[9] << K[7] + K[8] + K[9] << endl;

  return 0;
}
Problem p00456 (C++) — buggy submission, verdict: Time Limit Exceeded

/**********************************************

1.
2.炭田高輝
3.sumita.takaki@gmail.com

last updated: 2013-07-10@12:35

 **********************************************/
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main(void) {

  while (1) {

    vector<int> w_uni(10);
    vector<int> k_uni(10);

    for (int i = 0; i < 10; i++) {
      cin >> w_uni[i];
    }

    for (int i = 0; i < 10; i++) {
      cin >> k_uni[i];
    }

    sort(w_uni.begin(), w_uni.end());
    sort(k_uni.begin(), k_uni.end());

    int total1 = w_uni[7] + w_uni[8] + w_uni[9];
    int total2 = k_uni[7] + k_uni[8] + k_uni[9];

    if (total1 == 0 && total2 == 0)
      break;

    cout << total1 << ' ' << total2 << endl;
  }
}

Error: TLE

Fixed submission:

/**********************************************

1.
2.炭田高輝
3.sumita.takaki@gmail.com

last updated: 2013-07-10@12:15

 **********************************************/
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main(void) {

  while (1) {

    int w_uni[10], k_uni[10];

    for (int i = 0; i < 10; i++) {
      cin >> w_uni[i];
    }
    if (w_uni[0] == NULL)
      break;

    for (int i = 0; i < 10; i++) {
      cin >> k_uni[i];
    }

    sort(w_uni, w_uni + 9);
    sort(k_uni, k_uni + 9);

    int total1 = w_uni[0] + w_uni[1] + w_uni[2];
    int total2 = k_uni[0] + k_uni[1] + k_uni[2];

    cout << total1 << ' ' << total2 << endl;
  }
}
Problem p00456 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>

using namespace std;

int main() {
  int b = 0;
  int w = 0;
  int aa[3] = {0, 0, 0};
  int a[3] = {0, 0, 0};
  for (int i = 0; i < 10; i++) {
    cin >> b;
    if (b > a[2]) {
      a[2] = b;
    }
    if (a[2] > a[1]) {
      w = a[1];
      a[1] = a[2];
      a[2] = w;
    }
    if (a[1] > a[0]) {
      w = a[0];
      a[0] = a[1];
      a[1] = w;
    }
  }
  for (int i = 0; i < 10; i++) {
    cin >> b;
    if (b > aa[2]) {
      aa[2] = b;
    }
    if (aa[2] > aa[1]) {
      w = aa[1];
      aa[1] = aa[2];
      aa[2] = w;
    }
    if (aa[1] > aa[0]) {
      w = aa[0];
      aa[0] = aa[1];
      aa[1] = w;
    }
  }
  cout << a[0] + a[1] + a[2] << " " << aa[0] + aa[1] + aa[2] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
using namespace std;

int main() {
  set<int> a;
  int h = 0;
  int hh = 0;
  for (int b = 0; b < 10; b++) {
    int c;
    cin >> c;
    a.insert(-c);
  }
  set<int>::iterator it = a.begin();
  for (int g = 0; g < 3; g++) {
    h -= *it;
    *it++;
  }
  set<int> aa;
  for (int bb = 0; bb < 10; bb++) {
    int cc;
    cin >> cc;
    a.insert(-cc);
  }
  set<int>::iterator itt = aa.begin();
  for (int g = 0; g < 3; g++) {
    hh -= *itt;
    *itt++;
  }
  cout << h << endl;
  cout << hh << endl;
  return 0;
}
Problem p00457 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
int a[10000], n;
int F(int k, int x) {
  int i, j, r = 0, t = a[k];
  a[k] = x;
  for (i = k; i > 0 && a[i] == a[i - 1]; --i)
    ;
  for (j = k; j + 1 < n && a[j] == a[j + 1]; ++j)
    ;
  a[k] = t;
  if (j - i < 3)
    return 0;
  r = j - i + 1;
  for (;;) {
    if (--i < 0 || ++j >= n || a[i] - a[j])
      break;
    t = 2;
    for (; i > 0 && a[i] == a[i - 1]; --i)
      ++t;
    for (; j + 1 < n && a[j] == a[j + 1]; ++j)
      ++t;
    if (t < 4)
      break;
    r += t;
  }
  return r;
}
int main() {
  int i, r;
  while (scanf("%d", &n), n) {
    for (r = i = 0; i < n; ++i)
      scanf("%d", a + i);
    for (i = 0; i < n; ++i) {
      r = std::max(r, F(i, (a[i]) % 3 + 1));
      r = std::max(r, F(i, (a[i] + 1) % 3 + 1));
    }
    printf("%d\\n", n - r);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <list>
#include <stdio.h>
std::list<int> o, l;
std::list<int>::iterator i, j, k, z;
int f() {
  int x;
  for (i = l.begin(); i != l.end();) {
    x = 1;
    for (j = i, ++j; j != l.end(); ++j) {
      if (*i == *j)
        ++x;
      else
        break;
    }
    if (x > 3) {
      i = l.erase(i, j);
      if (l.empty())
        return 0;
      if (i != l.begin()) {
        for (k = i, --k; k != l.begin() && *k == *i; --k)
          ;
        i = k;
      }
    } else
      i = j;
  }
  return l.size();
}
int main() {
  int n, x, y, r;
  while (scanf("%d", &n), n) {
    o.clear();
    r = n;
    while (n--)
      scanf("%d", &x), o.push_back(x);
    for (z = o.begin(); z != o.end(); ++z) {
      for (y = 0;;) {
        *z = *z % 3 + 1;
        l = o;
        if (++y > 2)
          break;
        x = f(), r = (r < x ? r : x);
      }
    }
    printf("%d\\n", r);
  }
  return 0;
}
Problem p00457 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;
int no[4][100001], color[100001];
int n;
void calc_color(int depth, int first_pos, int first_color, int l, int r,
                int curr_color) {
  int s1 = 0, s2 = 0;
  for (; l > 0 && color[l] == curr_color; l--, s1++)
    ;
  for (; r <= n && color[r] == curr_color; r++, s2++)
    ;
  int len = s1 + s2 + (depth == 0 ? 1 : 0);
  if (4 <= len) {

    no[first_color][first_pos] += len;
    if (color[l] == color[r]) {
      calc_color(depth + 1, first_pos, first_color, l, r, color[l]);
    }
  }
}

int main() {

  while (cin >> n, n) {
    fill(color, color + 100001, -1);

    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= 3; j++) {
        no[j][i] = 0;
      }
    }
    for (int i = 1; i <= n; i++) {
      cin >> color[i];
    }

    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= 3; j++) {
        calc_color(0, i, j, i - 1, i + 1, j);
      }
    }
    int res = n;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= 3; j++) {
        res = min(res, n - no[j][i]);
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

int rensa(vector<int> vec) {
  vector<int> tmp;
  for (int i = 0; i + 3 < vec.size(); i++) {
    if (vec[i] == vec[i + 1] && vec[i + 1] == vec[i + 2] &&
        vec[i + 2] == vec[i + 3]) {
      int r = i + 3;

      while (vec[r + 1] == vec[r] && r + 1 < vec.size())
        r++;
      tmp.clear();
      for (int j = 0; j < vec.size(); j++) {
        if (j < i || r < j)
          tmp.push_back(vec[j]);
      }
      vec.clear();
      copy(tmp.begin(), tmp.end(), back_inserter(vec));

      i = 0;
    }
  }
  return vec.size();
}

int main() {
  int n;
  vector<int> cha;
  while (cin >> n, n) {
    cha.clear();
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      cha.push_back(a);
    }
    int ret = cha.size();
    for (int i = 0; i < cha.size(); i++) {
      vector<int> tmp;

      tmp.clear();
      copy(cha.begin(), cha.end(), back_inserter(tmp));

      if (0 <= i && i < cha.size() - 1) {

        tmp[i] = cha[i + 1];
        int l, r;
        l = r = i;

        while (tmp[l - 1] == tmp[l] && l >= 1) {
          l--;
        }

        while (tmp[r + 1] == tmp[r] && r <= tmp.size() - 1) {
          r++;
        }

        if (3 <= r - l) {
          ret = min(ret, rensa(tmp));
        }
      }

      tmp.clear();
      copy(cha.begin(), cha.end(), back_inserter(tmp));
      if (1 <= i && i < cha.size()) {
        tmp[i] = cha[i - 1];
        int l, r;
        l = r = i;
        while (tmp[l - 1] == tmp[l] && l >= 1) {
          l--;
        }
        while (tmp[r + 1] == tmp[r] && r <= tmp.size() - 1) {
          r++;
        }

        if (3 <= r - l) {
          ret = min(ret, rensa(tmp));
        }
      }
    }
    cout << ret << endl;
  }
  return 0;
}
Problem p00457 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define INF 100000005
#define MAX 100001
#define dmp make_pair
#define dpb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int a[10000];
int solve(int p, int n) {
  int l = p - 1, r = p + 1, t;
  int s1, s2, ans = 0, f = 1;
  while (0 <= l) {
    t = a[l];
    s1 = s2 = 0;
    if (f)
      s1 = 1, f = 0;
    // if(p == 5)printf("%d %d %d\\n", l, r, t);
    while (0 <= l && a[l] == t) {
      s1++;
      l--;
    }
    while (r < n && a[r] == t) {
      s2++;
      r++;
    }
    if (s1 + s2 >= 4) {
      ans += s1 + s2;
    } else
      break;
  }
  return n - ans;
}

int main() {
  int n, ans;
  while (1) {
    scanf("%d", &n);
    ans = INF;
    rep(i, n) scanf("%d", &a[i]);
    if (!n)
      break;
    for (int i = 1; i < n - 1; i++) {
      ans = min(ans, solve(i, n));
    }
    if (ans == INF)
      printf("%d\\n", n - 1);
    else
      printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define INF 100000005
#define MAX 100001
#define dmp make_pair
#define dpb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int a[10000];
int solve(int p, int n) {
  int l = p - 1, r = p + 1, t;
  int s1, s2, ans = 0, f = 1;
  while (0 <= l) {
    t = a[l];
    s1 = s2 = 0;
    if (f)
      s1 = 1, f = 0;
    // if(p == 5)printf("%d %d %d\\n", l, r, t);
    while (0 <= l && a[l] == t) {
      s1++;
      l--;
    }
    while (r < n && a[r] == t) {
      s2++;
      r++;
    }
    if (s1 + s2 >= 4) {
      ans += s1 + s2;
    } else
      break;
  }
  return n - ans;
}

int main() {
  int n, ans;
  while (1) {
    scanf("%d", &n);
    ans = INF;
    rep(i, n) scanf("%d", &a[i]);
    for (int i = 1; i < n - 1; i++) {
      ans = min(ans, solve(i, n));
    }
    if (ans == INF)
      printf("%d\\n", n - 1);
    else
      printf("%d\\n", ans);
  }
  return 0;
}
Problem p00457 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
int chr[10000], N;
int solve(int n) {
  int count = 1, check = chr[n];
  int i, j;
  for (i = n - 1; i >= 0; i--)
    if (chr[i] != check)
      break;
    else
      count++;
  for (j = n + 1; j < N; j++)
    if (chr[j] != check)
      break;
    else
      count++;
  if (count >= 4) {
    if (i == -1 || j == N)
      return count;
    if (chr[i] != chr[j])
      return count;
    int add;
    while (i >= 0 && j < N) {
      add = 0;
      check = chr[i];
      for (i; i >= 0; i--)
        if (chr[i] != check)
          break;
        else
          add++;
      for (j; j < N; j++)
        if (chr[j] != check)
          break;
        else
          add++;
      if (add >= 4)
        count += add;
      else
        break;
    }
    return count;
  } else
    return 0;
}
int main() {
  while (scanf("%d", &N), N) {
    for (int i = 0; i < N; i++)
      scanf("%d", &chr[i]);
    int res = 0, ret, tmp;
    for (int i = 0; i < N; i++) {
      tmp = chr[i];
      for (int j = 1; j <= 3; j++) {
        if (tmp == j)
          continue;
        chr[i] = j;
        ret = solve(i);
        if (res < ret)
          res = ret;
      }
      chr[i] = tmp;
    }
    printf("%d\\n", N - res);
  }
}

Error: -11

Fixed submission:

#include <cstdio>
int chr[10000], N;
int solve(int n) {
  int count = 1, check = chr[n];
  int i, j;
  for (i = n - 1; i >= 0; i--)
    if (chr[i] != check)
      break;
    else
      count++;
  for (j = n + 1; j < N; j++)
    if (chr[j] != check)
      break;
    else
      count++;
  if (count >= 4) {
    if (i == -1 || j == N)
      return count;
    if (chr[i] != chr[j])
      return count;
    int add;
    while (i >= 0 && j < N) {
      add = 0;
      check = chr[i];
      for (i; i >= 0; i--)
        if (chr[i] != check)
          break;
        else
          add++;
      for (j; j < N; j++)
        if (chr[j] != check)
          break;
        else
          add++;
      if (add >= 4)
        count += add;
      else
        break;
    }
    return count;
  } else
    return 0;
}
int main() {
  FILE *in = fopen("input.txt", "r");
  while (fscanf(in, "%d", &N), N) {
    for (int i = 0; i < N; i++)
      fscanf(in, "%d", &chr[i]);
    int res = 0, ret, tmp;
    for (int i = 0; i < N; i++) {
      tmp = chr[i];
      for (int j = 1; j <= 3; j++) {
        if (tmp == j)
          continue;
        chr[i] = j;
        ret = solve(i);
        if (res < ret)
          res = ret;
      }
      chr[i] = tmp;
    }
    printf("%d\\n", N - res);
  }
}