Problem p03008 (C++)

Buggy submission (status: Runtime Error):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author alireza_kaviani
 */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <class T>
using Tree =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef pair<ld, ld> pld;
typedef pair<string, string> pss;

#define all(x) (x).begin(), (x).end()
#define Sort(x) sort(all((x)))
#define X first
#define Y second
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << " = " << x << endl
#define SZ(x) ll(x.size())
#define fast_io                                                                \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define file_io                                                                \
  freopen("in.txt", "r+", stdin);                                              \
  freopen("out.txt", "w+", stdout);
#define set_random                                                             \
  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

inline ll input() {
  ll n;
  cin >> n;
  return n;
}

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}

set_random;

const ll MAXN = 2.5e7 + 10;
const ll INF = 1e15;
const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9;

ll n, ans, ga[4], gb[4], dp[MAXN], dp2[MAXN];

int main() {
  fast_io;
  for (ll j = 0; j < MAXN; j++) {
    dp[j] = -INF;
    dp2[j] = -INF;
  }

  cin >> n >> ga[1] >> ga[2] >> ga[3] >> gb[1] >> gb[2] >> gb[3];

  dp[n] = 0;
  for (ll i = 1; i < 4; i++) {
    for (ll j = MAXN - 1; j >= ga[i]; j--) {
      dp[j - ga[i]] = max(dp[j - ga[i]], dp[j] + gb[i]);
    }
  }

  n = 0;
  for (ll i = 0; i < MAXN; i++)
    n = max(n, i + dp[i]);

  dp2[n] = 0;
  for (ll i = 1; i < 4; i++) {
    for (ll j = MAXN - 1; j >= ga[i]; j--) {
      dp2[j - gb[i]] = max(dp2[j - gb[i]], dp2[j] + ga[i]);
    }
  }

  n = 0;
  for (ll i = 0; i < MAXN; i++)
    n = max(n, i + dp2[i]);

  cout << n << endl;

  return 0;
}
/*
todo :
    1- set constants
    2- check TimeLimit and MemoryLimit
    3- check special test cases
    4- don't forget BS can help you that is good idea(use for loop for floats)
    5- don't forget printf and scanf can help you in your code speed
*/


Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author alireza_kaviani
 */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <class T>
using Tree =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef pair<ld, ld> pld;
typedef pair<string, string> pss;

#define all(x) (x).begin(), (x).end()
#define Sort(x) sort(all((x)))
#define X first
#define Y second
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << " = " << x << endl
#define SZ(x) ll(x.size())
#define fast_io                                                                \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define file_io                                                                \
  freopen("in.txt", "r+", stdin);                                              \
  freopen("out.txt", "w+", stdout);
#define set_random                                                             \
  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

inline ll input() {
  ll n;
  cin >> n;
  return n;
}

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}

set_random;

const ll MAXN = 2.5e7 + 10;
const ll INF = 1e15;
const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9;

ll n, ans, ga[4], gb[4], dp[MAXN], dp2[MAXN];

int main() {
  fast_io;
  for (ll j = 0; j < MAXN; j++) {
    dp[j] = -INF;
    dp2[j] = -INF;
  }

  cin >> n >> ga[1] >> ga[2] >> ga[3] >> gb[1] >> gb[2] >> gb[3];

  dp[n] = 0;
  for (ll i = 1; i < 4; i++) {
    for (ll j = MAXN - 1; j >= ga[i]; j--) {
      dp[j - ga[i]] = max(dp[j - ga[i]], dp[j] + gb[i]);
    }
  }

  n = 0;
  for (ll i = 0; i < MAXN; i++)
    n = max(n, i + dp[i]);

  dp2[n] = 0;
  for (ll i = 1; i < 4; i++) {
    for (ll j = MAXN - 1; j >= gb[i]; j--) {
      dp2[j - gb[i]] = max(dp2[j - gb[i]], dp2[j] + ga[i]);
    }
  }

  n = 0;
  for (ll i = 0; i < MAXN; i++)
    n = max(n, i + dp2[i]);

  cout << n << endl;

  return 0;
}
/*
todo :
    1- set constants
    2- check TimeLimit and MemoryLimit
    3- check special test cases
    4- don't forget BS can help you that is good idea(use for loop for floats)
    5- don't forget printf and scanf can help you in your code speed
*/