提交 259791cb 编写于 作者: wrt2_860924229's avatar wrt2_860924229

01Bag

上级
#include<iostream>
using namespace std;
int main(){
int N;
int W;
int w[30]={0};
int v[30]={0};
int k,C;
int p;
int B[30][200] = {0};
cin>>W>>N;
for(p=1;p<N+1;p++){
cin>>w[p] >>v[p];
}
for(k=1;k<N+1;k++){
for(C = 1;C < W+1;C++){
if(w[k] > C){
B[k][C] = B[k-1][C];
}
else{
int value1 = B[k-1][C-w[k]] + v[k];
int value2 = B[k-1][C];
if(value1 > value2){
B[k][C] = value1;
}
else{
B[k][C] = value2;
}
}
}
}
cout<<B[N][W ]<<endl;
return 0;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册