4. Given $a, b (a \leqslant b)$ are positive integers, and real numbers $x, y$ satisfy $x+y=4(\sqrt{x+a}+\sqrt{y+b})$. If the maximum value of $x+y$ is 40, then the number of pairs $(a, b)$ that satisfy the condition is $(\quad)$.
(A) 1
(B) 3
(C) 5
(D) 7
4.C.

Since $(m+n)^{2} \leqslant 2\left(m^{2}+n^{2}\right)$, we have
$$
\begin{array}{l}
x+y=4(\sqrt{x+a}+\sqrt{y+b}) \\
\leqslant 4 \sqrt{2(x+a+y+b)} . \\
\text { Therefore, }(x+y)^{2}-32(x+y)-32(a+b) \leqslant 0 .
\end{array}
$$

Thus, $x+y \leqslant 16+4 \sqrt{16+2(a+b)}$.
From $16+4 \sqrt{16+2(a+b)}=40$, we get
$$
a+b=10 \text {. }
$$

When $x=\frac{1}{2}(b-a+40), y=\frac{1}{2}(a-b+40)$, $x+y$ reaches its maximum value.

Since $a \leqslant b$, there are 5 pairs of numbers $(a, b)$ that satisfy the condition.