Minimum Cost to Cut a Rod

00:00
MediumDynamic ProgrammingKnapsack
AmazonTCS

Given a rod of length n and prices for each length 1..n, find maximum revenue by cutting rod into pieces.

Examples

Input → length=8, prices=[1,5,8,9,10,17,17,20]
Output → 22
Note: cut into length 2+6=5+17=22
Input → length=4, prices=[2,5,7,8]
Output → 10
Note: two pieces of length 2: 5+5=10
Input → length=1, prices=[1]
Output → 1