Optimal game strategy

please tell where my code is incorrect

#include
using namespace std;

long long int optimalGame(long long int a[],long long int i,long long int j,long long int &vi,long long int &vj){
// base case
if(i==j){
return a[i];
}
if(i+1==j){
return max(a[i],a[j]);
}

// rec case

// if user takes 1st coin
vi+=a[i]+min(optimalGame(a,i+1,j-1,vi,vj),optimalGame(a,i+2,j,vi,vj));

// if user takes the last coin
vj+=a[j]+min(optimalGame(a,i,j-2,vi,vj),optimalGame(a,i+1,j-1,vi,vj));

// return max of the two choices made
return max(vi,vj);

}

int main() {
long long int n;
cin>>n;

long long int a[n];
for(long long int i=0;i<n;i++){
    cin>>a[i];
}
long long int vi=0,vj=0;
cout<<optimalGame(a,0,n-1,vi,vj);
return 0;

}

Hi @krikhi
Since you are passing vi and vj by reference that is why changes made to them in some recursive call is affecting the result in another call. So just make them as local variable in the function. And also you need not to add vi or vj to a[i]+min(calls).

Here is your corrected code :

got it ! thanks a lot