-
Notifications
You must be signed in to change notification settings - Fork 0
/
knap.cpp
48 lines (44 loc) · 799 Bytes
/
knap.cpp
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
#include<bits/stdc++.h>
using namespace std;
void fun(vector<int> wt,vector<int>val,int w,int op)
{
if(w==0||wt.size()==0)
{
cout<<op;
return;
}
if(w<wt[0])
{
wt.erase(wt.begin()+0);
val.erase(val.begin()+0);
fun(wt,val,w,op);
}
else
{
int x=op;
int y=op+val[0];
int w1=w;
int w2=w-wt[0];
wt.erase(wt.begin()+0);
val.erase(val.begin()+0);
fun(wt,val,w2,y);
fun(wt,val,w1,x);
}
return;
}
void solve(vector<int> wt,vector<int> val,int w)
{
int op=0;
fun(wt,val,w,op);
return ;
}
int main()
{
int n,w;
cin>>n>>w;
vector<int >wt(n),val(n);
for(int i=0;i<n;i++)
cin>>wt[i]>>val[i];
solve(wt,val,w);
return 0;
}