-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathKnapsack.cpp
125 lines (113 loc) · 2.4 KB
/
Knapsack.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Rishabh Agarwal
#include <bits/stdc++.h>
#define F first
#define S second
#define MAX 10000003
using namespace std;
typedef long long int ll;
const ll mod = 1e18;
const ll INF= 1e18;
long double PI=3.1415926;
ll power(ll a,ll b){
if(b==0){
return 1;
}
ll temp=power(a,b/2)%mod;
if(b%2==0){
return (temp*temp)%mod;
}
else{
return ((a*temp)%mod*temp)%mod;
}
}
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
vector<ll>v;
vector<ll>::iterator it;
map<ll,ll>mp;
void primeFactors(ll n)
{
// Print the number of 2s that divide n
while (n % 2 == 0)
{
//cout << 2 << " ";
mp[2]++;
n = n/2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (ll i = 3; i <= sqrt(n); i = i + 2)
{
// While i divides n, print i and divide n
while (n % i == 0)
{
//cout << i << " ";
mp[i]++;
n = n/i;
}
}
// This condition is to handle the case when n
// is a prime number greater than 2
if (n > 2){
mp[n]++;
//cout << n << " ";
}
}
ll max(ll a, ll b) { return (a > b)? a : b; }
ll knapSack(ll sum, ll wt[], ll val[], ll n,ll W)
{
ll i, w;
ll K[n+1][sum+1];
//memset(K,INF,sizeof(K));
for (i = 0; i <= n; i++)
{
for (w = 0; w <= sum; w++)
{
K[i][w]=INF;
if (w==0)
K[i][w] = 0;
else if(i==0)
continue;
else if (val[i-1] <= w)
K[i][w] = min(wt[i-1] + K[i-1][w-val[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
//cout<<K[i][w]<<" ";
}
//cout<<"\n";
}
for(ll x=sum; x>=0; x--){
if(K[n][x]<=W)
return x;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t;
//cin>>t;
t=1;
//ll c=1;
while(t--){
ll n,w;
cin>>n>>w;
ll we[n],va[n];
ll sum=0;
for(ll x=0; x<n; x++){
cin>>we[x]>>va[x];
sum=sum+va[x];
}
cout<<knapSack(sum,we,va,n,w)<<"\n";
mp.clear();
v.clear();
//cout<<"Case #"<<c<<": "<<ans<<"\n";
//c++;
}
return 0;
}