public int volumnwithwater(int[] A) {
        int[] Av = new int[A.length];
    	int volumn = 0;
        int lmax = 0;
        while (lmax < A.length) {
            int p = lmax;
            while (p < A.length && A[p] <= A[lmax])
                p += 1;
            if (p < A.length) {
                volumn += (p-lmax)*A[lmax];
                lmax = p; // update lmax
            } else { // reach the end, doesnt trap any water
                break;
            }
        }
        // lmax is now at the maximum of the bin
        //System.out.println(lmax);
        //System.out.println("l"+volumn);
        int b = A.length-1;
        while (b >= lmax) {
            int p = b;
            while (p >= lmax && A[p] <= A[b]) {
                p -= 1;
            }
            if (p >=lmax) {
                volumn += (b-p)*A[b];
                b = p;
            }
            if (p < lmax){
            	//System.out.println(b+"!"+p);
            	volumn += (b-p)*A[b];
                break;
            }
        }
        //System.out.println("r"+volumn);
        //volumn += A[lmax];
        return volumn;
    }