I am not able to solve optimal game problem

i am trying and trying and trying but constantly giving wrong…
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int max=INT_MIN;
void optimal(ll a[],ll s,ll e,ll turn,ll sum)
{
if(s>e)
{
if(sum>::max)
::max=sum;
return;
}
if(turn&1)
{
optimal(a,s,e-1,turn+1,sum+a[e]);
optimal(a,s+1,e,turn+1,sum+a[s]);

  }
  else
   {
       if(a[s]>a[e])
        optimal(a,s+1,e,turn+1,sum);
       else
        optimal(a,s,e-1,turn+1,sum);
   }

}
int main()
{
ll n;
cin>>n;
ll a[n];
for(ll i=0;i<n;i++)
cin>>a[i];
optimal(a,0,n-1,1,0);
cout<<::max<<endl;

}

this is my code…
please help me kindly
as soon as possible

Hi can you please share your code using https://ide.codingblocks.com/ save your code and share it here so that i can properly go through your code

please help me as soon as possible

There is a logical error in your code you are playing optimally i.e. taking all the possible outcomes and considering the best one but according to your code your opponent is not playing optimally as it just choose the value that is greater , try to change your approach considering the fact that your opponent is also playing optimally

In case of any doubt feel free to reach out :slight_smile:

what are you saying in this way 4 1 2 3 4 for this ans should be 7 but ans is given 6

Hey @swapnoneelmajumdar
Check this out https://www.geeksforgeeks.org/optimal-strategy-for-a-game-dp-31/

If your doubt is resolved after this kindly mark it as resolved. Thanks and best of luck !

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.