Language: C++

#include "bits/stdc++.h"

#define REP(i, n, N) for(ll i=(n); i<(N); i++)
#define RREP(i, n, N) for(ll i=(N-1); i>=n; i--)
#define CK(n, a, b) ((a)<=(n)&&(n)<(b))
#define ALL(v) (v).begin(),(v).end()
#define MCP(a, b) memcpy(b,a,sizeof(b))
#define p(s) cout<<(s)<<endl
#define p2(a, b) cout<<(a)<<" "<<(b)<<endl
#define v2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

ll N;
ll gsb[2][3];
ll dpAB[5010], dpBA[5010];
vector<pair<double, pair<ll, ll>>> AB; //b/a, b, a
vector<pair<double, pair<ll, ll>>> BA; //a/b, a, b

int main() {
	cin >> N;
	REP(i,0,2){
		REP(j,0,3){
			cin >> gsb[i][j];

		}
	}
	REP(j,0,3){
		AB.emplace_back(gsb[1][j]/gsb[0][j],gsb[1][j],gsb[0][j]);
	}
	REP(j,0,3){
		BA.emplace_back(gsb[0][j]/gsb[1][j],gsb[0][j],gsb[1][j]);
	}
	sort(AB.begin(), AB.end());
	sort(BA.begin(), BA.end());

	//A->B;
	ll maxAB = N;
	if(AB[0].first > 1){
		ll tmpN = N;
		for(ll i=0; i<tmpN+1; i+=AB[0].second.second){
			dpAB[i] = max(dpAB[i], i*AB[0].second.first);
			tmpN -= i;
			if(AB[1].first > 1) {
				for (int j = 0; j < tmpN + 1; j += AB[1].second.second) {
					dpAB[i + j] = max(dpAB[i + j], j * AB[1].second.first);
					tmpN -= j;
					if (AB[2].first > 1) {
						ll k = tmpN / AB[2].second.second;
						dpAB[i + j + k] = max(dpAB[i + j], k * AB[2].second.first);
					}
					tmpN += j;
				}
			}
			tmpN += i;
		}
	}
	REP(i,0,N+1){
		N = max(N, dpAB[i]);
	}
	//B->A max 5000*5000 = 25,000,000
	if(BA[0].first > 1){
		ll tmpN = N;
		for(ll i=0; i<tmpN+1; i+=BA[0].second.second){
			dpBA[i] = max(dpBA[i], i*BA[0].second.first);
			tmpN -= i;
			if(BA[1].first > 1) {
				for (int j = 0; j < tmpN + 1; j += BA[1].second.second) {
					dpBA[i + j] = max(dpBA[i + j], j * BA[1].second.first);
					tmpN -= j;
					if (BA[2].first > 1) {
						ll k = tmpN / BA[2].second.second;
						dpBA[i + j + k] = max(dpBA[i + j], k * BA[2].second.first);
					}
					tmpN += j;
				}
			}
			tmpN += i;
		}
	}
	REP(i,0,N+1){
		N = max(N, dpBA[i]);
	}
	p(N);


	return 0;
}


Compiler output:
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/c++allocator.h:33,
                 from /usr/include/c++/14/bits/allocator.h:46,
                 from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/new_allocator.h: In instantiation of 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<double, std::pair<long long int, long long int> >; _Args = {long long int, long long int&, long long int&}; _Tp = std::pair<double, std::pair<long long int, long long int> >]':
