Programming in QWERT

I was trying to solve the problem QWERT. I think it can be solved using Dynamic Programming. Any idea how to solve it.

Thanks

//