For a system of particles, all possible pairs are taken and total gravitational potential energy is the algebraic sum of the potential energies due to each pair, applying the principle of superposition.
Total gravitational potential energy =r1−Gm1m2−r2Gm2m3−r3Gm1m3 =(r1−Gm1m2)+(r2−Gm2m3)+(r3−Gm1m3)