Language: C++

#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <numeric>

using namespace std;
using ll = long long;
const ll mod =  1000000007;

#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REP_FROM(i, j, n) for (ll i = (j); i < (n); ++i)
#define all(x) (x).begin(),(x).end()

ll power(ll base, ll exponent, ll module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ ll root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }}
ll inverse(ll x){ return power(x, mod - 2, mod);}
ll gcd(ll a, ll b) {if(a < b) gcd(b, a);ll r;while(r=a%b) {a=b;b=r;}return b;}
struct combination{ vector<ll> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1){ fact[0] = 1; for(int i = 1; i <= sz; i++){ fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for(int i = sz - 1; i >= 0; i--){ inv[i] = inv[i + 1] * (i + 1) % mod; } } ll C(int p, int q) const{ if(q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } };

ll maxi(ll n, const vector<ll>& a, const vector<ll>& b) {
  ll ans = 0;
  REP(g, n/a[0]+1) {
    ll dg = n - g * a[0];
    // cout << dg << endl;
    REP(s, dg/a[1]+1) {
      ll ds = n - g * a[0] - s * a[1];
      // cout << n + g*(b[0]-a[0]) + s*(b[1]-a[1]) << endl;
      ans = max(ans, n + g*(b[0]-a[0]) + s*(b[1]-a[1]));
      ans = max(ans, n + g*(b[0]-a[0]) + s*(b[1]-a[1]) + ds/a[2]*(b[2] - a[2]));
    }
  }
  return ans;
}

ll maxiA(ll n, const vector<ll>& a, const vector<ll>& b, int best) {
  if(b[0] * 1.0 / a[0] != max({b[0] * 1.0 / a[0], b[1] * 1.0 / a[1], b[2] * 1.0 / a[2]})) {
    if(b[1] * 1.0 / a[1] > b[2] * 1.0 / a[2]) {
      return maxiA(n, a, b, 1);
    }
    else {
        return maxiA(n, a, b, 2);
    }
  }

  ll ans = n - n/a[best]*(b[best]-a[best]);
  ll g_max = n/a[best]*(b[best]-a[best])
  REP(i, 5000) {
    int g = g_max - i;
    if(g < 0) {
      break;
    }
    ll dg = n - g * a[0];
    // cout << dg << endl;
    REP(s, dg/a[1]+1) {
      ll ds = n - g * a[0] - s * a[1];
      // cout << n + g*(b[0]-a[0]) + s*(b[1]-a[1]) << endl;
      ans = max(ans, n + g*(b[0]-a[0]) + s*(b[1]-a[1]));
      ans = max(ans, n + g*(b[0]-a[0]) + s*(b[1]-a[1]) + ds/a[2]*(b[2] - a[2]));
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  ll n;
  vector<ll> a(3);
  vector<ll> b(3);
  cin >> n;
  REP(i, 3) cin >> a[i];
  REP(i, 3) cin >> b[i];
  n = maxi(n, a, b);
  if (n > 5000 * 3) {
    n = maxiA(n, b, a, 0);
  }
  else {
    n = maxi(n, b, a);
  }
  cout << n << endl;
  return 0;
}


Compiler output:
a.cc: In function 'll maxiA(ll, const std::vector<long long int>&, const std::vector<long long int>&, int)':
a.cc:13:19: error: expected ',' or ';' before 'for'
   13 | #define REP(i, n) for (ll i = 0; i < (n); ++i)
      |                   ^~~
a.cc:49:3: note: in expansion of macro 'REP'
   49 |   REP(i, 5000) {
      |   ^~~
a.cc:49:7: error: 'i' was not declared in this scope
   49 |   REP(i, 5000) {
      |       ^
a.cc:13:34: note: in definition of macro 'REP'
   13 | #define REP(i, n) for (ll i = 0; i < (n); ++i)
      |                                  ^
a.cc:48:40: warning: control reaches end of non-void function [-Wreturn-type]
   48 |   ll g_max = n/a[best]*(b[best]-a[best])
      |                                        ^