int trap(vector<int>& height) {
int n=height.size();
if(n==0)
return 0;
int left=0,right=n-1;
int leftmax=height[left],rightmax=height[right];
int s=0;
while(left<right)
{
if(leftmax<=rightmax)
{
left++;
s+=leftmax-height[left]>0?leftmax-height[left]:0;
leftmax=max(leftmax,height[left]);
}
else
{
right--;
s+=rightmax-height[right]>0?rightmax-height[right]:0;
rightmax=max(rightmax,height[right]);
}
}
return s;
}