/usr/include/c++/14/bits/alloc_traits.h:575:17:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<double, std::pair<long long int, long long int> >; _Args = {long long int, long long int&, long long int&}; _Tp = std::pair<double, std::pair<long long int, long long int> >; allocator_type = std::allocator<std::pair<double, std::pair<long long int, long long int> > >]'
  575 |           __a.construct(__p, std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/vector.tcc:117:30:   required from 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {long long int, long long int&, long long int&}; _Tp = std::pair<double, std::pair<long long int, long long int> >; _Alloc = std::allocator<std::pair<double, std::pair<long long int, long long int> > >; reference = std::pair<double, std::pair<long long int, long long int> >&]'
  117 |             _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  118 |                                      std::forward<_Args>(__args)...);
      |                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:31:18:   required from here
   31 |                 AB.emplace_back(gsb[1][j]/gsb[0][j],gsb[1][j],gsb[0][j]);
      |                 ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/new_allocator.h:191:11: error: no matching function for call to 'std::pair<double, std::pair<long long int, long long int> >::pair(long long int, long long int&, long long int&)'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/bits/stl_pair.h:913:28: note: candidate: 'template<class _U1, class _U2, typename std::enable_if<(std::_PCC<((! std::is_same<double, _U1>::value) || (! std::is_same<std::pair<long long int, long long int>, _U2>::value)), double, std::pair<long long int, long long int> >::_MoveConstructiblePair<_U1, _U2>() && (! std::_PCC<((! std::is_same<double, _U1>::value) || (! std::is_same<std::pair<long long int, long long int>, _U2>::value)), double, std::pair<long long int, long long int> >::_ImplicitlyMoveConvertiblePair<_U1, _U2>())), bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(std::pair<_U1, _U2>&&) [with _U2 = _U1; typename std::enable_if<(std::_PCC<((! std::is_same<_T1, _U1>::value) || (! std::is_same<_T2, _U2>::value)), _T1, _T2>::_MoveConstructiblePair<_U1, _U2>() && (! std::_PCC<((! std::is_same<_T1, _U1>::value) || (! std::is_same<_T2, _U2>::value)), _T1, _T2>::_ImplicitlyMoveConvertiblePair<_U1, _U2>())), bool>::type <anonymous> = _U2; _T1 = double; _T2 = std::pair<long long int, long long int>]'
  913 |         explicit constexpr pair(pair<_U1, _U2>&& __p)
      |                            ^~~~
/usr/include/c++/14/bits/stl_pair.h:913:28: note:   candidate expects 1 argument, 3 provided
/usr/include/c++/14/bits/stl_pair.h:902:19: note: candidate: 'template<class _U1, class _U2, typename std::enable_if<(std::_PCC<((! std::is_same<double, _U1>::value) || (! std::is_same<std::pair<long long int, long long int>, _U2>::value)), double, std::pair<long long int, long long int> >::_MoveConstructiblePair<_U1, _U2>() && std::_PCC<((! std::is_same<double, _U1>::value) || (! std::is_same<std::pair<long long int, long long int>, _U2>::value)), double, std::pair<long long int, long long int> >::_ImplicitlyMoveConvertiblePair<_U1, _U2>()), bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(std::pair<_U1, _U2>&&) [with _U2 = _U1; typename std::enable_if<(std::_PCC<((! std::is_same<_T1, _U1>::value) || (! std::is_same<_T2, _U2>::value)), _T1, _T2>::_MoveConstructiblePair<_U1, _U2>() && std::_PCC<((! std::is_same<_T1, _U1>::value) || (! std::is_same<_T2, _U2>::value)), _T1, _T2>::_ImplicitlyMoveConvertiblePair<_U1, _U2>()), bool>::type <anonymous> = _U2; _T1 = double; _T2 = std::pair<long long int, long long int>]'
  902 |         constexpr pair(pair<_U1, _U2>&& __p)
      |                   ^~~~
/usr/include/c++/14/bits/stl_pair.h:902:19: note:   candidate expects 1 argument, 3 provided
/usr/include/c++/14/bits/stl_pair.h:891:28: note: candidate: 'template<class _U1, class _U2, typename std::enable_if<(_MoveConstructiblePair<_U1, _U2>() && (! _ImplicitlyMoveConvertiblePair<_U1, _U2>())), bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(_U1&&, _U2&&) [with _U2 = _U1; typename std::enable_if<(std::_PCC<true, _T1, _T2>::_MoveConstructiblePair<_U1, _U2>() && (! std::_PCC<true, _T1, _T2>::_ImplicitlyMoveConvertiblePair<_U1, _U2>())), bool>::type <anonymous> = _U2; _T1 = double; _T2 = std::pair<long long int, long long int>]'
  891 |         explicit constexpr pair(_U1&& __x, _U2&& __y)
      |                            ^~~~
