5. Solution. First, let's prove that building towers taller than 2 is meaningless.

Indeed, suppose one of the towers has a height of \( h > 2 \). Let's remove the top floor from it, turning it into a new tower of height 1. How will this change the inspector's sum? The shortened tower no longer sees towers of height \( h-1 \). However, since \( h > 2 \), from all these towers (including the shortened one), the new tower is now visible. Therefore, the total sum has increased.

![](https://cdn.mathpix.com/cropped/2024_05_06_4ca17689025243fab126g-18.jpg?height=319&width=616&top_left_y=1354&top_left_x=465)

Fig. 15

It remains to study the case where all towers have a height of 1 or 2. If there are \( X \) towers of height 1 and \( Y \) towers of height 2 (by the condition \( X + 2Y = 30 \)), then the inspector's sum is

\[
X Y = (30 - 2Y) Y = 2Y(15 - Y) = 2\left(7.5^2 - (Y - 7.5)^2\right)
\]

For integer \( Y \), this expression takes its maximum value at \( Y = 7 \) and \( Y = 8 \), and equals 112.
Answer: Either 16 single-story and 7 double-story, or 14 single-story and 8 double-story towers need to be built.