最基本的01背包,不需要太多解释,刚学dp的同学可以参见dd大牛的背包九讲,直接度娘“背包九讲”即可搜到
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 const int maxn = 1000 + 500; 6 int dp[maxn]; 7 int t, m; 8 int s1, v1; 9 10 int main () { 11 scanf("%d %d", &t, &m); 12 for (int i = 1; i <= m; i++) { 13 scanf("%d %d", &s1, &v1); 14 for (int j = t; j >= s1; j--) { 15 dp[j] = std :: max(dp[j], dp[j - s1] + v1); 16 } 17 } 18 int ans = 0; 19 for (int i = 0; i <= t; i++) ans = std :: max(ans, dp[i]); 20 printf("%d", ans); 21 return 0; 22 }
时间: 2024-10-24 00:41:55