Example 2 Given $2 n$ non-negative integers $r_{1}, r_{2}, \cdots, r_{n}$ and $c_{1}, c_{2}, \cdots, c_{n}$, satisfying $\sum_{i=1}^{n} r_{i}=\sum_{i=1}^{n} c_{i}$. Fill in each small square of an $n \times n$ grid with a non-negative integer arbitrarily, so that the sum of the numbers in the $i$-th row equals $r_{i}(i=1$, $2, \cdots, n)$, and the sum of the numbers in the $j$-th column equals $c_{j}(j=1,2, \cdots, n)$. Let $S$ be the sum of the numbers in the $n$ small squares on the main diagonal (from the top left corner to the bottom right corner). Find the maximum and minimum values of $S$.
Let the number filled in the small square at the $i$-th row and $j$-th column be $x_{ij} (i, j=1,2, \cdots, n)$, then
$$
r_{i}=\sum_{j=1}^{n} x_{ij} (i=1,2, \cdots, n), c_{j}=\sum_{i=1}^{n} x_{ij} (j=1,2, \cdots, n), S=\sum_{i=1}^{n} x_{ii} \text{. }
$$

Since the set of values for $S$ is finite, the minimum and maximum values of $S$ both exist.
(1) Suppose a certain filling method makes $S$ take the minimum value, then at most one of $x_{11}, x_{22}, \cdots, x_{nn}$ is a positive integer.

In fact, if $x_{ii}$ and $x_{jj}$ are both positive integers $(i \neq j)$, then let $x'_{ii}=x_{ii}-1, x'_{ij}=x_{ij}-1, x'_{ij}=x_{ij}+1$, $x'_{ji}=x_{ji}+1$, and keep the rest of the numbers unchanged. The sum of the numbers on the main diagonal is $S'$, which still satisfies the conditions of the problem, but $S'=S-20$ and $x_{ij}=0 (j \neq i)$, then $x_{kt}=0 (k \neq i, t \neq i, k \neq t)$. In fact, if some $x_{ki} \neq 0 (k \neq i, t \neq i)$, then let $x'_{ii}=x_{ii}-1, x'_{k}=x_{kt}-1, x'_{ii}=x_{ii}+1, x'_{ki}=x_{ki}+1$, and keep the rest of the numbers unchanged. Thus, the sum of the numbers in each row and each column remains unchanged, but the sum of the numbers on the main diagonal is $S'=S-1$, which contradicts the fact that $S$ takes the minimum value. Therefore, $x_{kt}=0 (k \neq i, t \neq i, k \neq t)$, so $x_{ij}=c_{j} (j \neq i, j=1,2, \cdots, n)$, $x_{hi}=r_{k} (k \neq i, k=1,2, \cdots, n)$, thus $x_{ii}=r_{i}-\sum_{\substack{j=1 \\ j \neq i}}^{n} c_{j}=c_{i}-\sum_{\substack{k=1 \\ k \neq i}}^{n} r_{k} (i=1,2, \cdots, n)$, let
$$
m_{i}=r_{i}-\sum_{\substack{j=1 \\ j \neq i}}^{n} c_{j}=c_{i}-\sum_{\substack{k=1 \\ k \neq i}}^{n} r_{k} (i=1,2, \cdots, n), m_{0}=\max _{\substack{1 \leqslant i \leqslant n}}\left\{m_{i}\right\} \text{. Clearly, } m_{1}, m_{2}, \cdots, m_{n} \text{ contain }
$$

at most one positive number (because if $m_{i}>0, m_{j}>0 (i \neq j)$, then $r_{i}>c_{j}$ and $c_{j}>r_{i}$, which is a contradiction), hence only $x_{ii}=m_{0}$. Note that scenarios (i) and (ii) cannot occur simultaneously, so the minimum value of $S$ is
$$
S_{\text{min}}=\left\{\begin{array}{l}
m_{0} (m_{0}>0), \\
0 (m_{0} \leqslant 0)
\end{array}\right.
$$

where $\left.\left.m_{0}=\max _{1 \leqslant i \leqslant n}\left\{r_{i}-\sum_{\substack{j=1 \\ j \neq i}}^{n} c_{j}\right\}=\max _{1 \leqslant i \leqslant n}\right\} c_{i}-\sum_{\substack{k=1 \\ k \neq i}}^{n} r_{k}\right\}$.
(2) Suppose a certain filling method makes $S$ take the maximum value.
If there exists a number $x_{ij}>0 (j \neq i)$ in the same row as $x_{ii}$ and a number $x_{ki}>0 (k \neq i)$ in the same column, then let $x'_{ii}=x_{ii}+1, x'_{ij}=x_{ij}-1, x'_{ki}=x_{ki}-1, x'_{kj}=x_{kj}+1$, and keep the rest of the numbers unchanged. Thus, the sum of the numbers in each row and each column remains unchanged, but the sum of the numbers on the main diagonal is $S'=\left\{\begin{array}{l}S+1 (k \neq j) \\ S+2 (k=j)\end{array}\right.$, always $S'>S$, which contradicts the fact that $S$ takes the maximum value. Therefore, when $S$ takes the maximum value, either all the numbers in the $i$-th row except $x_{ii}$ are zero or all the numbers in the $i$-th column except $x_{ii}$ are zero, so $x_{ii}=\sum_{j=1}^{n} x_{ij}=r_{i} \leqslant \sum_{k=1}^{n} x_{ki}=c_{i}$ or $x_{ii}=\sum_{k=1}^{n} x_{ki}=c_{i} \leqslant \sum_{j=1}^{n} x_{ij}=r_{i}$, i.e., $x_{ii}=\min \left\{r_{i}, c_{i}\right\}$ $(i=1,2, \cdots, n)$, so the maximum value of $S$ is
$$
S_{\text{max}}=\sum_{i=1}^{n} \min \left\{r_{i}, c_{i}\right\} .
$$