• Source
    1. import java.util.*;
    2.  
    3. /**
    4.  *
    5.  * Assume a company buys long steel rods and cuts them into shorter rods for sale to its customers.
    6.  * If each cut is free and rods of different lengths can be sold for different amounts,
    7.  * we wish to determine how to best cut the original rods to maximize the revenue.
    8.  *
    9.  */
    10. class Ideone{
    11.  
    12. private static int MIN_VALUE = -23;
    13. public static void main(String[] args){
    14. int[] a = {1, 5, 8, 9, 10, 17, 17, 20};
    15.  
    16. //Calculate max costs obtained if we cut rod at different lengths
    17. int[] maxCosts = new int[a.length+1];
    18. maxCosts[0] = 0;
    19. for(int i=1; i<=a.length; i++){
    20. int maxValue = MIN_VALUE;
    21. for(int j=0; j<i; j++){
    22. maxValue = max(maxValue, maxCosts[i-j-1]+a[j]);
    23. }
    24. maxCosts[i] = maxValue;
    25. }
    26.  
    27. System.out.println("Max value obtained is => "+ maxCosts[maxCosts.length-1]);
    28. }
    29.  
    30. private static int max(int a, int b){
    31. return a>b ? a : b;
    32. }
    33. }
    34.