public int trap(int[] A) {
    	if(A.length<3) return 0;
    	int water = 0; 
    	int maxLeft[] = new int[A.length];
    	maxLeft[0] = 0; 
    	
    	int maxRight[] = new int[A.length];
    	maxRight[A.length-1] = 0;
    	
    	for(int i=1; i<A.length; i++)
    	{
    		if(maxLeft[i-1] < A[i-1])
    		{
    			maxLeft[i] = A[i-1];
    		}else
    		{
    			maxLeft[i] = maxLeft[i-1];
    		}
    	}
    	
    	for(int i=A.length-2; i>=0; i--)
    	{
    		if(maxRight[i+1] < A[i+1])
    		{
    			maxRight[i] = A[i+1];
    		}else
    		{
    			maxRight[i] = maxRight[i+1];
    		}
    	}
    	
    	for(int i=1; i<A.length; i++)
    	{
    		
    		if(Math.min(maxLeft[i], maxRight[i]) > A[i])
    			water += Math.min(maxLeft[i], maxRight[i]) - A[i];
    	}

        return water;
    }