An elementary problem on function The problem is:

let $p(x)$ be a polynomial with real Coefficients that is non-negative for all $x$. Prove that for some $k$, there are polynomials $f_1 (x),\cdots f_k (x)$ such that $p(x)=\sum_{i=0}^k (f_i (x))^2$.

I have tried a lot but don't able to make any substantial progress so that I can state it, I will be happy if anyone help how to approach this kind of problem or with any possible solutions. Thanks.
Edit  : thanks to those who tried to help with this $i=2$ version but I don't able to convert it into solution so ask for further help or any references.
 A: WLOG $p$ is monic. First, $\deg p$ is even. So we can pair off the roots in complex conjugate pairs (note that any real root has even multiplicity as $p\geq0$).
So we can write $p(x)=\prod (x-a_k)(x-\bar{a_k})=q(x)\overline{q(x)}$, where $q(x)=\prod(x-a_k)$. Then there exist real polynomials $f,g$ such that $q=f+ig$. Then $$p=(f+ig)(f-ig)=f^2+g^2.$$
A: You know that $p \in \mathbb{R}[X]$, so by factorizing it with irreducible polynomials you can always write it as :
$$p = \lambda \prod_{i=1}^r(X-x_i)^{\alpha_i}\prod_{j=1}^s(X^2+b_jX+c_j)^{\beta_j}$$
With all $x_i$ that are real and distinct, and $\forall j \in  \{1,\dots,q \}, (b_j,c_j)\in \mathbb{R}^2$ verifying $b_j^2-4c_j<0$.
If one of the $\alpha_i$ was an odd number so you could check that $p$ would change of sign near $x_i$, it implies that all $\alpha_i$ are even numbers (because you know $p$ has always the same sign). You can now check that $\lambda \geq 0$.
Now let's factorize $\displaystyle\prod_{j=1}^s(X^2+b_jX+c_j)^{\beta_j}$ in $\mathbb{C}[X]$ :
$$\prod_{j=1}^s(X^2+b_jX+c_j)^{\beta_j} = \prod_{j=1}^s(X-z_j)^{\beta_j}\prod_{j=1}^s(X-\overline{z_j})^{\beta_j}$$
But you can write $\displaystyle \prod_{j=1}^s(X-z_j)^{\beta_j} = g_1+ig_2$ where $g_1,g_2\in \mathbb{R}[X]^2$, therefore:
$$\prod_{j=1}^s(X-\overline{z_j})^{\beta_j} = \overline{\prod_{j=1}^s(X-z_j)^{\beta_j} } = \overline{g_1+ig_2}=g_1-ig_2$$
Thus:
$$\displaystyle \prod_{j=1}^s(X^2+b_jX+c_j)^{\beta_j} = \prod_{j=1}^s(X-z_j)^{\beta_j}\prod_{j=1}^s(X-\overline{z_j})^{\beta_j} = (g_1+i g_2)(g_1-ig_2) = g_1^2+g_2^2$$
Finally by taking $f_1 = \sqrt{\lambda} \left(\displaystyle \prod_{i=1}^r (X-x_i)^{\alpha_i/2}\right)g_1$ and $f_2 =\sqrt{\lambda} \left(\displaystyle \prod_{i=1}^r (X-x_i)^{\alpha_i/2}\right)g_2$, you have:
$$\boxed{f_1,f_2 \in \mathbb{R}[X] \text{ such that } p = f_1^2+f_2^2}$$
A: Since $P$ is positive, all its real roots have an even multiplicity.
So we can write $P = \prod (X-\gamma_i)(X-\overline{\gamma_i}) \prod (X-x_k)^2$, where the $\gamma_i$ are the complex roots, and the $x_k$ the real (eventually equal).
Then if we call $A = \prod (X-\gamma_i)$, we have $\overline{A} = \prod (X-\overline{\gamma_i})$. So that if we note $C = \prod (X-x_k)$, we have $$ P = AB\cdot \overline{A}B$$
Since $B$ is a real polynomial, $B=\overline{B}$.
So $P=AB\cdot\overline{AB}$.
If we note $AB = R+iQ$ with $R$ and $Q$ real polynomials, we have : $$P=R^2 + Q^2$$
So for the exercise, we can notice that $k=1$ or $2$.
