import java.util.*;


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

        return val;
    }
}