// 0-1背包问题
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
using namespace std;
int N,M;
struct Item
{
int w,d;
};
Item items[3520];
int f[13020];
int main()
{
cin >> N >> M;
for(int i=1; i<=N ;i++)
cin >> items[i].w >> items[i].d;
for(int j=0; j<=M ;j++)
if(items[1].w <= j)
f[j] = items[1].d;
else
f[j] = 0;
for(int i=2 ; i <= N ; i++)
{
for(int j=M; j >= 0 ; j--)
if(items[i].w <= j)
f[j] = max(f[j],f[j - items[i].w] + items[i].d);
}
cout << f[M] << endl;
}
0-1背包
最新推荐文章于 2025-05-02 12:09:55 发布