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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int i = 0; i < (n); i++)                                              \\
      cerr << v[i] << ' ';                                                     \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(10)

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

int dp[310][310];
int n;
vector<int> node;

int dfs(int l, int r) {
  if (l > r)
    return 0;
  if (l == r)
    return 0;
  if (dp[l][r] != -1)
    return dp[l][r];
  if (r - l == 1)
    return (abs(node[l] - node[r]) < 2) ? 2 : 0;

  int ret = 0;

  if (dfs(l + 1, r - 1) == r - l - 1 && (abs(node[l] - node[r]) < 2)) {
    return r - l + 1;
  }

  for (int i = 0; i < r - l; i++) {
    ret = max(ret, dfs(l, l + i) + dfs(l + i + 1, r));
  }

  return dp[l][r] = ret;
}

int main() {
  while (cin >> n, n) {

    REP(i, 310) REP(j, 310) dp[i][j] = -1;
    node.clear();

    REP(i, n) {
      int tmp;
      cin >> tmp;
      node.PB(tmp);
    }

    cout << dfs(0, n - 1) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int i = 0; i < (n); i++)                                              \\
      cerr << v[i] << ' ';                                                     \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(10)

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

int dp[310][310];
int n;
vector<int> node;

int dfs(int l, int r) {
  if (l > r)
    return 0;
  if (l == r)
    return 0;
  if (dp[l][r] != -1)
    return dp[l][r];
  if (r - l == 1)
    return (abs(node[l] - node[r]) < 2) ? 2 : 0;

  int ret = 0;

  if (dfs(l + 1, r - 1) == r - l - 1 && (abs(node[l] - node[r]) < 2)) {
    return r - l + 1;
  }

  for (int i = 1; i < r - l; i++) {
    ret = max(ret, dfs(l, l + i) + dfs(l + i + 1, r));
  }

  return ret;
}

int main() {
  while (cin >> n, n) {

    REP(i, 310) REP(j, 310) dp[i][j] = -1;
    node.clear();

    REP(i, n) {
      int tmp;
      cin >> tmp;
      node.PB(tmp);
    }

    cout << dfs(0, n - 1) << endl;
  }

  return 0;
}