#include <iostream>
using namespace std;
int knapsack (int W, int wt[], int val[], int n) {
int K[n+1][W+1] = {0};
// Building table K[][] in bottom manner
for(int i=0; i<=n; i++) {
for(int w=0; w <= W; w++) {
// Base case: empty knapsack
if (i == 0 || w == 0) {
K[i][w] = 0;
} else if (wt[i-1] <= w) {
// Selecting val[i-1] if it gives higher
// knapsack value
K[i][w] = max((val[i-1] + K[i-1][w-wt[i-1]]),
K[i-1][w]);
} else {
// Not including val[i-1]:
// if wt[i-1] is greater than
// available knapsack capacity
K[i][w] = K[i-1][w];
}
}
}
return K[n][W];
}
int main() {
// your code goes here
int val[] = {60, 100, 120};
int wt[] = {10, 20, 30};
int W = 50, n = 3;
printf("%d", knapsack(W, wt, val, n));
return 0;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IGtuYXBzYWNrIChpbnQgVywgaW50IHd0W10sIGludCB2YWxbXSwgaW50IG4pIHsKICBpbnQgS1tuKzFdW1crMV0gPSB7MH07CgogIC8vIEJ1aWxkaW5nIHRhYmxlIEtbXVtdIGluIGJvdHRvbSBtYW5uZXIKICBmb3IoaW50IGk9MDsgaTw9bjsgaSsrKSB7CiAgICBmb3IoaW50IHc9MDsgdyA8PSBXOyB3KyspIHsKICAgICAgLy8gQmFzZSBjYXNlOiBlbXB0eSBrbmFwc2FjawogICAgICBpZiAoaSA9PSAwIHx8IHcgPT0gMCkgewogICAgICAgIEtbaV1bd10gPSAwOwogICAgICB9IGVsc2UgaWYgKHd0W2ktMV0gPD0gdykgewogICAgICAgIC8vIFNlbGVjdGluZyB2YWxbaS0xXSBpZiBpdCBnaXZlcyBoaWdoZXIKICAgICAgICAvLyBrbmFwc2FjayB2YWx1ZQogICAgICAgIEtbaV1bd10gPSBtYXgoKHZhbFtpLTFdICsgS1tpLTFdW3ctd3RbaS0xXV0pLAogICAgICAgICAgICAgICAgICAgS1tpLTFdW3ddKTsKICAgICAgfSBlbHNlIHsKICAg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