01背包模板题。。。
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 const int MAX = 3410, max_w = 12885; 8 int N, M; 9 int W[MAX], D[MAX], dp[max_w]; 10 11 int Solve() { 12 memset(dp, 0, sizeof(dp)); 13 for (int i = 1; i <= N; ++i) { 14 for (int j = M; j >= W[i]; --j) { 15 dp[j] = max(dp[j], dp[j - W[i]] + D[i]); 16 } 17 } 18 return dp[M]; 19 } 20 21 int main() { 22 //freopen("input.txt", "r", stdin); 23 scanf("%d %d", &N, &M); 24 for (int i = 1; i <= N; ++i) { 25 scanf("%d %d", &W[i], &D[i]); 26 } 27 printf("%d ", Solve()); 28 return 0; 29 }