Problem 5. (6 points) In each cell of a table with 2005 rows and 2006 columns, one of the numbers 0, 1, or 2 is written. It is known that the sum of the numbers in each row and each column is divisible by 3. What is the maximum number of ones that can be in the table?
Solution. Let there be $n$ zeros and $d$ twos in the table. For the sum of the numbers in each row to be divisible by 3, there must be either at least one two or two zeros in each row. Therefore,

$$
d+\frac{n}{2} \geq 2005
$$

Similarly, in each column, there must be either at least two twos or one zero, from which we get

$$
n+\frac{d}{2} \geq 2006
$$

By adding the above inequalities, we obtain

$$
d+n \geq 2674
$$

Therefore, the number of ones is at most $2005 \times 2006 - 2674 = 4022030$.

We construct an example that achieves this bound as follows: we write 1338 zeros in pairs in the table, starting from the top left corner (in 669 rows and 1338 columns) and 1336 twos in pairs in columns, starting from the bottom right corner (in 1336 rows and 668 columns), and fill the remaining cells with ones. Since $669 + 1336 = 2005$ and $1338 + 668 = 2006$, there are zeros and twos in every row and column, and each sum is divisible by 3.

| 0 | 0 | 1 | 1 | $\ldots$ | 1 | 1 | 1 | $\ldots$ | $\ldots$ | 1 |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| 1 | 1 | 0 | 0 | $\ldots$ | 1 | 1 | 1 | $\ldots$ | $\ldots$ | 1 |
| $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ |
| 1 | 1 | 1 | 1 | $\ldots$ | 0 | 0 | 1 | $\ldots$ | $\ldots$ | 1 |
| 1 | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | 1 | 2 | 1 | $\ldots$ | 1 |
| 1 | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | 1 | 2 | 1 | $\ldots$ | 1 |
| 1 | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | 1 | 1 | 2 | $\ldots$ | 1 |
| 1 | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | 1 | 1 | 2 | $\ldots$ | 1 |
| $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ |
| 1 | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | 1 | 1 | 1 | $\ldots$ | 2 |
| 1 | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | $\ldots$ | 1 | 1 | 1 | $\ldots$ | 2 |