//采用双指针加贪心算法,假设左右两边为两个峰值,
//int hight = Math.min(arr[left], arr[right]);先取出两边的较小值,从较小的一边往中间移动
//如果小于较小峰值,则差值为该格收集的雨水,大于峰值,则取该点为新的峰值,继续向中间移动
import java.util.*;


public class Solution {
/**
 * max water
 * @param arr int整型一维数组 the array
 * @return long长整型
 */
public long maxWater (int[] arr) {
    // write code here
    if (arr.length < 2) {
        return 0;
    }
    Long res = 0L;
    int left = 0;
    int right = arr.length - 1;
    return water(arr, left, right, res);

}

public long  water(int[] arr, int left, int right, long res) {
    if (left >= right) {
        return res;
    }
    int hight = Math.min(arr[left], arr[right]);
    if (arr[left] < arr[right]) {

        while (left < right) {
            left++;
            if (arr[left] > hight) {
                return water(arr, left, right, res);
            } else {
                res += hight - arr[left];
            }
        }
    } else {
        while (left < right) {
            right--;
            if (arr[right] > hight) {
                return water(arr, left, right, res);
            } else {
                res += hight - arr[right];
            }

        }
    }

    return   res    ;
}

}