Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
542 B
1
Indexable
Never
// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;

int knapsack(int W, int wt[],int prof[], int n)
{
    if(n==0||W==0)
    return 0;
    
    if(wt[n-1]>W)
    return knapsack(W,wt,prof,n-1);
    
    else
    return max(prof[n-1]+knapsack(W-wt[n-1],wt,prof,n-1),knapsack(W,wt,prof,n-1));
}
int main() {
    int profit[] = { 2, 3, 1, 4 };
    int weight[] = { 3, 4, 6, 5 };
    int W = 8;
    int n = sizeof(profit) / sizeof(profit[0]);
    cout << knapsack(W, weight, profit, n);
    return 0;
}