Comments

What is the time complexity of your solution? I think the upper bound of your solution is O(n*log(n)*log(c)) and because of that, it is on the border of the time limit.

+1

I don't think there is any DP in your solution :p.