Language: C++

#include<bits/stdc++.h>
#define ll long long 
#define pb push_back
#define mod 1000000007
#define all(v) v.begin(),v.end()
#define MP make_pair
#define F first
#define S second
#define sz(v) (ll)v.size()
#define inf 100000000000000
#define vll vector<ll>
using namespace std;
const int N=50005;
ll dp[N],dp2[N];
ll a[N],b[N];
ll g(ll n)
{
	if(n<0)
		return -inf;
	if(n==0)
		return 0;
	ll &ans=dp2[n];
	ll i;
	if(ans!=-1)
		return ans;
	ans=n;
	for(i=0;i<3;i++)
		ans=max(ans,g(n-b[i])+a[i]);
	return  ans;
 
}
ll f(ll n)
{
	if(n<0)
		return -inf;
	if(n==0)
		return 0;
	ll &ans=dp[n];
	if(ans!=-1)
		return ans;
	ans=n;
	ans=max(ans,g(n));
	ll i;
	for(i=0;i<3;i++)
	{
		ans=max(ans,f(n-a[i])+b[i]);
		ans=max(ans,g(f(n-a[i])+b[i]));
	}	
	return ans;
}
void solve()
{
	ll n;
	cin>>n;
	cin>>a[0]>>a[1]>>a[2];
	cin>>b[0]>>b[1]>>b[2];
	memset(dp,-1LL,sizeof dp);
	memset(dp2,-1LL,sizeof dp2);
	cout<<f(n);
 
}
 
int main() {
    
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
    ll t=1;
    //cin>>t;
    while(t--)
        solve();
 
    
    return 0;
}
#include<bits/stdc++.h>
#define ll long long 
#define pb push_back
#define mod 1000000007
#define all(v) v.begin(),v.end()
#define MP make_pair
#define F first
#define S second
#define sz(v) (ll)v.size()
#define inf 100000000000000
#define vll vector<ll>
using namespace std;
const int N=5005;
ll dp[N],dp2[N];
ll a[N],b[N];
ll g(ll n)
{
	if(n<0)
		return -inf;
	if(n==0)
		return 0;
	ll &ans=dp2[n];
	ll i;
	if(ans!=-1)
		return ans;
	ans=n;
	for(i=0;i<3;i++)
		ans=max(ans,g(n-b[i])+a[i]);
	return  ans;

}
ll f(ll n)
{
	if(n<0)
		return -inf;
	if(n==0)
		return 0;
	ll &ans=dp[n];
	if(ans!=-1)
		return ans;
	ans=n;
	ans=max(ans,g(n));
	ll i;
	for(i=0;i<3;i++)
	{
		ans=max(ans,f(n-a[i])+b[i]);
		ans=max(ans,g(f(n-a[i])+b[i]));
	}	
	return ans;
}
void solve()
{
	ll n;
	cin>>n;
	cin>>a[0]>>a[1]>>a[2];
	cin>>b[0]>>b[1]>>b[2];
	memset(dp,-1LL,sizeof dp);
	memset(dp2,-1LL,sizeof dp2);
	cout<<f(n);

}
 
int main() {
    
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
    ll t=1;
    //cin>>t;
    while(t--)
        solve();
 
    
    return 0;
}


Compiler output:
a.cc:89:11: error: redefinition of 'const int N'
   89 | const int N=5005;
      |           ^
a.cc:13:11: note: 'const int N' previously defined here
   13 | const int N=50005;
      |           ^
a.cc:90:4: error: redefinition of 'long long int dp [50005]'
   90 | ll dp[N],dp2[N];
      |    ^~
a.cc:14:4: note: 'long long int dp [50005]' previously declared here
   14 | ll dp[N],dp2[N];
      |    ^~
a.cc:90:10: error: redefinition of 'long long int dp2 [50005]'
   90 | ll dp[N],dp2[N];
      |          ^~~
a.cc:14:10: note: 'long long int dp2 [50005]' previously declared here
   14 | ll dp[N],dp2[N];
      |          ^~~
a.cc:91:4: error: redefinition of 'long long int a [50005]'
   91 | ll a[N],b[N];
      |    ^
a.cc:15:4: note: 'long long int a [50005]' previously declared here
   15 | ll a[N],b[N];
      |    ^
a.cc:91:9: error: redefinition of 'long long int b [50005]'
   91 | ll a[N],b[N];
      |         ^
a.cc:15:9: note: 'long long int b [50005]' previously declared here
   15 | ll a[N],b[N];
      |         ^
a.cc:92:4: error: redefinition of 'long long int g(long long int)'
   92 | ll g(ll n)
      |    ^
a.cc:16:4: note: 'long long int g(long long int)' previously defined here
   16 | ll g(ll n)
      |    ^
a.cc:108:4: error: redefinition of 'long long int f(long long int)'
  108 | ll f(ll n)
      |    ^
a.cc:32:4: note: 'long long int f(long long int)' previously defined here
   32 | ll f(ll n)
      |    ^
a.cc:127:6: error: redefinition of 'void solve()'
  127 | void solve()
      |      ^~~~~
a.cc:51:6: note: 'void solve()' previously defined here
   51 | void solve()
      |      ^~~~~
a.cc:139:5: error: redefinition of 'int main()'
  139 | int main() {
      |     ^~~~
a.cc:63:5: note: 'int main()' previously defined here
   63 | int main() {
      |     ^~~~