简单01背包:
View Code
1 #include <iostream>
2 #include <cstring>
3 #include <cstdio>
4
5 using namespace std;
6
7 const int N = 12888;
8 const int M = 3407;
9
10 int f[N];
11 int w[M];
12 int v[M];
13
14 int main() {
15 //freopen("data.in", "r", stdin);
16
17 int n, V, i, j;
18 while(~scanf("%d%d", &n, &V)) {
19 for(i = 0; i < n; i++) {
20 scanf("%d%d", &w[i], &v[i]);
21 }
22 memset(f, 0, sizeof(f));
23 for(i = 0; i < n; i++) {
24 for(j = V; j >= w[i]; j--) {
25 f[j] = max(f[j], f[j-w[i]] + v[i]);
26 }
27 }
28 cout << f[V] << endl;
29 }
30 return 0;
31 }