Why does it give wrong ans although output is correct

#include
#include
using namespace std;

//Function to find maximum of two integers
int max(int x,int y)
{
return (x>y)?x:y;
}

int maxpathsum(int a[],int b[],int m,int n)
{
//initialising indexes for arrays
int i=0,j=0;
int result=0, sum1=0, sum2=0;

while(i<m&&j<n)
{
	if(a[i]<b[j])
	sum1+=a[i++]; //i++ means we use the value of i first and then increment it

	else if(a[i]>b[j])
		sum2+=b[j++];
	
	else{
		//we have reached a common point where we need to switch
		result+=max(sum1,sum2)+a[i]; //also add the common element
		sum1=0;
		sum2=0;

		//update i and j to move to next element of each array
		i++;
		j++;
	}
}

//add remaining elements of a
while(i<m)
	sum1+=a[i++];

//add remaining elements of b
while(j<n)
	sum2+=b[j++];

//add maximum of the two sums to the result
result+=max(sum1,sum2);
return result;

}

int main() {
int t,n,m,i,a[1000],b[1000],x;
cin>>t;
for(x=1;x<=t;x++)
{
cin>>m>>n;
for(i=0;i<m;i++)
cin>>a[i];
for(i=0;i<n;i++)
cin>>b[i];
cout<<maxpathsum(a,b,n,m);

}



return 0;

}

hi @muskanrathore01_4aab76aa7dafc189
try this -->

hi @muskanrathore01_4aab76aa7dafc189
i hope its clear now??

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.