(if you're using this as a template for your gravity sim, notice I don't use a gravitational constant)
Repeat Zones are a bit slow, not sure if the Stef's answer would be slower than mine, worth to check… Meanwhile here's an answer using the "quadratic geometry explosion" (just made that term up) pattern, that I also recently used here. And, like there, I'm optimizing distance checks by applying negative $-100 \%$ scale, so that the point I compare to is at origin, and position vector length equals distance to that point. Not sure if this is actually a useful optimization here, though. I also capture radius on the parent-point so it transfers to a spawned instance, and upon realizing instances transfers to the children-points. Now I can easily access the radius of a current point, and a parent point (captured). I use radii to store point masses (could use a separate attribute, but that would increase cache size)
The Attribute Statistic node returns a single value, but an Accumulate Field node also sums up values and can be separated by group. The positioning on index to sample nearest at that index could be optimized away as well as the last Capture Attribute node could be removed…
Notice this relies on the input geometry. I prepared two examples of input geometry for testing:
Test #1: Two-body problem
Test #2: Unstable Constellation
This is why triple-star systems don't exist: there's no stabilizing mechanism that puts bodies back at equal distances when two of them get closer than the other due to some external chaos (here the chaos is just floating point datatype inaccuracy).