For capacitors in series,the charge $Q$ remains constant.
Therefore,the total energy stored is:
$U = \frac{Q^2}{2C_{eq}} = \frac{Q^2}{2} \left[ \frac{1}{C_1} + \frac{1}{C_2} + \dots + \frac{1}{C_n} \right]$
$U = \frac{Q^2}{2C_1} + \frac{Q^2}{2C_2} + \dots + \frac{Q^2}{2C_n}$
$U = U_1 + U_2 + \dots + U_n$
For capacitors in parallel,the potential difference $V$ remains constant.
Therefore,the total energy stored is:
$U = \frac{1}{2} C_{eq} V^2 = \frac{1}{2} (C_1 + C_2 + \dots + C_n) V^2$
$U = \frac{1}{2} C_1 V^2 + \frac{1}{2} C_2 V^2 + \dots + \frac{1}{2} C_n V^2$
$U = U_1 + U_2 + \dots + U_n$
Thus,in both series and parallel combinations of capacitors,the total energy stored is the sum of the energies stored in individual capacitors.