Maximum subarray sum in array challenge part

#include
using namespace std;
int maxSum(int arr[],int n)
{
int maxsum;
maxsum=0;
for(int i=0;i<n;i++)
{
maxsum=maxsum+arr[i];
if(maxsum<0)
maxsum=0;
}
return maxsum;
}
int main() {

int t,n,arr[1000000];
cin>>t;
do
{
  cin>>n;
  for(int i=0;i<n;i++)
  cin>>arr[i];
 cout<<maxSum(arr,n)<<endl;
  t--;
}while(t>0);

return 0;

}

above is my code. it is working fine with inputs but failing in test cases. whats wrong int his code;

int max=0;
int val=0;
for(int i=0;i<n;i++)
{
val=val+arr[i];
if(val<0)
{
val=0;
}
else if(val>max)
{
max=val;
}
}
cout<<max<<endl;

you have to include one more condition like provided in my code