http://acm.nyist.net/JudgeOnline/problem.php?pid=1058
+ View code
#include<cstdio>
#include<stack>
using namespace std;
stack<int>v;
const int maxn = 22;
int a[maxn];
//int sta[maxn];
int n,m,k,pos;
bool dfs(int i,int sum){
if(i==n) return sum==k;
else if(sum > k) return false; //剪枝(这么专业的名词也不知道用的对不对,反正就是优化了一下)
if(dfs(i+1,sum)) return true;
if(dfs(i+1,sum+a[i])){
//sta[pos++] = a[i];
v.push(a[i]);
return true;
}
return false;
}
int main(){
while(~scanf("%d%d",&n,&k)){
pos = 0;
for(int i = 0;i < n;i++){
scanf("%d",&a[i]);
}if(dfs(0,0)){
printf("YES\n");
// for(int i = pos - 1;i >= 0;i--)
// printf("%d ",sta[i]);
while(!v.empty()){
printf("%d ",v.top());
v.pop();
}
printf("\n");
}else
printf("NO\n");
}
return 0;
}