最基础的DP
1 #include "iostream" 2 #include "cstdio" 3 #include "string" 4 using namespace std; 5 #define MAXN 222222 6 int c[MAXN],v[MAXN]; 7 int dp[MAXN]; 8 int main() 9 { 10 int n,C; 11 cin >> n >> C; 12 for (int i = 0;i < n; ++ i) 13 cin >> c[i] >> v[i]; 14 for (int i = 0;i < n ; ++ i) 15 for (int w = C;w >= 0 ; -- w) 16 if (w-c[i] >= 0) 17 dp[w] = max(dp[w],dp[w-c[i]] + v[i]); 18 cout << dp[C] << endl; 19 return 0; 20 }