Dynamic programming21 : I want to ask this question , because now it is many days that i am bothered by it
but couldnt come to an optimised solution for this problem,[here is my code][1]

    lli solve(lli n,lli i,lli c)

My solution is O(N^3) and it should pass the test cases(N=300),but still time limit exceeds for it, please i really need help!!!

[Here is the problem link][2]
 


  [1]: https://ideone.com/CMewEj
  [2]: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1254