#
# max water
# @param arr int整型一维数组 the array
# @return long长整型
#
class Solution:
    def maxWater(self , arr ):
        # write code here
        ans = 0
        left, right = 0, len(arr) - 1
        leftMax = rightMax = 0

        while left < right:
            leftMax = max(leftMax, arr[left])
            rightMax = max(rightMax, arr[right])
            if arr[left] < arr[right]:
                ans += leftMax - arr[left]
                left += 1
            else:
                ans += rightMax - arr[right]
                right -= 1
        
        return ans