Solution (source code)

= Solution
{c}

Choose versions
$$
p_k=\mathbb E[\mathbf1_{B_k}\mid\mathcal F_{k-1}]\in[0,1],\qquad A_0=0,\qquad A_n=\sum_{k=1}^np_k.
$$
The <stochastic process> $(A_n)$ is increasing, and its $k$th increment is $\mathcal F_{k-1}$-<measurable>, so it is <predictable>. The <Doob decomposition theorem> therefore gives
$$
\boxed{X_n=M_n+A_n,\qquad M_0=0,\qquad M_n=\sum_{k=1}^n(\mathbf1_{B_k}-p_k).}
$$
To check the <martingale> property directly, each summand is <integrable> and has zero <conditional expectation> given $\mathcal F_{k-1}$. The sums are an <adapted process> for the given <filtration>. Thus $\mathbb E[M_n\mid\mathcal F_{n-1}]=M_{n-1}$. Its <bounded increments> satisfy $|M_n-M_{n-1}|\leq1$.