AcWing 12. reverse the input
原题链接
中等
作者:
sysml
,
2019-05-27 23:21:21
,
所有人可见
,
阅读 1208
int main(){
int N,V;
cin>>N>>V;
vector<int> weight(N+1),value(N+1);
for(int i=1;i<=N;i++)
cin>>weight[i]>>value[i];
reverse(weight.begin()+1,weight.end());
reverse(value.begin()+1,value.end());
vector<vector<int>> dp(N+1,vector<int>(V+1,0));
for(int i=1;i<weight.size();i++){
for(int j=0;j<=V;j++){
if(j-weight[i]<0)
dp[i][j]=dp[i-1][j];
else
dp[i][j]=max(dp[i-1][j],dp[i-1][j-weight[i]]+value[i]);
}
}
for(int i=weight.size()-1;i>=1;i--){
if(V-weight[i]>=0 && dp[i][V]==dp[i-1][V-weight[i]]+value[i]){
cout<<weight.size()-i<<" ";
V-=weight[i];
}
}
cout<<endl;
}