Dynamin Programming problem on SPOJ- BORW

I’m trying to solve a DP question on spoj, http://www.spoj.com/problems/BORW/
.I have tried my best to memoize all the states, still its giving TLE. PLZ help…
Code : https://ideone.com/Vvk3fh

please tell your approach first,doing that can save time!

problem is solved! Thanks anyway…

hi @amulyagaur , if you have solved that with dp , can you please share your solution ??

Sure! https://ideone.com/MjTflM