Approximating a discrete distribution
The case where $\Pr(X=1)=\Pr(X=2)=\tfrac12$ is the subset sum problem, which is NP-complete so you won't find an efficient algorithm in general. Dynamic programming is probably the way to go.