def compute_neighborhood_value(nbd):
    """
    Computes the value of water trapped in a neighborhood.
    A neighborhood is defined as a sequence having the first and second largest
    elements being the first and last in the sequence.

    The value of the neighborhood is defined as the sum([min(first, last) - elem for elem in inner_elems])

    where the inner_elems are nbd[1:-1
    :param nbd:
    :return:
    """
    inner_elems = nbd.elements[1:-1]
    min_val = min(nbd.start_val, nbd.end_val)
    return sum([min_val - elem for elem in inner_elems])