题解 | #采药#
采药
https://www.nowcoder.com/practice/d7c03b114f0541dd8e32ce9987326c16
#include<cstdio> #include<vector> #include<iostream> using namespace std; int main() { int t, n; int time[1000]; int value[1000]; int dp[1000][1000]; scanf("%d%d", &t, &n); for (int i = 1; i <= n ; ++i) { scanf("%d%d", &time[i], &value[i]); } for (int i = 0 ; i <= n ; ++i) { for (int j = 0 ; j <= t ; ++j) { if (i == 0 || j == 0) dp[i][j] = 0; else { if (j <time[i]) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - time[i]] + value[i]); } } } printf("%d", dp[n][t]); }