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

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int x[10000][5], m[5], maxn, sum;
int n;
int a, b, c;
int main() {
  while (true) {
    sum = 0;
    maxn = 0;
    memset(x, 0, sizeof(x));
    memset(m, 0, sizeof(m));
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int h = 0; h < n; h++) {
      cin >> a >> b >> c;
      c--;
      if (a == 1) {
        maxn = 0;
        for (int j = c; j < c + b; j++) {
          maxn = max(maxn, m[j]);
        }
        for (int j = c; j < c + b; j++) {
          x[maxn][j] = 1;
          m[j] = maxn + 1;
        }
      } else {
        for (int j = 0; j < b; j++) {
          x[m[c] + j][c] = 1;
        }
        m[c] += b;
      }
      for (int i = 0; i < 10000; i++) {
        if (x[i][0] + x[i][1] + x[i][2] + x[i][3] + x[i][4] == 5) {
          x[i][0] = 0;
          x[i][1] = 0;
          x[i][2] = 0;
          x[i][3] = 0;
          x[i][4] = 0;
          for (int j = i; j < 9999; j++) {
            for (int k = 0; k < 5; k++) {
              x[j][k] = x[j + 1][k];
            }
          }
          x[9999][0] = 0;
          x[9999][1] = 0;
          x[9999][2] = 0;
          x[9999][3] = 0;
          x[9999][4] = 0;
          i--;
          for (int j = 0; j < 5; j++) {
            for (int k = m[j]; k >= 0; k--) {
              if (x[k][j] == 1) {
                m[j] = k + 1;
                goto E;
              }
            }
            m[j] = 0;
          E:;
          }
        }
      }
    }
    for (int i = 0; i < 10000; i++) {
      for (int j = 0; j < 5; j++) {
        sum += x[i][j];
      }
    }
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int x[1000][5], m[5], maxn, sum;
int n;
int a, b, c;
int main() {
  while (true) {
    sum = 0;
    memset(x, 0, sizeof(x));
    memset(m, 0, sizeof(m));
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int h = 0; h < n; h++) {
      cin >> a >> b >> c;
      c--;
      if (a == 1) {
        maxn = 0;
        for (int j = c; j < c + b; j++) {
          maxn = max(maxn, m[j]);
        }
        for (int j = c; j < c + b; j++) {
          x[maxn][j] = 1;
          m[j] = maxn + 1;
        }
      } else {
        for (int j = 0; j < b; j++) {
          x[m[c] + j][c] = 1;
        }
        m[c] += b;
      }
      for (int i = 0; i < 1000; i++) {
        if (x[i][0] + x[i][1] + x[i][2] + x[i][3] + x[i][4] == 5) {
          x[i][0] = 0;
          x[i][1] = 0;
          x[i][2] = 0;
          x[i][3] = 0;
          x[i][4] = 0;
          for (int j = i; j < 999; j++) {
            for (int k = 0; k < 5; k++) {
              x[j][k] = x[j + 1][k];
            }
          }
          x[999][0] = 0;
          x[999][1] = 0;
          x[999][2] = 0;
          x[999][3] = 0;
          x[999][4] = 0;
          i--;
          m[0]--;
          m[1]--;
          m[2]--;
          m[3]--;
          m[4]--;
        }
      }
    }
    for (int i = 0; i < 1000; i++) {
      for (int j = 0; j < 5; j++) {
        sum += x[i][j];
      }
    }
    cout << sum << endl;
  }
}