Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  vector<ll> AS(4, 0), BS(4, 0);
  cin >> N >> AS[0] >> AS[1] >> AS[2] >> BS[0] >> BS[1] >> BS[2];
  AS[3] = 1;
  BS[3] = 1;

  // const ll ma = 5000*5000 + 10;
  const ll ma = 10000000;
  ll dp[5][ma];
  REP(i, 5) REP(j, ma) dp[i][j] = 0;

  // former
  ll limit = N;
  REP(i, 4) {
    REP(j, limit + 1) {
      if (j < AS[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - AS[i]] + BS[i]);
    }
  }

  ll M = dp[4][limit];

  // latter
  REP(i, 5) REP(j, ma) dp[i][j] = 0;
  limit = M;

  REP(i, 4) {
    REP(j, limit + 1) {
      if (j < BS[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - BS[i]] + AS[i]);
    }
  }

  cout << dp[4][limit] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  vector<ll> AS(4, 0), BS(4, 0);
  cin >> N >> AS[0] >> AS[1] >> AS[2] >> BS[0] >> BS[1] >> BS[2];
  AS[3] = 1;
  BS[3] = 1;

  const ll ma = 5000 * 5000 + 10;
  // const ll ma = 10000;
  static ll dp[5][ma];
  REP(i, 5) REP(j, ma) dp[i][j] = 0;

  // former
  ll limit = N;
  REP(i, 4) {
    REP(j, limit + 1) {
      if (j < AS[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - AS[i]] + BS[i]);
    }
  }

  ll M = dp[4][limit];

  // latter
  REP(i, 5) REP(j, ma) dp[i][j] = 0;
  limit = M;

  REP(i, 4) {
    REP(j, limit + 1) {
      if (j < BS[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - BS[i]] + AS[i]);
    }
  }

  cout << dp[4][limit] << endl;
  return 0;
}