Language: C++

#include <bits/stdc++.h>

using namespace std;

int n;
int a[3][3];
long long ans;
const int B = 10;
map<vector<long long>, int> mp;
void solve(long long acr, long long g, long long s, long long b, int t, int d) {
  vector<long long> sst = {acr, g, s, b, t};
  if (mp[sst] == 1) {
    return;
  }
  mp[sst] = 1;
  if (t == 3) {
    ans = max(ans, acr + 1ll * g * a[0][0] + 1ll * s * a[0][1] + 1ll * b * a[0][2]);
    return;
  }
  if (t == 0) {
    if (acr >= a[0][0]) {
      for (long long cnt = max(acr / a[0][0] - B, 1); cnt <= acr / a[0][0]; cnt++) {
        solve(acr - a[0][0] * cnt, g + cnt, s, b, t, d + 1);
      }
    }
    if (acr >= a[0][1]) {
      for (long long cnt = max(acr / a[0][1] - B, 1); cnt <= acr / a[0][1]; cnt++) {
        solve(acr - a[0][1] * cnt, g, s + cnt, b, t, d + 1);
      }
    }
    if (acr >= a[0][2]) {
      for (long long cnt = max(acr / a[0][2] - B, 1); cnt <= acr / a[0][2]; cnt++) {
        solve(acr - a[0][2] * cnt, g, s, b + cnt, t, d + 1);
      }
    }
  } else if (t == 1) {
    if (g != 0) solve(acr + a[1][0] * g, 0, s, b, t, d + 1);
    if (s != 0) solve(acr + a[1][1] * s, g, 0, b, t, d + 1);
    if (b != 0) solve(acr + a[1][2] * b, g, s, 0, t, d + 1);
  } else if (t == 2) {
    if (acr >= a[1][0]) {
      for (long long cnt = max(acr / a[1][0] - B, 1); cnt <= acr / a[1][0]; cnt++) {
        solve(acr - a[1][0] * cnt, g + cnt, s, b, t, d + 1);
      }
    }
    if (acr >= a[1][1]) {
      for (long long cnt = max(acr / a[1][1] - B, 1); cnt <= acr / a[1][1]; cnt++) {
        solve(acr - a[1][1] * cnt, g, s + cnt, b, t, d + 1);
      }
    }
    if (acr >= a[1][2]) {
      for (long long cnt = max(acr / a[1][2] - B, 1); cnt <= acr / a[1][2]; cnt++) {
        solve(acr - a[1][2] * cnt, g, s, b + cnt, t, d + 1);
      }
    }
  }
  solve(acr, g, s, b, t + 1, d + 1);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> a[i][j];
    }
  }
  ans = n;
  solve(n, 0, 0, 0, 0, 0);
  cout << ans << "\n";
  return 0;
}

Compiler output:
a.cc: In function 'void solve(long long int, long long int, long long int, long long int, int, int)':
a.cc:22:31: error: no matching function for call to 'max(long long int, int)'
   22 |       for (long long cnt = max(acr / a[0][0] - B, 1); cnt <= acr / a[0][0]; cnt++) {
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:22:31: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   22 |       for (long long cnt = max(acr / a[0][0] - B, 1); cnt <= acr / a[0][0]; cnt++) {
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:22:31: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   22 |       for (long long cnt = max(acr / a[0][0] - B, 1); cnt <= acr / a[0][0]; cnt++) {
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~
a.cc:27:31: error: no matching function for call to 'max(long long int, int)'
   27 |       for (long long cnt = max(acr / a[0][1] - B, 1); cnt <= acr / a[0][1]; cnt++) {
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:27:31: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   27 |       for (long long cnt = max(acr / a[0][1] - B, 1); cnt <= acr / a[0][1]; cnt++) {
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:27:31: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   27 |       for (long long cnt = max(acr / a[0][1] - B, 1); cnt <= acr / a[0][1]; cnt++) {
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~
a.cc:32:31: error: no matching function for call to 'max(long long int, int)'
   32 |       for (long long cnt = max(acr / a[0][2] - B, 1); cnt <= acr / a[0][2]; cnt++) {
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:32:31: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   32 |       for (long long cnt = max(acr / a[0][2] - B, 1); cnt <= acr / a[0][2]; cnt++) {
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:32:31: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   32 |       for (long long cnt = max(acr / a[0][2] - B, 1); cnt <= acr / a[0][2]; cnt++) {
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~
a.cc:42:31: error: no matching function for call to 'max(long long int, int)'
   42 |       for (long long cnt = max(acr / a[1][0] - B, 1); cnt <= acr / a[1][0]; cnt++) {
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:42:31: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   42 |       for (long long cnt = max(acr / a[1][0] - B, 1); cnt <= acr / a[1][0]; cnt++) {
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:42:31: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   42 |       for (long long cnt = max(acr / a[1][0] - B, 1); cnt <= acr / a[1][0]; cnt++) {
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~
a.cc:47:31: error: no matching function for call to 'max(long long int, int)'
   47 |       for (long long cnt = max(acr / a[1][1] - B, 1); cnt <= acr / a[1][1]; cnt++) {
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:47:31: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   47 |       for (long long cnt = max(acr / a[1][1] - B, 1); cnt <= acr / a[1][1]; cnt++) {
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:47:31: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   47 |       for (long long cnt = max(acr / a[1][1] - B, 1); cnt <= acr / a[1][1]; cnt++) {
      |