What is to be included to get the right answer?

#include<bits/stdc++.h>
using namespace std;
int small(int a,int b)
{
if(a<b)
return a;
else
return b;
}
int main()
{
//Taking input.
int a[1000000],n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
//Approach for calculation
int sum_water=0;

for(int i=0;i<n;i++)
{
	int lmax=*max_element(a,a+i);//This is maximum element to the left side of the element a[i];
	int rmax=*max_element(&a[i],a+n);//This is maximum element to the right side of the element a[i];

	int small_height=small(lmax,rmax);//This is small of the two maximum element 
	
	if((small_height-a[i])>=0)
	sum_water+=(small_height-a[i]);// This is the total sum summing up.
} 
cout<<sum_water<<endl;


return 0;

}

Hello @coala008 please share your code by saving it on ide.codingblocks.com .

Hello @coala008 as you have used the inbuilt stl thats why your code is taking more time you need to apply your proper logic.
Here for your reference i am attaching the code:


if you have any doubt you can ask here:
Happy Learning!!

Sir, Can we interact in collabarative mode?

Actually, COding block IdE is not compiling !!

@coala008 yes sure we can .

@coala008 no coding blocks ide is compiling the code:
Happy Learning!!