#C590. Rod Cutting Profit Maximization
Rod Cutting Profit Maximization
Rod Cutting Profit Maximization
Given a rod of length , and an array of prices where represents the selling price for a rod piece of length , determine the maximum profit obtainable by cutting the rod into one or more pieces. You may choose not to cut the rod at all. The profit is defined as the sum of the prices of the pieces obtained after cutting.
The problem can be formulated using dynamic programming. For a rod of length , let denote the maximum profit obtainable for a rod of length . The recurrence relation is given by:
where . Your task is to compute given the rod length and the corresponding prices.
inputFormat
The input is given through stdin
and consists of two lines:
- The first line contains an integer , denoting the length of the rod.
- The second line contains space-separated integers. The -th integer represents the price of a rod piece of length .
outputFormat
Output a single integer to stdout
representing the maximum profit obtainable from the rod.## sample
4
1 5 8 9
10