题解 | #采药#
采药
https://www.nowcoder.com/practice/d7c03b114f0541dd8e32ce9987326c16
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn=101;
const int maxt=1001;
int dp[maxn][maxt];
int a[maxn];
int w[maxn];
int main() {
int t,m;
while(cin>>t>>m){
for(int i=1;i<=m;i++){
scanf("%d",&a[i]);
scanf("%d",&w[i]);
}
for(int i=0;i<=m;i++){
for(int j=0;j<=t;j++){
dp[i][j]=0;
}
}
for(int i=1;i<=m;i++){
for(int j=1;j<=t ;j++){
if(j<a[i]) dp[i][j]=dp[i-1][j];
else dp[i][j]=max(dp[i-1][j],dp[i-1][j-a[i]]+w[i]);
}
}
cout<<dp[m][t]<<endl;
}
}
