One approach to detecting multiple changepoints is by minimizing an objective function. In some cases, dynamic programming can be used to minimize this function. The standard dynamic programming algorithm use a recursion that conditions on the time of the last changepoint. I will present an alternative recursion that conditions on the value of the last segment parameter. This recursion is close to the recursion of the Viterbi algorithm. The resulting algorithm is in some cases much faster. Also, it can exactly minimize the cost (i) in situations where the loss function is not convex and (ii) in situations where there are dependencies in parameters associated with successive segments. I will present two applications of this algorithm : (1) the detection of changes in the presence of outliers and (2) the detection of peaks. This is joint work with Vincent Runge, Paul Fearnhead and Toby Hocking