public int trap(int[] A) {
        int len = A.length;
        int [] maxL = new int[len];
        int [] maxR = new int[len];

        int max = 0;
        for(int i = 0; i < len; ++i){
            maxL[i] = max;
            if(max < A[i]) max = A[i];
        }
        max = 0;
        for(int i = len - 1; i >= 0; --i){
            maxR[i] = max;
            if(max < A[i]) max = A[i];
        }

        int res = 0;
        for(int i = 0; i < len; ++i){
            int tmp = Math.min(maxL[i], maxR[i]);
            res += A[i] < tmp ? tmp - A[i] : 0;
        }

        return res;
    }