Little Red Riding Hood (ZCO '14)

http://www.iarcs.org.in/inoi/2013/zco2013/zco2013-1b.php

I think another dp problem. Totally confused! Anyone who could help me in this one?

The answer is here ( http://discuss.codechef.com/questions/57313/zco-problem-little-red-riding-hood )
Check for the comments.