Doubt in a DP problem

I am solving this question and i think it is very similar to Knapsack problem.
I am not able to figure out how to use knapsack in this problem.
I tried it using knapsack but i am getting TLE.
Can anyone please help me by sharing their approach(code).
question link: CodeChef: Practical coding for everyone

Thanks in advance.

Share your dp state and transitions.

https://www.codechef.com/viewsolution/44321836

Can you please help me in this also?

here is my code: CodeChef: Practical coding for everyone
Please explain me.