Algebra Score, how to solve?

Problem link: CodeChef: Practical coding for everyone
What should be the approach this question? Can’t we use stack in this?
I am not able to understand DP used in submitted solutions,how can we use DP in this question.

See this for Explanation : LINK

someOne help, please give any testcase where my code is failing
my code link