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

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
using namespace std;

int main() {

  int N, data[301];
  bool **table = new bool
      *[301]; // table[left][right] =
              // left???????§?????????????2*right??????????????¨?¶??????§??????????????????
  int **maximum =
      new int *[301]; //?????????????????????????????§?¶????????????°
  for (int i = 0; i <= 300; i++) {
    table[i] = new bool[151];
    maximum[i] = new int[301];
  }

  bool FLG;

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;

    if (N <= 1) {
      printf("0\\n");
      continue;
    }

    //?¶??????????????????????????????????
    for (int i = 1; i <= N; i++) {
      for (int k = 0; k <= N / 2; k++)
        table[i][k] = false;
      for (int k = 1; k <= N; k++)
        maximum[i][k] = 0;
    }

    for (int i = 1; i <= N; i++)
      scanf("%d", &data[i]);

    //????????????2???????¨?
    for (int i = 1; i <= N - 1; i++) {
      if (abs(data[i + 1] - data[i]) <= 1) {
        table[i][1] = true;
        maximum[i][i + 1] = 2;
      }
    }

    //?¶??????????????????????????¨????
    for (int length = 4; length <= N; length += 2) {       //??????????????????
      for (int left = 1; left + length - 1 <= N; left++) { //??????????????????
        FLG = false;

        if (table[left + 1][(length - 2) / 2] == true &&
            abs(data[left] - data[left + length - 1]) <= 1)
          FLG =
              true; //??????????????£?????????length-2??????????¶????????????????????????¨??????+length-1?????´???????????????????¶?????????´???

        if (!FLG) {
          for (int i = 2; i <= length - 2; i += 2) {
            if (table[left][i / 2] == true &&
                table[left + i][(length - i) / 2] == true) {
              FLG = true;
              break;
            }
          }
        }
        if (FLG) {
          table[left][length / 2] = true;
          maximum[left][left + length - 1] =
              length; //????¨?????????§??????OK????????§?????¶????????§???????¨????
        } else {
          // maximum[left][left+length-1]?????´??°????????°????????????
          for (int div = 0; left + div <= left + length - 2;
               div++) { //????´¢?§????????????????
            for (
                int i = 2; left + div + i - 1 <= left + length - 1;
                i +=
                2) { //????´¢?§?????????????????????´?????????????????§????????¢?????????
              maximum[left][left + length - 1] =
                  max(maximum[left][left + length - 1],
                      maximum[left + div][left + div + i - 1] +
                          maximum[min(left + length - 1, left + div + i)]
                                 [left + length - 1]);
            }
          }
          // printf("maximum[%d][%d]:%d\\n",left,left+length-1,maximum[left][left+length-1]);
        }
      }
    }

    if (N % 2 == 0)
      printf("%d\\n", maximum[1][N]);
    else {
      printf("%d\\n", max(maximum[1][N - 1], maximum[2][N]));
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
using namespace std;

int main() {

  int N, data[301];
  bool **table = new bool
      *[301]; // table[left][right] =
              // left???????§?????????????2*right??????????????¨?¶??????§??????????????????
  int **maximum =
      new int *[301]; //?????????????????????????????§?¶????????????°
  for (int i = 0; i <= 300; i++) {
    table[i] = new bool[151];
    maximum[i] = new int[301];
  }

  bool FLG;

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;

    if (N <= 1) {
      printf("0\\n");
      continue;
    }

    //?¶??????????????????????????????????
    for (int i = 1; i <= N; i++) {
      for (int k = 0; k <= N / 2; k++)
        table[i][k] = false;
      for (int k = 1; k <= N; k++)
        maximum[i][k] = 0;
    }

    for (int i = 1; i <= N; i++)
      scanf("%d", &data[i]);

    //????????????2???????¨?
    for (int i = 1; i <= N - 1; i++) {
      if (abs(data[i + 1] - data[i]) <= 1) {
        table[i][1] = true;
        maximum[i][i + 1] = 2;
      }
    }

    //?¶??????????????????????????¨????
    for (int length = 4; length <= N; length += 2) {       //??????????????????
      for (int left = 1; left + length - 1 <= N; left++) { //??????????????????
        FLG = false;

        if (table[left + 1][(length - 2) / 2] == true &&
            abs(data[left] - data[left + length - 1]) <= 1)
          FLG =
              true; //??????????????£?????????length-2??????????¶????????????????????????¨??????+length-1?????´???????????????????¶?????????´???

        if (!FLG) {
          for (int i = 2; i <= length - 2; i += 2) {
            if (table[left][i / 2] == true &&
                table[left + i][(length - i) / 2] == true) {
              FLG = true;
              break;
            }
          }
        }
        if (FLG) {
          table[left][length / 2] = true;
          maximum[left][left + length - 1] =
              length; //????¨?????????§??????OK????????§?????¶????????§???????¨????
        } else {
          // maximum[left][left+length-1]?????´??°????????°????????????
          for (int div = 0; left + div <= left + length - 2;
               div++) { //????´¢?§????????????????
            for (
                int i = 2; left + div + i - 1 <= left + length - 1;
                i +=
                2) { //????´¢?§?????????????????????´?????????????????§????????¢?????????
              maximum[left][left + length - 1] =
                  max(maximum[left][left + length - 1],
                      maximum[left + div][left + div + i - 1] +
                          maximum[left + div + i][left + length - 1]);
            }
          }
          // printf("maximum[%d][%d]:%d\\n",left,left+length-1,maximum[left][left+length-1]);
        }
      }
    }

    if (N % 2 == 0)
      printf("%d\\n", maximum[1][N]);
    else {
      printf("%d\\n", max(maximum[1][N - 1], maximum[2][N]));
    }
  }

  return 0;
}