You are given a list of integers prices where prices[i] is the price of a rod piece of length i + 1, and an integer n, the length of the rod you have.
You can cut the rod into any number of pieces of whole-number lengths, and every piece is sold at the price for its own length. Cuts are free. You do not have to cut the rod at all; selling it whole is also allowed.
Return the maximum total price you can get for the rod.
Examples
Example 1
Input: prices = [1,5,8,9,10,17,17,20], n = 8
Output: 22
Explanation: Cutting the rod into a piece of length 2 and a piece of length 6 gives 5 + 17 = 22.
Example 2
Input: prices = [3,5,8,9,10], n = 5
Output: 15
Explanation: Cutting the rod into five pieces of length 1 gives 5 * 3 = 15.
Example 3
Input: prices = [1,2,4], n = 3
Output: 4
Explanation: Selling the rod whole, uncut, gives the best price of 4.
Constraints
1≤n≤1000
prices.length == n
0≤prices[i]≤104
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up