CodeLink->https://ide.codingblocks.com/s/234971 iIused dp as well but it throughs TLE
@imsaurabh, here is the link to your code, I have described the bug in comment. (//*)
//*