12. 已知等差数列 $\left\{a_{n}\right\}$ 中, 记 $S_{n}$ 是它的前 $n$ 项和, 若 $S_{2}=16, S_{4}=24$, 求数列 $\left\{\left|a_{n}\right|\right\}$ 的前 $n$ 项和 $T_{n}$.
由 $S_{2}=16, S_{4}=24$, 得 $\left\{\begin{array}{l}2 a_{1}+\frac{2 \times 1}{2} d=16, \\ 4 a_{1}+\frac{4 \times 3}{2} d=24 \text {. }\end{array}\right.$

即 $\left\{\begin{array}{l}2 a_{1}+d=16, \\ 2 a_{1}+3 d=12 .\end{array}\right.$ 解得 $\left\{\begin{array}{l}a_{1}=9, \\ d=-2 .\end{array}\right.$

所以等差数列 $\left\{a_{n}\right\}$ 的通项公式为 $a_{n}=11-2 n\left(n \in \mathbf{N}^{*}\right)$.

(1) 当 $n \leqslant 5$ 时, $T_{n}=\left|a_{1}\right|+\left|a_{2}\right|+\cdots+\left|a_{n}\right|=a_{1}+a_{2}+\cdots+a_{n}=S_{n}=-n^{2}+10 n$.

(2) 当 $n \geqslant 6$ 时, $T_{n}=\left|a_{1}\right|+\left|a_{2}\right|+\cdots+\left|a_{n}\right|=a_{1}+a_{2}+\cdots+a_{5}-a_{6}-a_{7}-\cdots-a_{n}=2 S_{5}-S_{n}$ $=2 \times\left(-5^{2}+10 \times 5\right)-\left(-n^{2}+10 n\right)=n^{2}-10 n+50$,
故 $T_{n}= \begin{cases}-n^{2}+10 n & (n \leqslant 5), \\ n^{2}-10 n+50 & (n \geqslant 6) .\end{cases}$