def bs_cost(bw, bf, nodes=1, depth=1, max_depth=16, total=1):
    """
    Estimate the number of leg evaluations performed in a Beam Search,
    with a given beam width (`bw`) and branch factor (`bf`), to
    reach a score of `max_depth`.
    """
    if depth == max_depth:
        return total
    n = nodes * bf
    total += n
    n = min(n, bw)
    return bs_cost(bw, bf, n, depth + 1, max_depth, total)