#include <iostream>#define N 50usingnamespacestd;
int main()
{
int weight[N];
int power[N];
int dp[N][5*N];
int n, capacity;
cout << "please input the number of goods and the package's capacity" << endl;
cin >> n >> capacity;
cout << "please input every goods's weight and power" << endl;
for (int i=1; i<=n; ++i)
{
cin >> weight[i] >> power[i];
}
//初始化边界条件for (int i=1; i<=capacity; ++i)
{
if (i >= weight[1])
dp[1][i] = power[i];
else
dp[1][i] = 0;
}
//顺推计算i是物品个数 for (int i=2; i<=n; ++i)
{
//背包剩余重量 for (int j=1; j<=capacity; ++j)
{
//装不下if (weight[i] > j)
dp[i][j] = dp[i-1][j];
else//可以装下
dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+power[i]);
}
}
cout << "the most power is: ";
cout << dp[n][capacity] << endl;
return0;
}