Note that some of the an may be repeated, in which case B has multiple zeros at those points.
Thus, all that remains is to repeat the construction for f in place of g.