Problem p01096 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

bool used[310][310];
bool dp[310][310];
vi w;

int dp2[310][310];
template <typename T> T chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> T chmin(T &a, const T &b) { return a = min(a, b); }
//[l, r) de tatakeruka
bool calc(int l, int r) {
  if (r - l <= 1)
    return false;
  if (used[l][r])
    return dp[l][r];
  used[l][r] = true;
  if (r - l == 2) {
    return dp[l][r] = (abs(w[l] - w[r - 1]) <= 1);
  }
  for (int m = l + 1; m < r; m++) {
    bool f = true;
    f &= calc(l, m);
    f &= calc(m, r);
    dp[l][r] |= f;
  }
  if (abs(w[l] - w[r - 1]) <= 1)
    dp[l][r] |= calc(l + 1, r - 1);
  return dp[l][r];
}

int calc2(int l, int r) {
  if (dp2[l][r] != -1)
    return dp2[l][r];
  if (r - l <= 1)
    return dp2[l][r] = 0;
  if (dp[l][r])
    return dp2[l][r] = r - l;
  int ret = 0;
  for (int m = l + 1; m < r; m++) {
    chmax(ret, calc2(l, m) + calc2(m, r));
  }
  return ret;
}

int main() {
  int n;
  while (cin >> n && n) {
    w.clear();
    w.resize(n);
    rep(i, n) cin >> w[i];
    rep(i, 310) rep(j, 310) dp[i][j] = used[i][j] = false, dp2[i][j] = -1;
    REP(r, 1, n + 1) rep(l, r) calc(l, r);
    cout << calc2(0, n) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

bool used[310][310];
bool dp[310][310];
vi w;

int dp2[310][310];
template <typename T> T chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> T chmin(T &a, const T &b) { return a = min(a, b); }
//[l, r) de tatakeruka
bool calc(int l, int r) {
  if (r - l <= 1)
    return false;
  if (used[l][r])
    return dp[l][r];
  used[l][r] = true;
  if (r - l == 2) {
    return dp[l][r] = (abs(w[l] - w[r - 1]) <= 1);
  }
  for (int m = l + 1; m < r; m++) {
    bool f = true;
    f &= calc(l, m);
    f &= calc(m, r);
    dp[l][r] |= f;
  }
  if (abs(w[l] - w[r - 1]) <= 1)
    dp[l][r] |= calc(l + 1, r - 1);
  return dp[l][r];
}

int calc2(int l, int r) {
  if (dp2[l][r] != -1)
    return dp2[l][r];
  if (r - l <= 1)
    return dp2[l][r] = 0;
  if (dp[l][r])
    return dp2[l][r] = r - l;
  int ret = 0;
  for (int m = l + 1; m < r; m++) {
    chmax(ret, calc2(l, m) + calc2(m, r));
  }
  return dp2[l][r] = ret;
}

int main() {
  int n;
  while (cin >> n && n) {
    w.clear();
    w.resize(n);
    rep(i, n) cin >> w[i];
    rep(i, 310) rep(j, 310) dp[i][j] = used[i][j] = false, dp2[i][j] = -1;
    REP(r, 1, n + 1) rep(l, r) calc(l, r);
    cout << calc2(0, n) << endl;
  }
  return 0;
}