class Solution(object):
    def trap(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        ans=0
        n=len(height)
        def findmax(nums):
            index,temp=0,nums[0]
            for i in range(len(nums)):
                if nums[i]>temp:
                    index=i
                    temp=nums[i]
            return [index,temp]
        if n==0:
                return 0
        index,maxnum=findmax(height)
        left=height[0]
        for i in range(index):
            ans+=max(0,left-height[i])
            if height[i]>left:
                left=height[i]
        right=height[n-1]
        for i in range(n-1,index,-1):
            ans += max(0, right - height[i])
            if height[i]>right:
                right=height[i]
        return ans
height=[0,1,0,2,1,0,1,3,2,1,2,1]
s=Solution()
print(s.trap(height))
#找两侧的最大值中小的那个