5. Real numbers $x, y, a$ satisfy $x+y=a+1$ and $xy=a^2-7a+16$. Then the maximum value of $x^2+y^2$ is $\qquad$ .
5. 32 .

Notice that,
$$
\begin{array}{l}
x^{2}+y^{2}=(x+y)^{2}-2 x y \\
=(a+1)^{2}-2\left(a^{2}-7 a+16\right) \\
=-a^{2}+16 a-31=-(a-8)^{2}+33 . \\
\text { Also, }(x-y)^{2}=(a+1)^{2}-4\left(a^{2}-7 a+16\right) \geqslant 0 \\
\Rightarrow-3 a^{2}+30 a-63 \geqslant 0 \Rightarrow 3 \leqslant a \leqslant 7 .
\end{array}
$$

Thus, on the closed interval $[3,7]$, the function
$$
f(a)=-(a-8)^{2}+33
$$

is increasing.
Therefore, its maximum value is obtained at $a=7$, i.e., the maximum value of $x^{2}+y^{2}$ is 32.