Rain water harvesting

I have submitted 2 codes
but codes are getting run error
1
#include
using namespace std;

int main()
{
int n;
cin>>n;
int a[1000];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int s=0;

int left=0,right=0,m;
for(int i=0;i<n;i++)
{
	left=a[i];
	for(int j=0;j<i;j++)
	{
		left=max(left,a[j]);
	}
	right=a[i];
	for(int j=i+1;j<n;j++)
	{
		right = max(right,a[j]);
	}
	s=s+(min(left,right)-a[i]);
}
cout<<s;
return 0;

}

//2nd code
#include
using namespace std;

int main()
{
int n;
cin>>n;
int a[1000];
for(int i=0;i<n;i++)
{
cin>>a[i];
}int left[1000];
int right[1000];
int x=a[0];
//computing left max
for(int i=0;i<n;i++)
{
if(x<a[i])
{ x=a[i];

		}left[i]=x;	
}

//computoing right max
x=a[n-1];
for(int i=n-1;i>=0;i--)
{
    if(x<a[i])
	{    x=a[i];
	}
		right[i]=x	;
}

int m,s=0;
for(int i=0;i<n;i++)
{
	m=min(left[i],right[i])-a[i];
   	s=s+m;
   	
}	
cout<<s;


return 0;

}

where I’m wrong?

Check the constraints given in the question
Constraints
1 <= N <= 10^6
So increase the size of your arrays to 1000000 and then check.
Run Errors is most probably because of accessing illegal indexes

now also most testcases failed

done sir thanks🤩 for your help