提交时间:2024-04-06 14:57:00
运行 ID: 141932
#include<bits/stdc++.h> using namespace std; int s,n,a[33]; bool use[33],ok=0; void pri() { for (int i=1;i<=n;i++) { if (use[i]==1) cout<<a[i]<<endl; } ok=1; return; } void dfs(int k) { if (ok==1) return; if (k==s) { pri(); return; } else { for (int i=n;i>=1;i--) { if (use[i]==0&&k+a[i]<=s) { use[i]=1; dfs(k+a[i]); use[i]=0; } } } } int main() { cin>>s>>n; for (int i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+n+1); dfs(0); if (ok==0) cout<<"Failed!"<<endl; return 0; }