mike3 how it can be solved using dynamic programming

hi the solution proposed by officials on mike 3 is good but some guys have applied knapsack on it
in their codes so how can it be solved using dp
plz help i devoted those days in order to find a dp solution for it as i am a beginner is failed to
solve. but i figured out the matrix of 20X20