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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n;
int x[1000];
int dp[300][300];
int ans[300];

int C(int lb, int ub) {
  if (dp[lb][ub] != -1)
    return dp[lb][ub];
  if (ub - lb <= 0)
    return dp[ub][lb] = 0;
  else if (ub - lb == 1) {
    if (abs(x[lb] - x[ub]) <= 1)
      return dp[lb][ub] = 2;
    return dp[lb][ub] = 0;
  }
  int res = 0;
  if (abs(x[lb] - x[ub]) <= 1) {
    if (C(lb + 1, ub - 1) == ub - lb - 1) {
      res = C(lb + 1, ub - 1) + 2;
    }
  }
  FOR(mid, lb, ub) { res = max(res, C(lb, mid) + C(mid + 1, ub)); }
  return dp[lb][ub] = res;
}

int main() {
  while (cin >> n && n) {
    rep(i, 300) rep(j, 300) { dp[i][j] = -1; }
    rep(i, n) cin >> x[i];
    cout << C(0, n - 1) << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n;
int x[100];
int dp[300][300];
int ans[300];

int C(int lb, int ub) {
  if (dp[lb][ub] != -1)
    return dp[lb][ub];
  if (ub - lb <= 0)
    return dp[ub][lb] = 0;
  else if (ub - lb == 1) {
    if (abs(x[lb] - x[ub]) <= 1)
      return dp[lb][ub] = 2;
    return dp[lb][ub] = 0;
  }
  int res = 0;
  if (abs(x[lb] - x[ub]) <= 1) {
    if (C(lb + 1, ub - 1) == ub - lb - 1) {
      res = C(lb + 1, ub - 1) + 2;
    }
  }
  FOR(mid, lb, ub) { res = max(res, C(lb, mid) + C(mid + 1, ub)); }
  return dp[lb][ub] = res;
}

int main() {
  while (cin >> n && n) {
    rep(i, 300) rep(j, 300) { dp[i][j] = -1; }
    rep(i, n) cin >> x[i];
    cout << C(0, n - 1) << endl;
  }
}