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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<short, short> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

vector<P> v;
int n, m;
int dp[201][1001];

int solve(int i, int r) {
  if (i == v.size())
    return 0;
  if (dp[i][r] >= 0)
    return dp[i][r];
  int res;
  if (-v[i].second > r) {
    res = max(solve(i + 1, r), solve(i + 1, -v[i].second) + 1);
  } else
    res = solve(i + 1, r);
  return dp[i][r] = res;
}

int main() {
  while (cin >> n, n) {
    v.clear();
    MS(dp, -1);
    REP(i, n) {
      int a, b;
      cin >> a >> b;
      v.push_back(P(a, -b));
    }
    cin >> m;
    REP(i, m) {
      int a, b;
      cin >> a >> b;
      v.push_back(P(a, -b));
    }
    sort(ALL(v));

    cout << solve(0, 0) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<short, short> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

vector<P> v;
int n, m;
short dp[201][1001][1001];

int solve(int i, int h, int r) {
  if (i == v.size())
    return 0;
  if (dp[i][h][r] >= 0)
    return dp[i][h][r];
  int res;
  if (v[i].first > h && v[i].second > r) {
    res = max(solve(i + 1, h, r), solve(i + 1, v[i].first, v[i].second) + 1);
  } else
    res = solve(i + 1, h, r);
  return dp[i][h][r] = res;
}

int main() {
  while (cin >> n, n) {
    v.clear();
    MS(dp, -1);
    REP(i, n) {
      int a, b;
      cin >> a >> b;
      v.push_back(P(a, b));
    }
    cin >> m;
    REP(i, m) {
      int a, b;
      cin >> a >> b;
      v.push_back(P(a, b));
    }
    sort(ALL(v));
    cout << solve(0, 0, 0) << endl;
  }
  return 0;
}