My code is not submitting even though it passed the test case

//MY CODE
#include
#include
using namespace std;
int main() {
int ar[1000000];
int t,n;
cin>>t;
while(t–)
{
cin>>n;
int cs=0,ms=0, maxi=INT_MIN;;
for(int i=0;i<n;i++)
{
cin>>ar[i];
}

	for(int i=0;i<n;i++)
	{
		if(ar[i]>maxi)
		{
          maxi=i;
		}
	}

	if(maxi==0)
	{
		for(int i=0;i<n;i++)
		{
			cs=cs+ar[i];
			if(cs<0)
			{
				cs=0;
			}
			 ms=max(cs,ms);
		}
	}

	else
	{
		for(int i=maxi;i<n;i++)
		{
            cs=cs+ar[i];
			if(cs<0)
			{
				cs=0;
			}
          ms=max(cs,ms);
			
		}
      for(int i=0;i<maxi;i++)
	  {
		  cs=cs+ar[i];
		  
			  if(cs<0)
			  {
				  cs=0;
			  }
			  ms=max(cs,ms);
		  
	  }
	}

    cout<<ms<<"\n";
}
return 0;

}

your approach for solving this question is little bit wrong all you need to do is use kadane’s algorithm on the array and the inverse of the array.
Test case for which your code gives wrong answer
1
8
-1 40 -14 7 6 5 -4 -1
Correct Output: 52 (7 6 5 -4 -1 -1 40), Your output: 40

Study more about the correct method here