Can someone please help me in this problem, i am not getting how to do this. I think may be it is related to some dp problem or may be graphs. Can you please look into it.
UPD: Contest is already over.
UPD2: pdf link