背包问题(采药)

#include<bits/stdc++.h>
using namespace std;
int t,m,w[105],v[105],f[105][1005];
int main()
{
  cin>>t>>m;
  for(int i=1; i<=m; i++)
    cin>>w[i]>>v[i];
  for(int i=1; i<=m; i++)
  {
    for(int j=t; j>=0; j--)
    {
      if(j>=w[i])
        f[i][j]=max(f[i-1][j-w[i]]+v[i],f[i-1][j]);
      else f[i][j]=f[i-1][j];
    }
  }
  cout<<f[m][t];
}

 

posted @ 2023-07-21 14:07  siuuuuuuuu  阅读(25)  评论(0)    收藏  举报