Nice DP problem

problem link -

i got the idea to solve it in O(n^5) but it will not pass its a nice DP problem can any one help me plz