Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

#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;

bool **table;
int **dp;
int N;

int recursive(int left, int right) {

  if (left == right)
    return dp[left][right] = 0;
  if (dp[left][right] != -1)
    return dp[left][right];          //?????¢???????????´???
  if ((right - left + 1) % 2 == 1) { //??????????\\???°?????´???
    return dp[left][right] =
               max(recursive(left, right - 1), recursive(left + 1, right));
  }
  if (table[left][(right - left + 1) / 2] == true)
    return dp[left][right] =
               right - left + 1; // left???right?????¨????¶?????????´???
  if (right == left + 1 && table[left][1] == false)
    return 0;

  int ans = 0;
  for (int i = left + 1; i <= right - 2; i++) {
    ans = max(ans, recursive(left, i) + recursive(i + 1, right));
  }
  return dp[left][right] = ans;
}

int main() {

  int data[301];
  table = new bool
      *[301]; // table[left][right] =
              // left???????§?????????????2*right??????????????¨?¶??????§??????????????????
  dp = new int *[301]; //?????????????????????????????§?¶????????????°
  for (int i = 0; i <= 300; i++) {
    table[i] = new bool[151];
    dp[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 = 0; i <= 300; i++) {
      for (int k = 0; k <= 150; k++)
        table[i][k] = false;
      for (int k = 0; k <= 300; k++) {
        dp[i][k] = -1;
      }
    }

    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;
    }

    //?¶??????????????????????????¨????
    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;
        }
      }
    }

    printf("%d\\n", recursive(1, N));
  }

  return 0;
}

Error: TLE

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;

bool **table;
int **dp;
int N;

int recursive(int left, int right) {

  if (dp[left][right] != 0)
    return dp[left][right]; //?????¢???????????´???
  if (left == right)
    return 0;
  if ((right - left + 1) % 2 == 1) { //??????????\\???°?????´???
    return max(recursive(left, right - 1), recursive(left + 1, right));
  }
  if (table[left][(right - left + 1) / 2] == true)
    return dp[left][right] =
               right - left + 1; // left???right?????¨????¶?????????´???
  if (right == left + 1 && table[left][1] == false)
    return 0;

  int ans = 0;
  for (int i = left + 1; i <= right - 2; i++) {
    ans = max(ans, recursive(left, i) + recursive(i + 1, right));
  }
  return dp[left][right] = ans;
}

int main() {

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

  bool FLG;

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

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

    //?¶??????????????????????????????????
    for (int i = 0; i <= 300; i++) {
      for (int k = 0; k <= 150; k++)
        table[i][k] = false;
      for (int k = 0; k <= 150; k++) {
        dp[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;
    }

    //?¶??????????????????????????¨????
    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;
        }
      }
    }

    printf("%d\\n", recursive(1, N));
  }

  return 0;
}