Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
///////////////////library zone!!!!!!!!!!!!!!!!!!!!!!!!!!!!
typedef long long ll;
typedef long double ld;
#define all(a) (a).begin(), (a).end()
#define EPS (1e-5)
#define bit(n, k) ((n >> k) & 1)
const ll Mod = 1000000007;
const ll mod = 998244353;
struct H {
  ll x, y;
  bool operator<(const H &b) const {
    if (x != b.x)
      return x < b.x;
    return y < b.y;
  }
  bool operator>(const H &b) const {
    if (x != b.x)
      return x > b.x;
    return y > b.y;
  }
  bool operator==(const H &b) const { return x == b.x && y == b.y; }
  bool operator!=(const H &b) const { return (*this) != b; }
};
struct P {
  ll pos, cost;
  bool operator<(const P &b) const { return cost < b.cost; }
  bool operator>(const P &b) const { return cost > b.cost; }
};
struct B {
  ll to, cost;
};
struct E {
  ll from, to, cost;
  bool operator<(const E &b) const { return cost < b.cost; }
  bool operator>(const E &b) const { return cost > b.cost; }
};
template <typename T, typename U> void chmin(T &a, U b) {
  if (a > b)
    a = b;
}
template <typename T, typename U> void chmax(T &a, U b) {
  if (a < b)
    a = b;
}
template <typename T> T max_0(T a) {
  if (a < 0)
    return 0;
  return a;
}
template <typename T> T min_0(T a) {
  if (a > 0)
    return 0;
  return a;
}
ll read() {
  ll u;
  ll k = scanf("%lld", &u);
  return u;
}
ll gcd(ll i, ll j) {
  if (i > j)
    swap(i, j);
  if (i == 0)
    return j;
  return gcd(j % i, i);
}
ll mod_pow(ll x, ll n, ll p) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % p;
    x = x * x % p;
    n >>= 1;
  }
  return res;
} // x^n%p
const ll Inf = 3023372036854775807;
const int inf = 1500000000;
#define int long long
//----------------------------------------------------
int n;
int ga, sa, ba, gb, sb, bb;
int dp[300000];
signed main() {
  cin >> n >> ga >> sa >> ba >> gb >> sb >> bb;
  int g = 0, s = 0, b = 0;
  int ans = n;
  // AとBの換どんパートが終わった時点での最大値
  for (int i = 0; i <= n; i += ga) {
    s = 0;
    for (int j = 0; i + j <= n; j += sa) {
      if (bb > ba) {
        int b = (n - i - j) / ba;
        ans = max(ans, n - i - j - b * ba + g * gb + s * sb + b * bb);
      } else {
        ans = max(ans, n - i - j + s * sb + g * gb);
      }
      s++;
    }
    g++;
  }
  int res = ans;
  // 現在ドングリ250000個
  // dp[i]=使っていないドングリが残りiこの時の換金した後の増える値の最大値
  // これと決めたやつしか使う必要はない
  if (ga > gb) {
    for (int i = ans; i >= 0; i--) {
      if (i - gb >= 0) {
        dp[i - gb] = max(dp[i - gb], dp[i] + ga - gb);
      }
    }
  }
  if (sa > sb) {
    for (int i = ans; i >= 0; i--) {
      if (i - sb >= 0) {
        dp[i - sb] = max(dp[i - sb], dp[i] + sa - sb);
      }
    }
  }
  if (ba > bb) {
    for (int i = ans; i >= 0; i--) {
      if (i - bb >= 0) {
        dp[i - bb] = max(dp[i - bb], dp[i] + ba - bb);
      }
    }
  }
  int rer = 0;
  for (int i = 0; i <= ans; i++) {
    rer = max(rer, ans + dp[i]);
  }
  cout << rer << endl;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
///////////////////library zone!!!!!!!!!!!!!!!!!!!!!!!!!!!!
typedef long long ll;
typedef long double ld;
#define all(a) (a).begin(), (a).end()
#define EPS (1e-5)
#define bit(n, k) ((n >> k) & 1)
const ll Mod = 1000000007;
const ll mod = 998244353;
struct H {
  ll x, y;
  bool operator<(const H &b) const {
    if (x != b.x)
      return x < b.x;
    return y < b.y;
  }
  bool operator>(const H &b) const {
    if (x != b.x)
      return x > b.x;
    return y > b.y;
  }
  bool operator==(const H &b) const { return x == b.x && y == b.y; }
  bool operator!=(const H &b) const { return (*this) != b; }
};
struct P {
  ll pos, cost;
  bool operator<(const P &b) const { return cost < b.cost; }
  bool operator>(const P &b) const { return cost > b.cost; }
};
struct B {
  ll to, cost;
};
struct E {
  ll from, to, cost;
  bool operator<(const E &b) const { return cost < b.cost; }
  bool operator>(const E &b) const { return cost > b.cost; }
};
template <typename T, typename U> void chmin(T &a, U b) {
  if (a > b)
    a = b;
}
template <typename T, typename U> void chmax(T &a, U b) {
  if (a < b)
    a = b;
}
template <typename T> T max_0(T a) {
  if (a < 0)
    return 0;
  return a;
}
template <typename T> T min_0(T a) {
  if (a > 0)
    return 0;
  return a;
}
ll read() {
  ll u;
  ll k = scanf("%lld", &u);
  return u;
}
ll gcd(ll i, ll j) {
  if (i > j)
    swap(i, j);
  if (i == 0)
    return j;
  return gcd(j % i, i);
}
ll mod_pow(ll x, ll n, ll p) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % p;
    x = x * x % p;
    n >>= 1;
  }
  return res;
} // x^n%p
const ll Inf = 3023372036854775807;
const int inf = 1500000000;
#define int long long
//----------------------------------------------------
int n;
int ga, sa, ba, gb, sb, bb;
int dp[26000000];
signed main() {
  cin >> n >> ga >> sa >> ba >> gb >> sb >> bb;
  int g = 0, s = 0, b = 0;
  int ans = n;
  // AとBの換どんパートが終わった時点での最大値
  for (int i = 0; i <= n; i += ga) {
    s = 0;
    for (int j = 0; i + j <= n; j += sa) {
      if (bb > ba) {
        int b = (n - i - j) / ba;
        ans = max(ans, n - i - j - b * ba + g * gb + s * sb + b * bb);
      } else {
        ans = max(ans, n - i - j + s * sb + g * gb);
      }
      s++;
    }
    g++;
  }
  int res = ans;
  // 現在ドングリ250000個
  // dp[i]=使っていないドングリが残りiこの時の換金した後の増える値の最大値
  // これと決めたやつしか使う必要はない
  if (ga > gb) {
    for (int i = ans; i >= 0; i--) {
      if (i - gb >= 0) {
        dp[i - gb] = max(dp[i - gb], dp[i] + ga - gb);
      }
    }
  }
  if (sa > sb) {
    for (int i = ans; i >= 0; i--) {
      if (i - sb >= 0) {
        dp[i - sb] = max(dp[i - sb], dp[i] + sa - sb);
      }
    }
  }
  if (ba > bb) {
    for (int i = ans; i >= 0; i--) {
      if (i - bb >= 0) {
        dp[i - bb] = max(dp[i - bb], dp[i] + ba - bb);
      }
    }
  }
  int rer = 0;
  for (int i = 0; i <= ans; i++) {
    rer = max(rer, ans + dp[i]);
  }
  cout << rer << endl;
}