Algebra Score, how to solve?

Problem link: https://www.codechef.com/PROC2018/problems/PROC18D
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