Help with this problem

I tried this problem during the contest but was able to pass only a few test cases.I tried solving it using dynamic programming but time complexity of my solution was O((N^(3/2))*D) (which is an obvious tle given the constraints).Could someone give me some hint on how to reduce the complexity ?

it is alreaady asked in codechef march lunchtime 2014.
here is link of question:CodeChef: Practical coding for everyone

1 Like

thanks :slight_smile: