Advertisement
Rakibul_Ahasan

Rod Cutting Problem (Bottom Up)

Dec 25th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=2001;
  4. int len;
  5. int n,temp;
  6. int cost[N];
  7.  
  8. void price(){
  9.     for(int i=1;i<=n;i++){
  10.         cin>>cost[i];
  11.     }
  12. }
  13.  
  14. int cutRod(int l){
  15.     int value[l+1];
  16.     value[0]=0;
  17.  
  18.     for(int i=1;i<=l;i++){
  19.         int max_value=INT_MIN;
  20.         for(int j=1;j<=i;j++){
  21.             max_value=max(max_value,cost[j]+value[i-j]);
  22.         }
  23.         value[i]=max_value;
  24.     }
  25.     return value[l];
  26. }
  27.  
  28. // len<=n : YES
  29.  
  30. int main() {
  31.     cin>>len;
  32.     cin>>n;
  33.  
  34.     price();
  35.     cout<<cutRod(len);
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement