Problem link : http://www.spoj.com/problems/PYTRIP/
I am not able to think of any other solution except brute force and also, i am unable to find any pattern or something. If anyone could please give me a hint in approaching the problem or provide a reference to any material that you think might be helpful would be great.