Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;

// LLONG_MAX
#define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define LOWER_BOUND(v, a) (lower_bound((v).begin(), (v).end(), (a)))
#define UPPER_BOUND(v, a) (upper_bound((v).begin(), (v).end(), (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
using namespace std;

int N, GA, SA, BA, GB, SB, BB;

int dp0[4][5001];
int dp1[4][25000001];

signed main() {
  cin >> N;
  cin >> GA >> SA >> BA;
  cin >> GB >> SB >> BB;

  int m0[4] = {0, GA, SA, BA};
  int v0[4] = {0, GB, SB, BB};

  for (int i = 0; i <= N; i++) {
    dp0[0][i] = i;
  }
  for (int j = 1; j <= 3; j++) {
    for (int i = 0; i <= N; i++) {
      int a = (i >= m0[j]) ? dp0[j][i - m0[j]] + v0[j] : 0;
      int b = dp0[j - 1][i];
      dp0[j][i] = MAX(a, b);
    }
  }

  int M = dp0[3][N];

  int m1[4] = {0, GB, SB, BB};
  int v1[4] = {0, GA, SA, BA};

  for (int i = 0; i <= M; i++) {
    dp1[0][i] = i;
  }
  for (int j = 1; j <= 3; j++) {
    for (int i = 0; i <= M; i++) {
      int a = (i >= m1[j]) ? dp0[1][i - m1[j]] + v1[j] : 0;
      int b = dp1[j - 1][i];
      dp1[j][i] = MAX(a, b);
    }
  }

  cout << dp1[3][M] << endl;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;

// LLONG_MAX
#define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define LOWER_BOUND(v, a) (lower_bound((v).begin(), (v).end(), (a)))
#define UPPER_BOUND(v, a) (upper_bound((v).begin(), (v).end(), (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
using namespace std;

int N, GA, SA, BA, GB, SB, BB;

int dp0[4][5001];
int dp1[4][25000001];

signed main() {
  cin >> N;
  cin >> GA >> SA >> BA;
  cin >> GB >> SB >> BB;

  int m0[4] = {0, GA, SA, BA};
  int v0[4] = {0, GB, SB, BB};

  for (int i = 0; i <= N; i++) {
    dp0[0][i] = i;
  }
  for (int j = 1; j <= 3; j++) {
    for (int i = 0; i <= N; i++) {
      int a = (i >= m0[j]) ? dp0[j][i - m0[j]] + v0[j] : 0;
      int b = dp0[j - 1][i];
      dp0[j][i] = MAX(a, b);
    }
  }

  int M = dp0[3][N];

  int m1[4] = {0, GB, SB, BB};
  int v1[4] = {0, GA, SA, BA};

  for (int i = 0; i <= M; i++) {
    dp1[0][i] = i;
  }
  for (int j = 1; j <= 3; j++) {
    for (int i = 0; i <= M; i++) {
      int a = (i >= m1[j]) ? dp1[j][i - m1[j]] + v1[j] : 0;
      int b = dp1[j - 1][i];
      dp1[j][i] = MAX(a, b);
    }
  }

  cout << dp1[3][M] << endl;
}