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

#include <bits/stdc++.h>

using namespace std;

inline int abs(int a, int b) { return a < b ? b - a : a - b; }

int w[301];
bool rm[301][301];
int rmct[301][301];

int main() {
  int n;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> w[i];
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++)
        rm[i][j] = i == j;
    for (int r = 2; r <= n; r += 2) {
      for (int i = 0; i + r <= n; i++) {
        for (int s = 2; s < r; s += 2) {
          if (rm[i][i + s] && rm[i + s][i + r])
            rm[i][i + r] = true;
        }
        if (!rm[i][i + r] && rm[i + 1][i + r - 1] &&
            abs(w[i], w[i + r - 1]) <= 1)
          rm[i][i + r] = true;
      }
    }
    /*
    int rmw = -1;
    for(int rmr = n / 2 * 2; rmw==-1 && rmr >= 0; rmr-=2)
    {
        for(int i=0; i+rmr<=n; i++)
        {
            if(rm[i][i+rmr])
            {
                rmw = rmr;
                break;
            }
        }
    }
    */
    for (int y = 0; y <= n; y++)
      for (int x = 0; x <= n; x++)
        rmct[y][x] = rm[y][x] ? x - y : 0;
    for (int r = 1; r <= n; r++) {
      for (int i = 0; i + r <= n; i++) {
        for (int s = 1; s < r; s++) {
          rmct[i][i + r] =
              max(rmct[i][i + r], rmct[i][i + s] + rmct[i + s][i + r]);
        }
      }
    }
    cout << rmct[0][n] << endl;
    // XXX DEBUG
    /*
    printf("i\\\\j ");
    for(int x=0; x<=n; x++) printf("%3d ", x);
    cout << endl;
    for(int y=0; y<=n; y++)
    {
        printf("%3d ", y);
        for(int x=0; x<=n; x++)
        {
            printf("%3d ", rm[y][x]);
        }
        cout << endl;
    }
    */
    // XXX DEBUG
    // cout << rmw << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

inline int abs(int a, int b) { return a < b ? b - a : a - b; }

int w[301];
bool rm[301][301];
int rmct[301][301];

int main() {
  int n;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> w[i];
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++)
        rm[i][j] = i == j;
    for (int r = 2; r <= n; r += 2) {
      for (int i = 0; i + r <= n; i++) {
        for (int s = 2; s < r; s += 2) {
          if (rm[i][i + s] && rm[i + s][i + r])
            rm[i][i + r] = true;
        }
        if (!rm[i][i + r] && rm[i + 1][i + r - 1] &&
            abs(w[i], w[i + r - 1]) <= 1)
          rm[i][i + r] = true;
      }
    }
    /*
    int rmw = -1;
    for(int rmr = n / 2 * 2; rmw==-1 && rmr >= 0; rmr-=2)
    {
        for(int i=0; i+rmr<=n; i++)
        {
            if(rm[i][i+rmr])
            {
                rmw = rmr;
                break;
            }
        }
    }
    */
    for (int y = 0; y <= n; y++)
      for (int x = 0; x <= n; x++)
        rmct[y][x] = rm[y][x] ? x - y : 0;
    for (int r = 1; r <= n; r++) {
      for (int i = 0; i <= n; i++) {
        for (int s = 1; s < r; s++) {
          rmct[i][i + r] =
              max(rmct[i][i + r], rmct[i][i + s] + rmct[i + s][i + r]);
        }
      }
    }
    cout << rmct[0][n] << endl;
    // XXX DEBUG
    /*
    printf("i\\\\j ");
    for(int x=0; x<=n; x++) printf("%3d ", x);
    cout << endl;
    for(int y=0; y<=n; y++)
    {
        printf("%3d ", y);
        for(int x=0; x<=n; x++)
        {
            printf("%3d ", rm[y][x]);
        }
        cout << endl;
    }
    */
    // XXX DEBUG
    // cout << rmw << endl;
  }
}