题目描述
辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师。为此,他想拜附近最有威望的医师为师。医师为了判断他的资质,给他出了一个难题。医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个山洞里有一些不同的草药,采每一株都需要一些时间,每一株也有它自身的价值。我会给你一段时间,在这段时间里,你可以采到一些草药。如果你是一个聪明的孩子,你应该可以让采到的草药的总价值最大。”
如果你是辰辰,你能完成这个任务吗?
输入格式
第一行有 2 个整数 T(1≤T≤1000)和 M(1≤M≤100),用一个空格隔开,TT 代表总共能够用来采药的时间,M 代表山洞里的草药的数目。
接下来的 M 行每行包括两个在 1到 100 之间(包括 1和 100)的整数,分别表示采摘某株草药的时间和这株草药的价值。
输出格式
输出在规定的时间内可以采到的草药的最大总价值。
输入
70 3
71 100
69 1
1 2
输出
3
#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define mod 1e9+7
#define INF 0x3f3f3f
#define fi first
#define se second
#define it iterator
#define ins insert
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned long long
#define mem(a) memset(a,0,sizeof(a))
#define cio ios::sync_with_stdio(false)
#define T int t;scanf("%d",&t);while(t--)
#define CE cout << endl;
#define C(n) cout << n << endl
#define CY cout << "YES" << endl;
#define CN cout << "NO" << endl;
#define Cy cout << "Yes" << endl;
#define Cn cout << "No" << endl;
int s[110];
int v[110];
int dp[150][1010];
int main()
{
int t, m;
cin >> t >> m;
for(int i = 1; i <= m; i++) cin >> s[i] >> v[i];
for(int i = 1; i <= t; i++){
if(i>=s[1]) dp[1][i] = v[i];
}
for(int i = 1; i <= m; i++){
for(int j = 1; j <= t; j++){
// i:当前一共i种药
// j:当前拥有的时间
if(j>=s[i]){
// 转移方程
dp[i][j] = max(dp[i-1][j],dp[i-1][j-s[i]]+v[i]);
}else{
dp[i][j] = dp[i-1][j];
}
}
}
C(dp[m][t]);
return 0;
}