CQUOJ 2500 - Bone Collector
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 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 ?

Input
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.
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.
Output
One integer per line representing the maximum of the total value (this number will be less than 231).
Sample Input
1 5 10 1 2 3 4 5 5 4 3 2 1
Sample Output
14
1 /* 2 2016年4月22日23:42:23 3 01背包 4 5 */ 6 7 8 # include <iostream> 9 # include <cstdio> 10 # include <cstring> 11 # include <algorithm> 12 # include <queue> 13 # include <vector> 14 # include <cmath> 15 # define INF 0x3f3f3f3f 16 using namespace std; 17 const int N = 1003; 18 19 int main(void) 20 { 21 int test, i, j, n, m; 22 int v[N], w[N], dp[N]; 23 scanf("%d", &test); 24 while (test--){ 25 scanf("%d %d", &n, &m); 26 for (i = 1; i <= n; i++) 27 scanf("%d", &v[i]); 28 for (i = 1; i <= n; i++) 29 scanf("%d", &w[i]); 30 memset(dp, 0, sizeof(dp)); 31 dp[0] = 0; 32 for (i = 1; i <= n; i++){ 33 for (j = m; j >= w[i]; j--) 34 dp[j] = max(dp[j], dp[j-w[i]]+v[i]); 35 } 36 printf("%d\n", dp[m]); 37 } 38 return 0; 39 }

浙公网安备 33010602011771号