https://www.codechef.com/LRNDSA08/problems/DINCPATH

In The above problem i got a TLE Can Anyone help me find my mistake
https://www.codechef.com/viewsolution/34655777

The while(!s.empty()) loop is the cause for the TLE. I think, it could be fixed by an upper_bound approach.