Solution (source code)

= Solution

The <conditional expectation> given an event of positive probability is
$$
\boxed{\mathbb E(X\mid B_i)=\sum_{j=0}^{\infty}j\,\mathbb P(X=j\mid B_i)}.
$$
Apply the <law of total probability> to each event $\{X=j\}$:
$$
\begin{aligned}
\mathbb E(X)
&=\sum_{j=0}^{\infty}j\sum_{i=1}^{k}\mathbb P(X=j\mid B_i)\mathbb P(B_i)\\
&=\sum_{i=1}^{k}\mathbb P(B_i)\sum_{j=0}^{\infty}j\,\mathbb P(X=j\mid B_i)\\
&=\boxed{\sum_{i=1}^{k}\mathbb P(B_i)\mathbb E(X\mid B_i)}.
\end{aligned}
$$
This is the <law of total expectation>. Interchanging the sums is justified by nonnegative terms, for example by <Tonelli theorem>, so it remains valid when the expectation is infinite.