Optimal game strategy recursion , why is this codde wrong

Scanner sc=new Scanner(System.in);
int N=sc.nextInt();

	ArrayList<Integer> arr= new ArrayList<>();

	for(int i=0 ; i<N ; i++)
	{
		int a=sc.nextInt();
		arr.add(a);
	}

	int ans=ogs(arr,0,arr.size()-1 , arr.size());

	System.out.println(ans);
	
}


static int ogs(ArrayList<Integer> arr , int s , int e , int t)
{

	if(t==1) return 0  /*arr.get(s)*/;
	
	
	int piyush=0,p=0,n=0;
	
	
	
	
			if(arr.get(s)>arr.get(e))
			{		
				if(arr.get(e)>arr.get(e-1))
				{
					if(arr.get(s+1)>arr.get(s))
					{
						piyush+=arr.get(e);
						arr.remove(e);
					}
					else
					{
						piyush+=arr.get(s);
						arr.remove(s);
					}
				}
				else
				{
					if(arr.get(s+1)>arr.get(s))
					{
						piyush+=smaller(  arr.get(s+1)-arr.get(s)  ,   arr.get(e-1)-arr.get(e)  );
						arr.remove(  smaller(  arr.get(s+1)-arr.get(s)  ,   arr.get(e-1)-arr.get(e)  )   ) ;
					}
					else
					{
						piyush+=arr.get(s);
						arr.remove(s);
					}
				}
			}
			else
			{
				if(arr.get(s+1)>arr.get(s))
				{
					if(arr.get(e)>arr.get(e-1))
					{
						piyush+=arr.get(e);
						arr.remove(e);
					}
					else
					{
						piyush+=smaller(  arr.get(s+1)-arr.get(s)  ,   arr.get(e-1)-arr.get(e)  );
						arr.remove(  smaller(  arr.get(s+1)-arr.get(s)  ,   arr.get(e-1)-arr.get(e)  )   ) ;
					}
				}
				else
				{
					if(arr.get(e)>arr.get(e-1))
					{
						piyush+=arr.get(e);
						arr.remove(e);
					}
					else
					{
						piyush+=arr.get(s);
						arr.remove(s);
					}
				}				
			}
	
	
	
		if(t%2==0)
		{
			p=piyush;
			n+=ogs(arr,s,arr.size()-1,arr.size());
			if(t>2)
			p+=n;
		}
		else
		{
			n=piyush;
			p+=ogs(arr,s,arr.size()-1,arr.size());
		}
	
	
		return p;

}


static int smaller(int m,int n)
{
	if(m>n) return n;
	else return m;

@Himanshu-Jhawar-2273952536067590,
Don’t use an arraylist. Use simple recursion.

        Consider both the possibilities. You can pick either the first or the last coin.
        Since the opponent plays optimally , we would get the minimum of the remaining coins for each choice.
        Pick the max of two as your final result

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.