9. (16 points) Given a positive integer $n$, non-negative integers $a_{1}, a_{2}, \cdots, a_{n}$ satisfy that for $i(i=1,2, \cdots, n)$, $a_{i}+f(i)<n$, where $f(i)$ denotes the number of positive numbers in $a_{i+1}, a_{i+2}, \cdots, a_{n}$ (with the convention that $f(n)=0$). Try to find the maximum value of $\sum_{i=1}^{n} a_{i}$.
Second, $a_{i}=i-1(1 \leqslant i \leqslant n)$ satisfies
$$
f(i)=n-i(1 \leqslant i \leqslant n) .
$$

Therefore, it meets the requirements. At this point,
$$
\sum_{i=1}^{n} a_{i}=\frac{n(n-1)}{2} \text {. }
$$

Next, assume $a_{1}, a_{2}, \cdots, a_{n}$ satisfy the problem's requirements, and $a_{i_{1}}, a_{i_{2}}, \cdots, a_{i_{k}}=0\left(1 \leqslant i_{1}<i_{2}<\cdots<i_{k} \leqslant n\right.$, $k \leqslant n)$, with the rest of the $a_{i}$ not being 0. Then, define $i_{0}=0$, $i_{k+1}=n+1$, we have
$$
\begin{array}{l}
\sum_{i=1}^{n} a_{i}=\sum_{l=1}^{k+1} \sum_{j=i_{l-1}+1}^{i_{l}-1} a_{j} \\
\quad \leqslant \sum_{l=1}^{k+1} \sum_{j=i_{l-1}+1}^{i_{l}-1}(n-1-f(j)) \\
\quad=\sum_{l=1}^{k+1} \sum_{j=i_{l}-1}^{i_{l}-1}\{n-1-[(n-j)-(k+1-l)]\} \\
\quad=\sum_{l=1}^{k+1} \sum_{j=i_{l-1}-1}^{i_{l}-1}[(j-1)+(k+1-l)] \\
=\sum_{i=1}^{n}(i-1)-\sum_{l=1}^{k}\left(i_{l}-1\right)+ \\
\sum_{i=1}^{k+1}(k+1-l)\left(i_{l}-i_{l-1}-1\right) \\
=\frac{n(n-1)}{2}-\sum_{l=1}^{k} i_{l}+k-\sum_{l=1}^{k} i_{l}-\frac{k(k+1)}{2} \\
\leqslant \frac{n(n-1)}{2} .
\end{array}
$$

The above conclusion also holds for $k=0$.
Thus, the maximum value sought is $\frac{n(n-1)}{2}$.