Back
11
// Write a program to solve the 0-1 knapsack problem using Dynamic Programming.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int knapsack(int W, vector<int>& weights, vector<int>& values, int n) {
vector<vector<int>> dp(n + 1, vector<int>(W + 1));
for (int i = 0; i <= n; i++) {
for (int w = 0; w <= W; w++) {
if (i == 0 || w == 0) {
dp[i][w] = 0;
}
else if (weights[i - 1] <= w) {
dp[i][w] = max(values[i - 1] + dp[i - 1][w - weights[i - 1]],
dp[i - 1][w]);
}
else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][W];
}
int main() {
int n, W;
cout << "Enter the number of items: ";
cin >> n;
vector<int> values(n), weights(n);
cout << "Enter the values of the items: ";
for (int i = 0; i < n; i++) {
cin >> values[i];
}
cout << "Enter the weights of the items: ";
for (int i = 0; i < n; i++) {
cin >> weights[i];
}
cout << "Enter the maximum capacity of the knapsack: ";
cin >> W;
int maxValue = knapsack(W, weights, values, n);
cout << "\nMaximum value that can be obtained: " << maxValue << endl;
return 0;
}