Sir my code not run error is coming segmentation fault.
this is my code.
#include
#include
using namespace std;
int profit(int N,int C,int wt[],int prices[]){
// Base case
if(N==0 || C==0){ // if not any item or not any capacity
return 0;
}
// recursive case
int ans=0;
int including,excluding;
including = excluding = 0;
if(wt[N-1]<=C){ // capacity is 7 and item is 6 then true condition
//Include
int including =prices[N-1]+profit(N-1,C-wt[N-1],wt,prices); /*prices of last item(n-1)+
profit last item,Capacity-Weight of last item(n-1),weight,prices */
// means suppose item capacity is 6 and item is 8 then can not true the condition
}
// Exclude
excluding = profit(N,C,wt,prices); // (means price is 0 not add and profit is N item and capacity C )
ans =max(including,excluding);
return ans;
}
int main(){
int weights[] ={1,2,3,4,5};
int prices[] ={40,20,30,100};
int N=4; // Number items
int C=7;// Number of capacity
cout << profit(N,C,weights,prices);
return 0;
}