Solution (source code)

= Solution

Relabel the symbols so $p_1\geq\cdots\geq p_m$ and assign all finite binary strings in nondecreasing length order, beginning with the empty string. The $i$th string has length
$$
L(i)=\lfloor\log_2i\rfloor.
$$
Since $1\geq\sum_{j=1}^ip_j\geq ip_i$,
$$
L(i)\leq\log_2i\leq\log_2(1/p_i).
$$
Thus the <optimal one-to-one binary code> satisfies
$$
\boxed{\mathbb E[L(X)]\leq\sum_ip_i\log_2(1/p_i)=H(X)}.
$$