package org.example.test;

public class WaterTest {

    public long maxWater(int[] arr) {
        // write code here
        int maxLeft = 0;
        int maxRight = 0;
        int left = 0;
        int right = arr.length - 1;
        long ans = 0;
        while (left < right) {
            if (arr[left] < arr[right]) {
                if (arr[left] > maxLeft) {
                    maxLeft = arr[left];
                } else {
                    ans += (maxLeft - arr[left]);
                }
                ++left;
            } else {
                if (arr[right] > maxRight) {
                    maxRight = arr[right];
                } else {
                    ans += (maxRight - arr[right]);
                }
                --right;
            }
        }
        return ans;
    }
}