Define = number of segments in the optimal answer when penalty is . This function is monotonic: larger means fewer segments.
Binary search (halving the search range each step) on to find where . At that penalty, the optimal solution happens to use exactly segments. The answer is: (penalized cost at optimal ) . You subtract the penalty to get the true cost.