Problem p03008 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, N) for (int i = 0; i < N; ++i)
#define MAX_I INT_MAX      // 1e9
#define MIN_I INT_MIN      //-1e9
#define MAX_UI UINT_MAX    // 1e9
#define MAX_LL LLONG_MAX   // 1e18
#define MIN_LL LLONG_MIN   //-1e18
#define MAX_ULL ULLONG_MAX // 1e19
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<char, char> PCC;
typedef pair<ll, ll> PLL;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<ll, int> PLI;
typedef pair<int, ll> PIL;
typedef pair<ll, char> PLC;
typedef pair<char, ll> PCL;

bool comp(const PII &l, const PII &r) {
  return (double)l.second / l.first > (double)r.second / r.first;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(15);

  ll N;
  cin >> N;
  int N_MAX = 3e7;
  vector<PLL> x(3);
  cin >> x[0].first >> x[1].first >> x[2].first;
  cin >> x[0].second >> x[1].second >> x[2].second;
  int M = x.size();
  // dp[i+1][j]:i番目まで見て,j個ある時の変換後の最大数
  vector<vector<ll>> dp(M + 1, vector<ll>(N_MAX + 1));
  REP(j, N + 1) dp[0][j] = N;
  REP(i, M) {
    REP(j, N + 1) {
      if (j < x[i].first) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] =
            max(dp[i][j], dp[i + 1][j - x[i].first] + x[i].second - x[i].first);
      }
    }
  }
  N = dp[M][N];

  REP(j, N + 1) dp[0][j] = N;
  REP(i, M) {
    REP(j, N + 1) {
      if (j < x[i].second) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - x[i].second] + x[i].first -
                                         x[i].second);
      }
    }
  }
  N = dp[M][N];

  cout << N << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, N) for (int i = 0; i < N; ++i)
#define MAX_I INT_MAX      // 1e9
#define MIN_I INT_MIN      //-1e9
#define MAX_UI UINT_MAX    // 1e9
#define MAX_LL LLONG_MAX   // 1e18
#define MIN_LL LLONG_MIN   //-1e18
#define MAX_ULL ULLONG_MAX // 1e19
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<char, char> PCC;
typedef pair<ll, ll> PLL;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<ll, int> PLI;
typedef pair<int, ll> PIL;
typedef pair<ll, char> PLC;
typedef pair<char, ll> PCL;

bool comp(const PII &l, const PII &r) {
  return (double)l.second / l.first > (double)r.second / r.first;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(15);

  ll N;
  cin >> N;
  int N_MAX = 2.5e7;
  vector<PLL> x(3);
  cin >> x[0].first >> x[1].first >> x[2].first;
  cin >> x[0].second >> x[1].second >> x[2].second;
  int M = x.size();
  // dp[i+1][j]:i番目まで見て,j個ある時の変換後の最大数
  vector<vector<ll>> dp(M + 1, vector<ll>(N_MAX + 1));
  REP(j, N + 1) dp[0][j] = N;
  REP(i, M) {
    REP(j, N + 1) {
      if (j < x[i].first) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] =
            max(dp[i][j], dp[i + 1][j - x[i].first] + x[i].second - x[i].first);
      }
    }
  }
  N = dp[M][N];

  REP(j, N + 1) dp[0][j] = N;
  REP(i, M) {
    REP(j, N + 1) {
      if (j < x[i].second) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - x[i].second] + x[i].first -
                                         x[i].second);
      }
    }
  }
  N = dp[M][N];

  cout << N << endl;
  return 0;
}