Skip to content
BytePatterns

Cheapest Stick Cuts

HardDynamic Programming#interval-dp#bottom-up-dp~50m

Problem

A wooden stick of a given length must be cut at every position in a list of marks, each strictly between 0 and length and all different. A saw charges the current length of the piece it cuts, so the order of the cuts changes the bill. Return the smallest total charge for making all the cuts. With no marks there is nothing to pay.

Examples

Input:  length = 10, marks = [2, 4, 7]
Output: 20
Why:    cut at 4 (pays 10), then at 2 (pays 4), then at 7 (pays 6)
Input:  length = 9, marks = [5, 1, 6, 3]
Output: 21
Why:    marks can arrive in any order; only their positions matter
Input:  length = 6, marks = []
Output: 0
Why:    edge case, no cuts

Hints

0 / 3

Stuck on the idea rather than the code? Interval DP covers it.