Not passing all test cases

#include
#include
using namespace std;

int main()
{
long long n=0;
cin>>n;
int a[1000000]={0};
int maxleft[1000000]={0};

int max1=0;

for(int i=0;i<n;i++)
{
	cin>>a[i];
	max1=max(a[i],max1);
	maxleft[i]=max1;
}
max1=0;
int maxright[1000000]={0};

for(int i=n-1;i>=0;i--)
{
	max1=max(a[i],max1);
	maxright[i]=max1;
}
int watersum=0;
for(int i=0;i<n;i++)
{
	watersum+=(min(maxleft[i],maxright[i])) - a[i];
}
cout<<watersum;

}

hello @anasmir24
your logic is correct.
change datatype of water sum to long long and submit again

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.