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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF (ll)1E9
#define EPS 1E-9
#define MOD 1000000007
#define PI 3.1415926535897932384

template <class T> ostream &operator<<(std::ostream &o, const vector<T> &v) {
  rep(i, v.size()) { o << (i > 0 ? " " : "") << v[i]; }
  return o;
}

// int dx[]={1,1,1,0,-1,-1,-1,0},dy[8]={1,0,-1,-1,-1,0,1,1,1};

// constexpr ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }

void solve(int n) {
  vi v(n, 0);
  vi dist(n + 1, INF);
  vector<vector<bool>> dp(n + 1, vector<bool>(n, false));
  rep(i, n) { cin >> v[i]; }
  for (int l = 2; l <= n; l += 2) {
    for (int i = 0; i + l <= n; i++) {
      bool b = false;
      if (l != 2) {
        for (int j = 2; j < l; j += 2) {
          if (dp[j][i] && dp[l - j][i + j])
            b = true;
        }
      }
      int dif = abs(v[i] - v[i + l - 1]);
      if (dif <= 1 && (l == 2 ? 1 : dp[l - 2][i + 1]))
        b = true;
      if (b)
        dp[l][i] = true;
      // cout << l << " " << i << " " << dp[l][i] << endl;
    }
  }
  int pos = 0;
  deque<pii> q;
  q.push_back({0, 0});
  while (!q.empty()) {
    pii p = q.front();
    q.pop_front();
    if (p.sc < dist[p.fs]) {
      dist[p.fs] = p.sc;
      if (p.fs == n) {
        cout << n - p.sc << endl;
        return;
      }
      for (int i = 2; i < n + 1; i += 2) {
        if (dp[i][p.fs]) {
          q.push_front({p.fs + i, p.sc});
        }
      }
      q.push_back({p.fs + 1, p.sc + 1});
    }
  }
}

int main(void) {
  int n;
  while (cin >> n, n) {
    solve(n);
  }
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF (ll)1E9
#define EPS 1E-9
#define MOD 1000000007
#define PI 3.1415926535897932384

template <class T> ostream &operator<<(std::ostream &o, const vector<T> &v) {
  rep(i, v.size()) { o << (i > 0 ? " " : "") << v[i]; }
  return o;
}

// int dx[]={1,1,1,0,-1,-1,-1,0},dy[8]={1,0,-1,-1,-1,0,1,1,1};

// constexpr ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }

void solve(int n) {
  vi v(n, 0);
  vector<vector<bool>> dp(n + 1, vector<bool>(n, false));
  rep(i, n) { cin >> v[i]; }
  for (int l = 2; l <= n; l += 2) {
    for (int i = 0; i + l <= n; i++) {
      bool b = false;
      if (l != 2) {
        for (int j = 2; j < l; j += 2) {
          if (dp[j][i] && dp[l - j][i + j])
            b = true;
        }
      }
      int dif = abs(v[i] - v[i + l - 1]);
      if (dif <= 1 && (l == 2 ? 1 : dp[l - 2][i + 1]))
        b = true;
      if (b)
        dp[l][i] = true;
      // cout << l << " " << i << " " << dp[l][i] << endl;
    }
  }
  int pos = 0;
  queue<pii> q;
  q.push({0, 0});
  while (!q.empty()) {
    pii p = q.front();
    q.pop();
    if (p.fs == n) {
      cout << n - p.sc << endl;
      return;
    }
    for (int i = 2; i < n + 1; i += 2) {
      if (dp[i][p.fs]) {
        q.push({p.fs + i, p.sc});
      }
    }
    q.push({p.fs + 1, p.sc + 1});
  }
}

int main(void) {
  int n;
  while (cin >> n, n) {
    solve(n);
  }
}