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

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

int board[6000][6];

int main() {
  int n;
  while (cin >> n, n) {
    int top[8];

    for (int i = 0; i < 6000; i++)
      for (int j = 0; j < 6; j++)
        board[i][j] = 0;
    for (int i = 0; i < 8; i++)
      top[i] = 0;

    for (int i = 0; i < n; i++) {
      int d, p, q;
      cin >> d >> p >> q;

      if (d == 1) {
        int h_max = -1;
        for (int j = 0; j < p; j++) {
          h_max = max(top[q - 1 + j], h_max);
        }
        for (int j = 0; j < p; j++) {
          board[h_max][q - 1 + j] = 1;
          top[q - 1 + j] = h_max + 1;
        }

        bool flg = true;
        for (int j = 0; j < 5; j++)
          flg = flg && board[h_max][j];
        if (flg) {
          for (int j = 0; j < 5; j++) {
            for (int k = h_max; k < top[j]; k++) {
              board[k][j] = board[k + 1][j];
            }
          }

          for (int j = 0; j < 5; j++) {
            int tmp = top[j];
            top[j] = 0;
            for (int k = tmp; k >= 0; k--) {
              if (board[k][j]) {
                top[j] = k + 1;
                break;
              }
            }
          }
        }
      }

      else {
        for (int j = 0; j < p; j++) {
          board[top[q - 1] + j][q - 1] = 1;
        }

        int h = top[q - 1];
        top[q - 1] += p;
        int del = 0;
        for (int j = 0; j < p; j++) {
          bool flg = true;
          for (int k = 0; k < 5; k++) {
            flg = flg && board[h + j - del][k];
          }
          if (flg) {
            del++;
          }
          for (int k = 0; k < 5; k++) {
            board[h + j + 1 - del][k] = board[h + j + 1][k];
          }
        }
        for (int j = 0; j < 5; j++) {
          for (int k = h + p - del; k < top[j] + 10; k++) {
            board[k][j] = board[k + del][j];
          }
        }
        for (int j = 0; j < 5; j++) {
          int tmp = top[j];
          top[j] = 0;
          for (int k = tmp; k >= 0; k--) {
            if (board[k][j]) {
              top[j] = k + 1;
              break;
            }
          }
        }
      }
    }

    int ans = 0;
    for (int i = 0; i < 6000; i++) {
      for (int j = 0; j < 5; j++) {
        ans += board[i][j];
      }
    }

    /*
       for(int i = 10; i >= 0; i--) {
       for(int j = 0; j < 5; j++) {
       cout << (board[i][j] ? '*' : '.');
       }
       cout << endl;
       }
       cout << endl;
    // */
    /*
       for(int i = 0; i < 5; i++)
       cout << top[i] << " ";
       cout << endl;
    // */
    cout << ans << endl;
  }
}

Error: -6

Fixed submission:

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

int board[6000][6];

int main() {
  int n;
  while (cin >> n, n) {
    int top[8];

    for (int i = 0; i < 6000; i++)
      for (int j = 0; j < 6; j++)
        board[i][j] = 0;
    for (int i = 0; i < 8; i++)
      top[i] = 0;

    for (int i = 0; i < n; i++) {
      int d, p, q;
      cin >> d >> p >> q;

      if (d == 2 && q - 1 + p > 5)
        abort();

      if (d == 1) {
        int h_max = -1;
        for (int j = 0; j < p; j++) {
          h_max = max(top[q - 1 + j], h_max);
        }
        for (int j = 0; j < p; j++) {
          board[h_max][q - 1 + j] = 1;
          top[q - 1 + j] = h_max + 1;
        }

        bool flg = true;
        for (int j = 0; j < 5; j++)
          flg = flg && board[h_max][j];
        if (flg) {
          for (int j = 0; j < 5; j++) {
            for (int k = h_max; k < top[j]; k++) {
              board[k][j] = board[k + 1][j];
            }
          }

          for (int j = 0; j < 5; j++) {
            int tmp = top[j];
            top[j] = 0;
            for (int k = tmp; k >= 0; k--) {
              if (board[k][j]) {
                top[j] = k + 1;
                break;
              }
            }
          }
        }
      }

      else {
        for (int j = 0; j < p; j++) {
          board[top[q - 1] + j][q - 1] = 1;
        }

        int h = top[q - 1];
        top[q - 1] += p;
        int del = 0;
        for (int j = 0; j < p; j++) {
          bool flg = true;
          for (int k = 0; k < 5; k++) {
            flg = flg && board[h + j][k];
          }
          if (flg) {
            for (int k = 0; k < 5; k++) {
              board[h + j - del][k] = board[h + j + 1][k];
            }
            del++;
          }
        }
        for (int j = 0; j < 5; j++) {
          for (int k = h + p - del; k < top[j] + 10; k++) {
            board[k][j] = board[k + del][j];
          }
        }
        for (int j = 0; j < 5; j++) {
          int tmp = top[j];
          top[j] = 0;
          for (int k = tmp; k >= 0; k--) {
            if (board[k][j]) {
              top[j] = k + 1;
              break;
            }
          }
        }
      }
    }

    int ans = 0;
    for (int i = 0; i < 6000; i++) {
      for (int j = 0; j < 5; j++) {
        ans += board[i][j];
      }
    }

    /*
       for(int i = 10; i >= 0; i--) {
       for(int j = 0; j < 5; j++) {
       cout << (board[i][j] ? '*' : '.');
       }
       cout << endl;
       }
       cout << endl;
    // */
    /*
       for(int i = 0; i < 5; i++)
       cout << top[i] << " ";
       cout << endl;
    // */
    cout << ans << endl;
  }
}