Problem 294
Rod Cutting
Medium · 20 pts · Array, Dynamic Programming
Problem
You have a rod of length N and can cut it into pieces of any whole-number lengths (or leave it whole). A piece of length i sells for P[i]. Find the maximum total price you can get by cutting the rod and selling all the pieces. Let best[x] be the maximum price for a rod of length x. The first piece has some length i, so best[x] = max over i from 1 to x of P[i] + best[x - i], with best[0] = 0.
Input
First line: N. Second line: N prices P[1..N].
Output
Print the maximum total price.
Constraints
1 <= N <= 1000; 1 <= P[i] <= 100000
Example input
4 1 5 8 9
Example output
10
Explanation
The prices for lengths 1 to 4 are 1, 5, 8 and 9. Cutting the rod of length 4 into two pieces of length 2 earns 5 + 5 = 10, which beats selling it whole (9) and every other split.