40. At the vertices of a convex 2020-gon, numbers are placed such that among any three consecutive vertices, there is both a vertex with the number 7 and a vertex with the number 6. On each segment connecting two vertices, the product of the numbers at these two vertices is written. Andrey calculated the sum of the numbers written on the sides of the polygon and obtained a total of $A$, while Sasha calculated the sum of the numbers written on the diagonals connecting vertices two apart and obtained a total of $C$. Find the maximum possible value of the difference $C-A$.

![](https://cdn.mathpix.com/cropped/2024_05_06_06b680ebbb9c960a4ee1g-1.jpg?height=334&width=348&top_left_y=935&top_left_x=1639)
40. Answer: 1010. This answer is achieved if the numbers 7 and 6 alternate.

We can assume that all numbers are equal to 6 or 7. Indeed, if a number is different from 6 and 7, then among its neighbors, one is 6 and one is 7, and the same is true for the neighbors two steps away. Then, replacing this number with 7 does not change the difference $C-A$.

Now, let's reduce all numbers by 6.5 and prove that the value $C-A$ does not change. Let Sash's sum become $C_{1}$, and Andrey's sum become $A_{1}$. If before the reduction, the number $ab$ stood on some segment, then after we reduced the numbers, the number $(a-6.5)(b-6.5)=ab-6.5(a+b)+6.5^{2}$ stands on this segment. Then $C-C_{1}=2S \cdot 2020 \cdot 6.5-2020 \cdot 6.5^{2}=A-A_{1}$, where $S$ is the sum of all original numbers. From this, it follows that $C-A=C_{1}-A_{1}$.

Now $C_{1}-A_{1} \leqslant 2020 \cdot \frac{1}{4}-2020 \cdot \frac{-1}{4}=1010$, since after the reduction, all numbers are equal to $\pm \frac{1}{2}$, and the product on each segment is $\pm \frac{1}{4}$. In particular, equality is achieved if the original numbers 6 and 7 alternated.