解题思路:dp即可,f[v]=f[v-w[i]]+c[i];
注意事项:if (f[v]<f[v-w[i]]+c[i])不然会出问题
参考代码:
#include<?>//防抄
using namespace std;
int f[1100],w[120],c[120];
int main()
{
int v,t,m;
cin>>t>>m;
for (int i=1;i<=m;++i)
cin>>w[i]>>c[i];
for (int i=1;i<=m;++i)
for (int v=t;v>=w[i];--v)
if (f[v]<f[v-w[i]]+c[i])
f[v]=f[v-w[i]]+c[i];
cout<<f[t];
return 0;
}
0.0分
1 人评分