Help STPAR

I am trying to sole this and not getting the logic.
help…??

link to my code : https://ideone.com/faXMAR
link to problem : http://www.spoj.com/problems/STPAR/