Problem p03008 (C++)

Buggy submission (status: Time Limit Exceeded):
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define trav(x, a) for (const auto &x : a)
#define sz(x) (int)(x).size()
#define mem(a, v) memset((a), (v), sizeof(a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n)                                                              \
  for (int _i = 0; _i < (n); _i++)                                             \
  ni(a[_i])
#define nal(a, n)                                                              \
  for (int _i = 0; _i < (n); _i++)                                             \
  nl(a[_i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int, int, hash<int>> ht;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;
ll a[3][3], b[3], it[3];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 3; j++)
      scanf("%lld", &a[i][j]);
  ll ans = n;
  int c = 0;
  for (int i = 0; i < 3; i++)
    c += a[0][i] < a[1][i];
  for (int i = 0; i < 3; i++)
    it[i] = i;
  sort(it, it + 3,
       [](int x, int y) { return a[1][x] - a[0][x] > a[1][y] - a[0][y]; });
  if (c == 3 || c == 0) {
    for (int i = 0; i < 3; i++) {
      b[i] = abs(a[0][i] - a[1][i]);
      a[0][i] = max(a[0][i], a[1][i]);
      a[1][i] = a[0][i] - b[i];
    }
    for (int i = 0; i <= n; i += a[1][0])
      for (int j = 0; j + i <= n; j += a[1][1])
        ans = max(ans, i / a[1][0] * a[0][0] + j / a[1][1] * a[0][1] +
                           ((n - i - j) / a[1][2]) * a[0][2] +
                           ((n - i - j) % a[1][2]));
  } else if (c == 2) {
    for (int i = 0; i <= n; i += a[0][it[0]])
      for (int j = 0; j + i <= n; j += a[0][it[1]]) {
        ll nx = i / a[0][it[0]] * a[1][it[0]] + j / a[0][it[1]] * a[1][it[1]] +
                (n - i - j);
        ans = max(ans, (nx / a[1][it[2]]) * a[0][it[2]] + (nx % a[1][it[2]]));
      }
  } else {
    for (int i = 0; i <= n; i += a[0][it[0]]) {
      ll nx = i / a[0][it[0]] * a[1][it[0]] + n - i;
      for (int j = 0; j <= nx; j += a[1][it[1]])
        ans = max(ans, j / a[1][it[1]] * a[0][it[1]] +
                           ((nx - j) / a[1][it[2]]) * a[0][it[2]] +
                           ((nx - j) % a[1][it[2]]));
    }
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define trav(x, a) for (const auto &x : a)
#define sz(x) (int)(x).size()
#define mem(a, v) memset((a), (v), sizeof(a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n)                                                              \
  for (int _i = 0; _i < (n); _i++)                                             \
  ni(a[_i])
#define nal(a, n)                                                              \
  for (int _i = 0; _i < (n); _i++)                                             \
  nl(a[_i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int, int, hash<int>> ht;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;
ll a[3][3], b[3], it[3];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 3; j++)
      scanf("%lld", &a[i][j]);
  ll ans = n;
  int c = 0;
  for (int i = 0; i < 3; i++)
    c += a[0][i] < a[1][i];
  for (int i = 0; i < 3; i++)
    it[i] = i;
  sort(it, it + 3,
       [](int x, int y) { return a[1][x] - a[0][x] > a[1][y] - a[0][y]; });
  if (c == 3 || c == 0) {
    for (int i = 0; i < 3; i++) {
      b[i] = abs(a[0][i] - a[1][i]);
      a[0][i] = max(a[0][i], a[1][i]);
      a[1][i] = a[0][i] - b[i];
    }
    for (int i = 0; i <= n; i += a[1][0])
      for (int j = 0; j + i <= n; j += a[1][1])
        ans = max(ans, i / a[1][0] * a[0][0] + j / a[1][1] * a[0][1] +
                           ((n - i - j) / a[1][2]) * a[0][2] +
                           ((n - i - j) % a[1][2]));
  } else if (c == 2) {
    for (int i = 0; i <= n; i += a[0][it[0]])
      for (int j = 0; j + i <= n; j += a[0][it[1]]) {
        ll nx = i / a[0][it[0]] * a[1][it[0]] + j / a[0][it[1]] * a[1][it[1]] +
                (n - i - j);
        ans = max(ans, (nx / a[1][it[2]]) * a[0][it[2]] + (nx % a[1][it[2]]));
      }
  } else {
    ll nx = n / a[0][it[0]] * a[1][it[0]] + (n % a[0][it[0]]);
    if (a[0][it[1]] * a[1][it[2]] > a[0][it[2]] * a[1][it[1]])
      swap(it[1], it[2]);
    for (int j = 0; j <= nx; j += a[1][it[1]])
      ans = max(ans, j / a[1][it[1]] * a[0][it[1]] +
                         ((nx - j) / a[1][it[2]]) * a[0][it[2]] +
                         ((nx - j) % a[1][it[2]]));
  }
  printf("%lld\n", ans);
  return 0;
}