Maximising distance

Question-

Issue-
Unable to figure out how what approach to use?
I used the brute force method but it’s giving the expected tle.
Analyzed the only 2 submissions but can’t figure out what they are doing.
Anyone can help me?

//