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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>

using namespace std;
const long double eps = 1e-12;

typedef long long ll;
typedef pair<int, int> pt;

#define sz(a) ((int)a.size())
#define LL(x) (x << 1)
#define RR(x) ((x << 1) | 1)
#define For(i, a, b) for (int i = a; i <= b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)
#define Rep(i, n) for (int i = 0; i < n; i++)

const int maxn = 5006;
int n, m, a[maxn], b[maxn];
int Next[maxn][1005];

void ReadData() {
  cin >> n >> m;
  For(i, 1, n) cin >> a[i];
  For(i, 1, m) cin >> b[i];
  For(i, 1, 1000) Next[n + 1][i] = n + 1;
  Ford(i, n, 0) {
    For(j, 1, 1000) Next[i][j] = Next[i + 1][j];
    Next[i][a[i]] = i;
  }
}

int dp[3][maxn];

void Process() {
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  int last = 0;
  int res = 0;
  For(i, 1, m) {
    int cur = last ^ 1;
    For(j, 0, i) dp[cur][j] = -1;
    dp[cur][i + 1] = 0;
    For(j, 1, i) {
      if (j == i) {
        int z = Next[0][b[i]];
        if (z <= n)
          if (dp[cur][j] == -1)
            dp[cur][j] = z;
          else
            dp[cur][z] = min(dp[cur][j], z);
      } else if (dp[last][j] != -1) {
        int x = dp[last][j];
        int z = Next[x + 1][b[i]];
        if (z <= n) {
          if (dp[cur][j] == -1)
            dp[cur][j] = z;
          else
            dp[cur][j] = min(dp[cur][j], z);
        }
      }
      if (dp[cur][j] != -1)
        res = max(res, i - j + 1);
    }
    last = cur;
  }
  cout << res << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("/users/youaremysky/documents/workspace/input.inp" , "r" , stdin);
  ReadData();
  Process();
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>

using namespace std;
const long double eps = 1e-12;

typedef long long ll;
typedef pair<int, int> pt;

#define sz(a) ((int)a.size())
#define LL(x) (x << 1)
#define RR(x) ((x << 1) | 1)
#define For(i, a, b) for (int i = a; i <= b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)
#define Rep(i, n) for (int i = 0; i < n; i++)

const int maxn = 5006;
int n, m, a[maxn], b[maxn];
int Next[maxn][1005];

void ReadData() {
  cin >> n >> m;
  For(i, 1, n) cin >> a[i];
  For(i, 1, m) cin >> b[i];
  For(i, 1, 1000) Next[n + 1][i] = n + 1;
  Ford(i, n, 0) {
    For(j, 1, 1000) Next[i][j] = Next[i + 1][j];
    Next[i][a[i]] = i;
  }
}

int dp[maxn][maxn];

void Process() {
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  For(i, 1, m) {
    dp[i][i + 1] = 0;
    For(j, 1, i) {
      if (dp[i - 1][j] != -1) {
        int x = dp[i - 1][j];
        int z = Next[x + 1][b[i]];
        if (z <= n) {
          if (dp[i][j] == -1)
            dp[i][j] = z;
          else
            dp[i][j] = min(dp[i][j], z);
        }
      }
    }
  }
  int res = 0;
  For(i, 1, n) For(j, 1, i) if (dp[i][j] != -1) res = max(res, i - j + 1);
  cout << res << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("/users/youaremysky/documents/workspace/input.inp" , "r" , stdin);
  ReadData();
  Process();
}