Charm Bracelet一维数组背包
作者:互联网
# include <iostream>
using namespace std;
int a[12881] = {0};
//前i个构成w的最大价值 = max{前i-1个构成w-wi的最大价值+vi , 前i-1个构成w}
int w[3410] = {0};
int v[3410] = {0};
int main ()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> w[i] >> v[i];
}
//for (int i = 0; i < w[0] && i <= m; i++)
//{
// a[i][0] = 0;
// }
//a[w[0]] = v[0];
//
for (int i = 0; i < n; i++)
{
for (int j = m; j >= 0; j--)
{
if (j - w[i] >= 0)
a[j] = max(a[j - w[i]] + v[i], a[j]);
else
a[j] = a[j];
/*for (int k = 0; k <= m; k++)
{
cout << a[k] << ' ';
}
cout << endl;*/
}
}
int s = 0;
for (int i = 0; i <= m; i++)
{
if (a[i] > s)
s = a[i];
}
cout << s;
return 0;
}
标签:一维,int,max,Bracelet,cin,Charm,3410,vi,构成 来源: https://blog.csdn.net/weixin_44489823/article/details/90726589