June long challenge - Road Sign

https://www.codechef.com/JUNE19B/problems/RSIGNS
I hv partially solved it and getting TLE for other case…Pls tell me how to reduce time!