public class Solution {
    /**
     * max water
     * @param arr int整型一维数组 the array
     * @return long长整型
     */
    public long maxWater (int[] arr) {
        // write code here
        if (arr == null || arr.length < 3) {
            return 0;
        }
        int left = 0;
        int right = arr.length - 1;
        int maxL = 0;
        int maxR = 0;
        int res = 0;
        while (left <= right) {
            if (maxL <= maxR) {
                if (maxL > arr[left]) {
                    res += (maxL - arr[left]);
                } else {
                    maxL = arr[left];
                }
                left++;
            } else {
                if (maxR > arr[right]) {
                    res += (maxR - arr[right]);
                } else {
                    maxR = arr[right];
                }
                right--;
            }
        }
        return res;
    }
}