01背包
作者:
愛_
,
2024-12-10 19:25:30
,
所有人可见
,
阅读 6
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e3+10;
int n,bag;
int f[N][N];
int w[N];
int v[N];
int main()
{
cin>>n>>bag;
for(int i =1;i <= n;i++)cin>>v[i]>>w[i];
for (int i = 1; i <= n; i ++ )
for (int j = 0; j <= bag; j ++ )
{
f[i][j]=f[i-1][j];
if(j>=v[i])
f[i][j]=max(f[i][j],f[i-1][j-v[i]]+w[i]);
}
int res = 0;
for (int i = 0; i <= bag; i ++ )res = max(res,f[n][i]);
cout<<res<<endl;
return 0;
}