Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

// output
#define SPBR(w, n) std::cout << (w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline T chmax(T &a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T> inline T chmin(T &a, const T b) {
  return a = (a > b) ? b : a;
}

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

int f(int N, int a[], int b[]) {
  vector<int> dp(N + 1, -INF);
  dp[0] = 0;

  REP(i, N) REP(j, 3) {
    if (i != 0)
      chmax(dp[i], dp[i - 1]);
    if (i + a[j] <= N)
      chmax(dp[i + a[j]], dp[i] + b[j] - a[j]);
  }

  return dp[N] + N;
}

signed main() {
  int N;
  cin >> N;

  int a[3], b[3];
  REP(i, 3) cin >> a[i];
  REP(i, 3) cin >> b[i];

  int ret = f(N, a, b);

  cout << f(ret, b, a) << "\n";

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

// output
#define SPBR(w, n) std::cout << (w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline T chmax(T &a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T> inline T chmin(T &a, const T b) {
  return a = (a > b) ? b : a;
}

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

int f(int N, int a[], int b[]) {
  vector<int> dp(N + 1, -INF);
  dp[0] = 0;

  REP(i, N + 1) REP(j, 3) {
    if (i != 0)
      chmax(dp[i], dp[i - 1]);
    if (i + a[j] <= N)
      chmax(dp[i + a[j]], dp[i] + b[j] - a[j]);
  }

  return dp[N] + N;
}

signed main() {
  int N;
  cin >> N;

  int a[3], b[3];
  REP(i, 3) cin >> a[i];
  REP(i, 3) cin >> b[i];

  int ret = f(N, a, b);

  cout << f(ret, b, a) << "\n";

  return 0;
}