-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdventure.cpp
More file actions
54 lines (50 loc) · 1.08 KB
/
Adventure.cpp
File metadata and controls
54 lines (50 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1000;
const int maxW = 1000;
int dp[maxN][maxW];
int knapsack(int n, int weight[], int value[], int W)
{
if (n == 0 || W == 0)
return 0;
if (dp[n][W] != -1)
return dp[n][W];
if (weight[n - 1] <= W)
{
int op1 = knapsack(n - 1, weight, value, W - weight[n - 1]) + value[n - 1];
int op2 = knapsack(n - 1, weight, value, W);
return dp[n][W] = max(op1, op2);
}
else
{
return dp[n][W] = knapsack(n - 1, weight, value, W);
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n, W;
cin >> n >> W;
int weight[n], value[n];
for (int i = 0; i < n; i++)
{
cin >> weight[i];
}
for (int i = 0; i < n; i++)
{
cin >> value[i];
}
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= W; j++)
{
dp[i][j] = -1;
}
}
cout << knapsack(n, weight, value, W) << endl;
}
return 0;
}