#include<iostream>12using namespace std;34int main(){5int capacity = 10;6int items = 4;7int price[items + 1] = {0, 3, 7, 2, 9};8int wt[items + 1] = {0, 2, 2, 4, 5};9int dp[items + 1][capacity + 1];1011for(int i = 0; i <= items; i++){12for(int j = 0; j <= capacity; j++){13if(i == 0 || j == 0){14//There's nothing to add to Knapsack15dp[i][j] = 0;16}17else if(wt[i] <= j){18//Choose previously maximum or value of current item + value of remaining weight19dp[i][j] = max(dp[i - 1][j], price[i] + dp[i - 1][j - wt[i]]);20}21else{22//Add previously added item to knapsack23dp[i][j] = dp[i - 1][j];24}25}26}272829cout << "Maximum Profit Earned: " << dp[items][capacity] << "\n";30return 0;31}3233/*340/1 Knapsack :35Time Complexity: O(N*W).36where ‘N’ is the number of weight element and ‘W’ is capacity. As for every weight element we traverse through all weight capacities 1<=w<=W.37Auxiliary Space: O(N*W).38The use of 2-D array of size ‘N*W’.39*/4041