My code is not working . Idk why?

#include
using namespace std;
int main() {
int t;
cin>>t;
while(t–){
int n;
cin>>n;
int a[n+1];
for(int i=0;i<n;i++){
cin>>a[i];
}
int cursum=0;
int k=0;
int max_so_far=0;
for(int i=0; ;){
//cout<<cursum<<" ";
cursum=cursum+a[i];
if(cursum>max_so_far){
max_so_far=cursum;
}
if(cursum<0)
cursum=0;

		if(i==n-1 && k==0){
			k++;
			i=0;
		}
		else{
			i++;
			if(i==(n-1) && k==1)
			{
			break;
			}
		
		}

	}
cout<<max_so_far<<endl;
}
return 0;

}

hi @riagoel3999 kindly refer
https://www.geeksforgeeks.org/maximum-contiguous-circular-sum/ and do a dry run

Ok so basically we are inverting signs and running kadane to find out the sum of eleents not being included in the maxsum?

@riagoel3999 yes thats what

But how do we know the remaining array will make it up to minimum sum?

Please do a dry run of 2 3 test cases from gfg

I have done that
But I dont know how do they arrive at this approach
Can u please help me with that?
How do they accurately know that sum of remaining elements is minimum? May be one element of the maximum sum subarray minimises the sum of remaining elements?

they are looking for 2 cases either a normal kadane will work which will tell u the subarray anywhere in between the array, but there can be another case where ans can lie like first part and last part as it is a circular array, for this u need to remove the middle part if the dont contribute to your ans (its a trick basically u will also be able to come up after practicing these types of questions…)