Loading

P1048 [NOIP2005 普及组] 采药

题目链接

https://www.luogu.com.cn/problem/P1048

题目思路

经典01背包问题,每一步的状态为拿当前物品和不拿当前物品的价值最大值
image

#include <iostream>
#include <algorithm>

using namespace std;
const int N = 1010;
int f[N];
int t, m;

int main()
{
    cin >> t >> m;
    for(int i = 0; i < m; i ++ )
    {
        int v, w;
        cin >> v >> w;
        for(int j = t; j >= v; j -- )
        {
            f[j] = max(f[j], f[j - v] + w);
        }
    }
    cout << f[t] << endl;
    return 0;
}
posted @ 2022-03-30 19:48  vacilie  阅读(120)  评论(0编辑  收藏  举报