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

#include <bits/stdc++.h>
using namespace std;

int fld[5005][5];
void put1(int l, int p) {
  int curr = 5000;
  while (curr >= 0) {
    bool flag = true;
    for (int j = p; j < p + l; j++) {
      if (fld[curr][j])
        flag = false;
    }
    if (flag)
      curr--;
    else
      break;
  }

  for (int j = p; j < p + l; j++)
    fld[curr + 1][j] = 1;
}
void put2(int l, int p) {
  int curr = 5000;
  while (curr >= 0) {
    bool flag = true;
    for (int i = curr; i < curr + l; i++)
      if (fld[i][p])
        flag = false;
    if (flag)
      curr--;
    else
      break;
  }
  for (int i = curr + 1; i < curr + 1 + l; i++)
    fld[i][p] = 1;
}

void del() {
  int curr = 0;
  for (int i = 0; i < 5000; i++) {
    bool flag = true;
    for (int j = 0; j < 5; j++)
      if (fld[i][j] == 0)
        flag = false;
    if (flag) {
      for (int j = 0; j < 5; j++)
        fld[i][j] = 0;
      continue;
    }
    for (int j = 0; j < 5; j++)
      fld[curr][j] = fld[i][j];
    curr++;
  }
}

void debug(int h) {
  for (int i = h; i >= 0; i--) {
    for (int j = 0; j < 5; j++) {
      printf("%d", fld[i][j]);
    }
    puts("");
  }
}

int main() {
  int n;
  while (scanf("%d", &n), n) {
    fill_n(*fld, 5005 * 5, 0);
    while (n--) {
      int d, l, p;
      scanf("%d%d%d", &d, &l, &p);
      p--;
      if (d == 1)
        put1(l, p);
      else
        put2(l, p);

      del();

      // debug(10);
    }

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

    printf("%d\\n", ans);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int fld[5005][5];
void put1(int l, int p) {
  int curr = 5000;
  while (true) {
    bool flag = true;
    for (int j = p; j < p + l; j++) {
      if (fld[curr][j])
        flag = false;
    }
    if (flag)
      curr--;
    else
      break;
  }

  for (int j = p; j < p + l; j++)
    fld[curr + 1][j] = 1;
}
void put2(int l, int p) {
  int curr = 5000;
  while (true) {
    bool flag = true;
    for (int i = curr; i < curr + l; i++)
      if (fld[i][p])
        flag = false;
    if (flag)
      curr--;
    else
      break;
  }
  for (int i = curr + 1; i < curr + 1 + l; i++)
    fld[i][p] = 1;
}

void del() {
  int curr = 0;
  for (int i = 0; i < 5000; i++) {
    bool flag = true;
    for (int j = 0; j < 5; j++)
      if (fld[i][j] == 0)
        flag = false;
    if (flag) {
      for (int j = 0; j < 5; j++)
        fld[i][j] = 0;
      continue;
    }
    for (int j = 0; j < 5; j++)
      fld[curr][j] = fld[i][j];
    curr++;
  }
}

void debug(int h) {
  for (int i = h; i >= 0; i--) {
    for (int j = 0; j < 5; j++) {
      printf("%d", fld[i][j]);
    }
    puts("");
  }
}

int main() {
  int n;
  while (scanf("%d", &n), n) {
    fill_n(*fld, 5005 * 5, 0);
    while (n--) {
      int d, l, p;
      scanf("%d%d%d", &d, &l, &p);
      p--;
      if (d == 1)
        put1(l, p);
      else
        put2(l, p);

      del();

      // debug(10);
    }

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

    printf("%d\\n", ans);
  }
  return 0;
}