6. Given $n(n \geqslant 3)$ real numbers $a_{1}, a_{2}, \cdots, a_{n}$ satisfying $a_{1}=1, a_{n}=1,\left|a_{i+1}-a_{i}\right| \leqslant 2(1 \leqslant i \leqslant n-1)$.
If $a_{1}+a_{2}+\cdots+a_{n}=2016$, then the minimum value of the positive integer $n$ is ( ).
(A) 61
(B) 62
(C) 63
(D) 64
6. D.

First fix $n$. Let $S_{n}=a_{1}+a_{2}+\cdots+a_{n}$. When $n=2 k$, the maximum value of $S_{n}$ is
$$
1+3+\cdots+(2 k-1)+(2 k-1)+\cdots+3+1=2 k^{2} \text {; }
$$

When $n=2 k+1$, the maximum value of $S_{n}$ is
$$
\begin{array}{l}
1+3+\cdots+(2 k-1)+(2 k+1)+ \\
(2 k-1)+\cdots+3+1 \\
=2 k^{2}+2 k+1 .
\end{array}
$$

Notice that,
$$
\begin{array}{l}
2016=2 \times 31^{2}+94, \\
2016=2 \times 31^{2}+2 \times 31+1+31 .
\end{array}
$$

If it is equation (1), two more terms need to be added, so there are at least 64 terms; if it is equation (2), add one more 31 after 31, so there are also at least 64 terms.
In summary, the minimum value of $n$ is 64.