Dynamic Programming-RodCutting
<p>I've recently started learning algorithms.
I tried to implement the classic Rod cutting problem using a  Dynamic programming approach. I'm unable to get the correct output. Here is my code:</p>

<pre><code>public class RodCuttingProblem {

    public static void main(String[] args) {
        int len=5;
        int prices[]={2,5,7,3};
        rodCuttingImplementation(prices,len);
    }

    public static void rodCuttingImplementation(int prices[],int len){
        prices=reAdjustPrice(prices);
        System.out.println("");
        for(int i=0;i&lt;prices.length;i++){
            System.out.print(prices[i]+" ");
        }
        System.out.println(" ");
        int dp[][]=new int[prices.length+1][len+1];
        for(int i=1;i&lt;prices.length;++i){
            for(int j=1;j&lt;=len;++j){
                if(i&lt;=j){
                    dp[i][j]=Math.max(dp[i-1][j], prices[i]+dp[i][j-1]);
                }
                else{
                    dp[i][j]=dp[i-1][j];
                }
                System.out.print(dp[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("Optimal Profit : "+dp[prices.length-1][len]);
    }

    static int[] reAdjustPrice(int prices[]){
        int[] newPrices=new int[prices.length+1];
        newPrices[0]=0;
        for(int i=0;i&lt;prices.length;i++){
            newPrices[i+1]=prices[i];
        }
        return newPrices;
    }
}
</code></pre>

<p>Output:</p>

<pre><code>2 4 6 8 10 
2 7 12 17 22 
2 7 14 21 28 
2 7 14 21 28  
Optimal Profit : 28
</code></pre>

<p>As per my understanding, the output should be 12.</p>