Solution (source code)

= Solution

Pair the $ij$ and $ji$ terms and use $\mathbf f_{ji}=-\mathbf f_{ij}$:
$$
\sum_i\mathbf F_i\cdot\mathbf r_i
=\sum_i\sum_{j<i}\mathbf f_{ij}\cdot(\mathbf r_i-\mathbf r_j).
$$
For gravity, $\mathbf f_{ij}=-Gm^2(\mathbf r_i-\mathbf r_j)/|\mathbf r_i-\mathbf r_j|^3$, so each summand is $-Gm^2/|\mathbf r_i-\mathbf r_j|$. Their sum is $V$.