Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pci pair<char, int>
#define ppll pair<pll, pll>
#define vll vector<ll>
#define vvll vector<vll>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mll map<ll, ll>
#define fastmap gp_hash_table
#define cd complex<double>
#define vcd vector<cd>
#define PI 3.14159265358979
#pragma 03
using namespace std;
using namespace __gnu_pbds;
// yesterday was a series of blunders.
// first failed to properly implement E (which was just really basic dp/brute
// force), then failed to realize that F is just basic centroid decomposition +
// dsu. (if I could just decently implement that in contest time... 2100 is
// within reach...) such mistakes cannot repeat again.
ll a[3], b[3];
ll dp[2500005];
int main() {
  ll n;
  cin >> n;
  for (ll i = 0; i < 3; i++)
    cin >> a[i];
  for (ll i = 0; i < 3; i++)
    cin >> b[i];
  for (ll i = 0; i <= n; i++) {
    dp[i] = max(dp[i], i);
    for (ll j = 0; j < 3; j++) {
      if (i - a[j] >= 0)
        dp[i] = max(dp[i], dp[i - a[j]] + b[j]);
    }
  }
  n = dp[n];
  for (ll i = 0; i <= n; i++) {
    dp[i] = i;
    for (ll j = 0; j < 3; j++) {
      if (i - b[j] >= 0)
        dp[i] = max(dp[i], dp[i - b[j]] + a[j]);
    }
  }
  n = dp[n];
  cout << n << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pci pair<char, int>
#define ppll pair<pll, pll>
#define vll vector<ll>
#define vvll vector<vll>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mll map<ll, ll>
#define fastmap gp_hash_table
#define cd complex<double>
#define vcd vector<cd>
#define PI 3.14159265358979
#pragma 03
using namespace std;
using namespace __gnu_pbds;
// yesterday was a series of blunders.
// first failed to properly implement E (which was just really basic dp/brute
// force), then failed to realize that F is just basic centroid decomposition +
// dsu. (if I could just decently implement that in contest time... 2100 is
// within reach...) such mistakes cannot repeat again.
ll a[3], b[3];
ll dp[36000000];
int main() {
  ll n;
  cin >> n;
  for (ll i = 0; i < 3; i++)
    cin >> a[i];
  for (ll i = 0; i < 3; i++)
    cin >> b[i];
  for (ll i = 0; i <= n; i++) {
    dp[i] = max(dp[i], i);
    for (ll j = 0; j < 3; j++) {
      if (i - a[j] >= 0)
        dp[i] = max(dp[i], dp[i - a[j]] + b[j]);
    }
  }
  n = dp[n];
  for (ll i = 0; i <= n; i++) {
    dp[i] = i;
    for (ll j = 0; j < 3; j++) {
      if (i - b[j] >= 0)
        dp[i] = max(dp[i], dp[i - b[j]] + a[j]);
    }
  }
  n = dp[n];
  cout << n << endl;
}