解题思路:01背包+最小路径输出
代码如下:
#include<bits/stdc++.h>
#define lowbit(x) ((x)&(-(x)))
#define ll long long
#define INF 0x3f3f3f3f
#define CLR(a) memset(a, 0, sizeof(a))
using namespace std;
int dp[10005];
int pre[10005][105];
int v[10005];
int n,m;
int cmp(int a,int b){
return a>b;
}
int main() {
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>v[i];
sort(v+1,v+n+1,cmp);
for(int i=1;i<=n;i++){
for(int j=m;j>=v[i];j--){
if(dp[j]<=dp[j-v[i]]+v[i]){
pre[i][j]=1;
dp[j]=dp[j-v[i]]+v[i];
}
}
}
if(dp[m]!=m){
cout<<"No Solution\n";
}else{
vector<int> ans;
int value=m,i=n;
while(value>0){
if(pre[i][value]){
ans.push_back(v[i]);
value-=v[i];
}
i--;
}
cout<<ans[0];
for(int i=1;i<ans.size();i++){
cout<<' '<<ans[i];
}
}
return 0;
}