#C590. Rod Cutting Profit Maximization

    ID: 49600 Type: Default 1000ms 256MiB

Rod Cutting Profit Maximization

Rod Cutting Profit Maximization

Given a rod of length NN, and an array of prices P=[P1,P2,,PN]P = [P_1, P_2, \dots, P_N] where PiP_i represents the selling price for a rod piece of length ii, 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 NN, let dp[i]dp[i] denote the maximum profit obtainable for a rod of length ii. The recurrence relation is given by:

dp[i]=max1ji(Pj+dp[ij])dp[i] = \max_{1 \leq j \leq i} (P_j + dp[i-j])

where dp[0]=0dp[0] = 0. Your task is to compute dp[N]dp[N] given the rod length and the corresponding prices.

inputFormat

The input is given through stdin and consists of two lines:

  1. The first line contains an integer NN, denoting the length of the rod.
  2. The second line contains NN space-separated integers. The ii-th integer represents the price of a rod piece of length ii.

outputFormat

Output a single integer to stdout representing the maximum profit obtainable from the rod.## sample

4
1 5 8 9
10