class Solution {
public:
    /**
     * max water
     * @param arr int整型vector the array
     * @return long长整型
     */
    long long maxWater(vector<int>& arr) {
        // write code here
        long long res = 0;
        int left=0, right = arr.size()-1;
        int left_h = arr[left], right_h = arr[right];
        
        while(left < right){
            if(left_h <= right_h){
                if(left_h < arr[left]){
                    left_h = arr[left];
                }
                else{
                    res += left_h - arr[left];
                    left++;
                }
            }
            else{
                if(right_h < arr[right]){
                    right_h = arr[right];
                }
                else{
                    res += right_h - arr[right];
                    right--;
                }
            }
        }
        return res;
    }
};