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 < 1) {
            return 0;
        }
        int left = 0;
        int right = arr.length - 1;
        int maxL = 0;
        int maxR = 0;
        int res = 0;
        while (left < right) {
            maxL = Math.max(maxL, arr[left]);
            maxR = Math.max(maxR, arr[right]);
            if (maxL < maxR) {
                res += maxL - arr[left++];
            } else {
                res += maxR - arr[right--];
            }
        }
        return res;
    }
}