Generalizing demand for perfect substitutes utility function
Let $b_i := \frac{a_i}{p_i}$, now choose the good that maximize $b_i$, let it be $b_j$ i.e. the $j$th good. Optimal choice is $x_j=m/p_j$, and $x_i = 0$ for $i\neq j$.