Bone Collector (HDU - 2602)

题面

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave … The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?

输入

The first line contain a integer T , the number of cases. Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.

输出

One integer per line representing the maximum of the total value (this number will be less than 2^31).

样例输入

11
25 10
31 2 3 4 5
45 4 3 2 1

样例输出

114

提示

思路

代码

 1using namespace std;
 2typedef long long ll;
 3const int inf = 0x3f3f3f3f;
 4const int N = 1e3+5;
 5
 6int T, n, m;
 7int c[N], w[N];
 8int dp[N];
 9
10int main(void) {
11    scanf("%d", &T);
12    while(T--) {
13        scanf("%d%d", &n, &m);
14        for(int i=0; i<n; i++) {
15            scanf("%d", &w[i]);
16        }
17        for(int i=0; i<n; i++) {
18            scanf("%d", &c[i]);
19        }
20        memset(dp, 0, sizeof(dp));
21        for(int i=0; i<n; i++) {
22            for(int j=m; j>=c[i]; j--) {
23                dp[j] = max(dp[j], dp[j-c[i]]+w[i]);
24            }
25        }
26        printf("%d\n", dp[m]);
27    }
28    return 0;
29}