-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathB.cpp
More file actions
42 lines (39 loc) · 878 Bytes
/
Copy pathB.cpp
File metadata and controls
42 lines (39 loc) · 878 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
#include<bits/stdc++.h>
#define N 1001
using namespace std;
long knapSack(long W, long wt[], long val[], long n)
{
long i, w;
long K[n+1][W+1];
for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (wt[i-1] <= w)
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
}
return K[n][W];
}
int main()
{
freopen("B.in","r",stdin);
freopen("B.out","w",stdout);
long C[N];
long K[N];
long tc;
cin>>tc;
while(tc--){
long n,B;
cin>>n>>B;
// cerr<<n<<" "<<B<<endl;
for(long i=0;i<n;i++)
cin>>K[i]>>C[i];
long ans=knapSack(B,C,K,n);
cout<<ans<<endl;
}
}