```package main

/**
 * max water
 * @param arr int整型一维数组 the array
 * @return long长整型
*/
func maxWater( arr []int ) int64 {
    // write code here
    n := len(arr)
    if n == 0 {return 0}
    leftmax := make([]int, n)
    leftmax[0] = arr[0]
    for i := 1; i < n; i++ {
        leftmax[i] = max(leftmax[i-1], arr[i])
    }
    
    rightmax := make([]int, n)
    rightmax[n-1] = arr[n-1]
    for i := n-2; i >= 0; i-- {
        rightmax[i] = max(rightmax[i+1], arr[i])
    }
    ans := 0
    for i, h := range arr {
        ans += min(leftmax[i], rightmax[i]) - h
    }
    return int64(ans)
}
func min(a, b int) int{
    if a < b  {
        return a
    }
    return b
}
func max(a, b int) int {
    if a < b {
        return b
    }
    return a
}