I've finally found a solution, in Bash, fast enough.
The principle was described in the Niskaz's post:
It's quite easy to implement. But you have to take care about how you perform arthitmetic expressions: it is better to use
$(( Pi - Pj )) than
expr Pi - Pj.
This little detail allowed me to pass the test with a huge number of horses.
I hope this will help !