For each continuous function $f:[0,1]\rightarrow R$, let $$I(f)=\int_{0}^1x^2f(x)dx$$ and $$J(f)=\int_{0}^1x(f(x))^2dx$$ Then find the maximum value $I(f)-J(f)$ over all such functions $f$.
Now, the given solution is
$$I(f)-J(f)=\int_{0}^1x^2f(x)dx-\int_{0}^1x(f(x))^2dx=\int_{0}^1\left\{\frac{x^3}{4}-x\left(f(x)-\frac{x}{2}\right)^2dx\right\}dx\leq\int_{0}^1\frac{x^3}{4}dx=\frac{1}{16}$$
which means that maximum value occurs when $f(x)=\frac{x}{2}$.
However I attempted it in a different manner.
Let $$G(x)=\int_{0}^xt^2f(t)dt-\int_{0}^xt(f(t))^2dt$$ To obtain the maximum value of $G(x)$, I found $G'(x)$ and put it equal to zero. $$G'(x)=x^2f(x)-x(f(x))^2=0$$ On solving I get $$x=0,f(x)=0,f(x)=x$$ which is contrary to the solution given above.
What is the mistake I have made. Why can't I get the answer by doing what I did.