1267:【例9.11】01背包问题
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int N=205; 6 int a[N],b[N],f[N][N]; 7 int main(){ 8 int m,n; 9 cin>>m>>n; 10 for(int i=1;i<=n;i++)cin>>a[i]>>b[i]; 11 for(int i=1;i<=n;i++) 12 for(int j=1;j<=m;j++){ 13 f[i][j]=f[i-1][j]; 14 if(j>=a[i])f[i][j]=max(f[i][j],b[i]+f[i-1][j-a[i]]); 15 } 16 cout<<f[n][m]; 17 return 0; 18 }