Approach right or wrong its working for only one test case

#include
using namespace std;
int main() {
int n;
cin>>n;
int *arr = new int[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int summ=0;
for(int i=1;i<n-1;i++){
if(arr[i]<=2)
{
summ+=(2-arr[i]);
}

}
cout<<summ;

return 0;

}

hey, @supratik260699i didn’t get why are you taking a[i]<=2,for every index you have to check leftmost highest of that bar and rightmost highest of that bar than your answer will be sum of minimum of this bars-a[index],

/now I did in this way but the answer is coming 2 instead of 5/

#include
using namespace std;
int main() {
int n,sum=0,maxl,maxr;
cin>>n;
int *arr = new int[n];
int *arr1 = new int[n];
int *arr3 = new int[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
maxl = arr[0];
for(int i=1,j=0;i<n,j<n;i++,j++)
{
if(arr[i]>maxl)
{
maxl=arr[i];
arr1[j] = maxl;

	}
}
maxr=arr[n-1];
for(int i=n-2,j=n-1;i>=0,j>=0;i--,j--)
{
	if(arr[i]>maxr)
	{
		maxr=arr[i];
		arr3[j] = maxr;
	}
}
for(int j=0;j<n;j++)
{
	if(arr1[j]<arr3[j])
	sum = sum + arr1[j];
	else
	sum = sum + arr3[j];
}
cout<<sum;