using namespace std;
int main() {
int n;
int a[1000]={0},l[1000]={0},r[1000]={0};
int t=0;
cin>>n;
for(int i=0; i<n; i++)
{
cin>>a[i];
}
l[0]=a[0];
r[n-1]=a[n-1];
for(int i=1; i<n; i++)
{
l[i]= max(l[i-1], a[i]);
}
for(int i=n-2; i>=0; i–)
{
r[i]=max(r[i+1], a[i]);
}
int x;
for(int i=0;i<n;i++)
{
x = min(l[i], r[i]) - a[i];
t+=x;
}
cout<<t;
return 0;
}
This code is able to pass most of the test cases but not all. Kindly tell the mistake and improvement.