5. Given $f(x)=\sum_{i=1}^{2009}|x+i|+\sum_{i=1}^{2009}|x-i|$ $(x \in \mathbf{R})$, and $f\left(a^{2}-3 a+2\right)=f(a-1)$. Then the number of values of $a$ is ( ).
(A) 2
$(B) 3$
(C) 4
(D) infinitely many
5.D.

From the problem, we know that $f(x)$ is an even function.
Considering when $-1 \leqslant x \leqslant 1$, it always holds that
$$
\begin{array}{l}
f(x)=2(1+2+\cdots+2009) \\
=2010 \times 2009 .
\end{array}
$$

Therefore, when $-1 \leqslant a^{2}-3 a+2 \leqslant 1$ and $-1 \leqslant a-1 \leqslant 1$, it always holds that
$$
f\left(a^{2}-3 a+2\right)=f(a-1) .
$$

Since the solution set of the inequality $-1 \leqslant a^{2}-3 a+2 \leqslant 1$ is $\frac{3-\sqrt{5}}{2} \leqslant a \leqslant \frac{3+\sqrt{5}}{2}$, and the solution set of the inequality $-1 \leqslant a-1 \leqslant 1$ is $0 \leqslant a \leqslant 2$, then when $\frac{3-\sqrt{5}}{2} \leqslant a \leqslant 2$, it always holds that $f\left(a^{2}-3 a+2\right)=f(a-1)$.