/usr/include/c++/14/bits/stl_pair.h:891:28: note:   candidate expects 2 arguments, 3 provided
/usr/include/c++/14/bits/stl_pair.h:881:19: note: candidate: 'template<class _U1, class _U2, typename std::enable_if<(_MoveConstructiblePair<_U1, _U2>() && _ImplicitlyMoveConvertiblePair<_U1, _U2>()), bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(_U1&&, _U2&&) [with _U2 = _U1; typename std::enable_if<(std::_PCC<true, _T1, _T2>::_MoveConstructiblePair<_U1, _U2>() && std::_PCC<true, _T1, _T2>::_ImplicitlyMoveConvertiblePair<_U1, _U2>()), bool>::type <anonymous> = _U2; _T1 = double; _T2 = std::pair<long long int, long long int>]'
  881 |         constexpr pair(_U1&& __x, _U2&& __y)
      |                   ^~~~
/usr/include/c++/14/bits/stl_pair.h:881:19: note:   candidate expects 2 arguments, 3 provided
/usr/include/c++/14/bits/stl_pair.h:869:9: note: candidate: 'template<class _U2, typename std::enable_if<std::__and_<std::is_pointer<double>, std::__not_<std::is_reference<_Tp> >, std::is_constructible<std::pair<long long int, long long int>, _U2>, std::__not_<std::is_constructible<std::pair<long long int, long long int>, const _U1&> >, std::__not_<std::is_convertible<_U2, std::pair<long long int, long long int> > > >::value, bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(__zero_as_null_pointer_constant, _U2&&, ...) [with typename std::enable_if<std::__and_<std::is_pointer<_Tp>, std::__not_<std::is_reference<_U1> >, std::is_constructible<_T2, _U2>, std::__not_<std::is_constructible<_T1, const _U1&> >, std::__not_<std::is_convertible<_U2, _T2> > >::value, bool>::type <anonymous> = _U2; _T1 = double; _T2 = std::pair<long long int, long long int>]'
  869 |         pair(__zero_as_null_pointer_constant, _U2&& __y, ...)
      |         ^~~~
/usr/include/c++/14/bits/stl_pair.h:869:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_pair.h:866:38: error: no type named 'type' in 'struct std::enable_if<false, bool>'
  866 |                              bool> = false>
      |                                      ^~~~~
/usr/include/c++/14/bits/stl_pair.h:856:9: note: candidate: 'template<class _U2, typename std::enable_if<std::__and_<std::is_pointer<double>, std::__not_<std::is_reference<_Tp> >, std::is_constructible<std::pair<long long int, long long int>, _U2>, std::__not_<std::is_constructible<std::pair<long long int, long long int>, const _U1&> >, std::is_convertible<_U2, std::pair<long long int, long long int> > >::value, bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(__zero_as_null_pointer_constant, _U2&&, ...) [with typename std::enable_if<std::__and_<std::is_pointer<_Tp>, std::__not_<std::is_reference<_U1> >, std::is_constructible<_T2, _U2>, std::__not_<std::is_constructible<_T1, const _U1&> >, std::is_convertible<_U2, _T2> >::value, bool>::type <anonymous> = _U2; _T1 = double; _T2 = std::pair<long long int, long long int>]'
  856 |         pair(__zero_as_null_pointer_constant, _U2&& __y, ...)
      |         ^~~~
/usr/include/c++/14/bits/stl_pair.h:856:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_pair.h:853:38: error: no type named 'type' in 'struct std::enable_if<false, bool>'
  853 |                              bool> = true>
      |                                      ^~~~
/usr/include/c++/14/bits/stl_pair.h:843:9: note: candidate: 'template<class _U1, typename std::enable_if<std::__and_<std::__not_<std::is_reference<_Tp> >, std::is_pointer<std::pair<long long int, long long int> >, std::is_constructible<double, _U1>, std::__not_<std::is_constructible<double, const _U1&> >, std::__not_<std::is_convertible<_U1, double> > >::value, bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(_U1&&, __zero_as_null_pointer_constant, ...) [with typename std::enable_if<std::__and_<std::__not_<std::is_reference<_U1> >, std::is_pointer<_T2>, std::is_constructible<_T1, _U1>, std::__not_<std::is_constructible<_T1, const _U1&> >, std::__not_<std::is_convertible<_U1, _T1> > >::value, bool>::type <anonymous> = _U1; _T1 = double; _T2 = std::pair<long long int, long long int>]'
  843 |         pair(_U1&& __x, __zero_as_null_pointer_constant, ...)
      |         ^~~~
/usr/include/c++/14/bits/stl_pair.h:843:9: note:   template argument ded