Problem p03008 (C++)

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

#define int long long
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define fr(i, j, k) for (i = j; i < (k); i++)
#define all(x) x.begin(), x.end()
#define el '\n'
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)

typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<pii> vpi;

// ---------------------------  TEMPLATE ENDS
// -------------------------------------

const pii dxy[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
const int mod = 1e9 + 7;
const int inf = 2e18;
const ld eps = 1e-9;
const int NN = 2.5e7;

vvi adj;

int dp[NN][3];
int a[4], b[4];

void solve() {
  int i = 0, j = 0, k = 0, n = 0, m = 0;
  cin >> n;
  fr(i, 0, 3) { cin >> a[i]; }
  fr(i, 0, 3) { cin >> b[i]; }

  for (j = 0; j < 3; j++) {
    for (i = 0; i <= n; i++) {
      int &v = dp[i][j];
      if (j > 0) {
        v = dp[i][j - 1];
      } else {
        v = i;
      }

      if (b[j] - a[j] > 0 and i >= a[j]) {
        remax(v, dp[i - a[j]][j] + b[j]);
      }
    }
  }
  int now = dp[n][2];
  memset(dp, 0, sizeof dp);
  for (j = 0; j < 3; j++) {
    for (i = 0; i <= now; i++) {
      int &v = dp[i][j];
      if (j > 0) {
        v = dp[i][j - 1];
      } else {
        v = i;
      }

      if (a[j] - b[j] > 0 and i >= b[j]) {
        remax(v, dp[i - b[j]][j] + a[j]);
      }
    }
  }
  cout << dp[now][2] << endl;
}

int32_t main() {
#ifndef TRACE
  ios::sync_with_stdio(false);
  cin.tie(0);
#endif
  int T = 1, tc;
  for (tc = 1; tc <= T; tc++) {
    solve();
  }
  return 0;
}


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

#define int long long
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define fr(i, j, k) for (i = j; i < (k); i++)
#define all(x) x.begin(), x.end()
#define el '\n'
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)

typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<pii> vpi;

// ---------------------------  TEMPLATE ENDS
// -------------------------------------

const pii dxy[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
const int mod = 1e9 + 7;
const int inf = 2e18;
const ld eps = 1e-9;
const int NN = 3e7;

int dp[NN][3];
int a[4], b[4];

void solve() {
  int i = 0, j = 0, k = 0, n = 0, m = 0;
  cin >> n;
  fr(i, 0, 3) { cin >> a[i]; }
  fr(i, 0, 3) { cin >> b[i]; }

  for (j = 0; j < 3; j++) {
    for (i = 0; i <= n; i++) {
      int &v = dp[i][j];
      if (j > 0) {
        v = dp[i][j - 1];
      } else {
        v = i;
      }

      if (b[j] - a[j] > 0 and i >= a[j]) {
        remax(v, dp[i - a[j]][j] + b[j]);
      }
    }
  }
  int now = dp[n][2];
  memset(dp, 0, sizeof dp);
  for (j = 0; j < 3; j++) {
    for (i = 0; i <= now; i++) {
      int &v = dp[i][j];
      if (j > 0) {
        v = dp[i][j - 1];
      } else {
        v = i;
      }

      if (a[j] - b[j] > 0 and i >= b[j]) {
        remax(v, dp[i - b[j]][j] + a[j]);
      }
    }
  }
  cout << dp[now][2] << endl;
}

int32_t main() {
#ifndef TRACE
  ios::sync_with_stdio(false);
  cin.tie(0);
#endif
  int T = 1, tc;
  for (tc = 1; tc <= T; tc++) {
    solve();
  }
  return 0;
}