-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0 1 Knapsack.cpp
More file actions
55 lines (47 loc) · 863 Bytes
/
0 1 Knapsack.cpp
File metadata and controls
55 lines (47 loc) · 863 Bytes
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
55
/* Author Kartik Shukla */
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
typedef long long int ll;
#define mod 998244353
#define MOD 1000000007
#define PI 3.14159265358
vector<int> a[200005];
vector<bool> vis(200005,0);
vector<int> dis(200005,0);
int knapsack(int wt[],int val[],int w,int n)
{
if(w == 0 || n == 0)
return 0;
if(wt[n-1]>w)
{
return knapsack(wt,val,w,n-1);
}
else
{
return max(val[n-1]+knapsack(wt,val,w-wt[n-1],n-1),knapsack(wt,val,w,n-1));
}
}
void solve ()
{
int n,w;
cin>>n>>w;
int wt[n],val[n];
for(int i=0;i<n;i++)
cin>>wt[i];
for(int i=0;i<n;i++)
cin>>val[i];
cout<<knapsack(wt,val,w,n)<<endl;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}