If
$$ f({\bf p}) = \sum_i \left({\bf p}\cdot {\bf p} - 2{\bf p}\cdot {\bf p}_i + {\bf p}_i\cdot {\bf p}_i\right) $$
Why is the gradient of $f(p)$ equal to
$ \nabla f({\bf p}) = \frac{\partial}{\partial {\bf p}}\sum_i \left({\bf p}\cdot {\bf p} - 2{\bf p}\cdot {\bf p}_i + {\bf p}_i\cdot {\bf p}_i \right) =\sum_i \left( 2{\bf p} - 2{\bf p}_i \right) $
You can do it in components, consider the first term for example
$$ {\bf p}\cdot {\bf p} = p_1p_1 + p_2p_2 + \cdots $$
So the gradient of this term is
\begin{eqnarray} \frac{\partial {\bf p}\cdot {\bf p}}{\partial p_1} &=& 2p_1 \\ \frac{\partial {\bf p}\cdot {\bf p}}{\partial p_2} &=& 2p_2 \\ &\vdots & \end{eqnarray}
or in general
$$ \frac{\partial {\bf p}\cdot {\bf p}}{\partial {\bf p}} = 2{\bf p} $$
Similarly you can prove that
$$ \frac{\partial {\bf p}\cdot {\bf p}_i}{\partial {\bf p}} = {\bf p}_i $$